Wireframe.js 1.4 KB

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