Wireframe.js 1.9 KB

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