Wireframe.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. class Wireframe extends Mesh {
  12. constructor( geometry = new LineSegmentsGeometry(), material = new Line2NodeMaterial( { color: Math.random() * 0xffffff } ) ) {
  13. super( geometry, material );
  14. this.isWireframe = true;
  15. this.type = 'Wireframe';
  16. }
  17. // for backwards-compatibility, but could be a method of LineSegmentsGeometry...
  18. computeLineDistances() {
  19. const geometry = this.geometry;
  20. const instanceStart = geometry.attributes.instanceStart;
  21. const instanceEnd = geometry.attributes.instanceEnd;
  22. const lineDistances = new Float32Array( 2 * instanceStart.count );
  23. for ( let i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
  24. _start.fromBufferAttribute( instanceStart, i );
  25. _end.fromBufferAttribute( instanceEnd, i );
  26. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  27. lineDistances[ j + 1 ] = lineDistances[ j ] + _start.distanceTo( _end );
  28. }
  29. const instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  30. geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  31. geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  32. return this;
  33. }
  34. }
  35. export { Wireframe };
粤ICP备19079148号