Ray.js 11 KB

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