Ray.js 10 KB

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