Raycaster.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.length() > 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 inverseMatrix = new THREE.Matrix4();
  20. var descSort = function ( a, b ) {
  21. return a.distance - b.distance;
  22. };
  23. var intersectObject = function ( object, raycaster, intersects ) {
  24. if ( object instanceof THREE.Particle ) {
  25. var distance = raycaster.ray.distanceToPoint( object.matrixWorld.getPosition() );
  26. if ( distance > object.scale.x ) {
  27. return intersects;
  28. }
  29. intersects.push( {
  30. distance: distance,
  31. point: object.position,
  32. face: null,
  33. object: object
  34. } );
  35. } else if ( object instanceof THREE.Mesh ) {
  36. // Checking boundingSphere distance to ray
  37. sphere.set(
  38. object.matrixWorld.getPosition(),
  39. object.geometry.boundingSphere.radius* object.matrixWorld.getMaxScaleOnAxis() );
  40. if ( ! raycaster.ray.isIntersectionSphere( sphere ) ) {
  41. return intersects;
  42. }
  43. // Checking faces
  44. var geometry = object.geometry;
  45. var vertices = geometry.vertices;
  46. var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  47. var objectMaterials = isFaceMaterial === true ? object.material.materials : null;
  48. var side = object.material.side;
  49. var a, b, c, d;
  50. var precision = raycaster.precision;
  51. object.matrixRotationWorld.extractRotation( object.matrixWorld );
  52. inverseMatrix.getInverse( object.matrixWorld );
  53. localRay.copy( raycaster.ray ).transform( inverseMatrix );
  54. for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  55. var face = geometry.faces[ f ];
  56. var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : object.material;
  57. if ( material === undefined ) continue;
  58. facePlane.setFromNormalAndCoplanarPoint( face.normal, vertices[face.a] );
  59. var planeDistance = localRay.distanceToPlane( facePlane );
  60. // bail if raycaster and plane are parallel
  61. if ( Math.abs( planeDistance ) < precision ) continue;
  62. // if negative distance, then plane is behind raycaster
  63. if ( planeDistance < 0 ) continue;
  64. // check if we hit the wrong side of a single sided face
  65. side = material.side;
  66. if( side !== THREE.DoubleSide ) {
  67. var planeSign = localRay.direction.dot( facePlane.normal );
  68. if( ! ( side === THREE.FrontSide ? planeSign < 0 : planeSign > 0 ) ) continue;
  69. }
  70. // this can be done using the planeDistance from localRay because localRay wasn't normalized, but ray was
  71. if ( planeDistance < raycaster.near || planeDistance > raycaster.far ) continue;
  72. intersectPoint = localRay.at( planeDistance, intersectPoint ); // passing in intersectPoint avoids a copy
  73. if ( face instanceof THREE.Face3 ) {
  74. a = vertices[ face.a ];
  75. b = vertices[ face.b ];
  76. c = vertices[ face.c ];
  77. if ( ! THREE.Triangle.containsPoint( intersectPoint, a, b, c ) ) continue;
  78. } else if ( face instanceof THREE.Face4 ) {
  79. a = vertices[ face.a ];
  80. b = vertices[ face.b ];
  81. c = vertices[ face.c ];
  82. d = vertices[ face.d ];
  83. if ( ( ! THREE.Triangle.containsPoint( intersectPoint, a, b, d ) ) &&
  84. ( ! THREE.Triangle.containsPoint( intersectPoint, b, c, d ) ) ) continue;
  85. } else {
  86. // This is added because if we call out of this if/else group when none of the cases
  87. // match it will add a point to the intersection list erroneously.
  88. throw Error( "face type not supported" );
  89. }
  90. intersects.push( {
  91. distance: planeDistance, // this works because the original ray was normalized, and the transformed localRay wasn't
  92. point: raycaster.ray.at( planeDistance ),
  93. face: face,
  94. faceIndex: f,
  95. object: object
  96. } );
  97. }
  98. }
  99. };
  100. var intersectDescendants = function ( object, raycaster, intersects ) {
  101. var descendants = object.getDescendants();
  102. for ( var i = 0, l = descendants.length; i < l; i ++ ) {
  103. intersectObject( descendants[ i ], raycaster, intersects );
  104. }
  105. };
  106. //
  107. THREE.Raycaster.prototype.precision = 0.0001;
  108. THREE.Raycaster.prototype.set = function ( origin, direction ) {
  109. this.ray.set( origin, direction );
  110. // normalized ray.direction required for accurate distance calculations
  111. if( this.ray.direction.length() > 0 ) {
  112. this.ray.direction.normalize();
  113. }
  114. };
  115. THREE.Raycaster.prototype.intersectObject = function ( object, recursive ) {
  116. var intersects = [];
  117. if ( recursive === true ) {
  118. intersectDescendants( object, this, intersects );
  119. }
  120. intersectObject( object, this, intersects );
  121. intersects.sort( descSort );
  122. return intersects;
  123. };
  124. THREE.Raycaster.prototype.intersectObjects = function ( objects, recursive ) {
  125. var intersects = [];
  126. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  127. intersectObject( objects[ i ], this, intersects );
  128. if ( recursive === true ) {
  129. intersectDescendants( objects[ i ], this, intersects );
  130. }
  131. }
  132. intersects.sort( descSort );
  133. return intersects;
  134. };
  135. }( THREE ) );
粤ICP备19079148号