Ray.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. ( function ( THREE ) {
  5. THREE.Ray = function ( origin, direction, near, far ) {
  6. this.origin = origin || new THREE.Vector3();
  7. this.direction = direction || new THREE.Vector3();
  8. this.near = near || 0;
  9. this.far = far || Infinity;
  10. };
  11. var originCopy = new THREE.Vector3();
  12. var localOriginCopy = new THREE.Vector3();
  13. var localDirectionCopy = new THREE.Vector3();
  14. var vector = new THREE.Vector3();
  15. var normal = new THREE.Vector3();
  16. var intersectPoint = new THREE.Vector3();
  17. var inverseMatrix = new THREE.Matrix4();
  18. var descSort = function ( a, b ) {
  19. return a.distance - b.distance;
  20. };
  21. var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
  22. var distanceFromIntersection = function ( origin, direction, position ) {
  23. v0.sub( position, origin );
  24. var dot = v0.dot( direction );
  25. var intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
  26. var distance = position.distanceTo( intersect );
  27. return distance;
  28. };
  29. // http://www.blackpawn.com/texts/pointinpoly/default.html
  30. var pointInFace3 = function ( p, a, b, c ) {
  31. v0.sub( c, a );
  32. v1.sub( b, a );
  33. v2.sub( p, a );
  34. var dot00 = v0.dot( v0 );
  35. var dot01 = v0.dot( v1 );
  36. var dot02 = v0.dot( v2 );
  37. var dot11 = v1.dot( v1 );
  38. var dot12 = v1.dot( v2 );
  39. var invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 );
  40. var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  41. var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  42. return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 );
  43. };
  44. var intersectObject = function ( object, ray, intersects ) {
  45. if ( object instanceof THREE.Particle ) {
  46. var distance = distanceFromIntersection( ray.origin, ray.direction, object.matrixWorld.getPosition() );
  47. if ( distance > object.scale.x ) {
  48. return intersects;
  49. }
  50. intersects.push( {
  51. distance: distance,
  52. point: object.position,
  53. face: null,
  54. object: object
  55. } );
  56. } else if ( object instanceof THREE.Mesh ) {
  57. // Checking boundingSphere
  58. var scaledRadius = object.geometry.boundingSphere.radius * object.matrixWorld.getMaxScaleOnAxis();
  59. // Checking distance to ray
  60. var distance = distanceFromIntersection( ray.origin, ray.direction, object.matrixWorld.getPosition() );
  61. if ( distance > scaledRadius) {
  62. return intersects;
  63. }
  64. // Checking faces
  65. var geometry = object.geometry;
  66. var vertices = geometry.vertices;
  67. var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  68. var objectMaterials = isFaceMaterial === true ? object.material.materials : null;
  69. var side = object.material.side;
  70. var a, b, c, d;
  71. var precision = ray.precision;
  72. object.matrixRotationWorld.extractRotation( object.matrixWorld );
  73. originCopy.copy( ray.origin );
  74. inverseMatrix.getInverse( object.matrixWorld );
  75. localOriginCopy.copy( originCopy );
  76. inverseMatrix.multiplyVector3( localOriginCopy );
  77. localDirectionCopy.copy( ray.direction );
  78. inverseMatrix.rotateAxis( localDirectionCopy ).normalize();
  79. for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  80. var face = geometry.faces[ f ];
  81. var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : object.material;
  82. if ( material === undefined ) continue;
  83. side = material.side;
  84. vector.sub( face.centroid, localOriginCopy );
  85. var normal = face.normal;
  86. var dot = localDirectionCopy.dot( normal );
  87. // bail if ray and plane are parallel
  88. if ( Math.abs( dot ) < precision ) continue;
  89. // calc distance to plane
  90. var scalar = normal.dot( vector ) / dot;
  91. // if negative distance, then plane is behind ray
  92. if ( scalar < 0 ) continue;
  93. if ( side === THREE.DoubleSide || ( side === THREE.FrontSide ? dot < 0 : dot > 0 ) ) {
  94. intersectPoint.add( localOriginCopy, localDirectionCopy.multiplyScalar( scalar ) );
  95. if ( face instanceof THREE.Face3 ) {
  96. a = vertices[ face.a ];
  97. b = vertices[ face.b ];
  98. c = vertices[ face.c ];
  99. if ( pointInFace3( intersectPoint, a, b, c ) ) {
  100. var point = object.matrixWorld.multiplyVector3( intersectPoint.clone() );
  101. distance = originCopy.distanceTo( point );
  102. if ( distance < ray.near || distance > ray.far ) continue;
  103. intersects.push( {
  104. distance: distance,
  105. point: point,
  106. face: face,
  107. faceIndex: f,
  108. object: object
  109. } );
  110. }
  111. } else if ( face instanceof THREE.Face4 ) {
  112. a = vertices[ face.a ];
  113. b = vertices[ face.b ];
  114. c = vertices[ face.c ];
  115. d = vertices[ face.d ];
  116. if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
  117. var point = object.matrixWorld.multiplyVector3( intersectPoint.clone() );
  118. distance = originCopy.distanceTo( point );
  119. if ( distance < ray.near || distance > ray.far ) continue;
  120. intersects.push( {
  121. distance: distance,
  122. point: point,
  123. face: face,
  124. faceIndex: f,
  125. object: object
  126. } );
  127. }
  128. }
  129. }
  130. }
  131. }
  132. };
  133. var intersectDescendants = function ( object, ray, intersects ) {
  134. var descendants = object.getDescendants();
  135. for ( var i = 0, l = descendants.length; i < l; i ++ ) {
  136. intersectObject( descendants[ i ], ray, intersects );
  137. }
  138. };
  139. //
  140. THREE.Ray.prototype.precision = 0.0001;
  141. THREE.Ray.prototype.set = function ( origin, direction ) {
  142. this.origin = origin;
  143. this.direction = direction;
  144. };
  145. THREE.Ray.prototype.intersectObject = function ( object, recursive ) {
  146. var intersects = [];
  147. if ( recursive === true ) {
  148. intersectDescendants( object, this, intersects );
  149. }
  150. intersectObject( object, this, intersects );
  151. intersects.sort( descSort );
  152. return intersects;
  153. };
  154. THREE.Ray.prototype.intersectObjects = function ( objects, recursive ) {
  155. var intersects = [];
  156. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  157. intersectObject( objects[ i ], this, intersects );
  158. if ( recursive === true ) {
  159. intersectDescendants( objects[ i ], this, intersects );
  160. }
  161. }
  162. intersects.sort( descSort );
  163. return intersects;
  164. };
  165. }( THREE ) );