babylon.math.js 64 KB

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