Ray.js 11 KB

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