Ray.js 11 KB

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