Wireframe.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ( function () {
  2. const _start = new THREE.Vector3();
  3. const _end = new THREE.Vector3();
  4. class Wireframe extends THREE.Mesh {
  5. constructor( geometry = new THREE.LineSegmentsGeometry(), material = new THREE.LineMaterial( {
  6. color: Math.random() * 0xffffff
  7. } ) ) {
  8. super( geometry, material );
  9. this.type = 'Wireframe';
  10. } // for backwards-compatability, but could be a method of THREE.LineSegmentsGeometry...
  11. computeLineDistances() {
  12. const geometry = this.geometry;
  13. const instanceStart = geometry.attributes.instanceStart;
  14. const instanceEnd = geometry.attributes.instanceEnd;
  15. const lineDistances = new Float32Array( 2 * instanceStart.count );
  16. for ( let i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
  17. _start.fromBufferAttribute( instanceStart, i );
  18. _end.fromBufferAttribute( instanceEnd, i );
  19. lineDistances[ j ] = j === 0 ? 0 : lineDistances[ j - 1 ];
  20. lineDistances[ j + 1 ] = lineDistances[ j ] + _start.distanceTo( _end );
  21. }
  22. const instanceDistanceBuffer = new THREE.InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  23. geometry.setAttribute( 'instanceDistanceStart', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  24. geometry.setAttribute( 'instanceDistanceEnd', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  25. return this;
  26. }
  27. }
  28. Wireframe.prototype.isWireframe = true;
  29. THREE.Wireframe = Wireframe;
  30. } )();