babylon.math.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. ////////////////////////////////// Ray //////////////////////////////////
  4. BABYLON.Ray = function (origin, direction) {
  5. this.origin = origin;
  6. this.direction = direction;
  7. };
  8. // Methods
  9. BABYLON.Ray.prototype.intersectsSphere = function (sphere) {
  10. var x = sphere.center.x - this.origin.x;
  11. var y = sphere.center.y - this.origin.y;
  12. var z = sphere.center.z - this.origin.z;
  13. var pyth = (x * x) + (y * y) + (z * z);
  14. var rr = sphere.radius * sphere.radius;
  15. if (pyth <= rr) {
  16. return true;
  17. }
  18. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  19. if (dot < 0.0) {
  20. return false;
  21. }
  22. var temp = pyth - (dot * dot);
  23. return temp <= rr;
  24. };
  25. BABYLON.Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  26. var edge1 = vertex1.subtract(vertex0);
  27. var edge2 = vertex2.subtract(vertex0);
  28. var pvec = BABYLON.Vector3.Cross(this.direction, edge2);
  29. var det = BABYLON.Vector3.Dot(edge1, pvec);
  30. if (det === 0) {
  31. return {
  32. hit: false,
  33. distance: 0,
  34. bu: 0,
  35. bv: 0
  36. };
  37. }
  38. var invdet = 1 / det;
  39. var tvec = this.origin.subtract(vertex0);
  40. var bu = BABYLON.Vector3.Dot(tvec, pvec) * invdet;
  41. if (bu < 0 || bu > 1.0) {
  42. return {
  43. hit: false,
  44. distance: 0,
  45. bu: bu,
  46. bv: 0
  47. };
  48. }
  49. var qvec = BABYLON.Vector3.Cross(tvec, edge1);
  50. bv = BABYLON.Vector3.Dot(this.direction, qvec) * invdet;
  51. if (bv < 0 || bu + bv > 1.0) {
  52. return {
  53. hit: false,
  54. distance: 0,
  55. bu: bu,
  56. bv: bv
  57. };
  58. }
  59. distance = BABYLON.Vector3.Dot(edge2, qvec) * invdet;
  60. return {
  61. hit: true,
  62. distance: distance,
  63. bu: bu,
  64. bv: bv
  65. };
  66. };
  67. // Statics
  68. BABYLON.Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  69. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  70. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  71. var direction = end.subtract(start);
  72. direction.normalize();
  73. return new BABYLON.Ray(start, direction);
  74. };
  75. ////////////////////////////////// Color3 //////////////////////////////////
  76. BABYLON.Color3 = function (initialR, initialG, initialB) {
  77. this.r = initialR;
  78. this.g = initialG;
  79. this.b = initialB;
  80. };
  81. BABYLON.Color3.prototype.toString = function () {
  82. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  83. };
  84. // Operators
  85. BABYLON.Color3.prototype.multiply = function (otherColor) {
  86. return new BABYLON.Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  87. };
  88. BABYLON.Color3.prototype.equals = function (otherColor) {
  89. return this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  90. };
  91. BABYLON.Color3.prototype.scale = function (scale) {
  92. return new BABYLON.Color3(this.r * scale, this.g * scale, this.b * scale);
  93. };
  94. BABYLON.Color3.prototype.clone = function () {
  95. return new BABYLON.Color3(this.r, this.g, this.b);
  96. };
  97. // Statics
  98. BABYLON.Color3.FromArray = function (array) {
  99. return new BABYLON.Color3(array[0], array[1], array[2]);
  100. };
  101. ////////////////////////////////// Color4 //////////////////////////////////
  102. BABYLON.Color4 = function (initialR, initialG, initialB, initialA) {
  103. this.r = initialR;
  104. this.g = initialG;
  105. this.b = initialB;
  106. this.a = initialA;
  107. };
  108. // Operators
  109. BABYLON.Color4.prototype.add = function (right) {
  110. return new BABYLON.Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  111. };
  112. BABYLON.Color4.prototype.subtract = function (right) {
  113. return new BABYLON.Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  114. };
  115. BABYLON.Color4.prototype.scale = function (scale) {
  116. return new BABYLON.Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  117. };
  118. BABYLON.Color4.prototype.toString = function () {
  119. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  120. };
  121. BABYLON.Color4.prototype.clone = function () {
  122. return new BABYLON.Color4(this.r, this.g, this.b, this.a);
  123. };
  124. // Statics
  125. BABYLON.Color4.Lerp = function(left, right, amount) {
  126. var r = left.r + (right.r - left.r) * amount;
  127. var g = left.g + (right.g - left.g) * amount;
  128. var b = left.b + (right.b - left.b) * amount;
  129. var a = left.a + (right.a - left.a) * amount;
  130. return new BABYLON.Color4(r, g, b, a);
  131. };
  132. BABYLON.Color4.FromArray = function (array, offset) {
  133. if (!offset) {
  134. offset = 0;
  135. }
  136. return new BABYLON.Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  137. };
  138. ////////////////////////////////// Vector2 //////////////////////////////////
  139. BABYLON.Vector2 = function (initialX, initialY) {
  140. this.x = initialX;
  141. this.y = initialY;
  142. };
  143. BABYLON.Vector2.prototype.toString = function () {
  144. return "{X: " + this.x + " Y:" + this.y + "}";
  145. };
  146. // Operators
  147. BABYLON.Vector2.prototype.add = function (otherVector) {
  148. return new BABYLON.Vector2(this.x + otherVector.x, this.y + otherVector.y);
  149. };
  150. BABYLON.Vector2.prototype.subtract = function (otherVector) {
  151. return new BABYLON.Vector2(this.x - otherVector.x, this.y - otherVector.y);
  152. };
  153. BABYLON.Vector2.prototype.negate = function () {
  154. return new BABYLON.Vector2(-this.x, -this.y);
  155. };
  156. BABYLON.Vector2.prototype.scale = function (scale) {
  157. return new BABYLON.Vector2(this.x * scale, this.y * scale);
  158. };
  159. BABYLON.Vector2.prototype.equals = function (otherVector) {
  160. return this.x === otherVector.x && this.y === otherVector.y;
  161. };
  162. // Properties
  163. BABYLON.Vector2.prototype.length = function () {
  164. return Math.sqrt(this.x * this.x + this.y * this.y);
  165. };
  166. BABYLON.Vector2.prototype.lengthSquared = function () {
  167. return (this.x * this.x + this.y * this.y);
  168. };
  169. // Methods
  170. BABYLON.Vector2.prototype.normalize = function () {
  171. var len = this.length();
  172. if (len === 0)
  173. return;
  174. var num = 1.0 / len;
  175. this.x *= num;
  176. this.y *= num;
  177. };
  178. BABYLON.Vector2.prototype.clone = function () {
  179. return new BABYLON.Vector2(this.x, this.y);
  180. };
  181. // Statics
  182. BABYLON.Vector2.Zero = function () {
  183. return new BABYLON.Vector2(0, 0);
  184. };
  185. BABYLON.Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  186. var squared = amount * amount;
  187. var cubed = amount * squared;
  188. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  189. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  190. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  191. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  192. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  193. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  194. return new BABYLON.Vector2(x, y);
  195. };
  196. BABYLON.Vector2.Clamp = function (value, min, max) {
  197. var x = value.x;
  198. x = (x > max.x) ? max.x : x;
  199. x = (x < min.x) ? min.x : x;
  200. var y = value.y;
  201. y = (y > max.y) ? max.y : y;
  202. y = (y < min.y) ? min.y : y;
  203. return new BABYLON.Vector2(x, y);
  204. };
  205. BABYLON.Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  206. var squared = amount * amount;
  207. var cubed = amount * squared;
  208. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  209. var part2 = (-2.0 * cubed) + (3.0 * squared);
  210. var part3 = (cubed - (2.0 * squared)) + amount;
  211. var part4 = cubed - squared;
  212. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  213. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  214. return new BABYLON.Vector2(x, y);
  215. };
  216. BABYLON.Vector2.Lerp = function (start, end, amount) {
  217. var x = start.x + ((end.x - start.x) * amount);
  218. var y = start.y + ((end.y - start.y) * amount);
  219. return new BABYLON.Vector2(x, y);
  220. };
  221. BABYLON.Vector2.Dot = function (left, right) {
  222. return left.x * right.x + left.y * right.y;
  223. };
  224. BABYLON.Vector2.Normalize = function (vector) {
  225. var newVector = vector.clone();
  226. newVector.normalize();
  227. return newVector;
  228. };
  229. BABYLON.Vector2.Minimize = function (left, right) {
  230. var x = (left.x < right.x) ? left.x : right.x;
  231. var y = (left.y < right.y) ? left.y : right.y;
  232. return new BABYLON.Vector2(x, y);
  233. };
  234. BABYLON.Vector2.Maximize = function (left, right) {
  235. var x = (left.x > right.x) ? left.x : right.x;
  236. var y = (left.y > right.y) ? left.y : right.y;
  237. return new BABYLON.Vector2(x, y);
  238. };
  239. BABYLON.Vector2.Transform = function (vector, transformation) {
  240. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  241. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  242. return new BABYLON.Vector2(x, y);
  243. };
  244. BABYLON.Vector2.Distance = function (value1, value2) {
  245. return Math.sqrt(BABYLON.Vector2.DistanceSquared(value1, value2));
  246. };
  247. BABYLON.Vector2.DistanceSquared = function (value1, value2) {
  248. var x = value1.x - value2.x;
  249. var y = value1.y - value2.y;
  250. return (x * x) + (y * y);
  251. };
  252. ////////////////////////////////// Vector3 //////////////////////////////////
  253. BABYLON.Vector3 = function (initialX, initialY, initialZ) {
  254. this.x = initialX;
  255. this.y = initialY;
  256. this.z = initialZ;
  257. };
  258. BABYLON.Vector3.prototype.toString = function () {
  259. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  260. };
  261. // Operators
  262. BABYLON.Vector3.prototype.add = function (otherVector) {
  263. return new BABYLON.Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  264. };
  265. BABYLON.Vector3.prototype.subtract = function (otherVector) {
  266. return new BABYLON.Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  267. };
  268. BABYLON.Vector3.prototype.negate = function () {
  269. return new BABYLON.Vector3(-this.x, -this.y, -this.z);
  270. };
  271. BABYLON.Vector3.prototype.scale = function (scale) {
  272. return new BABYLON.Vector3(this.x * scale, this.y * scale, this.z * scale);
  273. };
  274. BABYLON.Vector3.prototype.equals = function (otherVector) {
  275. return this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  276. };
  277. BABYLON.Vector3.prototype.multiply = function (otherVector) {
  278. return new BABYLON.Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  279. };
  280. BABYLON.Vector3.prototype.divide = function (otherVector) {
  281. return new BABYLON.Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  282. };
  283. // Properties
  284. BABYLON.Vector3.prototype.length = function () {
  285. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  286. };
  287. BABYLON.Vector3.prototype.lengthSquared = function () {
  288. return (this.x * this.x + this.y * this.y + this.z * this.z);
  289. };
  290. // Methods
  291. BABYLON.Vector3.prototype.normalize = function () {
  292. var len = this.length();
  293. if (len === 0)
  294. return;
  295. var num = 1.0 / len;
  296. this.x *= num;
  297. this.y *= num;
  298. this.z *= num;
  299. };
  300. BABYLON.Vector3.prototype.clone = function () {
  301. return new BABYLON.Vector3(this.x, this.y, this.z);
  302. };
  303. // Statics
  304. BABYLON.Vector3.FromArray = function (array, offset) {
  305. if (!offset) {
  306. offset = 0;
  307. }
  308. return new BABYLON.Vector3(array[offset], array[offset + 1], array[offset + 2]);
  309. };
  310. BABYLON.Vector3.Zero = function () {
  311. return new BABYLON.Vector3(0, 0, 0);
  312. };
  313. BABYLON.Vector3.Up = function () {
  314. return new BABYLON.Vector3(0, 1.0, 0);
  315. };
  316. BABYLON.Vector3.TransformCoordinates = function (vector, transformation) {
  317. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  318. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  319. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  320. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  321. return new BABYLON.Vector3(x / w, y / w, z / w);
  322. };
  323. BABYLON.Vector3.TransformNormal = function (vector, transformation) {
  324. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  325. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  326. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  327. return new BABYLON.Vector3(x, y, z);
  328. };
  329. BABYLON.Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  330. var squared = amount * amount;
  331. var cubed = amount * squared;
  332. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  333. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  334. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  335. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  336. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  337. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  338. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  339. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  340. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  341. return new BABYLON.Vector3(x, y, z);
  342. };
  343. BABYLON.Vector3.Clamp = function (value, min, max) {
  344. var x = value.x;
  345. x = (x > max.x) ? max.x : x;
  346. x = (x < min.x) ? min.x : x;
  347. var y = value.y;
  348. y = (y > max.y) ? max.y : y;
  349. y = (y < min.y) ? min.y : y;
  350. var z = value.z;
  351. z = (z > max.z) ? max.z : z;
  352. z = (z < min.z) ? min.z : z;
  353. return new BABYLON.Vector3(x, y, z);
  354. };
  355. BABYLON.Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  356. var squared = amount * amount;
  357. var cubed = amount * squared;
  358. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  359. var part2 = (-2.0 * cubed) + (3.0 * squared);
  360. var part3 = (cubed - (2.0 * squared)) + amount;
  361. var part4 = cubed - squared;
  362. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  363. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  364. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  365. return new BABYLON.Vector3(x, y, z);
  366. };
  367. BABYLON.Vector3.Lerp = function (start, end, amount) {
  368. var x = start.x + ((end.x - start.x) * amount);
  369. var y = start.y + ((end.y - start.y) * amount);
  370. var z = start.z + ((end.z - start.z) * amount);
  371. return new BABYLON.Vector3(x, y, z);
  372. };
  373. BABYLON.Vector3.Dot = function (left, right) {
  374. return (left.x * right.x + left.y * right.y + left.z * right.z);
  375. };
  376. BABYLON.Vector3.Cross = function (left, right) {
  377. var x = left.y * right.z - left.z * right.y;
  378. var y = left.z * right.x - left.x * right.z;
  379. var z = left.x * right.y - left.y * right.x;
  380. return new BABYLON.Vector3(x, y, z);
  381. };
  382. BABYLON.Vector3.Normalize = function (vector) {
  383. var newVector = vector.clone();
  384. newVector.normalize();
  385. return newVector;
  386. };
  387. BABYLON.Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  388. var matrix = world.multiply(view).multiply(projection);
  389. matrix.invert();
  390. source.x = source.x / viewportWidth * 2 - 1;
  391. source.y = -(source.y / viewportHeight * 2 - 1);
  392. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  393. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  394. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  395. vector = vector.scale(1.0 / num);
  396. }
  397. return vector;
  398. };
  399. BABYLON.Vector3.Minimize = function (left, right) {
  400. var x = (left.x < right.x) ? left.x : right.x;
  401. var y = (left.y < right.y) ? left.y : right.y;
  402. var z = (left.z < right.z) ? left.z : right.z;
  403. return new BABYLON.Vector3(x, y, z);
  404. };
  405. BABYLON.Vector3.Maximize = function (left, right) {
  406. var x = (left.x > right.x) ? left.x : right.x;
  407. var y = (left.y > right.y) ? left.y : right.y;
  408. var z = (left.z > right.z) ? left.z : right.z;
  409. return new BABYLON.Vector3(x, y, z);
  410. };
  411. BABYLON.Vector3.Distance = function (value1, value2) {
  412. return Math.sqrt(BABYLON.Vector3.DistanceSquared(value1, value2));
  413. };
  414. BABYLON.Vector3.DistanceSquared = function (value1, value2) {
  415. var x = value1.x - value2.x;
  416. var y = value1.y - value2.y;
  417. var z = value1.z - value2.z;
  418. return (x * x) + (y * y) + (z * z);
  419. };
  420. ////////////////////////////////// Quaternion //////////////////////////////////
  421. BABYLON.Quaternion = function (initialX, initialY, initialZ, initialW) {
  422. this.x = initialX;
  423. this.y = initialY;
  424. this.z = initialZ;
  425. this.w = initialW;
  426. };
  427. BABYLON.Quaternion.prototype.toString = function () {
  428. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  429. };
  430. BABYLON.Quaternion.prototype.clone = function () {
  431. return new BABYLON.Quaternion(this.x, this.y, this.z, this.w);
  432. };
  433. BABYLON.Quaternion.prototype.add = function(other) {
  434. return new BABYLON.Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  435. };
  436. BABYLON.Quaternion.prototype.scale = function (value) {
  437. return new BABYLON.Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  438. };
  439. BABYLON.Quaternion.prototype.toEulerAngles = function () {
  440. var q0 = this.x;
  441. var q1 = this.y;
  442. var q2 = this.y;
  443. var q3 = this.w;
  444. var x = Math.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2));
  445. var y = Math.asin(2 * (q0 * q2 - q3 * q1));
  446. var z = Math.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3));
  447. return new BABYLON.Vector3(x, y, z);
  448. };
  449. // Statics
  450. BABYLON.Quaternion.FromArray = function (array, offset) {
  451. if (!offset) {
  452. offset = 0;
  453. }
  454. return new BABYLON.Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  455. };
  456. BABYLON.Quaternion.Slerp = function(left, right, amount) {
  457. var num2;
  458. var num3;
  459. var num = amount;
  460. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  461. var flag = false;
  462. if (num4 < 0)
  463. {
  464. flag = true;
  465. num4 = -num4;
  466. }
  467. if (num4 > 0.999999)
  468. {
  469. num3 = 1 - num;
  470. num2 = flag ? -num : num;
  471. }
  472. else
  473. {
  474. var num5 = Math.acos(num4);
  475. var num6 = (1.0 / Math.sin(num5));
  476. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  477. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  478. }
  479. return new BABYLON.Quaternion((num3 * left.x) + (num2 * right.x), (num3 * left.y) + (num2 * right.y), (num3 * left.z) + (num2 * right.z), (num3 * left.w) + (num2 * right.w));
  480. };
  481. ////////////////////////////////// Matrix //////////////////////////////////
  482. BABYLON.Matrix = function () {
  483. this.m = new Array(16);
  484. };
  485. // Properties
  486. BABYLON.Matrix.prototype.isIdentity = function () {
  487. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  488. return false;
  489. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  490. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  491. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  492. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  493. return false;
  494. return true;
  495. };
  496. BABYLON.Matrix.prototype.determinant = function () {
  497. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  498. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  499. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  500. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  501. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  502. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  503. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  504. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  505. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  506. };
  507. // Methods
  508. BABYLON.Matrix.prototype.toArray = function () {
  509. return this.m;
  510. };
  511. BABYLON.Matrix.prototype.invert = function () {
  512. var l1 = this.m[0];
  513. var l2 = this.m[1];
  514. var l3 = this.m[2];
  515. var l4 = this.m[3];
  516. var l5 = this.m[4];
  517. var l6 = this.m[5];
  518. var l7 = this.m[6];
  519. var l8 = this.m[7];
  520. var l9 = this.m[8];
  521. var l10 = this.m[9];
  522. var l11 = this.m[10];
  523. var l12 = this.m[11];
  524. var l13 = this.m[12];
  525. var l14 = this.m[13];
  526. var l15 = this.m[14];
  527. var l16 = this.m[15];
  528. var l17 = (l11 * l16) - (l12 * l15);
  529. var l18 = (l10 * l16) - (l12 * l14);
  530. var l19 = (l10 * l15) - (l11 * l14);
  531. var l20 = (l9 * l16) - (l12 * l13);
  532. var l21 = (l9 * l15) - (l11 * l13);
  533. var l22 = (l9 * l14) - (l10 * l13);
  534. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  535. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  536. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  537. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  538. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  539. var l28 = (l7 * l16) - (l8 * l15);
  540. var l29 = (l6 * l16) - (l8 * l14);
  541. var l30 = (l6 * l15) - (l7 * l14);
  542. var l31 = (l5 * l16) - (l8 * l13);
  543. var l32 = (l5 * l15) - (l7 * l13);
  544. var l33 = (l5 * l14) - (l6 * l13);
  545. var l34 = (l7 * l12) - (l8 * l11);
  546. var l35 = (l6 * l12) - (l8 * l10);
  547. var l36 = (l6 * l11) - (l7 * l10);
  548. var l37 = (l5 * l12) - (l8 * l9);
  549. var l38 = (l5 * l11) - (l7 * l9);
  550. var l39 = (l5 * l10) - (l6 * l9);
  551. this.m[0] = l23 * l27;
  552. this.m[4] = l24 * l27;
  553. this.m[8] = l25 * l27;
  554. this.m[12] = l26 * l27;
  555. this.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  556. this.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  557. this.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  558. this.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  559. this.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  560. this.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  561. this.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  562. this.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  563. this.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  564. this.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  565. this.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  566. this.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  567. };
  568. BABYLON.Matrix.prototype.multiply = function (other) {
  569. var result = new BABYLON.Matrix();
  570. result.m[0] = this.m[0] * other.m[0] + this.m[1] * other.m[4] + this.m[2] * other.m[8] + this.m[3] * other.m[12];
  571. result.m[1] = this.m[0] * other.m[1] + this.m[1] * other.m[5] + this.m[2] * other.m[9] + this.m[3] * other.m[13];
  572. result.m[2] = this.m[0] * other.m[2] + this.m[1] * other.m[6] + this.m[2] * other.m[10] + this.m[3] * other.m[14];
  573. result.m[3] = this.m[0] * other.m[3] + this.m[1] * other.m[7] + this.m[2] * other.m[11] + this.m[3] * other.m[15];
  574. result.m[4] = this.m[4] * other.m[0] + this.m[5] * other.m[4] + this.m[6] * other.m[8] + this.m[7] * other.m[12];
  575. result.m[5] = this.m[4] * other.m[1] + this.m[5] * other.m[5] + this.m[6] * other.m[9] + this.m[7] * other.m[13];
  576. result.m[6] = this.m[4] * other.m[2] + this.m[5] * other.m[6] + this.m[6] * other.m[10] + this.m[7] * other.m[14];
  577. result.m[7] = this.m[4] * other.m[3] + this.m[5] * other.m[7] + this.m[6] * other.m[11] + this.m[7] * other.m[15];
  578. result.m[8] = this.m[8] * other.m[0] + this.m[9] * other.m[4] + this.m[10] * other.m[8] + this.m[11] * other.m[12];
  579. result.m[9] = this.m[8] * other.m[1] + this.m[9] * other.m[5] + this.m[10] * other.m[9] + this.m[11] * other.m[13];
  580. result.m[10] = this.m[8] * other.m[2] + this.m[9] * other.m[6] + this.m[10] * other.m[10] + this.m[11] * other.m[14];
  581. result.m[11] = this.m[8] * other.m[3] + this.m[9] * other.m[7] + this.m[10] * other.m[11] + this.m[11] * other.m[15];
  582. result.m[12] = this.m[12] * other.m[0] + this.m[13] * other.m[4] + this.m[14] * other.m[8] + this.m[15] * other.m[12];
  583. result.m[13] = this.m[12] * other.m[1] + this.m[13] * other.m[5] + this.m[14] * other.m[9] + this.m[15] * other.m[13];
  584. result.m[14] = this.m[12] * other.m[2] + this.m[13] * other.m[6] + this.m[14] * other.m[10] + this.m[15] * other.m[14];
  585. result.m[15] = this.m[12] * other.m[3] + this.m[13] * other.m[7] + this.m[14] * other.m[11] + this.m[15] * other.m[15];
  586. return result;
  587. };
  588. BABYLON.Matrix.prototype.equals = function (value) {
  589. return (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  590. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  591. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  592. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  593. };
  594. BABYLON.Matrix.prototype.clone = function () {
  595. return BABYLON.Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  596. this.m[4], this.m[5], this.m[6], this.m[7],
  597. this.m[8], this.m[9], this.m[10], this.m[11],
  598. this.m[12], this.m[13], this.m[14], this.m[15]);
  599. };
  600. // Statics
  601. BABYLON.Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14,
  602. initialM21, initialM22, initialM23, initialM24,
  603. initialM31, initialM32, initialM33, initialM34,
  604. initialM41, initialM42, initialM43, initialM44) {
  605. var result = new BABYLON.Matrix();
  606. result.m[0] = initialM11;
  607. result.m[1] = initialM12;
  608. result.m[2] = initialM13;
  609. result.m[3] = initialM14;
  610. result.m[4] = initialM21;
  611. result.m[5] = initialM22;
  612. result.m[6] = initialM23;
  613. result.m[7] = initialM24;
  614. result.m[8] = initialM31;
  615. result.m[9] = initialM32;
  616. result.m[10] = initialM33;
  617. result.m[11] = initialM34;
  618. result.m[12] = initialM41;
  619. result.m[13] = initialM42;
  620. result.m[14] = initialM43;
  621. result.m[15] = initialM44;
  622. return result;
  623. };
  624. BABYLON.Matrix.Identity = function () {
  625. return BABYLON.Matrix.FromValues(1.0, 0, 0, 0,
  626. 0, 1.0, 0, 0,
  627. 0, 0, 1.0, 0,
  628. 0, 0, 0, 1.0);
  629. };
  630. BABYLON.Matrix.Zero = function () {
  631. return BABYLON.Matrix.FromValues(0, 0, 0, 0,
  632. 0, 0, 0, 0,
  633. 0, 0, 0, 0,
  634. 0, 0, 0, 0);
  635. };
  636. BABYLON.Matrix.RotationX = function (angle) {
  637. var result = BABYLON.Matrix.Zero();
  638. var s = Math.sin(angle);
  639. var c = Math.cos(angle);
  640. result.m[0] = 1.0;
  641. result.m[15] = 1.0;
  642. result.m[5] = c;
  643. result.m[10] = c;
  644. result.m[9] = -s;
  645. result.m[6] = s;
  646. return result;
  647. };
  648. BABYLON.Matrix.RotationY = function (angle) {
  649. var result = BABYLON.Matrix.Zero();
  650. var s = Math.sin(angle);
  651. var c = Math.cos(angle);
  652. result.m[5] = 1.0;
  653. result.m[15] = 1.0;
  654. result.m[0] = c;
  655. result.m[2] = -s;
  656. result.m[8] = s;
  657. result.m[10] = c;
  658. return result;
  659. };
  660. BABYLON.Matrix.RotationZ = function (angle) {
  661. var result = BABYLON.Matrix.Zero();
  662. var s = Math.sin(angle);
  663. var c = Math.cos(angle);
  664. result.m[10] = 1.0;
  665. result.m[15] = 1.0;
  666. result.m[0] = c;
  667. result.m[1] = s;
  668. result.m[4] = -s;
  669. result.m[5] = c;
  670. return result;
  671. };
  672. BABYLON.Matrix.RotationAxis = function (axis, angle) {
  673. var s = Math.sin(-angle);
  674. var c = Math.cos(-angle);
  675. var c1 = 1 - c;
  676. axis.normalize();
  677. var result = BABYLON.Matrix.Zero();
  678. result.m[0] = (axis.x * axis.x) * c1 + c;
  679. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  680. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  681. result.m[3] = 0.0;
  682. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  683. result.m[5] = (axis.y * axis.y) * c1 + c;
  684. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  685. result.m[7] = 0.0;
  686. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  687. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  688. result.m[10] = (axis.z * axis.z) * c1 + c;
  689. result.m[11] = 0.0;
  690. result.m[15] = 1.0;
  691. return result;
  692. };
  693. BABYLON.Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  694. return BABYLON.Matrix.RotationZ(roll).multiply(BABYLON.Matrix.RotationX(pitch)).multiply(BABYLON.Matrix.RotationY(yaw));
  695. };
  696. BABYLON.Matrix.Scaling = function (x, y, z) {
  697. var result = BABYLON.Matrix.Zero();
  698. result.m[0] = x;
  699. result.m[5] = y;
  700. result.m[10] = z;
  701. result.m[15] = 1.0;
  702. return result;
  703. };
  704. BABYLON.Matrix.Translation = function (x, y, z) {
  705. var result = BABYLON.Matrix.Identity();
  706. result.m[12] = x;
  707. result.m[13] = y;
  708. result.m[14] = z;
  709. return result;
  710. };
  711. BABYLON.Matrix.LookAtLH = function (eye, target, up) {
  712. // Z axis
  713. var zAxis = target.subtract(eye);
  714. zAxis.normalize();
  715. // X axis
  716. var xAxis = BABYLON.Vector3.Cross(up, zAxis);
  717. xAxis.normalize();
  718. // Y axis
  719. var yAxis = BABYLON.Vector3.Cross(zAxis, xAxis);
  720. yAxis.normalize();
  721. // Eye angles
  722. var ex = -BABYLON.Vector3.Dot(xAxis, eye);
  723. var ey = -BABYLON.Vector3.Dot(yAxis, eye);
  724. var ez = -BABYLON.Vector3.Dot(zAxis, eye);
  725. return BABYLON.Matrix.FromValues(xAxis.x, yAxis.x, zAxis.x, 0,
  726. xAxis.y, yAxis.y, zAxis.y, 0,
  727. xAxis.z, yAxis.z, zAxis.z, 0,
  728. ex, ey, ez, 1);
  729. };
  730. BABYLON.Matrix.OrthoLH = function (width, height, znear, zfar) {
  731. var hw = 2.0 / width;
  732. var hh = 2.0 / height;
  733. var id = 1.0 / (zfar - znear);
  734. var nid = znear / (znear - zfar);
  735. return BABYLON.Matrix.FromValues(hw, 0, 0, 0,
  736. 0, hh, 0, 0,
  737. 0, 0, id, 0,
  738. 0, 0, nid, 1);
  739. };
  740. BABYLON.Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  741. var matrix = BABYLON.Matrix.Zero();
  742. matrix.m[0] = 2.0 / (right - left);
  743. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0;
  744. matrix.m[5] = 2.0 / (top - bottom);
  745. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0;
  746. matrix.m[10] = -1.0 / (znear - zfar);
  747. matrix.m[8] = matrix.m[9] = matrix.m[11] = 0;
  748. matrix.m[12] = (left + right) / (left - right);
  749. matrix.m[13] = (top + bottom) / (bottom - top);
  750. matrix.m[14] = znear / (znear - zfar);
  751. matrix.m[15] = 1.0;
  752. return matrix;
  753. };
  754. BABYLON.Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  755. var matrix = BABYLON.Matrix.Zero();
  756. matrix.m[0] = (2.0 * znear) / width;
  757. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  758. matrix.m[5] = (2.0 * znear) / height;
  759. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  760. matrix.m[10] = -zfar / (znear - zfar);
  761. matrix.m[8] = matrix.m[9] = 0.0;
  762. matrix.m[11] = 1.0;
  763. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  764. matrix.m[14] = (znear * zfar) / (znear - zfar);
  765. return matrix;
  766. };
  767. BABYLON.Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  768. var matrix = BABYLON.Matrix.Zero();
  769. var tan = 1.0 / (Math.tan(fov * 0.5));
  770. matrix.m[0] = tan / aspect;
  771. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  772. matrix.m[5] = tan;
  773. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  774. matrix.m[8] = matrix.m[9] = 0.0;
  775. matrix.m[10] = -zfar / (znear - zfar);
  776. matrix.m[11] = 1.0;
  777. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  778. matrix.m[14] = (znear * zfar) / (znear - zfar);
  779. return matrix;
  780. };
  781. BABYLON.Matrix.AffineTransformation = function (scaling, rotationCenter, rotation, translation) {
  782. return BABYLON.Matrix.Scaling(scaling, scaling, scaling) * BABYLON.Matrix.Translation(-rotationCenter) *
  783. BABYLON.Matrix.RotationQuaternion(rotation) * BABYLON.Matrix.Translation(rotationCenter) * BABYLON.Matrix.Translation(translation);
  784. };
  785. BABYLON.Matrix.GetFinalMatrix = function (viewport, world, view, projection) {
  786. var cw = viewport.width;
  787. var ch = viewport.height;
  788. var cx = viewport.x;
  789. var cy = viewport.y;
  790. var zmin = viewport.minZ;
  791. var zmax = viewport.maxZ;
  792. var viewportMatrix = new BABYLON.Matrix(cw / 2.0, 0, 0, 0,
  793. 0, -ch / 2.0, 0, 0,
  794. 0, 0, zmax - zmin, 0,
  795. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  796. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  797. };
  798. BABYLON.Matrix.Transpose = function (matrix) {
  799. var result = new BABYLON.Matrix();
  800. result.m[0] = matrix.m[0];
  801. result.m[1] = matrix.m[4];
  802. result.m[2] = matrix.m[8];
  803. result.m[3] = matrix.m[12];
  804. result.m[4] = matrix.m[1];
  805. result.m[5] = matrix.m[5];
  806. result.m[6] = matrix.m[9];
  807. result.m[7] = matrix.m[13];
  808. result.m[8] = matrix.m[2];
  809. result.m[9] = matrix.m[6];
  810. result.m[10] = matrix.m[10];
  811. result.m[11] = matrix.m[14];
  812. result.m[12] = matrix.m[3];
  813. result.m[13] = matrix.m[7];
  814. result.m[14] = matrix.m[11];
  815. result.m[15] = matrix.m[15];
  816. return result;
  817. };
  818. BABYLON.Matrix.Reflection = function (plane) {
  819. var matrix = new BABYLON.Matrix();
  820. plane.normalize();
  821. var x = plane.normal.x;
  822. var y = plane.normal.y;
  823. var z = plane.normal.z;
  824. var temp = -2 * x;
  825. var temp2 = -2 * y;
  826. var temp3 = -2 * z;
  827. matrix.m[0] = (temp * x) + 1;
  828. matrix.m[1] = temp2 * x;
  829. matrix.m[2] = temp3 * x;
  830. matrix.m[3] = 0.0;
  831. matrix.m[4] = temp * y;
  832. matrix.m[5] = (temp2 * y) + 1;
  833. matrix.m[6] = temp3 * y;
  834. matrix.m[7] = 0.0;
  835. matrix.m[8] = temp * z;
  836. matrix.m[9] = temp2 * z;
  837. matrix.m[10] = (temp3 * z) + 1;
  838. matrix.m[11] = 0.0;
  839. matrix.m[12] = temp * plane.d;
  840. matrix.m[13] = temp2 * plane.d;
  841. matrix.m[14] = temp3 * plane.d;
  842. matrix.m[15] = 1.0;
  843. return matrix;
  844. };
  845. ////////////////////////////////// Plane //////////////////////////////////
  846. BABYLON.Plane = function (a, b, c, d) {
  847. this.normal = new BABYLON.Vector3(a, b, c);
  848. this.d = d;
  849. };
  850. // Methods
  851. BABYLON.Plane.prototype.normalize = function () {
  852. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  853. var magnitude = 0;
  854. if (norm != 0) {
  855. magnitude = 1.0 / norm;
  856. }
  857. this.normal.x *= magnitude;
  858. this.normal.y *= magnitude;
  859. this.normal.z *= magnitude;
  860. this.d *= magnitude;
  861. };
  862. BABYLON.Plane.prototype.transform = function(transformation) {
  863. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  864. var x = this.normal.x;
  865. var y = this.normal.y;
  866. var z = this.normal.z;
  867. var d = this.d;
  868. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  869. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  870. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  871. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  872. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  873. };
  874. BABYLON.Plane.prototype.dotCoordinate = function (point) {
  875. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  876. };
  877. // Statics
  878. BABYLON.Plane.FromArray = function (array) {
  879. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  880. };
  881. BABYLON.Plane.FromPoints = function(point1, point2, point3) {
  882. var x1 = point2.x - point1.x;
  883. var y1 = point2.y - point1.y;
  884. var z1 = point2.z - point1.z;
  885. var x2 = point3.x - point1.x;
  886. var y2 = point3.y - point1.y;
  887. var z2 = point3.z - point1.z;
  888. var yz = (y1 * z2) - (z1 * y2);
  889. var xz = (z1 * x2) - (x1 * z2);
  890. var xy = (x1 * y2) - (y1 * x2);
  891. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  892. var invPyth;
  893. if (pyth != 0)
  894. invPyth = 1.0 / pyth;
  895. else
  896. invPyth = 0;
  897. var normal = new BABYLON.Vector3(yz * invPyth, xz * invPyth, xy * invPyth);
  898. var d = -((normal.x * point1.x) + (normal.y * point1.y) + (normal.z * point1.z));
  899. return new BABYLON.Plane(normal.x, normal.y, normal.z, d);
  900. };
  901. ////////////////////////////////// Frustum //////////////////////////////////
  902. BABYLON.Frustum = {};
  903. // Statics
  904. BABYLON.Frustum.GetPlanes = function (transform) {
  905. var frustumPlanes = [];
  906. frustumPlanes.push(new BABYLON.Plane( // near
  907. transform.m[3] + transform.m[2],
  908. transform.m[7] + transform.m[6],
  909. transform.m[10] + transform.m[10],
  910. transform.m[15] + transform.m[14]));
  911. frustumPlanes[0].normalize();
  912. frustumPlanes.push(new BABYLON.Plane( // far
  913. transform.m[3] - transform.m[2],
  914. transform.m[7] - transform.m[6],
  915. transform.m[11] - transform.m[10],
  916. transform.m[15] - transform.m[14]));
  917. frustumPlanes[1].normalize();
  918. frustumPlanes.push(new BABYLON.Plane( // left
  919. transform.m[3] + transform.m[0],
  920. transform.m[7] + transform.m[4],
  921. transform.m[11] + transform.m[8],
  922. transform.m[15] + transform.m[12]));
  923. frustumPlanes[2].normalize();
  924. frustumPlanes.push(new BABYLON.Plane( // right
  925. transform.m[3] - transform.m[0],
  926. transform.m[7] - transform.m[4],
  927. transform.m[11] - transform.m[8],
  928. transform.m[15] - transform.m[12]));
  929. frustumPlanes[3].normalize();
  930. frustumPlanes.push(new BABYLON.Plane( // top
  931. transform.m[3] - transform.m[1],
  932. transform.m[7] - transform.m[5],
  933. transform.m[11] - transform.m[9],
  934. transform.m[15] - transform.m[13]));
  935. frustumPlanes[4].normalize();
  936. frustumPlanes.push(new BABYLON.Plane( // bottom
  937. transform.m[3] + transform.m[1],
  938. transform.m[7] + transform.m[5],
  939. transform.m[11] + transform.m[9],
  940. transform.m[15] + transform.m[13]));
  941. frustumPlanes[5].normalize();
  942. return frustumPlanes;
  943. };
  944. })();