Ray.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. import { Vector3 } from './Vector3.js';
  2. const _vector = new Vector3();
  3. const _segCenter = new Vector3();
  4. const _segDir = new Vector3();
  5. const _diff = new Vector3();
  6. const _edge1 = new Vector3();
  7. const _edge2 = new Vector3();
  8. const _normal = new Vector3();
  9. /**
  10. * @author bhouston / http://clara.io
  11. */
  12. function Ray( origin, direction ) {
  13. this.origin = ( origin !== undefined ) ? origin : new Vector3();
  14. this.direction = ( direction !== undefined ) ? direction : new Vector3( 0, 0, - 1 );
  15. }
  16. Object.assign( Ray.prototype, {
  17. set: function ( origin, direction ) {
  18. this.origin.copy( origin );
  19. this.direction.copy( direction );
  20. return this;
  21. },
  22. clone: function () {
  23. return new this.constructor().copy( this );
  24. },
  25. copy: function ( ray ) {
  26. this.origin.copy( ray.origin );
  27. this.direction.copy( ray.direction );
  28. return this;
  29. },
  30. at: function ( t, target ) {
  31. if ( target === undefined ) {
  32. console.warn( 'THREE.Ray: .at() target is now required' );
  33. target = new Vector3();
  34. }
  35. return target.copy( this.direction ).multiplyScalar( t ).add( this.origin );
  36. },
  37. lookAt: function ( v ) {
  38. this.direction.copy( v ).sub( this.origin ).normalize();
  39. return this;
  40. },
  41. recast: function ( t ) {
  42. this.origin.copy( this.at( t, _vector ) );
  43. return this;
  44. },
  45. closestPointToPoint: function ( point, target ) {
  46. if ( target === undefined ) {
  47. console.warn( 'THREE.Ray: .closestPointToPoint() target is now required' );
  48. target = new Vector3();
  49. }
  50. target.subVectors( point, this.origin );
  51. const directionDistance = target.dot( this.direction );
  52. if ( directionDistance < 0 ) {
  53. return target.copy( this.origin );
  54. }
  55. return target.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  56. },
  57. distanceToPoint: function ( point ) {
  58. return Math.sqrt( this.distanceSqToPoint( point ) );
  59. },
  60. distanceSqToPoint: function ( point ) {
  61. const directionDistance = _vector.subVectors( point, this.origin ).dot( this.direction );
  62. // point behind the ray
  63. if ( directionDistance < 0 ) {
  64. return this.origin.distanceToSquared( point );
  65. }
  66. _vector.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  67. return _vector.distanceToSquared( point );
  68. },
  69. distanceSqToSegment: function ( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
  70. // from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteDistRaySegment.h
  71. // It returns the min distance between the ray and the segment
  72. // defined by v0 and v1
  73. // It can also set two optional targets :
  74. // - The closest point on the ray
  75. // - The closest point on the segment
  76. _segCenter.copy( v0 ).add( v1 ).multiplyScalar( 0.5 );
  77. _segDir.copy( v1 ).sub( v0 ).normalize();
  78. _diff.copy( this.origin ).sub( _segCenter );
  79. const segExtent = v0.distanceTo( v1 ) * 0.5;
  80. const a01 = - this.direction.dot( _segDir );
  81. const b0 = _diff.dot( this.direction );
  82. const b1 = - _diff.dot( _segDir );
  83. const c = _diff.lengthSq();
  84. const det = Math.abs( 1 - a01 * a01 );
  85. let s0, s1, sqrDist, extDet;
  86. if ( det > 0 ) {
  87. // The ray and segment are not parallel.
  88. s0 = a01 * b1 - b0;
  89. s1 = a01 * b0 - b1;
  90. extDet = segExtent * det;
  91. if ( s0 >= 0 ) {
  92. if ( s1 >= - extDet ) {
  93. if ( s1 <= extDet ) {
  94. // region 0
  95. // Minimum at interior points of ray and segment.
  96. const invDet = 1 / det;
  97. s0 *= invDet;
  98. s1 *= invDet;
  99. sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c;
  100. } else {
  101. // region 1
  102. s1 = segExtent;
  103. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  104. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  105. }
  106. } else {
  107. // region 5
  108. s1 = - segExtent;
  109. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  110. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  111. }
  112. } else {
  113. if ( s1 <= - extDet ) {
  114. // region 4
  115. s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) );
  116. s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  117. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  118. } else if ( s1 <= extDet ) {
  119. // region 3
  120. s0 = 0;
  121. s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent );
  122. sqrDist = s1 * ( s1 + 2 * b1 ) + c;
  123. } else {
  124. // region 2
  125. s0 = Math.max( 0, - ( a01 * segExtent + b0 ) );
  126. s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  127. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  128. }
  129. }
  130. } else {
  131. // Ray and segment are parallel.
  132. s1 = ( a01 > 0 ) ? - segExtent : segExtent;
  133. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  134. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  135. }
  136. if ( optionalPointOnRay ) {
  137. optionalPointOnRay.copy( this.direction ).multiplyScalar( s0 ).add( this.origin );
  138. }
  139. if ( optionalPointOnSegment ) {
  140. optionalPointOnSegment.copy( _segDir ).multiplyScalar( s1 ).add( _segCenter );
  141. }
  142. return sqrDist;
  143. },
  144. intersectSphere: function ( sphere, target ) {
  145. _vector.subVectors( sphere.center, this.origin );
  146. const tca = _vector.dot( this.direction );
  147. const d2 = _vector.dot( _vector ) - tca * tca;
  148. const radius2 = sphere.radius * sphere.radius;
  149. if ( d2 > radius2 ) return null;
  150. const thc = Math.sqrt( radius2 - d2 );
  151. // t0 = first intersect point - entrance on front of sphere
  152. const t0 = tca - thc;
  153. // t1 = second intersect point - exit point on back of sphere
  154. const t1 = tca + thc;
  155. // test to see if both t0 and t1 are behind the ray - if so, return null
  156. if ( t0 < 0 && t1 < 0 ) return null;
  157. // test to see if t0 is behind the ray:
  158. // if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
  159. // in order to always return an intersect point that is in front of the ray.
  160. if ( t0 < 0 ) return this.at( t1, target );
  161. // else t0 is in front of the ray, so return the first collision point scaled by t0
  162. return this.at( t0, target );
  163. },
  164. intersectsSphere: function ( sphere ) {
  165. return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius );
  166. },
  167. distanceToPlane: function ( plane ) {
  168. const 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. const 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, target ) {
  182. const t = this.distanceToPlane( plane );
  183. if ( t === null ) {
  184. return null;
  185. }
  186. return this.at( t, target );
  187. },
  188. intersectsPlane: function ( plane ) {
  189. // check if the ray lies on the plane first
  190. const distToPoint = plane.distanceToPoint( this.origin );
  191. if ( distToPoint === 0 ) {
  192. return true;
  193. }
  194. const 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, target ) {
  202. let tmin, tmax, tymin, tymax, tzmin, tzmax;
  203. const invdirx = 1 / this.direction.x,
  204. invdiry = 1 / this.direction.y,
  205. invdirz = 1 / this.direction.z;
  206. const 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, target );
  239. },
  240. intersectsBox: function ( box ) {
  241. return this.intersectBox( box, _vector ) !== null;
  242. },
  243. intersectTriangle: function ( a, b, c, backfaceCulling, target ) {
  244. // Compute the offset origin, edges, and normal.
  245. // from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h
  246. _edge1.subVectors( b, a );
  247. _edge2.subVectors( c, a );
  248. _normal.crossVectors( _edge1, _edge2 );
  249. // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
  250. // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
  251. // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
  252. // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
  253. // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
  254. let DdN = this.direction.dot( _normal );
  255. let sign;
  256. if ( DdN > 0 ) {
  257. if ( backfaceCulling ) return null;
  258. sign = 1;
  259. } else if ( DdN < 0 ) {
  260. sign = - 1;
  261. DdN = - DdN;
  262. } else {
  263. return null;
  264. }
  265. _diff.subVectors( this.origin, a );
  266. const DdQxE2 = sign * this.direction.dot( _edge2.crossVectors( _diff, _edge2 ) );
  267. // b1 < 0, no intersection
  268. if ( DdQxE2 < 0 ) {
  269. return null;
  270. }
  271. const DdE1xQ = sign * this.direction.dot( _edge1.cross( _diff ) );
  272. // b2 < 0, no intersection
  273. if ( DdE1xQ < 0 ) {
  274. return null;
  275. }
  276. // b1+b2 > 1, no intersection
  277. if ( DdQxE2 + DdE1xQ > DdN ) {
  278. return null;
  279. }
  280. // Line intersects triangle, check if ray does.
  281. const QdN = - sign * _diff.dot( _normal );
  282. // t < 0, no intersection
  283. if ( QdN < 0 ) {
  284. return null;
  285. }
  286. // Ray intersects triangle.
  287. return this.at( QdN / DdN, target );
  288. },
  289. applyMatrix4: function ( matrix4 ) {
  290. this.origin.applyMatrix4( matrix4 );
  291. this.direction.transformDirection( matrix4 );
  292. return this;
  293. },
  294. equals: function ( ray ) {
  295. return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
  296. }
  297. } );
  298. export { Ray };