babylon.math.js 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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.intersectsBox = function (box) {
  10. var d = 0.0;
  11. var maxValue = Number.MAX_VALUE;
  12. if (Math.abs(this.direction.x) < 0.0000001)
  13. {
  14. if (this.origin.x < box.minimum.x || this.origin.x > box.maximum.x)
  15. {
  16. return false;
  17. }
  18. }
  19. else
  20. {
  21. var inv = 1.0 / this.direction.x;
  22. var min = (box.minimum.x - this.origin.x) * inv;
  23. var max = (box.maximum.x - this.origin.x) * inv;
  24. if (min > max)
  25. {
  26. var temp = min;
  27. min = max;
  28. max = temp;
  29. }
  30. d = Math.max(min, d);
  31. maxValue = Math.min(max, maxValue);
  32. if (d > maxValue)
  33. {
  34. return false;
  35. }
  36. }
  37. if (Math.abs(this.direction.y) < 0.0000001)
  38. {
  39. if (this.origin.y < box.minimum.y || this.origin.y > box.maximum.y)
  40. {
  41. return false;
  42. }
  43. }
  44. else
  45. {
  46. var inv = 1.0 / this.direction.y;
  47. var min = (box.minimum.y - this.origin.y) * inv;
  48. var max = (box.maximum.y - this.origin.y) * inv;
  49. if (min > max)
  50. {
  51. var temp = min;
  52. min = max;
  53. max = temp;
  54. }
  55. d = Math.max(min, d);
  56. maxValue = Math.min(max, maxValue);
  57. if (d > maxValue)
  58. {
  59. return false;
  60. }
  61. }
  62. if (Math.abs(this.direction.z) < 0.0000001)
  63. {
  64. if (this.origin.z < box.minimum.z || this.origin.z > box.maximum.z)
  65. {
  66. return false;
  67. }
  68. }
  69. else
  70. {
  71. var inv = 1.0 / this.direction.z;
  72. var min = (box.minimum.z - this.origin.z) * inv;
  73. var max = (box.maximum.z - this.origin.z) * inv;
  74. if (min > max)
  75. {
  76. var temp = min;
  77. min = max;
  78. max = temp;
  79. }
  80. d = Math.max(min, d);
  81. maxValue = Math.min(max, maxValue);
  82. if (d > maxValue)
  83. {
  84. return false;
  85. }
  86. }
  87. return true;
  88. };
  89. BABYLON.Ray.prototype.intersectsSphere = function (sphere) {
  90. var x = sphere.center.x - this.origin.x;
  91. var y = sphere.center.y - this.origin.y;
  92. var z = sphere.center.z - this.origin.z;
  93. var pyth = (x * x) + (y * y) + (z * z);
  94. var rr = sphere.radius * sphere.radius;
  95. if (pyth <= rr) {
  96. return true;
  97. }
  98. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  99. if (dot < 0.0) {
  100. return false;
  101. }
  102. var temp = pyth - (dot * dot);
  103. return temp <= rr;
  104. };
  105. BABYLON.Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  106. var edge1 = vertex1.subtract(vertex0);
  107. var edge2 = vertex2.subtract(vertex0);
  108. var pvec = BABYLON.Vector3.Cross(this.direction, edge2);
  109. var det = BABYLON.Vector3.Dot(edge1, pvec);
  110. if (det === 0) {
  111. return {
  112. hit: false,
  113. distance: 0,
  114. bu: 0,
  115. bv: 0
  116. };
  117. }
  118. var invdet = 1 / det;
  119. var tvec = this.origin.subtract(vertex0);
  120. var bu = BABYLON.Vector3.Dot(tvec, pvec) * invdet;
  121. if (bu < 0 || bu > 1.0) {
  122. return {
  123. hit: false,
  124. distance: 0,
  125. bu: bu,
  126. bv: 0
  127. };
  128. }
  129. var qvec = BABYLON.Vector3.Cross(tvec, edge1);
  130. bv = BABYLON.Vector3.Dot(this.direction, qvec) * invdet;
  131. if (bv < 0 || bu + bv > 1.0) {
  132. return {
  133. hit: false,
  134. distance: 0,
  135. bu: bu,
  136. bv: bv
  137. };
  138. }
  139. distance = BABYLON.Vector3.Dot(edge2, qvec) * invdet;
  140. return {
  141. hit: true,
  142. distance: distance,
  143. bu: bu,
  144. bv: bv
  145. };
  146. };
  147. // Statics
  148. BABYLON.Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  149. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  150. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  151. var direction = end.subtract(start);
  152. direction.normalize();
  153. return new BABYLON.Ray(start, direction);
  154. };
  155. ////////////////////////////////// Color3 //////////////////////////////////
  156. BABYLON.Color3 = function (initialR, initialG, initialB) {
  157. this.r = initialR;
  158. this.g = initialG;
  159. this.b = initialB;
  160. };
  161. BABYLON.Color3.prototype.toString = function () {
  162. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  163. };
  164. // Operators
  165. BABYLON.Color3.prototype.multiply = function (otherColor) {
  166. return new BABYLON.Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  167. };
  168. BABYLON.Color3.prototype.multiplyToRef = function (otherColor, result) {
  169. result.r = this.r * otherColor.r;
  170. result.g = this.g * otherColor.g;
  171. result.b = this.b * otherColor.b;
  172. };
  173. BABYLON.Color3.prototype.equals = function (otherColor) {
  174. return this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  175. };
  176. BABYLON.Color3.prototype.scale = function (scale) {
  177. return new BABYLON.Color3(this.r * scale, this.g * scale, this.b * scale);
  178. };
  179. BABYLON.Color3.prototype.scaleToRef = function (scale, result) {
  180. result.r = this.r * scale;
  181. result.g = this.g * scale;
  182. result.b = this.b * scale;
  183. };
  184. BABYLON.Color3.prototype.clone = function () {
  185. return new BABYLON.Color3(this.r, this.g, this.b);
  186. };
  187. BABYLON.Color3.prototype.copyFrom = function (source) {
  188. this.r = source.r;
  189. this.g = source.g;
  190. this.b = source.b;
  191. };
  192. BABYLON.Color3.prototype.copyFromFloats = function (r, g, b) {
  193. this.r = r;
  194. this.g = g;
  195. this.b = b;
  196. };
  197. // Statics
  198. BABYLON.Color3.FromArray = function (array) {
  199. return new BABYLON.Color3(array[0], array[1], array[2]);
  200. };
  201. ////////////////////////////////// Color4 //////////////////////////////////
  202. BABYLON.Color4 = function (initialR, initialG, initialB, initialA) {
  203. this.r = initialR;
  204. this.g = initialG;
  205. this.b = initialB;
  206. this.a = initialA;
  207. };
  208. // Operators
  209. BABYLON.Color4.prototype.addInPlace = function (right) {
  210. this.r += right.r;
  211. this.g += right.g;
  212. this.b += right.b;
  213. this.a += right.a;
  214. };
  215. BABYLON.Color4.prototype.add = function (right) {
  216. return new BABYLON.Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  217. };
  218. BABYLON.Color4.prototype.subtract = function (right) {
  219. return new BABYLON.Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  220. };
  221. BABYLON.Color4.prototype.subtractToRef = function (right, result) {
  222. result.r = this.r - right.r;
  223. result.g = this.g - right.g;
  224. result.b = this.b - right.b;
  225. result.a = this.a - right.a;
  226. };
  227. BABYLON.Color4.prototype.scale = function (scale) {
  228. return new BABYLON.Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  229. };
  230. BABYLON.Color4.prototype.scaleToRef = function (scale, result) {
  231. result.r = this.r * scale;
  232. result.g = this.g * scale;
  233. result.b = this.b * scale;
  234. result.a = this.a * scale;
  235. };
  236. BABYLON.Color4.prototype.toString = function () {
  237. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  238. };
  239. BABYLON.Color4.prototype.clone = function () {
  240. return new BABYLON.Color4(this.r, this.g, this.b, this.a);
  241. };
  242. // Statics
  243. BABYLON.Color4.Lerp = function(left, right, amount) {
  244. var result = new BABYLON.Color4(0, 0, 0, 0);
  245. BABYLON.Color4.LerpToRef(left, right, amount, result);
  246. return result;
  247. };
  248. BABYLON.Color4.LerpToRef = function (left, right, amount, result) {
  249. result.r = left.r + (right.r - left.r) * amount;
  250. result.g = left.g + (right.g - left.g) * amount;
  251. result.b = left.b + (right.b - left.b) * amount;
  252. result.a = left.a + (right.a - left.a) * amount;
  253. };
  254. BABYLON.Color4.FromArray = function (array, offset) {
  255. if (!offset) {
  256. offset = 0;
  257. }
  258. return new BABYLON.Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  259. };
  260. ////////////////////////////////// Vector2 //////////////////////////////////
  261. BABYLON.Vector2 = function (initialX, initialY) {
  262. this.x = initialX;
  263. this.y = initialY;
  264. };
  265. BABYLON.Vector2.prototype.toString = function () {
  266. return "{X: " + this.x + " Y:" + this.y + "}";
  267. };
  268. // Operators
  269. BABYLON.Vector2.prototype.add = function (otherVector) {
  270. return new BABYLON.Vector2(this.x + otherVector.x, this.y + otherVector.y);
  271. };
  272. BABYLON.Vector2.prototype.subtract = function (otherVector) {
  273. return new BABYLON.Vector2(this.x - otherVector.x, this.y - otherVector.y);
  274. };
  275. BABYLON.Vector2.prototype.negate = function () {
  276. return new BABYLON.Vector2(-this.x, -this.y);
  277. };
  278. BABYLON.Vector2.prototype.scaleInPlace = function (scale) {
  279. this.x *= scale;
  280. this.y *= scale;
  281. };
  282. BABYLON.Vector2.prototype.scale = function (scale) {
  283. return new BABYLON.Vector2(this.x * scale, this.y * scale);
  284. };
  285. BABYLON.Vector2.prototype.equals = function (otherVector) {
  286. return this.x === otherVector.x && this.y === otherVector.y;
  287. };
  288. // Properties
  289. BABYLON.Vector2.prototype.length = function () {
  290. return Math.sqrt(this.x * this.x + this.y * this.y);
  291. };
  292. BABYLON.Vector2.prototype.lengthSquared = function () {
  293. return (this.x * this.x + this.y * this.y);
  294. };
  295. // Methods
  296. BABYLON.Vector2.prototype.normalize = function () {
  297. var len = this.length();
  298. if (len === 0)
  299. return;
  300. var num = 1.0 / len;
  301. this.x *= num;
  302. this.y *= num;
  303. };
  304. BABYLON.Vector2.prototype.clone = function () {
  305. return new BABYLON.Vector2(this.x, this.y);
  306. };
  307. // Statics
  308. BABYLON.Vector2.Zero = function () {
  309. return new BABYLON.Vector2(0, 0);
  310. };
  311. BABYLON.Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  312. var squared = amount * amount;
  313. var cubed = amount * squared;
  314. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  315. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  316. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  317. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  318. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  319. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  320. return new BABYLON.Vector2(x, y);
  321. };
  322. BABYLON.Vector2.Clamp = function (value, min, max) {
  323. var x = value.x;
  324. x = (x > max.x) ? max.x : x;
  325. x = (x < min.x) ? min.x : x;
  326. var y = value.y;
  327. y = (y > max.y) ? max.y : y;
  328. y = (y < min.y) ? min.y : y;
  329. return new BABYLON.Vector2(x, y);
  330. };
  331. BABYLON.Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  332. var squared = amount * amount;
  333. var cubed = amount * squared;
  334. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  335. var part2 = (-2.0 * cubed) + (3.0 * squared);
  336. var part3 = (cubed - (2.0 * squared)) + amount;
  337. var part4 = cubed - squared;
  338. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  339. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  340. return new BABYLON.Vector2(x, y);
  341. };
  342. BABYLON.Vector2.Lerp = function (start, end, amount) {
  343. var x = start.x + ((end.x - start.x) * amount);
  344. var y = start.y + ((end.y - start.y) * amount);
  345. return new BABYLON.Vector2(x, y);
  346. };
  347. BABYLON.Vector2.Dot = function (left, right) {
  348. return left.x * right.x + left.y * right.y;
  349. };
  350. BABYLON.Vector2.Normalize = function (vector) {
  351. var newVector = vector.clone();
  352. newVector.normalize();
  353. return newVector;
  354. };
  355. BABYLON.Vector2.Minimize = function (left, right) {
  356. var x = (left.x < right.x) ? left.x : right.x;
  357. var y = (left.y < right.y) ? left.y : right.y;
  358. return new BABYLON.Vector2(x, y);
  359. };
  360. BABYLON.Vector2.Maximize = function (left, right) {
  361. var x = (left.x > right.x) ? left.x : right.x;
  362. var y = (left.y > right.y) ? left.y : right.y;
  363. return new BABYLON.Vector2(x, y);
  364. };
  365. BABYLON.Vector2.Transform = function (vector, transformation) {
  366. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  367. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  368. return new BABYLON.Vector2(x, y);
  369. };
  370. BABYLON.Vector2.Distance = function (value1, value2) {
  371. return Math.sqrt(BABYLON.Vector2.DistanceSquared(value1, value2));
  372. };
  373. BABYLON.Vector2.DistanceSquared = function (value1, value2) {
  374. var x = value1.x - value2.x;
  375. var y = value1.y - value2.y;
  376. return (x * x) + (y * y);
  377. };
  378. ////////////////////////////////// Vector3 //////////////////////////////////
  379. BABYLON.Vector3 = function (initialX, initialY, initialZ) {
  380. this.x = initialX;
  381. this.y = initialY;
  382. this.z = initialZ;
  383. };
  384. BABYLON.Vector3.prototype.toString = function () {
  385. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  386. };
  387. // Operators
  388. BABYLON.Vector3.prototype.addInPlace = function (otherVector) {
  389. this.x += otherVector.x;
  390. this.y += otherVector.y;
  391. this.z += otherVector.z;
  392. };
  393. BABYLON.Vector3.prototype.add = function (otherVector) {
  394. return new BABYLON.Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  395. };
  396. BABYLON.Vector3.prototype.addToRef = function (otherVector, result) {
  397. result.x = this.x + otherVector.x;
  398. result.y = this.y + otherVector.y;
  399. result.z = this.z + otherVector.z;
  400. };
  401. BABYLON.Vector3.prototype.subtractInPlace = function (otherVector) {
  402. this.x -= otherVector.x;
  403. this.y -= otherVector.y;
  404. this.z -= otherVector.z;
  405. };
  406. BABYLON.Vector3.prototype.subtract = function (otherVector) {
  407. return new BABYLON.Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  408. };
  409. BABYLON.Vector3.prototype.subtractToRef = function (otherVector, result) {
  410. result.x = this.x - otherVector.x;
  411. result.y = this.y - otherVector.y;
  412. result.z = this.z - otherVector.z;
  413. };
  414. BABYLON.Vector3.prototype.subtractFromFloats = function (x, y, z) {
  415. return new BABYLON.Vector3(this.x - x, this.y - y, this.z - z);
  416. };
  417. BABYLON.Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  418. result.x = this.x - x;
  419. result.y = this.y - y;
  420. result.z = this.z - z;
  421. };
  422. BABYLON.Vector3.prototype.negate = function () {
  423. return new BABYLON.Vector3(-this.x, -this.y, -this.z);
  424. };
  425. BABYLON.Vector3.prototype.scaleInPlace = function (scale) {
  426. this.x *= scale;
  427. this.y *= scale;
  428. this.z *= scale;
  429. };
  430. BABYLON.Vector3.prototype.scale = function (scale) {
  431. return new BABYLON.Vector3(this.x * scale, this.y * scale, this.z * scale);
  432. };
  433. BABYLON.Vector3.prototype.scaleToRef = function (scale, result) {
  434. result.x = this.x * scale;
  435. result.y = this.y * scale;
  436. result.z = this.z * scale;
  437. };
  438. BABYLON.Vector3.prototype.equals = function (otherVector) {
  439. return this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  440. };
  441. BABYLON.Vector3.prototype.equalsToFloats = function (x, y, z) {
  442. return this.x === x && this.y === y && this.z === z;
  443. };
  444. BABYLON.Vector3.prototype.multiplyInPlace = function (otherVector) {
  445. this.x *= otherVector.x;
  446. this.y *= otherVector.y;
  447. this.z *= otherVector.z;
  448. };
  449. BABYLON.Vector3.prototype.multiply = function (otherVector) {
  450. return new BABYLON.Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  451. };
  452. BABYLON.Vector3.prototype.multiplyToRef = function (otherVector, result) {
  453. result.x = this.x * otherVector.x;
  454. result.y = this.y * otherVector.y;
  455. result.z = this.z * otherVector.z;
  456. };
  457. BABYLON.Vector3.prototype.divide = function (otherVector) {
  458. return new BABYLON.Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  459. };
  460. BABYLON.Vector3.prototype.divideToRef = function (otherVector, result) {
  461. result.x = this.x / otherVector.x;
  462. result.y = this.y / otherVector.y;
  463. result.z = this.z / otherVector.z;
  464. };
  465. // Properties
  466. BABYLON.Vector3.prototype.length = function () {
  467. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  468. };
  469. BABYLON.Vector3.prototype.lengthSquared = function () {
  470. return (this.x * this.x + this.y * this.y + this.z * this.z);
  471. };
  472. // Methods
  473. BABYLON.Vector3.prototype.normalize = function () {
  474. var len = this.length();
  475. if (len === 0)
  476. return;
  477. var num = 1.0 / len;
  478. this.x *= num;
  479. this.y *= num;
  480. this.z *= num;
  481. };
  482. BABYLON.Vector3.prototype.clone = function () {
  483. return new BABYLON.Vector3(this.x, this.y, this.z);
  484. };
  485. BABYLON.Vector3.prototype.copyFrom = function (source) {
  486. this.x = source.x;
  487. this.y = source.y;
  488. this.z = source.z;
  489. };
  490. BABYLON.Vector3.prototype.copyFromFloats = function (x, y, z) {
  491. this.x = x;
  492. this.y = y;
  493. this.z = z;
  494. };
  495. // Statics
  496. BABYLON.Vector3.FromArray = function (array, offset) {
  497. if (!offset) {
  498. offset = 0;
  499. }
  500. return new BABYLON.Vector3(array[offset], array[offset + 1], array[offset + 2]);
  501. };
  502. BABYLON.Vector3.FromArrayToRef = function (array, offset, result) {
  503. if (!offset) {
  504. offset = 0;
  505. }
  506. result.x = array[offset];
  507. result.y = array[offset + 1];
  508. result.z = array[offset + 2];
  509. };
  510. BABYLON.Vector3.FromFloatsToRef = function (x, y, z, result) {
  511. result.x = x;
  512. result.y = y;
  513. result.z = z;
  514. };
  515. BABYLON.Vector3.Zero = function () {
  516. return new BABYLON.Vector3(0, 0, 0);
  517. };
  518. BABYLON.Vector3.Up = function () {
  519. return new BABYLON.Vector3(0, 1.0, 0);
  520. };
  521. BABYLON.Vector3.TransformCoordinates = function (vector, transformation) {
  522. var result = BABYLON.Vector3.Zero();
  523. BABYLON.Vector3.TransformCoordinatesToRef(vector, transformation, result);
  524. return result;
  525. };
  526. BABYLON.Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  527. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  528. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  529. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  530. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  531. result.x = x / w;
  532. result.y = y / w;
  533. result.z = z / w;
  534. };
  535. BABYLON.Vector3.TransformCoordinatesFromFloatsToRef = function (x, y ,z, transformation, result) {
  536. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  537. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  538. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  539. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  540. result.x = rx / rw;
  541. result.y = ry / rw;
  542. result.z = rz / rw;
  543. };
  544. BABYLON.Vector3.TransformNormal = function (vector, transformation) {
  545. var result = BABYLON.Vector3.Zero();
  546. BABYLON.Vector3.TransformNormalToRef(vector, transformation, result);
  547. return result;
  548. };
  549. BABYLON.Vector3.TransformNormalToRef = function (vector, transformation, result) {
  550. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  551. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  552. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  553. };
  554. BABYLON.Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  555. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  556. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  557. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  558. };
  559. BABYLON.Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  560. var squared = amount * amount;
  561. var cubed = amount * squared;
  562. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  563. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  564. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  565. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  566. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  567. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  568. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  569. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  570. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  571. return new BABYLON.Vector3(x, y, z);
  572. };
  573. BABYLON.Vector3.Clamp = function (value, min, max) {
  574. var x = value.x;
  575. x = (x > max.x) ? max.x : x;
  576. x = (x < min.x) ? min.x : x;
  577. var y = value.y;
  578. y = (y > max.y) ? max.y : y;
  579. y = (y < min.y) ? min.y : y;
  580. var z = value.z;
  581. z = (z > max.z) ? max.z : z;
  582. z = (z < min.z) ? min.z : z;
  583. return new BABYLON.Vector3(x, y, z);
  584. };
  585. BABYLON.Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  586. var squared = amount * amount;
  587. var cubed = amount * squared;
  588. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  589. var part2 = (-2.0 * cubed) + (3.0 * squared);
  590. var part3 = (cubed - (2.0 * squared)) + amount;
  591. var part4 = cubed - squared;
  592. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  593. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  594. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  595. return new BABYLON.Vector3(x, y, z);
  596. };
  597. BABYLON.Vector3.Lerp = function (start, end, amount) {
  598. var x = start.x + ((end.x - start.x) * amount);
  599. var y = start.y + ((end.y - start.y) * amount);
  600. var z = start.z + ((end.z - start.z) * amount);
  601. return new BABYLON.Vector3(x, y, z);
  602. };
  603. BABYLON.Vector3.Dot = function (left, right) {
  604. return (left.x * right.x + left.y * right.y + left.z * right.z);
  605. };
  606. BABYLON.Vector3.Cross = function (left, right) {
  607. var result = BABYLON.Vector3.Zero();
  608. BABYLON.Vector3.CrossToRef(left, right, result);
  609. return result;
  610. };
  611. BABYLON.Vector3.CrossToRef = function (left, right, result) {
  612. result.x = left.y * right.z - left.z * right.y;
  613. result.y = left.z * right.x - left.x * right.z;
  614. result.z = left.x * right.y - left.y * right.x;
  615. };
  616. BABYLON.Vector3.Normalize = function (vector) {
  617. var result = BABYLON.Vector3.Zero();
  618. BABYLON.Vector3.NormalizeToRef(vector, result);
  619. return result;
  620. };
  621. BABYLON.Vector3.NormalizeToRef = function (vector, result) {
  622. result.copyFrom(vector);
  623. result.normalize();
  624. };
  625. BABYLON.Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  626. var matrix = world.multiply(view).multiply(projection);
  627. matrix.invert();
  628. source.x = source.x / viewportWidth * 2 - 1;
  629. source.y = -(source.y / viewportHeight * 2 - 1);
  630. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  631. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  632. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  633. vector = vector.scale(1.0 / num);
  634. }
  635. return vector;
  636. };
  637. BABYLON.Vector3.Minimize = function (left, right) {
  638. var x = (left.x < right.x) ? left.x : right.x;
  639. var y = (left.y < right.y) ? left.y : right.y;
  640. var z = (left.z < right.z) ? left.z : right.z;
  641. return new BABYLON.Vector3(x, y, z);
  642. };
  643. BABYLON.Vector3.Maximize = function (left, right) {
  644. var x = (left.x > right.x) ? left.x : right.x;
  645. var y = (left.y > right.y) ? left.y : right.y;
  646. var z = (left.z > right.z) ? left.z : right.z;
  647. return new BABYLON.Vector3(x, y, z);
  648. };
  649. BABYLON.Vector3.Distance = function (value1, value2) {
  650. return Math.sqrt(BABYLON.Vector3.DistanceSquared(value1, value2));
  651. };
  652. BABYLON.Vector3.DistanceSquared = function (value1, value2) {
  653. var x = value1.x - value2.x;
  654. var y = value1.y - value2.y;
  655. var z = value1.z - value2.z;
  656. return (x * x) + (y * y) + (z * z);
  657. };
  658. ////////////////////////////////// Quaternion //////////////////////////////////
  659. BABYLON.Quaternion = function (initialX, initialY, initialZ, initialW) {
  660. this.x = initialX;
  661. this.y = initialY;
  662. this.z = initialZ;
  663. this.w = initialW;
  664. };
  665. BABYLON.Quaternion.prototype.toString = function () {
  666. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  667. };
  668. BABYLON.Quaternion.prototype.clone = function () {
  669. return new BABYLON.Quaternion(this.x, this.y, this.z, this.w);
  670. };
  671. BABYLON.Quaternion.prototype.add = function(other) {
  672. return new BABYLON.Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  673. };
  674. BABYLON.Quaternion.prototype.scale = function (value) {
  675. return new BABYLON.Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  676. };
  677. BABYLON.Quaternion.prototype.toEulerAngles = function () {
  678. var q0 = this.x;
  679. var q1 = this.y;
  680. var q2 = this.y;
  681. var q3 = this.w;
  682. var x = Math.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2));
  683. var y = Math.asin(2 * (q0 * q2 - q3 * q1));
  684. var z = Math.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3));
  685. return new BABYLON.Vector3(x, y, z);
  686. };
  687. BABYLON.Quaternion.prototype.toRotationMatrix = function(result)
  688. {
  689. var xx = this.x * this.x;
  690. var yy = this.y * this.y;
  691. var zz = this.z * this.z;
  692. var xy = this.x * this.y;
  693. var zw = this.z * this.w;
  694. var zx = this.z * this.x;
  695. var yw = this.y * this.w;
  696. var yz = this.y * this.z;
  697. var xw = this.x * this.w;
  698. result.m[0] = 1.0 - (2.0 * (yy + zz));
  699. result.m[1] = 2.0 * (xy + zw);
  700. result.m[2] = 2.0 * (zx - yw);
  701. result.m[3] = 0;
  702. result.m[4] = 2.0 * (xy - zw);
  703. result.m[5] = 1.0 - (2.0 * (zz + xx));
  704. result.m[6] = 2.0 * (yz + xw);
  705. result.m[7] = 0;
  706. result.m[8] = 2.0 * (zx + yw);
  707. result.m[9] = 2.0 * (yz - xw);
  708. result.m[10] = 1.0 - (2.0 * (yy + xx));
  709. result.m[11] = 0;
  710. result.m[12] = 0;
  711. result.m[13] = 0;
  712. result.m[14] = 0;
  713. result.m[15] = 1.0;
  714. };
  715. // Statics
  716. BABYLON.Quaternion.FromArray = function (array, offset) {
  717. if (!offset) {
  718. offset = 0;
  719. }
  720. return new BABYLON.Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  721. };
  722. BABYLON.Quaternion.RotationYawPitchRoll = function(yaw, pitch, roll) {
  723. var result = new BABYLON.Quaternion();
  724. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  725. return result;
  726. };
  727. BABYLON.Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  728. var halfRoll = roll * 0.5;
  729. var halfPitch = pitch * 0.5;
  730. var halfYaw = yaw * 0.5;
  731. var sinRoll = Math.sin(halfRoll);
  732. var cosRoll = Math.cos(halfRoll);
  733. var sinPitch = Math.sin(halfPitch);
  734. var cosPitch = Math.cos(halfPitch);
  735. var sinYaw = Math.sin(halfYaw);
  736. var cosYaw = Math.cos(halfYaw);
  737. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  738. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  739. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  740. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  741. };
  742. BABYLON.Quaternion.Slerp = function(left, right, amount) {
  743. var num2;
  744. var num3;
  745. var num = amount;
  746. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  747. var flag = false;
  748. if (num4 < 0)
  749. {
  750. flag = true;
  751. num4 = -num4;
  752. }
  753. if (num4 > 0.999999)
  754. {
  755. num3 = 1 - num;
  756. num2 = flag ? -num : num;
  757. }
  758. else
  759. {
  760. var num5 = Math.acos(num4);
  761. var num6 = (1.0 / Math.sin(num5));
  762. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  763. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  764. }
  765. 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));
  766. };
  767. ////////////////////////////////// Matrix //////////////////////////////////
  768. if (!BABYLON.MatrixType) {
  769. BABYLON.MatrixType = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  770. }
  771. BABYLON.Matrix = function () {
  772. this.m = new BABYLON.MatrixType(16);
  773. };
  774. // Properties
  775. BABYLON.Matrix.prototype.isIdentity = function () {
  776. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  777. return false;
  778. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  779. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  780. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  781. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  782. return false;
  783. return true;
  784. };
  785. BABYLON.Matrix.prototype.determinant = function () {
  786. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  787. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  788. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  789. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  790. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  791. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  792. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  793. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  794. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  795. };
  796. // Methods
  797. BABYLON.Matrix.prototype.toArray = function () {
  798. return this.m;
  799. };
  800. BABYLON.Matrix.prototype.invert = function () {
  801. var l1 = this.m[0];
  802. var l2 = this.m[1];
  803. var l3 = this.m[2];
  804. var l4 = this.m[3];
  805. var l5 = this.m[4];
  806. var l6 = this.m[5];
  807. var l7 = this.m[6];
  808. var l8 = this.m[7];
  809. var l9 = this.m[8];
  810. var l10 = this.m[9];
  811. var l11 = this.m[10];
  812. var l12 = this.m[11];
  813. var l13 = this.m[12];
  814. var l14 = this.m[13];
  815. var l15 = this.m[14];
  816. var l16 = this.m[15];
  817. var l17 = (l11 * l16) - (l12 * l15);
  818. var l18 = (l10 * l16) - (l12 * l14);
  819. var l19 = (l10 * l15) - (l11 * l14);
  820. var l20 = (l9 * l16) - (l12 * l13);
  821. var l21 = (l9 * l15) - (l11 * l13);
  822. var l22 = (l9 * l14) - (l10 * l13);
  823. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  824. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  825. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  826. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  827. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  828. var l28 = (l7 * l16) - (l8 * l15);
  829. var l29 = (l6 * l16) - (l8 * l14);
  830. var l30 = (l6 * l15) - (l7 * l14);
  831. var l31 = (l5 * l16) - (l8 * l13);
  832. var l32 = (l5 * l15) - (l7 * l13);
  833. var l33 = (l5 * l14) - (l6 * l13);
  834. var l34 = (l7 * l12) - (l8 * l11);
  835. var l35 = (l6 * l12) - (l8 * l10);
  836. var l36 = (l6 * l11) - (l7 * l10);
  837. var l37 = (l5 * l12) - (l8 * l9);
  838. var l38 = (l5 * l11) - (l7 * l9);
  839. var l39 = (l5 * l10) - (l6 * l9);
  840. this.m[0] = l23 * l27;
  841. this.m[4] = l24 * l27;
  842. this.m[8] = l25 * l27;
  843. this.m[12] = l26 * l27;
  844. this.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  845. this.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  846. this.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  847. this.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  848. this.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  849. this.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  850. this.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  851. this.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  852. this.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  853. this.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  854. this.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  855. this.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  856. };
  857. BABYLON.Matrix.prototype.multiply = function (other) {
  858. var result = new BABYLON.Matrix();
  859. this.multiplyToRef(other, result);
  860. return result;
  861. };
  862. BABYLON.Matrix.prototype.multiplyToRef = function (other, result) {
  863. 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];
  864. 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];
  865. 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];
  866. 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];
  867. 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];
  868. 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];
  869. 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];
  870. 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];
  871. 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];
  872. 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];
  873. 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];
  874. 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];
  875. 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];
  876. 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];
  877. 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];
  878. 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];
  879. };
  880. BABYLON.Matrix.prototype.equals = function (value) {
  881. 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] &&
  882. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  883. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  884. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  885. };
  886. BABYLON.Matrix.prototype.clone = function () {
  887. return BABYLON.Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  888. this.m[4], this.m[5], this.m[6], this.m[7],
  889. this.m[8], this.m[9], this.m[10], this.m[11],
  890. this.m[12], this.m[13], this.m[14], this.m[15]);
  891. };
  892. // Statics
  893. BABYLON.Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14,
  894. initialM21, initialM22, initialM23, initialM24,
  895. initialM31, initialM32, initialM33, initialM34,
  896. initialM41, initialM42, initialM43, initialM44, result) {
  897. result.m[0] = initialM11;
  898. result.m[1] = initialM12;
  899. result.m[2] = initialM13;
  900. result.m[3] = initialM14;
  901. result.m[4] = initialM21;
  902. result.m[5] = initialM22;
  903. result.m[6] = initialM23;
  904. result.m[7] = initialM24;
  905. result.m[8] = initialM31;
  906. result.m[9] = initialM32;
  907. result.m[10] = initialM33;
  908. result.m[11] = initialM34;
  909. result.m[12] = initialM41;
  910. result.m[13] = initialM42;
  911. result.m[14] = initialM43;
  912. result.m[15] = initialM44;
  913. };
  914. BABYLON.Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14,
  915. initialM21, initialM22, initialM23, initialM24,
  916. initialM31, initialM32, initialM33, initialM34,
  917. initialM41, initialM42, initialM43, initialM44) {
  918. var result = new BABYLON.Matrix();
  919. result.m[0] = initialM11;
  920. result.m[1] = initialM12;
  921. result.m[2] = initialM13;
  922. result.m[3] = initialM14;
  923. result.m[4] = initialM21;
  924. result.m[5] = initialM22;
  925. result.m[6] = initialM23;
  926. result.m[7] = initialM24;
  927. result.m[8] = initialM31;
  928. result.m[9] = initialM32;
  929. result.m[10] = initialM33;
  930. result.m[11] = initialM34;
  931. result.m[12] = initialM41;
  932. result.m[13] = initialM42;
  933. result.m[14] = initialM43;
  934. result.m[15] = initialM44;
  935. return result;
  936. };
  937. BABYLON.Matrix.Identity = function () {
  938. return BABYLON.Matrix.FromValues(1.0, 0, 0, 0,
  939. 0, 1.0, 0, 0,
  940. 0, 0, 1.0, 0,
  941. 0, 0, 0, 1.0);
  942. };
  943. BABYLON.Matrix.IdentityToRef = function (result) {
  944. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  945. 0, 1.0, 0, 0,
  946. 0, 0, 1.0, 0,
  947. 0, 0, 0, 1.0, result);
  948. };
  949. BABYLON.Matrix.Zero = function () {
  950. return BABYLON.Matrix.FromValues(0, 0, 0, 0,
  951. 0, 0, 0, 0,
  952. 0, 0, 0, 0,
  953. 0, 0, 0, 0);
  954. };
  955. BABYLON.Matrix.RotationX = function (angle) {
  956. var result = new BABYLON.Matrix();
  957. BABYLON.Matrix.RotationXToRef(angle, result);
  958. return result;
  959. };
  960. BABYLON.Matrix.RotationXToRef = function (angle, result) {
  961. var s = Math.sin(angle);
  962. var c = Math.cos(angle);
  963. result.m[0] = 1.0;
  964. result.m[15] = 1.0;
  965. result.m[5] = c;
  966. result.m[10] = c;
  967. result.m[9] = -s;
  968. result.m[6] = s;
  969. result.m[1] = 0;
  970. result.m[2] = 0;
  971. result.m[3] = 0;
  972. result.m[4] = 0;
  973. result.m[7] = 0;
  974. result.m[8] = 0;
  975. result.m[11] = 0;
  976. result.m[12] = 0;
  977. result.m[13] = 0;
  978. result.m[14] = 0;
  979. };
  980. BABYLON.Matrix.RotationY = function (angle) {
  981. var result = new BABYLON.Matrix();
  982. BABYLON.Matrix.RotationYToRef(angle, result);
  983. return result;
  984. };
  985. BABYLON.Matrix.RotationYToRef = function (angle, result) {
  986. var s = Math.sin(angle);
  987. var c = Math.cos(angle);
  988. result.m[5] = 1.0;
  989. result.m[15] = 1.0;
  990. result.m[0] = c;
  991. result.m[2] = -s;
  992. result.m[8] = s;
  993. result.m[10] = c;
  994. result.m[1] = 0;
  995. result.m[3] = 0;
  996. result.m[4] = 0;
  997. result.m[6] = 0;
  998. result.m[7] = 0;
  999. result.m[9] = 0;
  1000. result.m[11] = 0;
  1001. result.m[12] = 0;
  1002. result.m[13] = 0;
  1003. result.m[14] = 0;
  1004. };
  1005. BABYLON.Matrix.RotationZ = function (angle) {
  1006. var result = new BABYLON.Matrix();
  1007. BABYLON.Matrix.RotationZToRef(angle, result);
  1008. return result;
  1009. };
  1010. BABYLON.Matrix.RotationZToRef = function (angle, result) {
  1011. var s = Math.sin(angle);
  1012. var c = Math.cos(angle);
  1013. result.m[10] = 1.0;
  1014. result.m[15] = 1.0;
  1015. result.m[0] = c;
  1016. result.m[1] = s;
  1017. result.m[4] = -s;
  1018. result.m[5] = c;
  1019. result.m[2] = 0;
  1020. result.m[3] = 0;
  1021. result.m[6] = 0;
  1022. result.m[7] = 0;
  1023. result.m[8] = 0;
  1024. result.m[9] = 0;
  1025. result.m[11] = 0;
  1026. result.m[12] = 0;
  1027. result.m[13] = 0;
  1028. result.m[14] = 0;
  1029. };
  1030. BABYLON.Matrix.RotationAxis = function (axis, angle) {
  1031. var s = Math.sin(-angle);
  1032. var c = Math.cos(-angle);
  1033. var c1 = 1 - c;
  1034. axis.normalize();
  1035. var result = BABYLON.Matrix.Zero();
  1036. result.m[0] = (axis.x * axis.x) * c1 + c;
  1037. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1038. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1039. result.m[3] = 0.0;
  1040. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1041. result.m[5] = (axis.y * axis.y) * c1 + c;
  1042. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1043. result.m[7] = 0.0;
  1044. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1045. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1046. result.m[10] = (axis.z * axis.z) * c1 + c;
  1047. result.m[11] = 0.0;
  1048. result.m[15] = 1.0;
  1049. return result;
  1050. };
  1051. BABYLON.Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1052. var result = new BABYLON.Matrix();
  1053. BABYLON.Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1054. return result;
  1055. };
  1056. var tempQuaternion = new BABYLON.Quaternion(); // For RotationYawPitchRoll
  1057. BABYLON.Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1058. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, tempQuaternion);
  1059. tempQuaternion.toRotationMatrix(result);
  1060. };
  1061. BABYLON.Matrix.Scaling = function (x, y, z) {
  1062. var result = BABYLON.Matrix.Zero();
  1063. BABYLON.Matrix.ScalingToRef(x, y, z, result);
  1064. return result;
  1065. };
  1066. BABYLON.Matrix.ScalingToRef = function (x, y, z, result) {
  1067. result.m[0] = x;
  1068. result.m[1] = 0;
  1069. result.m[2] = 0;
  1070. result.m[3] = 0;
  1071. result.m[4] = 0;
  1072. result.m[5] = y;
  1073. result.m[6] = 0;
  1074. result.m[7] = 0;
  1075. result.m[8] = 0;
  1076. result.m[9] = 0;
  1077. result.m[10] = z;
  1078. result.m[11] = 0;
  1079. result.m[12] = 0;
  1080. result.m[13] = 0;
  1081. result.m[14] = 0;
  1082. result.m[15] = 1.0;
  1083. };
  1084. BABYLON.Matrix.Translation = function (x, y, z) {
  1085. var result = BABYLON.Matrix.Identity();
  1086. BABYLON.Matrix.TranslationToRef(x, y, z, result);
  1087. return result;
  1088. };
  1089. BABYLON.Matrix.TranslationToRef = function (x, y, z, result) {
  1090. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1091. 0, 1.0, 0, 0,
  1092. 0, 0, 1.0, 0,
  1093. x, y, z, 1.0, result);
  1094. };
  1095. BABYLON.Matrix.LookAtLH = function (eye, target, up) {
  1096. var result = BABYLON.Matrix.Zero();
  1097. BABYLON.Matrix.LookAtLHToRef(eye, target, up, result);
  1098. return result;
  1099. };
  1100. var xAxis = BABYLON.Vector3.Zero();
  1101. var yAxis = BABYLON.Vector3.Zero();
  1102. var zAxis = BABYLON.Vector3.Zero();
  1103. BABYLON.Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1104. // Z axis
  1105. target.subtractToRef(eye, zAxis);
  1106. zAxis.normalize();
  1107. // X axis
  1108. BABYLON.Vector3.CrossToRef(up, zAxis, xAxis);
  1109. xAxis.normalize();
  1110. // Y axis
  1111. BABYLON.Vector3.CrossToRef(zAxis, xAxis, yAxis);
  1112. yAxis.normalize();
  1113. // Eye angles
  1114. var ex = -BABYLON.Vector3.Dot(xAxis, eye);
  1115. var ey = -BABYLON.Vector3.Dot(yAxis, eye);
  1116. var ez = -BABYLON.Vector3.Dot(zAxis, eye);
  1117. return BABYLON.Matrix.FromValuesToRef(xAxis.x, yAxis.x, zAxis.x, 0,
  1118. xAxis.y, yAxis.y, zAxis.y, 0,
  1119. xAxis.z, yAxis.z, zAxis.z, 0,
  1120. ex, ey, ez, 1, result);
  1121. };
  1122. BABYLON.Matrix.OrthoLH = function (width, height, znear, zfar) {
  1123. var hw = 2.0 / width;
  1124. var hh = 2.0 / height;
  1125. var id = 1.0 / (zfar - znear);
  1126. var nid = znear / (znear - zfar);
  1127. return BABYLON.Matrix.FromValues(hw, 0, 0, 0,
  1128. 0, hh, 0, 0,
  1129. 0, 0, id, 0,
  1130. 0, 0, nid, 1);
  1131. };
  1132. BABYLON.Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1133. var matrix = BABYLON.Matrix.Zero();
  1134. BABYLON.Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1135. return matrix;
  1136. };
  1137. BABYLON.Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1138. result.m[0] = 2.0 / (right - left);
  1139. result.m[1] = result.m[2] = result.m[3] = 0;
  1140. result.m[5] = 2.0 / (top - bottom);
  1141. result.m[4] = result.m[6] = result.m[7] = 0;
  1142. result.m[10] = -1.0 / (znear - zfar);
  1143. result.m[8] = result.m[9] = result.m[11] = 0;
  1144. result.m[12] = (left + right) / (left - right);
  1145. result.m[13] = (top + bottom) / (bottom - top);
  1146. result.m[14] = znear / (znear - zfar);
  1147. result.m[15] = 1.0;
  1148. };
  1149. BABYLON.Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1150. var matrix = BABYLON.Matrix.Zero();
  1151. matrix.m[0] = (2.0 * znear) / width;
  1152. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1153. matrix.m[5] = (2.0 * znear) / height;
  1154. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1155. matrix.m[10] = -zfar / (znear - zfar);
  1156. matrix.m[8] = matrix.m[9] = 0.0;
  1157. matrix.m[11] = 1.0;
  1158. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1159. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1160. return matrix;
  1161. };
  1162. BABYLON.Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1163. var matrix = BABYLON.Matrix.Zero();
  1164. BABYLON.Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1165. return matrix;
  1166. };
  1167. BABYLON.Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result) {
  1168. var tan = 1.0 / (Math.tan(fov * 0.5));
  1169. result.m[0] = tan / aspect;
  1170. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1171. result.m[5] = tan;
  1172. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1173. result.m[8] = result.m[9] = 0.0;
  1174. result.m[10] = -zfar / (znear - zfar);
  1175. result.m[11] = 1.0;
  1176. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1177. result.m[14] = (znear * zfar) / (znear - zfar);
  1178. };
  1179. BABYLON.Matrix.AffineTransformation = function (scaling, rotationCenter, rotation, translation) {
  1180. return BABYLON.Matrix.Scaling(scaling, scaling, scaling) * BABYLON.Matrix.Translation(-rotationCenter) *
  1181. BABYLON.Matrix.RotationQuaternion(rotation) * BABYLON.Matrix.Translation(rotationCenter) * BABYLON.Matrix.Translation(translation);
  1182. };
  1183. BABYLON.Matrix.GetFinalMatrix = function (viewport, world, view, projection) {
  1184. var cw = viewport.width;
  1185. var ch = viewport.height;
  1186. var cx = viewport.x;
  1187. var cy = viewport.y;
  1188. var zmin = viewport.minZ;
  1189. var zmax = viewport.maxZ;
  1190. var viewportMatrix = new BABYLON.Matrix(cw / 2.0, 0, 0, 0,
  1191. 0, -ch / 2.0, 0, 0,
  1192. 0, 0, zmax - zmin, 0,
  1193. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  1194. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1195. };
  1196. BABYLON.Matrix.Transpose = function (matrix) {
  1197. var result = new BABYLON.Matrix();
  1198. result.m[0] = matrix.m[0];
  1199. result.m[1] = matrix.m[4];
  1200. result.m[2] = matrix.m[8];
  1201. result.m[3] = matrix.m[12];
  1202. result.m[4] = matrix.m[1];
  1203. result.m[5] = matrix.m[5];
  1204. result.m[6] = matrix.m[9];
  1205. result.m[7] = matrix.m[13];
  1206. result.m[8] = matrix.m[2];
  1207. result.m[9] = matrix.m[6];
  1208. result.m[10] = matrix.m[10];
  1209. result.m[11] = matrix.m[14];
  1210. result.m[12] = matrix.m[3];
  1211. result.m[13] = matrix.m[7];
  1212. result.m[14] = matrix.m[11];
  1213. result.m[15] = matrix.m[15];
  1214. return result;
  1215. };
  1216. BABYLON.Matrix.Reflection = function (plane) {
  1217. var matrix = new BABYLON.Matrix();
  1218. BABYLON.Matrix.ReflectionToRef(plane, matrix);
  1219. return matrix;
  1220. };
  1221. BABYLON.Matrix.ReflectionToRef = function (plane, result) {
  1222. plane.normalize();
  1223. var x = plane.normal.x;
  1224. var y = plane.normal.y;
  1225. var z = plane.normal.z;
  1226. var temp = -2 * x;
  1227. var temp2 = -2 * y;
  1228. var temp3 = -2 * z;
  1229. result.m[0] = (temp * x) + 1;
  1230. result.m[1] = temp2 * x;
  1231. result.m[2] = temp3 * x;
  1232. result.m[3] = 0.0;
  1233. result.m[4] = temp * y;
  1234. result.m[5] = (temp2 * y) + 1;
  1235. result.m[6] = temp3 * y;
  1236. result.m[7] = 0.0;
  1237. result.m[8] = temp * z;
  1238. result.m[9] = temp2 * z;
  1239. result.m[10] = (temp3 * z) + 1;
  1240. result.m[11] = 0.0;
  1241. result.m[12] = temp * plane.d;
  1242. result.m[13] = temp2 * plane.d;
  1243. result.m[14] = temp3 * plane.d;
  1244. result.m[15] = 1.0;
  1245. };
  1246. ////////////////////////////////// Plane //////////////////////////////////
  1247. BABYLON.Plane = function (a, b, c, d) {
  1248. this.normal = new BABYLON.Vector3(a, b, c);
  1249. this.d = d;
  1250. };
  1251. // Methods
  1252. BABYLON.Plane.prototype.normalize = function () {
  1253. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1254. var magnitude = 0;
  1255. if (norm != 0) {
  1256. magnitude = 1.0 / norm;
  1257. }
  1258. this.normal.x *= magnitude;
  1259. this.normal.y *= magnitude;
  1260. this.normal.z *= magnitude;
  1261. this.d *= magnitude;
  1262. };
  1263. BABYLON.Plane.prototype.transform = function(transformation) {
  1264. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1265. var x = this.normal.x;
  1266. var y = this.normal.y;
  1267. var z = this.normal.z;
  1268. var d = this.d;
  1269. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1270. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1271. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1272. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1273. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1274. };
  1275. BABYLON.Plane.prototype.dotCoordinate = function (point) {
  1276. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1277. };
  1278. BABYLON.Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  1279. var x1 = point2.x - point1.x;
  1280. var y1 = point2.y - point1.y;
  1281. var z1 = point2.z - point1.z;
  1282. var x2 = point3.x - point1.x;
  1283. var y2 = point3.y - point1.y;
  1284. var z2 = point3.z - point1.z;
  1285. var yz = (y1 * z2) - (z1 * y2);
  1286. var xz = (z1 * x2) - (x1 * z2);
  1287. var xy = (x1 * y2) - (y1 * x2);
  1288. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1289. var invPyth;
  1290. if (pyth != 0)
  1291. invPyth = 1.0 / pyth;
  1292. else
  1293. invPyth = 0;
  1294. this.normal.x = yz * invPyth;
  1295. this.normal.y = xz * invPyth;
  1296. this.normal.z = xy * invPyth;
  1297. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1298. };
  1299. BABYLON.Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  1300. var dot = BABYLON.Vector3.Dot(this.normal, direction);
  1301. return (dot <= epsilon);
  1302. };
  1303. BABYLON.Plane.prototype.signedDistanceTo = function (point) {
  1304. return BABYLON.Vector3.Dot(point, this.normal) + this.d;
  1305. };
  1306. // Statics
  1307. BABYLON.Plane.FromArray = function (array) {
  1308. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1309. };
  1310. BABYLON.Plane.FromPoints = function(point1, point2, point3) {
  1311. var result = new BABYLON.Plane(0, 0, 0, 0);
  1312. result.copyFromPoints(point1, point2, point3);
  1313. return result;
  1314. };
  1315. BABYLON.Plane.FromPositionAndNormal = function (origin, normal) {
  1316. var result = new BABYLON.Plane(0, 0, 0, 0);
  1317. normal.normalize();
  1318. result.normal = normal;
  1319. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1320. return result;
  1321. };
  1322. BABYLON.Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  1323. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1324. return BABYLON.Vector3.Dot(point, normal) + d;
  1325. };
  1326. ////////////////////////////////// Frustum //////////////////////////////////
  1327. BABYLON.Frustum = {};
  1328. // Statics
  1329. BABYLON.Frustum.GetPlanes = function (transform) {
  1330. var frustumPlanes = [];
  1331. for (var index = 0; index < 6; index++) {
  1332. frustumPlanes.push(new BABYLON.Plane(0, 0, 0, 0));
  1333. }
  1334. BABYLON.Frustum.GetPlanesToRef(transform, frustumPlanes);
  1335. return frustumPlanes;
  1336. };
  1337. BABYLON.Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  1338. // Near
  1339. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1340. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1341. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1342. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1343. frustumPlanes[0].normalize();
  1344. // Far
  1345. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1346. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1347. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1348. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1349. frustumPlanes[1].normalize();
  1350. // Left
  1351. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1352. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1353. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1354. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1355. frustumPlanes[2].normalize();
  1356. // Right
  1357. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1358. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1359. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1360. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1361. frustumPlanes[3].normalize();
  1362. // Top
  1363. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1364. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1365. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1366. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1367. frustumPlanes[4].normalize();
  1368. // Bottom
  1369. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1370. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1371. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1372. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1373. frustumPlanes[5].normalize();
  1374. };
  1375. })();