babylon.math.js 65 KB

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