Line.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 = new Vector3();
  10. const _end = new Vector3();
  11. const _inverseMatrix = new Matrix4();
  12. const _ray = new Ray();
  13. const _sphere = new Sphere();
  14. function Line( geometry, material, mode ) {
  15. if ( mode === 1 ) {
  16. console.error( 'THREE.Line: parameter THREE.LinePieces no longer supported. Use THREE.LineSegments instead.' );
  17. }
  18. Object3D.call( this );
  19. this.type = 'Line';
  20. this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
  21. this.material = material !== undefined ? material : new LineBasicMaterial();
  22. this.updateMorphTargets();
  23. }
  24. Line.prototype = Object.assign( Object.create( Object3D.prototype ), {
  25. constructor: Line,
  26. isLine: true,
  27. copy: function ( source ) {
  28. Object3D.prototype.copy.call( this, source );
  29. this.material = source.material;
  30. this.geometry = source.geometry;
  31. return this;
  32. },
  33. computeLineDistances: function () {
  34. const geometry = this.geometry;
  35. if ( geometry.isBufferGeometry ) {
  36. // we assume non-indexed geometry
  37. if ( geometry.index === null ) {
  38. const positionAttribute = geometry.attributes.position;
  39. const lineDistances = [ 0 ];
  40. for ( let i = 1, l = positionAttribute.count; i < l; i ++ ) {
  41. _start.fromBufferAttribute( positionAttribute, i - 1 );
  42. _end.fromBufferAttribute( positionAttribute, i );
  43. lineDistances[ i ] = lineDistances[ i - 1 ];
  44. lineDistances[ i ] += _start.distanceTo( _end );
  45. }
  46. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  47. } else {
  48. console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  49. }
  50. } else if ( geometry.isGeometry ) {
  51. const vertices = geometry.vertices;
  52. const lineDistances = geometry.lineDistances;
  53. lineDistances[ 0 ] = 0;
  54. for ( let i = 1, l = vertices.length; i < l; i ++ ) {
  55. lineDistances[ i ] = lineDistances[ i - 1 ];
  56. lineDistances[ i ] += vertices[ i - 1 ].distanceTo( vertices[ i ] );
  57. }
  58. }
  59. return this;
  60. },
  61. raycast: function ( raycaster, intersects ) {
  62. const geometry = this.geometry;
  63. const matrixWorld = this.matrixWorld;
  64. const threshold = raycaster.params.Line.threshold;
  65. // Checking boundingSphere distance to ray
  66. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  67. _sphere.copy( geometry.boundingSphere );
  68. _sphere.applyMatrix4( matrixWorld );
  69. _sphere.radius += threshold;
  70. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  71. //
  72. _inverseMatrix.getInverse( matrixWorld );
  73. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  74. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  75. const localThresholdSq = localThreshold * localThreshold;
  76. const vStart = new Vector3();
  77. const vEnd = new Vector3();
  78. const interSegment = new Vector3();
  79. const interRay = new Vector3();
  80. const step = this.isLineSegments ? 2 : 1;
  81. if ( geometry.isBufferGeometry ) {
  82. const index = geometry.index;
  83. const attributes = geometry.attributes;
  84. const positionAttribute = attributes.position;
  85. if ( index !== null ) {
  86. const indices = index.array;
  87. for ( let i = 0, l = indices.length - 1; i < l; i += step ) {
  88. const a = indices[ i ];
  89. const b = indices[ i + 1 ];
  90. vStart.fromBufferAttribute( positionAttribute, a );
  91. vEnd.fromBufferAttribute( positionAttribute, b );
  92. const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  93. if ( distSq > localThresholdSq ) continue;
  94. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  95. const distance = raycaster.ray.origin.distanceTo( interRay );
  96. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  97. intersects.push( {
  98. distance: distance,
  99. // What do we want? intersection point on the ray or on the segment??
  100. // point: raycaster.ray.at( distance ),
  101. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  102. index: i,
  103. face: null,
  104. faceIndex: null,
  105. object: this
  106. } );
  107. }
  108. } else {
  109. for ( let i = 0, l = positionAttribute.count - 1; i < l; i += step ) {
  110. vStart.fromBufferAttribute( positionAttribute, i );
  111. vEnd.fromBufferAttribute( positionAttribute, i + 1 );
  112. const distSq = _ray.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
  113. if ( distSq > localThresholdSq ) continue;
  114. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  115. const distance = raycaster.ray.origin.distanceTo( interRay );
  116. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  117. intersects.push( {
  118. distance: distance,
  119. // What do we want? intersection point on the ray or on the segment??
  120. // point: raycaster.ray.at( distance ),
  121. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  122. index: i,
  123. face: null,
  124. faceIndex: null,
  125. object: this
  126. } );
  127. }
  128. }
  129. } else if ( geometry.isGeometry ) {
  130. const vertices = geometry.vertices;
  131. const nbVertices = vertices.length;
  132. for ( let i = 0; i < nbVertices - 1; i += step ) {
  133. const distSq = _ray.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
  134. if ( distSq > localThresholdSq ) continue;
  135. interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
  136. const distance = raycaster.ray.origin.distanceTo( interRay );
  137. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  138. intersects.push( {
  139. distance: distance,
  140. // What do we want? intersection point on the ray or on the segment??
  141. // point: raycaster.ray.at( distance ),
  142. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  143. index: i,
  144. face: null,
  145. faceIndex: null,
  146. object: this
  147. } );
  148. }
  149. }
  150. },
  151. updateMorphTargets: function () {
  152. const geometry = this.geometry;
  153. if ( geometry.isBufferGeometry ) {
  154. const morphAttributes = geometry.morphAttributes;
  155. const keys = Object.keys( morphAttributes );
  156. if ( keys.length > 0 ) {
  157. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  158. if ( morphAttribute !== undefined ) {
  159. this.morphTargetInfluences = [];
  160. this.morphTargetDictionary = {};
  161. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  162. const name = morphAttribute[ m ].name || String( m );
  163. this.morphTargetInfluences.push( 0 );
  164. this.morphTargetDictionary[ name ] = m;
  165. }
  166. }
  167. }
  168. } else {
  169. const morphTargets = geometry.morphTargets;
  170. if ( morphTargets !== undefined && morphTargets.length > 0 ) {
  171. console.error( 'THREE.Line.updateMorphTargets() does not support THREE.Geometry. Use THREE.BufferGeometry instead.' );
  172. }
  173. }
  174. }
  175. } );
  176. export { Line };