babylon.ray.ts 14 KB

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