babylon.ray.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // Statics
  142. Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  143. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  144. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  145. var direction = end.subtract(start);
  146. direction.normalize();
  147. return new Ray(start, direction);
  148. };
  149. /**
  150. * 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
  151. * transformed to the given world matrix.
  152. * @param origin The origin point
  153. * @param end The end point
  154. * @param world a matrix to transform the ray to. Default is the identity matrix.
  155. */
  156. Ray.CreateNewFromTo = function (origin, end, world) {
  157. if (world === void 0) { world = BABYLON.Matrix.Identity(); }
  158. var direction = end.subtract(origin);
  159. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  160. direction.normalize();
  161. return Ray.Transform(new Ray(origin, direction, length), world);
  162. };
  163. Ray.Transform = function (ray, matrix) {
  164. var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
  165. var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
  166. return new Ray(newOrigin, newDirection, ray.length);
  167. };
  168. return Ray;
  169. })();
  170. BABYLON.Ray = Ray;
  171. })(BABYLON || (BABYLON = {}));