babylon.math.js 66 KB

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