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