babylon.math.js 74 KB

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