Ray.js 6.3 KB

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