LineSegments.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Line } from './Line.js';
  2. import { Vector3 } from '../math/Vector3.js';
  3. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  4. /**
  5. * @author mrdoob / http://mrdoob.com/
  6. */
  7. var _start = new Vector3();
  8. var _end = new Vector3();
  9. function LineSegments( geometry, material ) {
  10. Line.call( this, geometry, material );
  11. this.type = 'LineSegments';
  12. }
  13. LineSegments.prototype = Object.assign( Object.create( Line.prototype ), {
  14. constructor: LineSegments,
  15. isLineSegments: true,
  16. computeLineDistances: function () {
  17. var geometry = this.geometry;
  18. if ( geometry.isBufferGeometry ) {
  19. // we assume non-indexed geometry
  20. if ( geometry.index === null ) {
  21. var positionAttribute = geometry.attributes.position;
  22. var lineDistances = [];
  23. for ( var i = 0, l = positionAttribute.count; i < l; i += 2 ) {
  24. _start.fromBufferAttribute( positionAttribute, i );
  25. _end.fromBufferAttribute( positionAttribute, i + 1 );
  26. lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
  27. lineDistances[ i + 1 ] = lineDistances[ i ] + _start.distanceTo( _end );
  28. }
  29. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  30. } else {
  31. console.warn( 'THREE.LineSegments.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  32. }
  33. } else if ( geometry.isGeometry ) {
  34. var vertices = geometry.vertices;
  35. var lineDistances = geometry.lineDistances;
  36. for ( var i = 0, l = vertices.length; i < l; i += 2 ) {
  37. _start.copy( vertices[ i ] );
  38. _end.copy( vertices[ i + 1 ] );
  39. lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
  40. lineDistances[ i + 1 ] = lineDistances[ i ] + _start.distanceTo( _end );
  41. }
  42. }
  43. return this;
  44. }
  45. } );
  46. export { LineSegments };