Ray.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Ray = function ( origin, direction ) {
  5. this.origin = ( origin !== undefined ) ? origin : new THREE.Vector3();
  6. this.direction = ( direction !== undefined ) ? direction : new THREE.Vector3();
  7. };
  8. THREE.Ray.prototype = {
  9. constructor: THREE.Ray,
  10. set: function ( origin, direction ) {
  11. this.origin.copy( origin );
  12. this.direction.copy( direction );
  13. return this;
  14. },
  15. copy: function ( ray ) {
  16. this.origin.copy( ray.origin );
  17. this.direction.copy( ray.direction );
  18. return this;
  19. },
  20. at: function ( t, optionalTarget ) {
  21. var result = optionalTarget || new THREE.Vector3();
  22. return result.copy( this.direction ).multiplyScalar( t ).add( this.origin );
  23. },
  24. recast: function () {
  25. var v1 = new THREE.Vector3();
  26. return function ( t ) {
  27. this.origin.copy( this.at( t, v1 ) );
  28. return this;
  29. };
  30. }(),
  31. closestPointToPoint: function ( point, optionalTarget ) {
  32. var result = optionalTarget || new THREE.Vector3();
  33. result.subVectors( point, this.origin );
  34. var directionDistance = result.dot( this.direction );
  35. if ( directionDistance < 0 ) {
  36. return result.copy( this.origin );
  37. }
  38. return result.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  39. },
  40. distanceToPoint: function () {
  41. var v1 = new THREE.Vector3();
  42. return function ( point ) {
  43. var directionDistance = v1.subVectors( point, this.origin ).dot( this.direction );
  44. // point behind the ray
  45. if ( directionDistance < 0 ) {
  46. return this.origin.distanceTo( point );
  47. }
  48. v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  49. return v1.distanceTo( point );
  50. };
  51. }(),
  52. distanceSqToSegment: function () {
  53. var segCenter = new THREE.Vector3();
  54. var segDir = new THREE.Vector3();
  55. var diff = new THREE.Vector3();
  56. return function ( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
  57. // from http://www.geometrictools.com/LibMathematics/Distance/Wm5DistRay3Segment3.cpp
  58. // It returns the min distance between the ray and the segment
  59. // defined by v0 and v1
  60. // It can also set two optional targets :
  61. // - The closest point on the ray
  62. // - The closest point on the segment
  63. segCenter.copy( v0 ).add( v1 ).multiplyScalar( 0.5 );
  64. segDir.copy( v1 ).sub( v0 ).normalize();
  65. diff.copy( this.origin ).sub( segCenter );
  66. var segExtent = v0.distanceTo( v1 ) * 0.5;
  67. var a01 = - this.direction.dot( segDir );
  68. var b0 = diff.dot( this.direction );
  69. var b1 = - diff.dot( segDir );
  70. var c = diff.lengthSq();
  71. var det = Math.abs( 1 - a01 * a01 );
  72. var s0, s1, sqrDist, extDet;
  73. if ( det > 0 ) {
  74. // The ray and segment are not parallel.
  75. s0 = a01 * b1 - b0;
  76. s1 = a01 * b0 - b1;
  77. extDet = segExtent * det;
  78. if ( s0 >= 0 ) {
  79. if ( s1 >= - extDet ) {
  80. if ( s1 <= extDet ) {
  81. // region 0
  82. // Minimum at interior points of ray and segment.
  83. var invDet = 1 / det;
  84. s0 *= invDet;
  85. s1 *= invDet;
  86. sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c;
  87. } else {
  88. // region 1
  89. s1 = segExtent;
  90. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  91. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  92. }
  93. } else {
  94. // region 5
  95. s1 = - segExtent;
  96. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  97. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  98. }
  99. } else {
  100. if ( s1 <= - extDet ) {
  101. // region 4
  102. s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) );
  103. s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  104. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  105. } else if ( s1 <= extDet ) {
  106. // region 3
  107. s0 = 0;
  108. s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent );
  109. sqrDist = s1 * ( s1 + 2 * b1 ) + c;
  110. } else {
  111. // region 2
  112. s0 = Math.max( 0, - ( a01 * segExtent + b0 ) );
  113. s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  114. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  115. }
  116. }
  117. } else {
  118. // Ray and segment are parallel.
  119. s1 = ( a01 > 0 ) ? - segExtent : segExtent;
  120. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  121. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  122. }
  123. if ( optionalPointOnRay ) {
  124. optionalPointOnRay.copy( this.direction ).multiplyScalar( s0 ).add( this.origin );
  125. }
  126. if ( optionalPointOnSegment ) {
  127. optionalPointOnSegment.copy( segDir ).multiplyScalar( s1 ).add( segCenter );
  128. }
  129. return sqrDist;
  130. };
  131. }(),
  132. isIntersectionSphere: function ( sphere ) {
  133. return this.distanceToPoint( sphere.center ) <= sphere.radius;
  134. },
  135. intersectSphere: function () {
  136. // from http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-sphere-intersection/
  137. var v1 = new THREE.Vector3();
  138. return function ( sphere, optionalTarget ) {
  139. v1.subVectors( sphere.center, this.origin );
  140. var tca = v1.dot( this.direction );
  141. var d2 = v1.dot( v1 ) - tca * tca;
  142. var radius2 = sphere.radius * sphere.radius;
  143. if ( d2 > radius2 ) return null;
  144. var thc = Math.sqrt( radius2 - d2 );
  145. // t0 = first intersect point - entrance on front of sphere
  146. var t0 = tca - thc;
  147. // t1 = second intersect point - exit point on back of sphere
  148. var t1 = tca + thc;
  149. // test to see if both t0 and t1 are behind the ray - if so, return null
  150. if ( t0 < 0 && t1 < 0 ) return null;
  151. // test to see if t0 is behind the ray:
  152. // if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
  153. // in order to always return an intersect point that is in front of the ray.
  154. if ( t0 < 0 ) return this.at( t1, optionalTarget );
  155. // else t0 is in front of the ray, so return the first collision point scaled by t0
  156. return this.at( t0, optionalTarget );
  157. }
  158. }(),
  159. isIntersectionPlane: function ( plane ) {
  160. // check if the ray lies on the plane first
  161. var distToPoint = plane.distanceToPoint( this.origin );
  162. if ( distToPoint === 0 ) {
  163. return true;
  164. }
  165. var denominator = plane.normal.dot( this.direction );
  166. if ( denominator * distToPoint < 0 ) {
  167. return true;
  168. }
  169. // ray origin is behind the plane (and is pointing behind it)
  170. return false;
  171. },
  172. distanceToPlane: function ( plane ) {
  173. var denominator = plane.normal.dot( this.direction );
  174. if ( denominator == 0 ) {
  175. // line is coplanar, return origin
  176. if ( plane.distanceToPoint( this.origin ) == 0 ) {
  177. return 0;
  178. }
  179. // Null is preferable to undefined since undefined means.... it is undefined
  180. return null;
  181. }
  182. var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
  183. // Return if the ray never intersects the plane
  184. return t >= 0 ? t : null;
  185. },
  186. intersectPlane: function ( plane, optionalTarget ) {
  187. var t = this.distanceToPlane( plane );
  188. if ( t === null ) {
  189. return null;
  190. }
  191. return this.at( t, optionalTarget );
  192. },
  193. isIntersectionBox: function () {
  194. var v = new THREE.Vector3();
  195. return function ( box ) {
  196. return this.intersectBox( box, v ) !== null;
  197. };
  198. }(),
  199. intersectBox: function ( box , optionalTarget ) {
  200. // http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-box-intersection/
  201. var tmin,tmax,tymin,tymax,tzmin,tzmax;
  202. var invdirx = 1 / this.direction.x,
  203. invdiry = 1 / this.direction.y,
  204. invdirz = 1 / this.direction.z;
  205. var origin = this.origin;
  206. if ( invdirx >= 0 ) {
  207. tmin = ( box.min.x - origin.x ) * invdirx;
  208. tmax = ( box.max.x - origin.x ) * invdirx;
  209. } else {
  210. tmin = ( box.max.x - origin.x ) * invdirx;
  211. tmax = ( box.min.x - origin.x ) * invdirx;
  212. }
  213. if ( invdiry >= 0 ) {
  214. tymin = ( box.min.y - origin.y ) * invdiry;
  215. tymax = ( box.max.y - origin.y ) * invdiry;
  216. } else {
  217. tymin = ( box.max.y - origin.y ) * invdiry;
  218. tymax = ( box.min.y - origin.y ) * invdiry;
  219. }
  220. if ( ( tmin > tymax ) || ( tymin > tmax ) ) return null;
  221. // These lines also handle the case where tmin or tmax is NaN
  222. // (result of 0 * Infinity). x !== x returns true if x is NaN
  223. if ( tymin > tmin || tmin !== tmin ) tmin = tymin;
  224. if ( tymax < tmax || tmax !== tmax ) tmax = tymax;
  225. if ( invdirz >= 0 ) {
  226. tzmin = ( box.min.z - origin.z ) * invdirz;
  227. tzmax = ( box.max.z - origin.z ) * invdirz;
  228. } else {
  229. tzmin = ( box.max.z - origin.z ) * invdirz;
  230. tzmax = ( box.min.z - origin.z ) * invdirz;
  231. }
  232. if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) return null;
  233. if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin;
  234. if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax;
  235. //return point closest to the ray (positive side)
  236. if ( tmax < 0 ) return null;
  237. return this.at( tmin >= 0 ? tmin : tmax, optionalTarget );
  238. },
  239. intersectTriangle: function () {
  240. // Compute the offset origin, edges, and normal.
  241. var diff = new THREE.Vector3();
  242. var edge1 = new THREE.Vector3();
  243. var edge2 = new THREE.Vector3();
  244. var normal = new THREE.Vector3();
  245. return function ( a, b, c, backfaceCulling, optionalTarget ) {
  246. // from http://www.geometrictools.com/LibMathematics/Intersection/Wm5IntrRay3Triangle3.cpp
  247. edge1.subVectors( b, a );
  248. edge2.subVectors( c, a );
  249. normal.crossVectors( edge1, edge2 );
  250. // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
  251. // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
  252. // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
  253. // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
  254. // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
  255. var DdN = this.direction.dot( normal );
  256. var sign;
  257. if ( DdN > 0 ) {
  258. if ( backfaceCulling ) return null;
  259. sign = 1;
  260. } else if ( DdN < 0 ) {
  261. sign = - 1;
  262. DdN = - DdN;
  263. } else {
  264. return null;
  265. }
  266. diff.subVectors( this.origin, a );
  267. var DdQxE2 = sign * this.direction.dot( edge2.crossVectors( diff, edge2 ) );
  268. // b1 < 0, no intersection
  269. if ( DdQxE2 < 0 ) {
  270. return null;
  271. }
  272. var DdE1xQ = sign * this.direction.dot( edge1.cross( diff ) );
  273. // b2 < 0, no intersection
  274. if ( DdE1xQ < 0 ) {
  275. return null;
  276. }
  277. // b1+b2 > 1, no intersection
  278. if ( DdQxE2 + DdE1xQ > DdN ) {
  279. return null;
  280. }
  281. // Line intersects triangle, check if ray does.
  282. var QdN = - sign * diff.dot( normal );
  283. // t < 0, no intersection
  284. if ( QdN < 0 ) {
  285. return null;
  286. }
  287. // Ray intersects triangle.
  288. return this.at( QdN / DdN, optionalTarget );
  289. };
  290. }(),
  291. applyMatrix4: function ( matrix4 ) {
  292. this.direction.add( this.origin ).applyMatrix4( matrix4 );
  293. this.origin.applyMatrix4( matrix4 );
  294. this.direction.sub( this.origin );
  295. this.direction.normalize();
  296. return this;
  297. },
  298. equals: function ( ray ) {
  299. return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
  300. },
  301. clone: function () {
  302. return new THREE.Ray().copy( this );
  303. }
  304. };