Ray.js 6.3 KB

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