LineSegments2.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 LineSegments2 = function ( geometry, material ) {
  14. Mesh.call( this );
  15. this.type = 'LineSegments2';
  16. this.geometry = geometry !== undefined ? geometry : new LineSegmentsGeometry();
  17. this.material = material !== undefined ? material : new LineMaterial( { color: Math.random() * 0xffffff } );
  18. };
  19. LineSegments2.prototype = Object.assign( Object.create( Mesh.prototype ), {
  20. constructor: LineSegments2,
  21. isLineSegments2: 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. copy: function ( /* source */ ) {
  43. // todo
  44. return this;
  45. }
  46. } );
  47. export { LineSegments2 };