LineSegments.js 1.8 KB

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