LineSegments2.js 1.7 KB

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