Wireframe.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {
  2. InstancedInterleavedBuffer,
  3. InterleavedBufferAttribute,
  4. Mesh,
  5. Vector3
  6. } from 'three';
  7. import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry.js';
  8. import { LineMaterial } from '../lines/LineMaterial.js';
  9. const _start = new Vector3();
  10. const _end = new Vector3();
  11. class Wireframe extends Mesh {
  12. constructor( geometry = new LineSegmentsGeometry(), material = new LineMaterial( { 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 };