Wireframe.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. InstancedInterleavedBuffer,
  3. InterleavedBufferAttribute,
  4. Line2NodeMaterial,
  5. Mesh,
  6. Vector3
  7. } from 'three/webgpu';
  8. import { LineSegmentsGeometry } from '../../lines/LineSegmentsGeometry.js';
  9. const _start = new Vector3();
  10. const _end = new Vector3();
  11. /**
  12. * A class for creating wireframes based on wide lines.
  13. *
  14. * This module can only be used with {@link WebGPURenderer}. When using {@link WebGLRenderer},
  15. * import the class from `lines/Wireframe.js`.
  16. *
  17. * @augments Mesh
  18. * @three_import import { Wireframe } from 'three/addons/lines/webgpu/Wireframe.js';
  19. */
  20. class Wireframe extends Mesh {
  21. /**
  22. * Constructs a new wireframe.
  23. *
  24. * @param {LineSegmentsGeometry} [geometry] - The line geometry.
  25. * @param {Line2NodeMaterial} [material] - The line material.
  26. */
  27. constructor( geometry = new LineSegmentsGeometry(), material = new Line2NodeMaterial( { color: Math.random() * 0xffffff } ) ) {
  28. super( geometry, material );
  29. /**
  30. * This flag can be used for type testing.
  31. *
  32. * @type {boolean}
  33. * @readonly
  34. * @default true
  35. */
  36. this.isWireframe = true;
  37. this.type = 'Wireframe';
  38. }
  39. /**
  40. * Computes an array of distance values which are necessary for rendering dashed lines.
  41. * For each vertex in the geometry, the method calculates the cumulative length from the
  42. * current point to the very beginning of the line.
  43. *
  44. * @return {Wireframe} A reference to this instance.
  45. */
  46. computeLineDistances() {
  47. // for backwards-compatibility, but could be a method of LineSegmentsGeometry...
  48. const geometry = this.geometry;
  49. const instanceStart = geometry.attributes.instanceStart;
  50. const instanceEnd = geometry.attributes.instanceEnd;
  51. const lineDistances = new Float32Array( 2 * instanceStart.count );
  52. for ( let i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
  53. _start.fromBufferAttribute( instanceStart, i );
  54. _end.fromBufferAttribute( instanceEnd, i );
  55. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  56. lineDistances[ j + 1 ] = lineDistances[ j ] + _start.distanceTo( _end );
  57. }
  58. const instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  59. geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  60. geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  61. return this;
  62. }
  63. }
  64. export { Wireframe };