Line.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { Sphere } from '../math/Sphere.js';
  2. import { Ray } from '../math/Ray.js';
  3. import { Matrix4 } from '../math/Matrix4.js';
  4. import { Object3D } from '../core/Object3D.js';
  5. import { Vector3 } from '../math/Vector3.js';
  6. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  7. import { BufferGeometry } from '../core/BufferGeometry.js';
  8. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  9. const _start = /*@__PURE__*/ new Vector3();
  10. const _end = /*@__PURE__*/ new Vector3();
  11. const _inverseMatrix = /*@__PURE__*/ new Matrix4();
  12. const _ray = /*@__PURE__*/ new Ray();
  13. const _sphere = /*@__PURE__*/ new Sphere();
  14. class Line extends Object3D {
  15. constructor( geometry = new BufferGeometry(), material = new LineBasicMaterial() ) {
  16. super();
  17. this.isLine = true;
  18. this.type = 'Line';
  19. this.geometry = geometry;
  20. this.material = material;
  21. this.updateMorphTargets();
  22. }
  23. copy( source, recursive ) {
  24. super.copy( source, recursive );
  25. this.material = source.material;
  26. this.geometry = source.geometry;
  27. return this;
  28. }
  29. computeLineDistances() {
  30. const geometry = this.geometry;
  31. // we assume non-indexed geometry
  32. if ( geometry.index === null ) {
  33. const positionAttribute = geometry.attributes.position;
  34. const lineDistances = [ 0 ];
  35. for ( let i = 1, l = positionAttribute.count; i < l; i ++ ) {
  36. _start.fromBufferAttribute( positionAttribute, i - 1 );
  37. _end.fromBufferAttribute( positionAttribute, i );
  38. lineDistances[ i ] = lineDistances[ i - 1 ];
  39. lineDistances[ i ] += _start.distanceTo( _end );
  40. }
  41. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  42. } else {
  43. console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  44. }
  45. return this;
  46. }
  47. raycast( raycaster, intersects ) {
  48. const geometry = this.geometry;
  49. const matrixWorld = this.matrixWorld;
  50. const threshold = raycaster.params.Line.threshold;
  51. const drawRange = geometry.drawRange;
  52. // Checking boundingSphere distance to ray
  53. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  54. _sphere.copy( geometry.boundingSphere );
  55. _sphere.applyMatrix4( matrixWorld );
  56. _sphere.radius += threshold;
  57. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  58. //
  59. _inverseMatrix.copy( matrixWorld ).invert();
  60. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  61. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  62. const localThresholdSq = localThreshold * localThreshold;
  63. const vStart = new Vector3();
  64. const vEnd = new Vector3();
  65. const interSegment = new Vector3();
  66. const interRay = new Vector3();
  67. const step = this.isLineSegments ? 2 : 1;
  68. const index = geometry.index;
  69. const attributes = geometry.attributes;
  70. const positionAttribute = attributes.position;
  71. if ( index !== null ) {
  72. const start = Math.max( 0, drawRange.start );
  73. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  74. for ( let i = start, l = end - 1; i < l; i += step ) {
  75. const a = index.getX( i );
  76. const b = index.getX( i + 1 );
  77. vStart.fromBufferAttribute( positionAttribute, a );
  78. vEnd.fromBufferAttribute( positionAttribute, b );
  79. const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  80. if ( distSq > localThresholdSq ) continue;
  81. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  82. const distance = raycaster.ray.origin.distanceTo( interRay );
  83. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  84. intersects.push( {
  85. distance: distance,
  86. // What do we want? intersection point on the ray or on the segment??
  87. // point: raycaster.ray.at( distance ),
  88. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  89. index: i,
  90. face: null,
  91. faceIndex: null,
  92. object: this
  93. } );
  94. }
  95. } else {
  96. const start = Math.max( 0, drawRange.start );
  97. const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  98. for ( let i = start, l = end - 1; i < l; i += step ) {
  99. vStart.fromBufferAttribute( positionAttribute, i );
  100. vEnd.fromBufferAttribute( positionAttribute, i + 1 );
  101. const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  102. if ( distSq > localThresholdSq ) continue;
  103. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  104. const distance = raycaster.ray.origin.distanceTo( interRay );
  105. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  106. intersects.push( {
  107. distance: distance,
  108. // What do we want? intersection point on the ray or on the segment??
  109. // point: raycaster.ray.at( distance ),
  110. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  111. index: i,
  112. face: null,
  113. faceIndex: null,
  114. object: this
  115. } );
  116. }
  117. }
  118. }
  119. updateMorphTargets() {
  120. const geometry = this.geometry;
  121. const morphAttributes = geometry.morphAttributes;
  122. const keys = Object.keys( morphAttributes );
  123. if ( keys.length > 0 ) {
  124. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  125. if ( morphAttribute !== undefined ) {
  126. this.morphTargetInfluences = [];
  127. this.morphTargetDictionary = {};
  128. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  129. const name = morphAttribute[ m ].name || String( m );
  130. this.morphTargetInfluences.push( 0 );
  131. this.morphTargetDictionary[ name ] = m;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. export { Line };
粤ICP备19079148号