Raycaster.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author bhouston / http://exocortex.com/
  4. */
  5. ( function ( THREE ) {
  6. THREE.Raycaster = function ( origin, direction, near, far ) {
  7. this.ray = new THREE.Ray( origin, direction );
  8. // normalized ray.direction required for accurate distance calculations
  9. if ( this.ray.direction.lengthSq() > 0 ) {
  10. this.ray.direction.normalize();
  11. }
  12. this.near = near || 0;
  13. this.far = far || Infinity;
  14. };
  15. var sphere = new THREE.Sphere();
  16. var localRay = new THREE.Ray();
  17. var facePlane = new THREE.Plane();
  18. var intersectPoint = new THREE.Vector3();
  19. var matrixPosition = new THREE.Vector3();
  20. var inverseMatrix = new THREE.Matrix4();
  21. var descSort = function ( a, b ) {
  22. return a.distance - b.distance;
  23. };
  24. var intersectObject = function ( object, raycaster, intersects ) {
  25. if ( object instanceof THREE.Particle ) {
  26. matrixPosition.getPositionFromMatrix( object.matrixWorld );
  27. var distance = raycaster.ray.distanceToPoint( matrixPosition );
  28. if ( distance > object.scale.x ) {
  29. return intersects;
  30. }
  31. intersects.push( {
  32. distance: distance,
  33. point: object.position,
  34. face: null,
  35. object: object
  36. } );
  37. } else if ( object instanceof THREE.LOD ) {
  38. matrixPosition.getPositionFromMatrix( object.matrixWorld );
  39. var distance = raycaster.ray.origin.distanceTo( matrixPosition );
  40. intersectObject( object.getObjectForDistance( distance ), raycaster, intersects );
  41. } else if (object instanceof THREE.Mesh ) {
  42. // Checking boundingSphere distance to ray
  43. matrixPosition.getPositionFromMatrix( object.matrixWorld );
  44. sphere.set( matrixPosition, object.geometry.boundingSphere.radius * object.matrixWorld.getMaxScaleOnAxis() );
  45. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  46. return intersects;
  47. }
  48. var geometry = object.geometry;
  49. var vertices = geometry.vertices;
  50. if ( geometry instanceof THREE.BufferGeometry ) {
  51. var material = object.material;
  52. if (material === undefined) return intersects;
  53. if (!geometry.dynamic) return intersects;
  54. var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  55. var objectMaterials = isFaceMaterial === true ? object.material.materials : null;
  56. var side = object.material.side;
  57. var a, b, c;
  58. var precision = raycaster.precision;
  59. inverseMatrix.getInverse(object.matrixWorld);
  60. localRay.copy(raycaster.ray).applyMatrix4(inverseMatrix);
  61. var fl;
  62. var indexed = false;
  63. if (geometry.attributes.index) {
  64. indexed = true;
  65. fl = geometry.attributes.index.numItems / 3;
  66. } else {
  67. fl = geometry.attributes.position.numItems / 9;
  68. }
  69. var vA = new THREE.Vector3();
  70. var vB = new THREE.Vector3();
  71. var vC = new THREE.Vector3();
  72. var vCB = new THREE.Vector3();
  73. var vAB = new THREE.Vector3();
  74. for ( var oi = 0; oi < geometry.offsets.length; ++oi ) {
  75. var start = geometry.offsets[ oi ].start;
  76. var count = geometry.offsets[ oi ].count;
  77. var index = geometry.offsets[ oi ].index;
  78. for ( var i = start, il = start + count; i < il; i += 3 ) {
  79. if ( indexed ) {
  80. a = index + geometry.attributes.index.array[ i ];
  81. b = index + geometry.attributes.index.array[ i + 1 ];
  82. c = index + geometry.attributes.index.array[ i + 2 ];
  83. } else {
  84. a = index;
  85. b = index + 1;
  86. c = index + 2;
  87. }
  88. vA.set( geometry.attributes.position.array[a * 3],
  89. geometry.attributes.position.array[a * 3 + 1],
  90. geometry.attributes.position.array[a * 3 + 2] );
  91. vB.set( geometry.attributes.position.array[b * 3],
  92. geometry.attributes.position.array[b * 3 + 1],
  93. geometry.attributes.position.array[b * 3 + 2] );
  94. vC.set( geometry.attributes.position.array[c * 3],
  95. geometry.attributes.position.array[c * 3 + 1],
  96. geometry.attributes.position.array[c * 3 + 2] );
  97. vCB.subVectors(vC, vB);
  98. vAB.subVectors(vA, vB);
  99. vCB.cross(vAB);
  100. vCB.normalize();
  101. facePlane.setFromNormalAndCoplanarPoint(vCB, vA);
  102. var planeDistance = localRay.distanceToPlane(facePlane);
  103. // bail if raycaster and plane are parallel
  104. if (Math.abs(planeDistance) < precision) continue;
  105. // if negative distance, then plane is behind raycaster
  106. if (planeDistance < 0) continue;
  107. // check if we hit the wrong side of a single sided face
  108. side = material.side;
  109. if (side !== THREE.DoubleSide) {
  110. var planeSign = localRay.direction.dot(facePlane.normal);
  111. if (!(side === THREE.FrontSide ? planeSign < 0 : planeSign > 0)) continue;
  112. }
  113. // this can be done using the planeDistance from localRay because localRay wasn't normalized, but ray was
  114. if (planeDistance < raycaster.near || planeDistance > raycaster.far) continue;
  115. intersectPoint = localRay.at(planeDistance, intersectPoint); // passing in intersectPoint avoids a copy
  116. if (!THREE.Triangle.containsPoint(intersectPoint, vA, vB, vC)) continue;
  117. var face = new THREE.Face3(a, b, c);
  118. if ( geometry.attributes.color != null ) {
  119. var colors = geometry.attributes.color.array;
  120. face.vertexColors[0] = new THREE.Color(colors[a * 3], colors[a * 3 + 1], colors[a * 3 + 2]);
  121. face.vertexColors[1] = new THREE.Color(colors[b * 3], colors[b * 3 + 1], colors[b * 3 + 2]);
  122. face.vertexColors[2] = new THREE.Color(colors[c * 3], colors[c * 3 + 1], colors[c * 3 + 2]);
  123. }
  124. intersects.push({
  125. distance: planeDistance, // this works because the original ray was normalized, and the transformed localRay wasn't
  126. point: raycaster.ray.at(planeDistance),
  127. face: face,
  128. faceIndex: f,
  129. object: object
  130. });
  131. }
  132. }
  133. } else if ( geometry instanceof THREE.Geometry ) {
  134. var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  135. var objectMaterials = isFaceMaterial === true ? object.material.materials : null;
  136. var side = object.material.side;
  137. var a, b, c, d;
  138. var precision = raycaster.precision;
  139. inverseMatrix.getInverse( object.matrixWorld );
  140. localRay.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  141. for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  142. var face = geometry.faces[ f ];
  143. var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : object.material;
  144. if ( material === undefined ) continue;
  145. facePlane.setFromNormalAndCoplanarPoint( face.normal, vertices[face.a] );
  146. var planeDistance = localRay.distanceToPlane( facePlane );
  147. // bail if raycaster and plane are parallel
  148. if ( Math.abs( planeDistance ) < precision ) continue;
  149. // if negative distance, then plane is behind raycaster
  150. if ( planeDistance < 0 ) continue;
  151. // check if we hit the wrong side of a single sided face
  152. side = material.side;
  153. if ( side !== THREE.DoubleSide ) {
  154. var planeSign = localRay.direction.dot( facePlane.normal );
  155. if ( ! ( side === THREE.FrontSide ? planeSign < 0 : planeSign > 0 ) ) continue;
  156. }
  157. // this can be done using the planeDistance from localRay because localRay wasn't normalized, but ray was
  158. if ( planeDistance < raycaster.near || planeDistance > raycaster.far ) continue;
  159. intersectPoint = localRay.at( planeDistance, intersectPoint ); // passing in intersectPoint avoids a copy
  160. if ( face instanceof THREE.Face3 ) {
  161. a = vertices[ face.a ];
  162. b = vertices[ face.b ];
  163. c = vertices[ face.c ];
  164. if ( ! THREE.Triangle.containsPoint( intersectPoint, a, b, c ) ) continue;
  165. } else if ( face instanceof THREE.Face4 ) {
  166. a = vertices[ face.a ];
  167. b = vertices[ face.b ];
  168. c = vertices[ face.c ];
  169. d = vertices[ face.d ];
  170. if ( ( ! THREE.Triangle.containsPoint( intersectPoint, a, b, d ) ) &&
  171. ( ! THREE.Triangle.containsPoint( intersectPoint, b, c, d ) ) ) continue;
  172. } else {
  173. // This is added because if we call out of this if/else group when none of the cases
  174. // match it will add a point to the intersection list erroneously.
  175. throw Error( "face type not supported" );
  176. }
  177. intersects.push( {
  178. distance: planeDistance, // this works because the original ray was normalized, and the transformed localRay wasn't
  179. point: raycaster.ray.at( planeDistance ),
  180. face: face,
  181. faceIndex: f,
  182. object: object
  183. } );
  184. }
  185. }
  186. }
  187. };
  188. var intersectDescendants = function ( object, raycaster, intersects ) {
  189. var descendants = object.getDescendants();
  190. for ( var i = 0, l = descendants.length; i < l; i ++ ) {
  191. intersectObject( descendants[ i ], raycaster, intersects );
  192. }
  193. };
  194. //
  195. THREE.Raycaster.prototype.precision = 0.0001;
  196. THREE.Raycaster.prototype.set = function ( origin, direction ) {
  197. this.ray.set( origin, direction );
  198. // normalized ray.direction required for accurate distance calculations
  199. if ( this.ray.direction.length() > 0 ) {
  200. this.ray.direction.normalize();
  201. }
  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号