babylon.ray.js 11 KB

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