Wireframe.js 1.6 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.type = 'Wireframe';
  15. }
  16. // for backwards-compatability, but could be a method of LineSegmentsGeometry...
  17. computeLineDistances() {
  18. const geometry = this.geometry;
  19. const instanceStart = geometry.attributes.instanceStart;
  20. const instanceEnd = geometry.attributes.instanceEnd;
  21. const lineDistances = new Float32Array( 2 * instanceStart.count );
  22. for ( let i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
  23. _start.fromBufferAttribute( instanceStart, i );
  24. _end.fromBufferAttribute( instanceEnd, i );
  25. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  26. lineDistances[ j + 1 ] = lineDistances[ j ] + _start.distanceTo( _end );
  27. }
  28. const instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  29. geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  30. geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  31. return this;
  32. }
  33. }
  34. Wireframe.prototype.isWireframe = true;
  35. export { Wireframe };