LineSegments.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Line } from './Line.js';
  2. import { Vector3 } from '../math/Vector3.js';
  3. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  4. const _start = /*@__PURE__*/ new Vector3();
  5. const _end = /*@__PURE__*/ new Vector3();
  6. /**
  7. * A series of lines drawn between pairs of vertices.
  8. *
  9. * @augments Line
  10. */
  11. class LineSegments extends Line {
  12. /**
  13. * Constructs a new line segments.
  14. *
  15. * @param {BufferGeometry} [geometry] - The line geometry.
  16. * @param {Material|Array<Material>} [material] - The line material.
  17. */
  18. constructor( geometry, material ) {
  19. super( geometry, material );
  20. /**
  21. * This flag can be used for type testing.
  22. *
  23. * @type {boolean}
  24. * @readonly
  25. * @default true
  26. */
  27. this.isLineSegments = true;
  28. this.type = 'LineSegments';
  29. }
  30. computeLineDistances() {
  31. const geometry = this.geometry;
  32. // we assume non-indexed geometry
  33. if ( geometry.index === null ) {
  34. const positionAttribute = geometry.attributes.position;
  35. const lineDistances = [];
  36. for ( let i = 0, l = positionAttribute.count; i < l; i += 2 ) {
  37. _start.fromBufferAttribute( positionAttribute, i );
  38. _end.fromBufferAttribute( positionAttribute, i + 1 );
  39. lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
  40. lineDistances[ i + 1 ] = lineDistances[ i ] + _start.distanceTo( _end );
  41. }
  42. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  43. } else {
  44. console.warn( 'THREE.LineSegments.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  45. }
  46. return this;
  47. }
  48. }
  49. export { LineSegments };
粤ICP备19079148号