Line.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.type = 'Line';
  18. this.geometry = geometry;
  19. this.material = material;
  20. this.updateMorphTargets();
  21. }
  22. copy( source ) {
  23. super.copy( source );
  24. this.material = source.material;
  25. this.geometry = source.geometry;
  26. return this;
  27. }
  28. computeLineDistances() {
  29. const geometry = this.geometry;
  30. if ( geometry.isBufferGeometry ) {
  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. } else if ( geometry.isGeometry ) {
  46. console.error( 'THREE.Line.computeLineDistances() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  47. }
  48. return this;
  49. }
  50. raycast( raycaster, intersects ) {
  51. const geometry = this.geometry;
  52. const matrixWorld = this.matrixWorld;
  53. const threshold = raycaster.params.Line.threshold;
  54. const drawRange = geometry.drawRange;
  55. // Checking boundingSphere distance to ray
  56. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  57. _sphere.copy( geometry.boundingSphere );
  58. _sphere.applyMatrix4( matrixWorld );
  59. _sphere.radius += threshold;
  60. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  61. //
  62. _inverseMatrix.copy( matrixWorld ).invert();
  63. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  64. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  65. const localThresholdSq = localThreshold * localThreshold;
  66. const vStart = new Vector3();
  67. const vEnd = new Vector3();
  68. const interSegment = new Vector3();
  69. const interRay = new Vector3();
  70. const step = this.isLineSegments ? 2 : 1;
  71. if ( geometry.isBufferGeometry ) {
  72. const index = geometry.index;
  73. const attributes = geometry.attributes;
  74. const positionAttribute = attributes.position;
  75. if ( index !== null ) {
  76. const start = Math.max( 0, drawRange.start );
  77. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  78. for ( let i = start, l = end - 1; i < l; i += step ) {
  79. const a = index.getX( i );
  80. const b = index.getX( i + 1 );
  81. vStart.fromBufferAttribute( positionAttribute, a );
  82. vEnd.fromBufferAttribute( positionAttribute, b );
  83. const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  84. if ( distSq > localThresholdSq ) continue;
  85. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  86. const distance = raycaster.ray.origin.distanceTo( interRay );
  87. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  88. intersects.push( {
  89. distance: distance,
  90. // What do we want? intersection point on the ray or on the segment??
  91. // point: raycaster.ray.at( distance ),
  92. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  93. index: i,
  94. face: null,
  95. faceIndex: null,
  96. object: this
  97. } );
  98. }
  99. } else {
  100. const start = Math.max( 0, drawRange.start );
  101. const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  102. for ( let i = start, l = end - 1; i < l; i += step ) {
  103. vStart.fromBufferAttribute( positionAttribute, i );
  104. vEnd.fromBufferAttribute( positionAttribute, i + 1 );
  105. const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  106. if ( distSq > localThresholdSq ) continue;
  107. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  108. const distance = raycaster.ray.origin.distanceTo( interRay );
  109. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  110. intersects.push( {
  111. distance: distance,
  112. // What do we want? intersection point on the ray or on the segment??
  113. // point: raycaster.ray.at( distance ),
  114. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  115. index: i,
  116. face: null,
  117. faceIndex: null,
  118. object: this
  119. } );
  120. }
  121. }
  122. } else if ( geometry.isGeometry ) {
  123. console.error( 'THREE.Line.raycast() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  124. }
  125. }
  126. updateMorphTargets() {
  127. const geometry = this.geometry;
  128. if ( geometry.isBufferGeometry ) {
  129. const morphAttributes = geometry.morphAttributes;
  130. const keys = Object.keys( morphAttributes );
  131. if ( keys.length > 0 ) {
  132. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  133. if ( morphAttribute !== undefined ) {
  134. this.morphTargetInfluences = [];
  135. this.morphTargetDictionary = {};
  136. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  137. const name = morphAttribute[ m ].name || String( m );
  138. this.morphTargetInfluences.push( 0 );
  139. this.morphTargetDictionary[ name ] = m;
  140. }
  141. }
  142. }
  143. } else {
  144. const morphTargets = geometry.morphTargets;
  145. if ( morphTargets !== undefined && morphTargets.length > 0 ) {
  146. console.error( 'THREE.Line.updateMorphTargets() does not support THREE.Geometry. Use THREE.BufferGeometry instead.' );
  147. }
  148. }
  149. }
  150. }
  151. Line.prototype.isLine = true;
  152. export { Line };