babylon.ray.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. module BABYLON {
  2. export class Ray {
  3. private _edge1: Vector3;
  4. private _edge2: Vector3;
  5. private _pvec: Vector3;
  6. private _tvec: Vector3;
  7. private _qvec: Vector3;
  8. private _tmpRay: Ray;
  9. constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
  10. }
  11. // Methods
  12. public intersectsBoxMinMax(minimum: Vector3, maximum: Vector3): boolean {
  13. var d = 0.0;
  14. var maxValue = Number.MAX_VALUE;
  15. var inv: number;
  16. var min: number;
  17. var max: number;
  18. var temp: number;
  19. if (Math.abs(this.direction.x) < 0.0000001) {
  20. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  21. return false;
  22. }
  23. }
  24. else {
  25. inv = 1.0 / this.direction.x;
  26. min = (minimum.x - this.origin.x) * inv;
  27. max = (maximum.x - this.origin.x) * inv;
  28. if (max === -Infinity) {
  29. max = Infinity;
  30. }
  31. if (min > max) {
  32. temp = min;
  33. min = max;
  34. max = temp;
  35. }
  36. d = Math.max(min, d);
  37. maxValue = Math.min(max, maxValue);
  38. if (d > maxValue) {
  39. return false;
  40. }
  41. }
  42. if (Math.abs(this.direction.y) < 0.0000001) {
  43. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  44. return false;
  45. }
  46. }
  47. else {
  48. inv = 1.0 / this.direction.y;
  49. min = (minimum.y - this.origin.y) * inv;
  50. max = (maximum.y - this.origin.y) * inv;
  51. if (max === -Infinity) {
  52. max = Infinity;
  53. }
  54. if (min > max) {
  55. temp = min;
  56. min = max;
  57. max = temp;
  58. }
  59. d = Math.max(min, d);
  60. maxValue = Math.min(max, maxValue);
  61. if (d > maxValue) {
  62. return false;
  63. }
  64. }
  65. if (Math.abs(this.direction.z) < 0.0000001) {
  66. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  67. return false;
  68. }
  69. }
  70. else {
  71. inv = 1.0 / this.direction.z;
  72. min = (minimum.z - this.origin.z) * inv;
  73. max = (maximum.z - this.origin.z) * inv;
  74. if (max === -Infinity) {
  75. max = Infinity;
  76. }
  77. if (min > max) {
  78. temp = min;
  79. min = max;
  80. max = temp;
  81. }
  82. d = Math.max(min, d);
  83. maxValue = Math.min(max, maxValue);
  84. if (d > maxValue) {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. public intersectsBox(box: BoundingBox): boolean {
  91. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  92. }
  93. public intersectsSphere(sphere: BoundingSphere): boolean {
  94. var x = sphere.center.x - this.origin.x;
  95. var y = sphere.center.y - this.origin.y;
  96. var z = sphere.center.z - this.origin.z;
  97. var pyth = (x * x) + (y * y) + (z * z);
  98. var rr = sphere.radius * sphere.radius;
  99. if (pyth <= rr) {
  100. return true;
  101. }
  102. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  103. if (dot < 0.0) {
  104. return false;
  105. }
  106. var temp = pyth - (dot * dot);
  107. return temp <= rr;
  108. }
  109. public intersectsTriangle(vertex0: Vector3, vertex1: Vector3, vertex2: Vector3): Nullable<IntersectionInfo> {
  110. if (!this._edge1) {
  111. this._edge1 = Vector3.Zero();
  112. this._edge2 = Vector3.Zero();
  113. this._pvec = Vector3.Zero();
  114. this._tvec = Vector3.Zero();
  115. this._qvec = Vector3.Zero();
  116. }
  117. vertex1.subtractToRef(vertex0, this._edge1);
  118. vertex2.subtractToRef(vertex0, this._edge2);
  119. Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  120. var det = Vector3.Dot(this._edge1, this._pvec);
  121. if (det === 0) {
  122. return null;
  123. }
  124. var invdet = 1 / det;
  125. this.origin.subtractToRef(vertex0, this._tvec);
  126. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  127. if (bu < 0 || bu > 1.0) {
  128. return null;
  129. }
  130. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  131. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  132. if (bv < 0 || bu + bv > 1.0) {
  133. return null;
  134. }
  135. //check if the distance is longer than the predefined length.
  136. var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
  137. if (distance > this.length) {
  138. return null;
  139. }
  140. return new IntersectionInfo(bu, bv, distance);
  141. }
  142. public intersectsPlane(plane: Plane): Nullable<number> {
  143. var distance: number;
  144. var result1 = Vector3.Dot(plane.normal, this.direction);
  145. if (Math.abs(result1) < 9.99999997475243E-07) {
  146. return null;
  147. }
  148. else {
  149. var result2 = Vector3.Dot(plane.normal, this.origin);
  150. distance = (-plane.d - result2) / result1;
  151. if (distance < 0.0) {
  152. if (distance < -9.99999997475243E-07) {
  153. return null;
  154. } else {
  155. return 0;
  156. }
  157. }
  158. return distance;
  159. }
  160. }
  161. public intersectsMesh(mesh:AbstractMesh, fastCheck?: boolean): PickingInfo {
  162. var tm = Tmp.Matrix[0];
  163. mesh.getWorldMatrix().invertToRef(tm);
  164. if(this._tmpRay){
  165. Ray.TransformToRef(this, tm, this._tmpRay);
  166. }else{
  167. this._tmpRay = Ray.Transform(this, tm);
  168. }
  169. return mesh.intersects(this._tmpRay, fastCheck);
  170. }
  171. public intersectsMeshes(meshes:Array<AbstractMesh>, fastCheck?: boolean, results?:Array<PickingInfo>): Array<PickingInfo> {
  172. if(results){
  173. results.length = 0;
  174. }else{
  175. results = [];
  176. }
  177. for(var i = 0; i < meshes.length; i++){
  178. var pickInfo = this.intersectsMesh(meshes[i], fastCheck);
  179. if(pickInfo.hit){
  180. results.push(pickInfo);
  181. }
  182. }
  183. results.sort(this._comparePickingInfo);
  184. return results;
  185. }
  186. private _comparePickingInfo(pickingInfoA:PickingInfo, pickingInfoB:PickingInfo): number{
  187. if(pickingInfoA.distance < pickingInfoB.distance){
  188. return -1;
  189. }else if(pickingInfoA.distance > pickingInfoB.distance){
  190. return 1;
  191. }else{
  192. return 0;
  193. }
  194. }
  195. private static smallnum = 0.00000001;
  196. private static rayl = 10e8;
  197. /**
  198. * Intersection test between the ray and a given segment whithin a given tolerance (threshold)
  199. * @param sega the first point of the segment to test the intersection against
  200. * @param segb the second point of the segment to test the intersection against
  201. * @param threshold the tolerance margin, if the ray doesn't intersect the segment but is close to the given threshold, the intersection is successful
  202. * @return the distance from the ray origin to the intersection point if there's intersection, or -1 if there's no intersection
  203. */
  204. intersectionSegment(sega: Vector3, segb: Vector3, threshold: number): number {
  205. var rsegb = this.origin.add(this.direction.multiplyByFloats(Ray.rayl, Ray.rayl, Ray.rayl));
  206. var u = segb.subtract(sega);
  207. var v = rsegb.subtract(this.origin);
  208. var w = sega.subtract(this.origin);
  209. var a = Vector3.Dot(u, u); // always >= 0
  210. var b = Vector3.Dot(u, v);
  211. var c = Vector3.Dot(v, v); // always >= 0
  212. var d = Vector3.Dot(u, w);
  213. var e = Vector3.Dot(v, w);
  214. var D = a * c - b * b; // always >= 0
  215. var sc: number, sN: number, sD = D; // sc = sN / sD, default sD = D >= 0
  216. var tc: number, tN: number, tD = D; // tc = tN / tD, default tD = D >= 0
  217. // compute the line parameters of the two closest points
  218. if (D < Ray.smallnum) { // the lines are almost parallel
  219. sN = 0.0; // force using point P0 on segment S1
  220. sD = 1.0; // to prevent possible division by 0.0 later
  221. tN = e;
  222. tD = c;
  223. }
  224. else { // get the closest points on the infinite lines
  225. sN = (b * e - c * d);
  226. tN = (a * e - b * d);
  227. if (sN < 0.0) { // sc < 0 => the s=0 edge is visible
  228. sN = 0.0;
  229. tN = e;
  230. tD = c;
  231. } else if (sN > sD) { // sc > 1 => the s=1 edge is visible
  232. sN = sD;
  233. tN = e + b;
  234. tD = c;
  235. }
  236. }
  237. if (tN < 0.0) { // tc < 0 => the t=0 edge is visible
  238. tN = 0.0;
  239. // recompute sc for this edge
  240. if (-d < 0.0) {
  241. sN = 0.0;
  242. } else if (-d > a)
  243. sN = sD;
  244. else {
  245. sN = -d;
  246. sD = a;
  247. }
  248. } else if (tN > tD) { // tc > 1 => the t=1 edge is visible
  249. tN = tD;
  250. // recompute sc for this edge
  251. if ((-d + b) < 0.0) {
  252. sN = 0;
  253. } else if ((-d + b) > a) {
  254. sN = sD;
  255. } else {
  256. sN = (-d + b);
  257. sD = a;
  258. }
  259. }
  260. // finally do the division to get sc and tc
  261. sc = (Math.abs(sN) < Ray.smallnum ? 0.0 : sN / sD);
  262. tc = (Math.abs(tN) < Ray.smallnum ? 0.0 : tN / tD);
  263. // get the difference of the two closest points
  264. let qtc = v.multiplyByFloats(tc, tc, tc);
  265. var dP = w.add(u.multiplyByFloats(sc, sc, sc)).subtract(qtc); // = S1(sc) - S2(tc)
  266. var isIntersected = (tc > 0) && (tc <= this.length) && (dP.lengthSquared() < (threshold * threshold)); // return intersection result
  267. if (isIntersected) {
  268. return qtc.length();
  269. }
  270. return -1;
  271. }
  272. public update(x: number, y: number, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Ray {
  273. Vector3.UnprojectFloatsToRef(x, y, 0, viewportWidth, viewportHeight, world, view, projection, this.origin);
  274. Vector3.UnprojectFloatsToRef(x, y, 1, viewportWidth, viewportHeight, world, view, projection, Tmp.Vector3[0]);
  275. Tmp.Vector3[0].subtractToRef(this.origin, this.direction);
  276. this.direction.normalize();
  277. return this;
  278. }
  279. // Statics
  280. public static Zero(): Ray {
  281. return new Ray(Vector3.Zero(), Vector3.Zero());
  282. }
  283. public static CreateNew(x: number, y: number, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Ray {
  284. let result = Ray.Zero();
  285. return result.update(x, y, viewportWidth, viewportHeight, world, view, projection);
  286. }
  287. /**
  288. * Function will create a new transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be
  289. * transformed to the given world matrix.
  290. * @param origin The origin point
  291. * @param end The end point
  292. * @param world a matrix to transform the ray to. Default is the identity matrix.
  293. */
  294. public static CreateNewFromTo(origin: Vector3, end: Vector3, world: Matrix = Matrix.Identity()): Ray {
  295. var direction = end.subtract(origin);
  296. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  297. direction.normalize();
  298. return Ray.Transform(new Ray(origin, direction, length), world);
  299. }
  300. public static Transform(ray: Ray, matrix: Matrix): Ray {
  301. var result = new Ray(new Vector3(0,0,0), new Vector3(0,0,0));
  302. Ray.TransformToRef(ray, matrix, result);
  303. return result;
  304. }
  305. public static TransformToRef(ray: Ray, matrix: Matrix, result:Ray): void {
  306. Vector3.TransformCoordinatesToRef(ray.origin, matrix, result.origin);
  307. Vector3.TransformNormalToRef(ray.direction, matrix, result.direction);
  308. result.length = ray.length;
  309. var dir = result.direction;
  310. var len = dir.length();
  311. if(!(len === 0 || len === 1)){
  312. var num = 1.0 / len;
  313. dir.x *= num;
  314. dir.y *= num;
  315. dir.z *= num;
  316. result.length *= len;
  317. }
  318. }
  319. }
  320. }