LineSegments.js 1.7 KB

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