Wireframe.js 1.4 KB

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