Wireframe.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. */
  19. class Wireframe extends Mesh {
  20. /**
  21. * Constructs a new wireframe.
  22. *
  23. * @param {LineSegmentsGeometry} [geometry] - The line geometry.
  24. * @param {Line2NodeMaterial} [material] - The line material.
  25. */
  26. constructor( geometry = new LineSegmentsGeometry(), material = new Line2NodeMaterial( { color: Math.random() * 0xffffff } ) ) {
  27. super( geometry, material );
  28. /**
  29. * This flag can be used for type testing.
  30. *
  31. * @type {boolean}
  32. * @readonly
  33. * @default true
  34. */
  35. this.isWireframe = true;
  36. this.type = 'Wireframe';
  37. }
  38. /**
  39. * Computes an array of distance values which are necessary for rendering dashed lines.
  40. * For each vertex in the geometry, the method calculates the cumulative length from the
  41. * current point to the very beginning of the line.
  42. *
  43. * @return {Wireframe} A reference to this instance.
  44. */
  45. computeLineDistances() {
  46. // for backwards-compatibility, but could be a method of LineSegmentsGeometry...
  47. const geometry = this.geometry;
  48. const instanceStart = geometry.attributes.instanceStart;
  49. const instanceEnd = geometry.attributes.instanceEnd;
  50. const lineDistances = new Float32Array( 2 * instanceStart.count );
  51. for ( let i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
  52. _start.fromBufferAttribute( instanceStart, i );
  53. _end.fromBufferAttribute( instanceEnd, i );
  54. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  55. lineDistances[ j + 1 ] = lineDistances[ j ] + _start.distanceTo( _end );
  56. }
  57. const instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  58. geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  59. geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  60. return this;
  61. }
  62. }
  63. export { Wireframe };
粤ICP备19079148号