Raycaster.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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.Sprite ) {
  27. matrixPosition.setFromMatrixPosition( 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.setFromMatrixPosition( 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. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  46. sphere.copy( geometry.boundingSphere );
  47. sphere.applyMatrix4( object.matrixWorld );
  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. if ( geometry instanceof THREE.BufferGeometry ) {
  60. var material = object.material;
  61. if ( material === undefined ) return intersects;
  62. if ( geometry.dynamic === false ) return intersects;
  63. var a, b, c;
  64. var precision = raycaster.precision;
  65. if ( geometry.attributes.index !== undefined ) {
  66. var offsets = geometry.offsets;
  67. var indices = geometry.attributes.index.array;
  68. var positions = geometry.attributes.position.array;
  69. var offLength = geometry.offsets.length;
  70. var fl = geometry.attributes.index.array.length / 3;
  71. for ( var oi = 0; oi < offLength; ++oi ) {
  72. var start = offsets[ oi ].start;
  73. var count = offsets[ oi ].count;
  74. var index = offsets[ oi ].index;
  75. for ( var i = start, il = start + count; i < il; i += 3 ) {
  76. a = index + indices[ i ];
  77. b = index + indices[ i + 1 ];
  78. c = index + indices[ i + 2 ];
  79. vA.set(
  80. positions[ a * 3 ],
  81. positions[ a * 3 + 1 ],
  82. positions[ a * 3 + 2 ]
  83. );
  84. vB.set(
  85. positions[ b * 3 ],
  86. positions[ b * 3 + 1 ],
  87. positions[ b * 3 + 2 ]
  88. );
  89. vC.set(
  90. positions[ c * 3 ],
  91. positions[ c * 3 + 1 ],
  92. positions[ c * 3 + 2 ]
  93. );
  94. if ( material.side === THREE.BackSide ) {
  95. var intersectionPoint = localRay.intersectTriangle( vC, vB, vA, true );
  96. } else {
  97. var intersectionPoint = localRay.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
  98. }
  99. if ( intersectionPoint === null ) continue;
  100. intersectionPoint.applyMatrix4( object.matrixWorld );
  101. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  102. if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue;
  103. intersects.push( {
  104. distance: distance,
  105. point: intersectionPoint,
  106. face: null,
  107. faceIndex: null,
  108. object: object
  109. } );
  110. }
  111. }
  112. } else {
  113. var offsets = geometry.offsets;
  114. var positions = geometry.attributes.position.array;
  115. var offLength = geometry.offsets.length;
  116. var fl = geometry.attributes.position.array.length;
  117. for ( var i = 0; i < fl; i += 3 ) {
  118. a = i;
  119. b = i + 1;
  120. c = i + 2;
  121. vA.set(
  122. positions[ a * 3 ],
  123. positions[ a * 3 + 1 ],
  124. positions[ a * 3 + 2 ]
  125. );
  126. vB.set(
  127. positions[ b * 3 ],
  128. positions[ b * 3 + 1 ],
  129. positions[ b * 3 + 2 ]
  130. );
  131. vC.set(
  132. positions[ c * 3 ],
  133. positions[ c * 3 + 1 ],
  134. positions[ c * 3 + 2 ]
  135. );
  136. if ( material.side === THREE.BackSide ) {
  137. var intersectionPoint = localRay.intersectTriangle( vC, vB, vA, true );
  138. } else {
  139. var intersectionPoint = localRay.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
  140. }
  141. if ( intersectionPoint === null ) continue;
  142. intersectionPoint.applyMatrix4( object.matrixWorld );
  143. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  144. if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue;
  145. intersects.push( {
  146. distance: distance,
  147. point: intersectionPoint,
  148. face: null,
  149. faceIndex: null,
  150. object: object
  151. } );
  152. }
  153. }
  154. } else if ( geometry instanceof THREE.Geometry ) {
  155. var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  156. var objectMaterials = isFaceMaterial === true ? object.material.materials : null;
  157. var a, b, c, d;
  158. var precision = raycaster.precision;
  159. var vertices = geometry.vertices;
  160. for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  161. var face = geometry.faces[ f ];
  162. var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : object.material;
  163. if ( material === undefined ) continue;
  164. a = vertices[ face.a ];
  165. b = vertices[ face.b ];
  166. c = vertices[ face.c ];
  167. if ( material.morphTargets === true ) {
  168. var morphTargets = geometry.morphTargets;
  169. var morphInfluences = object.morphTargetInfluences;
  170. vA.set( 0, 0, 0 );
  171. vB.set( 0, 0, 0 );
  172. vC.set( 0, 0, 0 );
  173. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  174. var influence = morphInfluences[ t ];
  175. if ( influence === 0 ) continue;
  176. var targets = morphTargets[ t ].vertices;
  177. vA.x += ( targets[ face.a ].x - a.x ) * influence;
  178. vA.y += ( targets[ face.a ].y - a.y ) * influence;
  179. vA.z += ( targets[ face.a ].z - a.z ) * influence;
  180. vB.x += ( targets[ face.b ].x - b.x ) * influence;
  181. vB.y += ( targets[ face.b ].y - b.y ) * influence;
  182. vB.z += ( targets[ face.b ].z - b.z ) * influence;
  183. vC.x += ( targets[ face.c ].x - c.x ) * influence;
  184. vC.y += ( targets[ face.c ].y - c.y ) * influence;
  185. vC.z += ( targets[ face.c ].z - c.z ) * influence;
  186. }
  187. vA.add( a );
  188. vB.add( b );
  189. vC.add( c );
  190. a = vA;
  191. b = vB;
  192. c = vC;
  193. }
  194. if ( material.side === THREE.BackSide ) {
  195. var intersectionPoint = localRay.intersectTriangle( c, b, a, true );
  196. } else {
  197. var intersectionPoint = localRay.intersectTriangle( a, b, c, material.side !== THREE.DoubleSide );
  198. }
  199. if ( intersectionPoint === null ) continue;
  200. intersectionPoint.applyMatrix4( object.matrixWorld );
  201. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  202. if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue;
  203. intersects.push( {
  204. distance: distance,
  205. point: intersectionPoint,
  206. face: face,
  207. faceIndex: f,
  208. object: object
  209. } );
  210. }
  211. }
  212. } else if ( object instanceof THREE.Line ) {
  213. var precision = raycaster.linePrecision;
  214. var precisionSq = precision * precision;
  215. var geometry = object.geometry;
  216. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  217. // Checking boundingSphere distance to ray
  218. sphere.copy( geometry.boundingSphere );
  219. sphere.applyMatrix4( object.matrixWorld );
  220. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  221. return intersects;
  222. }
  223. inverseMatrix.getInverse( object.matrixWorld );
  224. localRay.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  225. /* if ( geometry instanceof THREE.BufferGeometry ) {
  226. } else */ if ( geometry instanceof THREE.Geometry ) {
  227. var vertices = geometry.vertices;
  228. var nbVertices = vertices.length;
  229. var interSegment = new THREE.Vector3();
  230. var interRay = new THREE.Vector3();
  231. var step = object.type === THREE.LineStrip ? 1 : 2;
  232. for ( var i = 0; i < nbVertices - 1; i = i + step ) {
  233. var distSq = localRay.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
  234. if ( distSq > precisionSq ) continue;
  235. var distance = localRay.origin.distanceTo( interRay );
  236. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  237. intersects.push( {
  238. distance: distance,
  239. // What do we want? intersection point on the ray or on the segment??
  240. // point: raycaster.ray.at( distance ),
  241. point: interSegment.clone().applyMatrix4( object.matrixWorld ),
  242. face: null,
  243. faceIndex: null,
  244. object: object
  245. } );
  246. }
  247. }
  248. }
  249. };
  250. var intersectDescendants = function ( object, raycaster, intersects ) {
  251. var descendants = object.getDescendants();
  252. for ( var i = 0, l = descendants.length; i < l; i ++ ) {
  253. intersectObject( descendants[ i ], raycaster, intersects );
  254. }
  255. };
  256. //
  257. THREE.Raycaster.prototype.precision = 0.0001;
  258. THREE.Raycaster.prototype.linePrecision = 1;
  259. THREE.Raycaster.prototype.set = function ( origin, direction ) {
  260. this.ray.set( origin, direction );
  261. // direction is assumed to be normalized (for accurate distance calculations)
  262. };
  263. THREE.Raycaster.prototype.intersectObject = function ( object, recursive ) {
  264. var intersects = [];
  265. if ( recursive === true ) {
  266. intersectDescendants( object, this, intersects );
  267. }
  268. intersectObject( object, this, intersects );
  269. intersects.sort( descSort );
  270. return intersects;
  271. };
  272. THREE.Raycaster.prototype.intersectObjects = function ( objects, recursive ) {
  273. var intersects = [];
  274. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  275. intersectObject( objects[ i ], this, intersects );
  276. if ( recursive === true ) {
  277. intersectDescendants( objects[ i ], this, intersects );
  278. }
  279. }
  280. intersects.sort( descSort );
  281. return intersects;
  282. };
  283. }( THREE ) );