LineSegments.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. class LineSegments extends Line {
  7. constructor( geometry, material ) {
  8. super( geometry, material );
  9. this.type = 'LineSegments';
  10. }
  11. computeLineDistances() {
  12. const geometry = this.geometry;
  13. if ( geometry.isBufferGeometry ) {
  14. // we assume non-indexed geometry
  15. if ( geometry.index === null ) {
  16. const positionAttribute = geometry.attributes.position;
  17. const lineDistances = [];
  18. for ( let i = 0, l = positionAttribute.count; i < l; i += 2 ) {
  19. _start.fromBufferAttribute( positionAttribute, i );
  20. _end.fromBufferAttribute( positionAttribute, i + 1 );
  21. lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
  22. lineDistances[ i + 1 ] = lineDistances[ i ] + _start.distanceTo( _end );
  23. }
  24. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  25. } else {
  26. console.warn( 'THREE.LineSegments.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  27. }
  28. } else if ( geometry.isGeometry ) {
  29. console.error( 'THREE.LineSegments.computeLineDistances() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  30. }
  31. return this;
  32. }
  33. }
  34. LineSegments.prototype.isLineSegments = true;
  35. export { LineSegments };