1
0

Raycaster.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author bhouston / http://exocortex.com/
  4. * @author stephomi / http://stephaneginier.com/
  5. */
  6. ( function ( THREE ) {
  7. THREE.Raycaster = function ( origin, direction, near, far ) {
  8. this.ray = new THREE.Ray( origin, direction );
  9. // direction is assumed to be normalized (for accurate distance calculations)
  10. this.near = near || 0;
  11. this.far = far || Infinity;
  12. };
  13. var sphere = new THREE.Sphere();
  14. var localRay = new THREE.Ray();
  15. var facePlane = new THREE.Plane();
  16. var intersectPoint = new THREE.Vector3();
  17. var matrixPosition = new THREE.Vector3();
  18. var inverseMatrix = new THREE.Matrix4();
  19. var descSort = function ( a, b ) {
  20. return a.distance - b.distance;
  21. };
  22. var vA = new THREE.Vector3();
  23. var vB = new THREE.Vector3();
  24. var vC = new THREE.Vector3();
  25. var intersectObject = function ( object, raycaster, intersects ) {
  26. if ( object instanceof THREE.Particle ) {
  27. matrixPosition.getPositionFromMatrix( object.matrixWorld );
  28. var distance = raycaster.ray.distanceToPoint( matrixPosition );
  29. if ( distance > object.scale.x ) {
  30. return intersects;
  31. }
  32. intersects.push( {
  33. distance: distance,
  34. point: object.position,
  35. face: null,
  36. object: object
  37. } );
  38. } else if ( object instanceof THREE.LOD ) {
  39. matrixPosition.getPositionFromMatrix( object.matrixWorld );
  40. var distance = raycaster.ray.origin.distanceTo( matrixPosition );
  41. intersectObject( object.getObjectForDistance( distance ), raycaster, intersects );
  42. } else if ( object instanceof THREE.Mesh ) {
  43. var geometry = object.geometry;
  44. // Checking boundingSphere distance to ray
  45. matrixPosition.getPositionFromMatrix( object.matrixWorld );
  46. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  47. sphere.set( matrixPosition, geometry.boundingSphere.radius * object.matrixWorld.getMaxScaleOnAxis() );
  48. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  49. return intersects;
  50. }
  51. //Check boundingBox before continuing
  52. inverseMatrix.getInverse( object.matrixWorld );
  53. localRay.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  54. if ( geometry.boundingBox !== null) {
  55. if ( localRay.isIntersectionBox(geometry.boundingBox) === false ) {
  56. return intersects;
  57. }
  58. }
  59. var vertices = geometry.vertices;
  60. if ( geometry instanceof THREE.BufferGeometry ) {
  61. var material = object.material;
  62. if ( material === undefined ) return intersects;
  63. if ( geometry.dynamic === false ) return intersects;
  64. var a, b, c;
  65. var precision = raycaster.precision;
  66. var fl;
  67. var indexed = false;
  68. if ( geometry.attributes.index ) {
  69. indexed = true;
  70. fl = geometry.attributes.index.numItems / 3;
  71. } else {
  72. fl = geometry.attributes.position.numItems / 9;
  73. }
  74. var offsets = geometry.offsets;
  75. var indices = geometry.attributes.index.array;
  76. var positions = geometry.attributes.position.array;
  77. var offLength = geometry.offsets.length;
  78. for ( var oi = 0; oi < offLength; ++oi ) {
  79. var start = offsets[ oi ].start;
  80. var count = offsets[ oi ].count;
  81. var index = offsets[ oi ].index;
  82. for ( var i = start, il = start + count; i < il; i += 3 ) {
  83. if ( indexed ) {
  84. a = index + indices[ i ];
  85. b = index + indices[ i + 1 ];
  86. c = index + indices[ i + 2 ];
  87. } else {
  88. a = index;
  89. b = index + 1;
  90. c = index + 2;
  91. }
  92. vA.set(
  93. positions[ a * 3 ],
  94. positions[ a * 3 + 1 ],
  95. positions[ a * 3 + 2 ]
  96. );
  97. vB.set(
  98. positions[ b * 3 ],
  99. positions[ b * 3 + 1 ],
  100. positions[ b * 3 + 2 ]
  101. );
  102. vC.set(
  103. positions[ c * 3 ],
  104. positions[ c * 3 + 1 ],
  105. positions[ c * 3 + 2 ]
  106. );
  107. var interPoint = localRay.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
  108. if ( !interPoint ) continue;
  109. interPoint.applyMatrix4( object.matrixWorld );
  110. var distance = raycaster.ray.origin.distanceTo( interPoint );
  111. // bail if the ray is too close to the plane
  112. if ( distance < precision ) continue;
  113. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  114. intersects.push( {
  115. distance: distance,
  116. point: interPoint,
  117. face: null,
  118. faceIndex: null,
  119. object: object
  120. } );
  121. }
  122. }
  123. } else if ( geometry instanceof THREE.Geometry ) {
  124. var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  125. var objectMaterials = isFaceMaterial === true ? object.material.materials : null;
  126. var a, b, c, d;
  127. var precision = raycaster.precision;
  128. for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  129. var face = geometry.faces[ f ];
  130. var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : object.material;
  131. if ( material === undefined ) continue;
  132. a = vertices[ face.a ];
  133. b = vertices[ face.b ];
  134. c = vertices[ face.c ];
  135. var interPoint = localRay.intersectTriangle( a, b, c, material.side !== THREE.DoubleSide );
  136. if ( !interPoint ) {
  137. continue;
  138. }
  139. interPoint.applyMatrix4( object.matrixWorld );
  140. var distance = raycaster.ray.origin.distanceTo( interPoint );
  141. // bail if the ray is too close to the plane
  142. if ( distance < precision ) continue;
  143. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  144. intersects.push( {
  145. distance: distance,
  146. point: interPoint,
  147. face: face,
  148. faceIndex: f,
  149. object: object
  150. } );
  151. }
  152. }
  153. } else if ( object instanceof THREE.Line ) {
  154. var precision = raycaster.linePrecision;
  155. var precisionSq = precision * precision;
  156. var geometry = object.geometry;
  157. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  158. // Checking boundingSphere distance to ray
  159. matrixPosition.getPositionFromMatrix(object.matrixWorld);
  160. sphere.set( matrixPosition, geometry.boundingSphere.radius * object.matrixWorld.getMaxScaleOnAxis() );
  161. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  162. return intersects;
  163. }
  164. inverseMatrix.getInverse( object.matrixWorld );
  165. localRay.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  166. var vertices = geometry.vertices;
  167. var nbVertices = vertices.length;
  168. var interSegment = new THREE.Vector3();
  169. var interRay = new THREE.Vector3();
  170. var step = object.type === THREE.LineStrip ? 1 : 2;
  171. for ( var i = 0; i < nbVertices - 1; i = i + step ) {
  172. var distSq = localRay.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
  173. if ( distSq <= precisionSq ) {
  174. var distance = localRay.origin.distanceTo( interRay );
  175. if ( raycaster.near <= distance && distance <= raycaster.far ) {
  176. intersects.push( {
  177. distance: distance,
  178. // What do we want? intersection point on the ray or on the segment??
  179. // point: raycaster.ray.at( distance ),
  180. point: interSegment.clone().applyMatrix4( object.matrixWorld ),
  181. face: null,
  182. faceIndex: null,
  183. object: object
  184. } );
  185. }
  186. }
  187. }
  188. }
  189. };
  190. var intersectDescendants = function ( object, raycaster, intersects ) {
  191. var descendants = object.getDescendants();
  192. for ( var i = 0, l = descendants.length; i < l; i ++ ) {
  193. intersectObject( descendants[ i ], raycaster, intersects );
  194. }
  195. };
  196. //
  197. THREE.Raycaster.prototype.precision = 0.0001;
  198. THREE.Raycaster.prototype.linePrecision = 1;
  199. THREE.Raycaster.prototype.set = function ( origin, direction ) {
  200. this.ray.set( origin, direction );
  201. // direction is assumed to be normalized (for accurate distance calculations)
  202. };
  203. THREE.Raycaster.prototype.intersectObject = function ( object, recursive ) {
  204. var intersects = [];
  205. if ( recursive === true ) {
  206. intersectDescendants( object, this, intersects );
  207. }
  208. intersectObject( object, this, intersects );
  209. intersects.sort( descSort );
  210. return intersects;
  211. };
  212. THREE.Raycaster.prototype.intersectObjects = function ( objects, recursive ) {
  213. var intersects = [];
  214. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  215. intersectObject( objects[ i ], this, intersects );
  216. if ( recursive === true ) {
  217. intersectDescendants( objects[ i ], this, intersects );
  218. }
  219. }
  220. intersects.sort( descSort );
  221. return intersects;
  222. };
  223. }( THREE ) );
粤ICP备19079148号