Raycaster.js 5.5 KB

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