Ray.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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, optionalTarget ) {
  24. var result = optionalTarget || new 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. return this;
  30. },
  31. recast: function () {
  32. var v1 = new Vector3();
  33. return function recast( t ) {
  34. this.origin.copy( this.at( t, v1 ) );
  35. return this;
  36. };
  37. }(),
  38. closestPointToPoint: function ( point, optionalTarget ) {
  39. var result = optionalTarget || new Vector3();
  40. result.subVectors( point, this.origin );
  41. var directionDistance = result.dot( this.direction );
  42. if ( directionDistance < 0 ) {
  43. return result.copy( this.origin );
  44. }
  45. return result.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  46. },
  47. distanceToPoint: function ( point ) {
  48. return Math.sqrt( this.distanceSqToPoint( point ) );
  49. },
  50. distanceSqToPoint: function () {
  51. var v1 = new Vector3();
  52. return function distanceSqToPoint( point ) {
  53. var directionDistance = v1.subVectors( point, this.origin ).dot( this.direction );
  54. // point behind the ray
  55. if ( directionDistance < 0 ) {
  56. return this.origin.distanceToSquared( point );
  57. }
  58. v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  59. return v1.distanceToSquared( point );
  60. };
  61. }(),
  62. distanceSqToSegment: function () {
  63. var segCenter = new Vector3();
  64. var segDir = new Vector3();
  65. var diff = new Vector3();
  66. return function 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. var segExtent = v0.distanceTo( v1 ) * 0.5;
  77. var a01 = - this.direction.dot( segDir );
  78. var b0 = diff.dot( this.direction );
  79. var b1 = - diff.dot( segDir );
  80. var c = diff.lengthSq();
  81. var det = Math.abs( 1 - a01 * a01 );
  82. var 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. var 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. }(),
  142. intersectSphere: function () {
  143. var v1 = new Vector3();
  144. return function intersectSphere( sphere, optionalTarget ) {
  145. v1.subVectors( sphere.center, this.origin );
  146. var tca = v1.dot( this.direction );
  147. var d2 = v1.dot( v1 ) - tca * tca;
  148. var radius2 = sphere.radius * sphere.radius;
  149. if ( d2 > radius2 ) return null;
  150. var thc = Math.sqrt( radius2 - d2 );
  151. // t0 = first intersect point - entrance on front of sphere
  152. var t0 = tca - thc;
  153. // t1 = second intersect point - exit point on back of sphere
  154. var 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, optionalTarget );
  161. // else t0 is in front of the ray, so return the first collision point scaled by t0
  162. return this.at( t0, optionalTarget );
  163. };
  164. }(),
  165. intersectsSphere: function ( sphere ) {
  166. return this.distanceToPoint( sphere.center ) <= sphere.radius;
  167. },
  168. distanceToPlane: function ( plane ) {
  169. var denominator = plane.normal.dot( this.direction );
  170. if ( denominator === 0 ) {
  171. // line is coplanar, return origin
  172. if ( plane.distanceToPoint( this.origin ) === 0 ) {
  173. return 0;
  174. }
  175. // Null is preferable to undefined since undefined means.... it is undefined
  176. return null;
  177. }
  178. var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
  179. // Return if the ray never intersects the plane
  180. return t >= 0 ? t : null;
  181. },
  182. intersectPlane: function ( plane, optionalTarget ) {
  183. var t = this.distanceToPlane( plane );
  184. if ( t === null ) {
  185. return null;
  186. }
  187. return this.at( t, optionalTarget );
  188. },
  189. intersectsPlane: function ( plane ) {
  190. // check if the ray lies on the plane first
  191. var distToPoint = plane.distanceToPoint( this.origin );
  192. if ( distToPoint === 0 ) {
  193. return true;
  194. }
  195. var denominator = plane.normal.dot( this.direction );
  196. if ( denominator * distToPoint < 0 ) {
  197. return true;
  198. }
  199. // ray origin is behind the plane (and is pointing behind it)
  200. return false;
  201. },
  202. intersectBox: function ( box, optionalTarget ) {
  203. var tmin, tmax, tymin, tymax, tzmin, tzmax;
  204. var invdirx = 1 / this.direction.x,
  205. invdiry = 1 / this.direction.y,
  206. invdirz = 1 / this.direction.z;
  207. var origin = this.origin;
  208. if ( invdirx >= 0 ) {
  209. tmin = ( box.min.x - origin.x ) * invdirx;
  210. tmax = ( box.max.x - origin.x ) * invdirx;
  211. } else {
  212. tmin = ( box.max.x - origin.x ) * invdirx;
  213. tmax = ( box.min.x - origin.x ) * invdirx;
  214. }
  215. if ( invdiry >= 0 ) {
  216. tymin = ( box.min.y - origin.y ) * invdiry;
  217. tymax = ( box.max.y - origin.y ) * invdiry;
  218. } else {
  219. tymin = ( box.max.y - origin.y ) * invdiry;
  220. tymax = ( box.min.y - origin.y ) * invdiry;
  221. }
  222. if ( ( tmin > tymax ) || ( tymin > tmax ) ) return null;
  223. // These lines also handle the case where tmin or tmax is NaN
  224. // (result of 0 * Infinity). x !== x returns true if x is NaN
  225. if ( tymin > tmin || tmin !== tmin ) tmin = tymin;
  226. if ( tymax < tmax || tmax !== tmax ) tmax = tymax;
  227. if ( invdirz >= 0 ) {
  228. tzmin = ( box.min.z - origin.z ) * invdirz;
  229. tzmax = ( box.max.z - origin.z ) * invdirz;
  230. } else {
  231. tzmin = ( box.max.z - origin.z ) * invdirz;
  232. tzmax = ( box.min.z - origin.z ) * invdirz;
  233. }
  234. if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) return null;
  235. if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin;
  236. if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax;
  237. //return point closest to the ray (positive side)
  238. if ( tmax < 0 ) return null;
  239. return this.at( tmin >= 0 ? tmin : tmax, optionalTarget );
  240. },
  241. intersectsBox: ( function () {
  242. var v = new Vector3();
  243. return function intersectsBox( box ) {
  244. return this.intersectBox( box, v ) !== null;
  245. };
  246. } )(),
  247. intersectTriangle: function () {
  248. // Compute the offset origin, edges, and normal.
  249. var diff = new Vector3();
  250. var edge1 = new Vector3();
  251. var edge2 = new Vector3();
  252. var normal = new Vector3();
  253. return function intersectTriangle( a, b, c, backfaceCulling, optionalTarget ) {
  254. // from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h
  255. edge1.subVectors( b, a );
  256. edge2.subVectors( c, a );
  257. normal.crossVectors( edge1, edge2 );
  258. // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
  259. // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
  260. // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
  261. // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
  262. // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
  263. var DdN = this.direction.dot( normal );
  264. var sign;
  265. if ( DdN > 0 ) {
  266. if ( backfaceCulling ) return null;
  267. sign = 1;
  268. } else if ( DdN < 0 ) {
  269. sign = - 1;
  270. DdN = - DdN;
  271. } else {
  272. return null;
  273. }
  274. diff.subVectors( this.origin, a );
  275. var DdQxE2 = sign * this.direction.dot( edge2.crossVectors( diff, edge2 ) );
  276. // b1 < 0, no intersection
  277. if ( DdQxE2 < 0 ) {
  278. return null;
  279. }
  280. var DdE1xQ = sign * this.direction.dot( edge1.cross( diff ) );
  281. // b2 < 0, no intersection
  282. if ( DdE1xQ < 0 ) {
  283. return null;
  284. }
  285. // b1+b2 > 1, no intersection
  286. if ( DdQxE2 + DdE1xQ > DdN ) {
  287. return null;
  288. }
  289. // Line intersects triangle, check if ray does.
  290. var QdN = - sign * diff.dot( normal );
  291. // t < 0, no intersection
  292. if ( QdN < 0 ) {
  293. return null;
  294. }
  295. // Ray intersects triangle.
  296. return this.at( QdN / DdN, optionalTarget );
  297. };
  298. }(),
  299. applyMatrix4: function ( matrix4 ) {
  300. this.origin.applyMatrix4( matrix4 );
  301. this.direction.transformDirection( matrix4 );
  302. return this;
  303. },
  304. equals: function ( ray ) {
  305. return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
  306. }
  307. } );
  308. export { Ray };
粤ICP备19079148号