2
0

Wireframe.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {
  2. InstancedInterleavedBuffer,
  3. InterleavedBufferAttribute,
  4. Mesh,
  5. Vector3
  6. } from '../../../build/three.module.js';
  7. import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry.js';
  8. import { LineMaterial } from '../lines/LineMaterial.js';
  9. var Wireframe = function ( geometry, material ) {
  10. Mesh.call( this );
  11. this.type = 'Wireframe';
  12. this.geometry = geometry !== undefined ? geometry : new LineSegmentsGeometry();
  13. this.material = material !== undefined ? material : new LineMaterial( { color: Math.random() * 0xffffff } );
  14. };
  15. Wireframe.prototype = Object.assign( Object.create( Mesh.prototype ), {
  16. constructor: Wireframe,
  17. isWireframe: true,
  18. computeLineDistances: ( function () { // for backwards-compatability, but could be a method of LineSegmentsGeometry...
  19. var start = new Vector3();
  20. var end = new Vector3();
  21. return function computeLineDistances() {
  22. var geometry = this.geometry;
  23. var instanceStart = geometry.attributes.instanceStart;
  24. var instanceEnd = geometry.attributes.instanceEnd;
  25. var lineDistances = new Float32Array( 2 * instanceStart.data.count );
  26. for ( var i = 0, j = 0, l = instanceStart.data.count; i < l; i ++, j += 2 ) {
  27. start.fromBufferAttribute( instanceStart, i );
  28. end.fromBufferAttribute( instanceEnd, i );
  29. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  30. lineDistances[ j + 1 ] = lineDistances[ j ] + start.distanceTo( end );
  31. }
  32. var instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  33. geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  34. geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  35. return this;
  36. };
  37. }() )
  38. } );
  39. export { Wireframe };