babylon.ray.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
  9. }
  10. // Methods
  11. public intersectsBoxMinMax(minimum: Vector3, maximum: Vector3): boolean {
  12. var d = 0.0;
  13. var maxValue = Number.MAX_VALUE;
  14. var inv: number;
  15. var min: number;
  16. var max: number;
  17. var temp: number;
  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. public intersectsBox(box: BoundingBox): boolean {
  90. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  91. }
  92. public intersectsSphere(sphere): boolean {
  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. public intersectsTriangle(vertex0: Vector3, vertex1: Vector3, vertex2: Vector3): IntersectionInfo {
  109. if (!this._edge1) {
  110. this._edge1 = Vector3.Zero();
  111. this._edge2 = Vector3.Zero();
  112. this._pvec = Vector3.Zero();
  113. this._tvec = Vector3.Zero();
  114. this._qvec = Vector3.Zero();
  115. }
  116. vertex1.subtractToRef(vertex0, this._edge1);
  117. vertex2.subtractToRef(vertex0, this._edge2);
  118. Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  119. var det = 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 = Vector3.Dot(this._tvec, this._pvec) * invdet;
  126. if (bu < 0 || bu > 1.0) {
  127. return null;
  128. }
  129. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  130. var bv = 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 = Vector3.Dot(this._edge2, this._qvec) * invdet;
  136. if (distance > this.length) {
  137. return null;
  138. }
  139. return new IntersectionInfo(bu, bv, distance);
  140. }
  141. public intersectsPlane(plane: Plane): number {
  142. var distance: number;
  143. var result1 = Vector3.Dot(plane.normal, this.direction);
  144. if (Math.abs(result1) < 9.99999997475243E-07) {
  145. return null;
  146. }
  147. else {
  148. var result2 = 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. } else {
  154. return 0;
  155. }
  156. }
  157. return distance;
  158. }
  159. }
  160. // Statics
  161. public static CreateNew(x: number, y: number, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Ray {
  162. var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  163. var end = Vector3.Unproject(new Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  164. var direction = end.subtract(start);
  165. direction.normalize();
  166. return new Ray(start, direction);
  167. }
  168. /**
  169. * 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
  170. * transformed to the given world matrix.
  171. * @param origin The origin point
  172. * @param end The end point
  173. * @param world a matrix to transform the ray to. Default is the identity matrix.
  174. */
  175. public static CreateNewFromTo(origin: Vector3, end: Vector3, world: Matrix = Matrix.Identity()): Ray {
  176. var direction = end.subtract(origin);
  177. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  178. direction.normalize();
  179. return Ray.Transform(new Ray(origin, direction, length), world);
  180. }
  181. public static Transform(ray: Ray, matrix: Matrix): Ray {
  182. var newOrigin = Vector3.TransformCoordinates(ray.origin, matrix);
  183. var newDirection = Vector3.TransformNormal(ray.direction, matrix);
  184. return new Ray(newOrigin, newDirection, ray.length);
  185. }
  186. }
  187. }