Line.js 6.3 KB

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