babylon.math.js 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Color3 = (function () {
  4. function Color3(r, g, b) {
  5. if (r === void 0) { r = 0; }
  6. if (g === void 0) { g = 0; }
  7. if (b === void 0) { 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. return this;
  24. };
  25. Color3.prototype.toColor4 = function (alpha) {
  26. if (alpha === void 0) { alpha = 1; }
  27. return new Color4(this.r, this.g, this.b, alpha);
  28. };
  29. Color3.prototype.asArray = function () {
  30. var result = [];
  31. this.toArray(result, 0);
  32. return result;
  33. };
  34. Color3.prototype.toLuminance = function () {
  35. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  36. };
  37. Color3.prototype.multiply = function (otherColor) {
  38. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  39. };
  40. Color3.prototype.multiplyToRef = function (otherColor, result) {
  41. result.r = this.r * otherColor.r;
  42. result.g = this.g * otherColor.g;
  43. result.b = this.b * otherColor.b;
  44. return this;
  45. };
  46. Color3.prototype.equals = function (otherColor) {
  47. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  48. };
  49. Color3.prototype.scale = function (scale) {
  50. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  51. };
  52. Color3.prototype.scaleToRef = function (scale, result) {
  53. result.r = this.r * scale;
  54. result.g = this.g * scale;
  55. result.b = this.b * scale;
  56. return this;
  57. };
  58. Color3.prototype.add = function (otherColor) {
  59. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  60. };
  61. Color3.prototype.addToRef = function (otherColor, result) {
  62. result.r = this.r + otherColor.r;
  63. result.g = this.g + otherColor.g;
  64. result.b = this.b + otherColor.b;
  65. return this;
  66. };
  67. Color3.prototype.subtract = function (otherColor) {
  68. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  69. };
  70. Color3.prototype.subtractToRef = function (otherColor, result) {
  71. result.r = this.r - otherColor.r;
  72. result.g = this.g - otherColor.g;
  73. result.b = this.b - otherColor.b;
  74. return this;
  75. };
  76. Color3.prototype.clone = function () {
  77. return new Color3(this.r, this.g, this.b);
  78. };
  79. Color3.prototype.copyFrom = function (source) {
  80. this.r = source.r;
  81. this.g = source.g;
  82. this.b = source.b;
  83. return this;
  84. };
  85. Color3.prototype.copyFromFloats = function (r, g, b) {
  86. this.r = r;
  87. this.g = g;
  88. this.b = b;
  89. return this;
  90. };
  91. // Statics
  92. Color3.FromArray = function (array, offset) {
  93. if (offset === void 0) { offset = 0; }
  94. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  95. };
  96. Color3.FromInts = function (r, g, b) {
  97. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  98. };
  99. Color3.Lerp = function (start, end, amount) {
  100. var r = start.r + ((end.r - start.r) * amount);
  101. var g = start.g + ((end.g - start.g) * amount);
  102. var b = start.b + ((end.b - start.b) * amount);
  103. return new Color3(r, g, b);
  104. };
  105. Color3.Red = function () {
  106. return new Color3(1, 0, 0);
  107. };
  108. Color3.Green = function () {
  109. return new Color3(0, 1, 0);
  110. };
  111. Color3.Blue = function () {
  112. return new Color3(0, 0, 1);
  113. };
  114. Color3.Black = function () {
  115. return new Color3(0, 0, 0);
  116. };
  117. Color3.White = function () {
  118. return new Color3(1, 1, 1);
  119. };
  120. Color3.Purple = function () {
  121. return new Color3(0.5, 0, 0.5);
  122. };
  123. Color3.Magenta = function () {
  124. return new Color3(1, 0, 1);
  125. };
  126. Color3.Yellow = function () {
  127. return new Color3(1, 1, 0);
  128. };
  129. Color3.Gray = function () {
  130. return new Color3(0.5, 0.5, 0.5);
  131. };
  132. return Color3;
  133. })();
  134. BABYLON.Color3 = Color3;
  135. var Color4 = (function () {
  136. function Color4(r, g, b, a) {
  137. this.r = r;
  138. this.g = g;
  139. this.b = b;
  140. this.a = a;
  141. }
  142. // Operators
  143. Color4.prototype.addInPlace = function (right) {
  144. this.r += right.r;
  145. this.g += right.g;
  146. this.b += right.b;
  147. this.a += right.a;
  148. return this;
  149. };
  150. Color4.prototype.asArray = function () {
  151. var result = [];
  152. this.toArray(result, 0);
  153. return result;
  154. };
  155. Color4.prototype.toArray = function (array, index) {
  156. if (index === undefined) {
  157. index = 0;
  158. }
  159. array[index] = this.r;
  160. array[index + 1] = this.g;
  161. array[index + 2] = this.b;
  162. array[index + 3] = this.a;
  163. return this;
  164. };
  165. Color4.prototype.add = function (right) {
  166. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  167. };
  168. Color4.prototype.subtract = function (right) {
  169. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  170. };
  171. Color4.prototype.subtractToRef = function (right, result) {
  172. result.r = this.r - right.r;
  173. result.g = this.g - right.g;
  174. result.b = this.b - right.b;
  175. result.a = this.a - right.a;
  176. return this;
  177. };
  178. Color4.prototype.scale = function (scale) {
  179. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  180. };
  181. Color4.prototype.scaleToRef = function (scale, result) {
  182. result.r = this.r * scale;
  183. result.g = this.g * scale;
  184. result.b = this.b * scale;
  185. result.a = this.a * scale;
  186. return this;
  187. };
  188. Color4.prototype.toString = function () {
  189. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  190. };
  191. Color4.prototype.clone = function () {
  192. return new Color4(this.r, this.g, this.b, this.a);
  193. };
  194. Color4.prototype.copyFrom = function (source) {
  195. this.r = source.r;
  196. this.g = source.g;
  197. this.b = source.b;
  198. this.a = source.a;
  199. return this;
  200. };
  201. // Statics
  202. Color4.Lerp = function (left, right, amount) {
  203. var result = new Color4(0, 0, 0, 0);
  204. Color4.LerpToRef(left, right, amount, result);
  205. return result;
  206. };
  207. Color4.LerpToRef = function (left, right, amount, result) {
  208. result.r = left.r + (right.r - left.r) * amount;
  209. result.g = left.g + (right.g - left.g) * amount;
  210. result.b = left.b + (right.b - left.b) * amount;
  211. result.a = left.a + (right.a - left.a) * amount;
  212. };
  213. Color4.FromArray = function (array, offset) {
  214. if (offset === void 0) { offset = 0; }
  215. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  216. };
  217. Color4.FromInts = function (r, g, b, a) {
  218. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  219. };
  220. return Color4;
  221. })();
  222. BABYLON.Color4 = Color4;
  223. var Vector2 = (function () {
  224. function Vector2(x, y) {
  225. this.x = x;
  226. this.y = y;
  227. }
  228. Vector2.prototype.toString = function () {
  229. return "{X: " + this.x + " Y:" + this.y + "}";
  230. };
  231. // Operators
  232. Vector2.prototype.toArray = function (array, index) {
  233. if (index === void 0) { index = 0; }
  234. array[index] = this.x;
  235. array[index + 1] = this.y;
  236. return this;
  237. };
  238. Vector2.prototype.asArray = function () {
  239. var result = [];
  240. this.toArray(result, 0);
  241. return result;
  242. };
  243. Vector2.prototype.copyFrom = function (source) {
  244. this.x = source.x;
  245. this.y = source.y;
  246. return this;
  247. };
  248. Vector2.prototype.copyFromFloats = function (x, y) {
  249. this.x = x;
  250. this.y = y;
  251. return this;
  252. };
  253. Vector2.prototype.add = function (otherVector) {
  254. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  255. };
  256. Vector2.prototype.addVector3 = function (otherVector) {
  257. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  258. };
  259. Vector2.prototype.subtract = function (otherVector) {
  260. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  261. };
  262. Vector2.prototype.subtractInPlace = function (otherVector) {
  263. this.x -= otherVector.x;
  264. this.y -= otherVector.y;
  265. return this;
  266. };
  267. Vector2.prototype.multiplyInPlace = function (otherVector) {
  268. this.x *= otherVector.x;
  269. this.y *= otherVector.y;
  270. return this;
  271. };
  272. Vector2.prototype.multiply = function (otherVector) {
  273. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  274. };
  275. Vector2.prototype.multiplyToRef = function (otherVector, result) {
  276. result.x = this.x * otherVector.x;
  277. result.y = this.y * otherVector.y;
  278. return this;
  279. };
  280. Vector2.prototype.multiplyByFloats = function (x, y) {
  281. return new Vector2(this.x * x, this.y * y);
  282. };
  283. Vector2.prototype.divide = function (otherVector) {
  284. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  285. };
  286. Vector2.prototype.divideToRef = function (otherVector, result) {
  287. result.x = this.x / otherVector.x;
  288. result.y = this.y / otherVector.y;
  289. return this;
  290. };
  291. Vector2.prototype.negate = function () {
  292. return new Vector2(-this.x, -this.y);
  293. };
  294. Vector2.prototype.scaleInPlace = function (scale) {
  295. this.x *= scale;
  296. this.y *= scale;
  297. return this;
  298. };
  299. Vector2.prototype.scale = function (scale) {
  300. return new Vector2(this.x * scale, this.y * scale);
  301. };
  302. Vector2.prototype.equals = function (otherVector) {
  303. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  304. };
  305. // Properties
  306. Vector2.prototype.length = function () {
  307. return Math.sqrt(this.x * this.x + this.y * this.y);
  308. };
  309. Vector2.prototype.lengthSquared = function () {
  310. return (this.x * this.x + this.y * this.y);
  311. };
  312. // Methods
  313. Vector2.prototype.normalize = function () {
  314. var len = this.length();
  315. if (len === 0)
  316. return this;
  317. var num = 1.0 / len;
  318. this.x *= num;
  319. this.y *= num;
  320. return this;
  321. };
  322. Vector2.prototype.clone = function () {
  323. return new Vector2(this.x, this.y);
  324. };
  325. // Statics
  326. Vector2.Zero = function () {
  327. return new Vector2(0, 0);
  328. };
  329. Vector2.FromArray = function (array, offset) {
  330. if (offset === void 0) { offset = 0; }
  331. return new Vector2(array[offset], array[offset + 1]);
  332. };
  333. Vector2.FromArrayToRef = function (array, offset, result) {
  334. result.x = array[offset];
  335. result.y = array[offset + 1];
  336. };
  337. Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  338. var squared = amount * amount;
  339. var cubed = amount * squared;
  340. 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));
  341. 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));
  342. return new Vector2(x, y);
  343. };
  344. Vector2.Clamp = function (value, min, max) {
  345. var x = value.x;
  346. x = (x > max.x) ? max.x : x;
  347. x = (x < min.x) ? min.x : x;
  348. var y = value.y;
  349. y = (y > max.y) ? max.y : y;
  350. y = (y < min.y) ? min.y : y;
  351. return new Vector2(x, y);
  352. };
  353. Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  354. var squared = amount * amount;
  355. var cubed = amount * squared;
  356. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  357. var part2 = (-2.0 * cubed) + (3.0 * squared);
  358. var part3 = (cubed - (2.0 * squared)) + amount;
  359. var part4 = cubed - squared;
  360. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  361. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  362. return new Vector2(x, y);
  363. };
  364. Vector2.Lerp = function (start, end, amount) {
  365. var x = start.x + ((end.x - start.x) * amount);
  366. var y = start.y + ((end.y - start.y) * amount);
  367. return new Vector2(x, y);
  368. };
  369. Vector2.Dot = function (left, right) {
  370. return left.x * right.x + left.y * right.y;
  371. };
  372. Vector2.Normalize = function (vector) {
  373. var newVector = vector.clone();
  374. newVector.normalize();
  375. return newVector;
  376. };
  377. Vector2.Minimize = function (left, right) {
  378. var x = (left.x < right.x) ? left.x : right.x;
  379. var y = (left.y < right.y) ? left.y : right.y;
  380. return new Vector2(x, y);
  381. };
  382. Vector2.Maximize = function (left, right) {
  383. var x = (left.x > right.x) ? left.x : right.x;
  384. var y = (left.y > right.y) ? left.y : right.y;
  385. return new Vector2(x, y);
  386. };
  387. Vector2.Transform = function (vector, transformation) {
  388. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  389. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  390. return new Vector2(x, y);
  391. };
  392. Vector2.Distance = function (value1, value2) {
  393. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  394. };
  395. Vector2.DistanceSquared = function (value1, value2) {
  396. var x = value1.x - value2.x;
  397. var y = value1.y - value2.y;
  398. return (x * x) + (y * y);
  399. };
  400. return Vector2;
  401. })();
  402. BABYLON.Vector2 = Vector2;
  403. var Vector3 = (function () {
  404. function Vector3(x, y, z) {
  405. this.x = x;
  406. this.y = y;
  407. this.z = z;
  408. }
  409. Vector3.prototype.toString = function () {
  410. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  411. };
  412. // Operators
  413. Vector3.prototype.asArray = function () {
  414. var result = [];
  415. this.toArray(result, 0);
  416. return result;
  417. };
  418. Vector3.prototype.toArray = function (array, index) {
  419. if (index === void 0) { index = 0; }
  420. array[index] = this.x;
  421. array[index + 1] = this.y;
  422. array[index + 2] = this.z;
  423. return this;
  424. };
  425. Vector3.prototype.toQuaternion = function () {
  426. var result = new Quaternion(0, 0, 0, 1);
  427. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  428. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  429. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  430. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  431. var cosy = Math.cos(this.y * 0.5);
  432. var siny = Math.sin(this.y * 0.5);
  433. result.x = coszMinusx * siny;
  434. result.y = -sinzMinusx * siny;
  435. result.z = sinxPlusz * cosy;
  436. result.w = cosxPlusz * cosy;
  437. return result;
  438. };
  439. Vector3.prototype.addInPlace = function (otherVector) {
  440. this.x += otherVector.x;
  441. this.y += otherVector.y;
  442. this.z += otherVector.z;
  443. return this;
  444. };
  445. Vector3.prototype.add = function (otherVector) {
  446. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  447. };
  448. Vector3.prototype.addToRef = function (otherVector, result) {
  449. result.x = this.x + otherVector.x;
  450. result.y = this.y + otherVector.y;
  451. result.z = this.z + otherVector.z;
  452. return this;
  453. };
  454. Vector3.prototype.subtractInPlace = function (otherVector) {
  455. this.x -= otherVector.x;
  456. this.y -= otherVector.y;
  457. this.z -= otherVector.z;
  458. return this;
  459. };
  460. Vector3.prototype.subtract = function (otherVector) {
  461. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  462. };
  463. Vector3.prototype.subtractToRef = function (otherVector, result) {
  464. result.x = this.x - otherVector.x;
  465. result.y = this.y - otherVector.y;
  466. result.z = this.z - otherVector.z;
  467. return this;
  468. };
  469. Vector3.prototype.subtractFromFloats = function (x, y, z) {
  470. return new Vector3(this.x - x, this.y - y, this.z - z);
  471. };
  472. Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  473. result.x = this.x - x;
  474. result.y = this.y - y;
  475. result.z = this.z - z;
  476. return this;
  477. };
  478. Vector3.prototype.negate = function () {
  479. return new Vector3(-this.x, -this.y, -this.z);
  480. };
  481. Vector3.prototype.scaleInPlace = function (scale) {
  482. this.x *= scale;
  483. this.y *= scale;
  484. this.z *= scale;
  485. return this;
  486. };
  487. Vector3.prototype.scale = function (scale) {
  488. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  489. };
  490. Vector3.prototype.scaleToRef = function (scale, result) {
  491. result.x = this.x * scale;
  492. result.y = this.y * scale;
  493. result.z = this.z * scale;
  494. };
  495. Vector3.prototype.equals = function (otherVector) {
  496. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  497. };
  498. Vector3.prototype.equalsWithEpsilon = function (otherVector) {
  499. 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;
  500. };
  501. Vector3.prototype.equalsToFloats = function (x, y, z) {
  502. return this.x === x && this.y === y && this.z === z;
  503. };
  504. Vector3.prototype.multiplyInPlace = function (otherVector) {
  505. this.x *= otherVector.x;
  506. this.y *= otherVector.y;
  507. this.z *= otherVector.z;
  508. return this;
  509. };
  510. Vector3.prototype.multiply = function (otherVector) {
  511. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  512. };
  513. Vector3.prototype.multiplyToRef = function (otherVector, result) {
  514. result.x = this.x * otherVector.x;
  515. result.y = this.y * otherVector.y;
  516. result.z = this.z * otherVector.z;
  517. return this;
  518. };
  519. Vector3.prototype.multiplyByFloats = function (x, y, z) {
  520. return new Vector3(this.x * x, this.y * y, this.z * z);
  521. };
  522. Vector3.prototype.divide = function (otherVector) {
  523. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  524. };
  525. Vector3.prototype.divideToRef = function (otherVector, result) {
  526. result.x = this.x / otherVector.x;
  527. result.y = this.y / otherVector.y;
  528. result.z = this.z / otherVector.z;
  529. return this;
  530. };
  531. Vector3.prototype.MinimizeInPlace = function (other) {
  532. if (other.x < this.x)
  533. this.x = other.x;
  534. if (other.y < this.y)
  535. this.y = other.y;
  536. if (other.z < this.z)
  537. this.z = other.z;
  538. return this;
  539. };
  540. Vector3.prototype.MaximizeInPlace = function (other) {
  541. if (other.x > this.x)
  542. this.x = other.x;
  543. if (other.y > this.y)
  544. this.y = other.y;
  545. if (other.z > this.z)
  546. this.z = other.z;
  547. return this;
  548. };
  549. // Properties
  550. Vector3.prototype.length = function () {
  551. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  552. };
  553. Vector3.prototype.lengthSquared = function () {
  554. return (this.x * this.x + this.y * this.y + this.z * this.z);
  555. };
  556. // Methods
  557. Vector3.prototype.normalize = function () {
  558. var len = this.length();
  559. if (len === 0)
  560. return this;
  561. var num = 1.0 / len;
  562. this.x *= num;
  563. this.y *= num;
  564. this.z *= num;
  565. return this;
  566. };
  567. Vector3.prototype.clone = function () {
  568. return new Vector3(this.x, this.y, this.z);
  569. };
  570. Vector3.prototype.copyFrom = function (source) {
  571. this.x = source.x;
  572. this.y = source.y;
  573. this.z = source.z;
  574. return this;
  575. };
  576. Vector3.prototype.copyFromFloats = function (x, y, z) {
  577. this.x = x;
  578. this.y = y;
  579. this.z = z;
  580. return this;
  581. };
  582. // Statics
  583. Vector3.FromArray = function (array, offset) {
  584. if (!offset) {
  585. offset = 0;
  586. }
  587. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  588. };
  589. Vector3.FromArrayToRef = function (array, offset, result) {
  590. result.x = array[offset];
  591. result.y = array[offset + 1];
  592. result.z = array[offset + 2];
  593. };
  594. Vector3.FromFloatArrayToRef = function (array, offset, result) {
  595. result.x = array[offset];
  596. result.y = array[offset + 1];
  597. result.z = array[offset + 2];
  598. };
  599. Vector3.FromFloatsToRef = function (x, y, z, result) {
  600. result.x = x;
  601. result.y = y;
  602. result.z = z;
  603. };
  604. Vector3.Zero = function () {
  605. return new Vector3(0, 0, 0);
  606. };
  607. Vector3.Up = function () {
  608. return new Vector3(0, 1.0, 0);
  609. };
  610. Vector3.TransformCoordinates = function (vector, transformation) {
  611. var result = Vector3.Zero();
  612. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  613. return result;
  614. };
  615. Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  616. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  617. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  618. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  619. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  620. result.x = x / w;
  621. result.y = y / w;
  622. result.z = z / w;
  623. };
  624. Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  625. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  626. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  627. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  628. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  629. result.x = rx / rw;
  630. result.y = ry / rw;
  631. result.z = rz / rw;
  632. };
  633. Vector3.TransformNormal = function (vector, transformation) {
  634. var result = Vector3.Zero();
  635. Vector3.TransformNormalToRef(vector, transformation, result);
  636. return result;
  637. };
  638. Vector3.TransformNormalToRef = function (vector, transformation, result) {
  639. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  640. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  641. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  642. };
  643. Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  644. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  645. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  646. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  647. };
  648. Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  649. var squared = amount * amount;
  650. var cubed = amount * squared;
  651. 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));
  652. 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));
  653. 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));
  654. return new Vector3(x, y, z);
  655. };
  656. Vector3.Clamp = function (value, min, max) {
  657. var x = value.x;
  658. x = (x > max.x) ? max.x : x;
  659. x = (x < min.x) ? min.x : x;
  660. var y = value.y;
  661. y = (y > max.y) ? max.y : y;
  662. y = (y < min.y) ? min.y : y;
  663. var z = value.z;
  664. z = (z > max.z) ? max.z : z;
  665. z = (z < min.z) ? min.z : z;
  666. return new Vector3(x, y, z);
  667. };
  668. Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  669. var squared = amount * amount;
  670. var cubed = amount * squared;
  671. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  672. var part2 = (-2.0 * cubed) + (3.0 * squared);
  673. var part3 = (cubed - (2.0 * squared)) + amount;
  674. var part4 = cubed - squared;
  675. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  676. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  677. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  678. return new Vector3(x, y, z);
  679. };
  680. Vector3.Lerp = function (start, end, amount) {
  681. var x = start.x + ((end.x - start.x) * amount);
  682. var y = start.y + ((end.y - start.y) * amount);
  683. var z = start.z + ((end.z - start.z) * amount);
  684. return new Vector3(x, y, z);
  685. };
  686. Vector3.Dot = function (left, right) {
  687. return (left.x * right.x + left.y * right.y + left.z * right.z);
  688. };
  689. Vector3.Cross = function (left, right) {
  690. var result = Vector3.Zero();
  691. Vector3.CrossToRef(left, right, result);
  692. return result;
  693. };
  694. Vector3.CrossToRef = function (left, right, result) {
  695. result.x = left.y * right.z - left.z * right.y;
  696. result.y = left.z * right.x - left.x * right.z;
  697. result.z = left.x * right.y - left.y * right.x;
  698. };
  699. Vector3.Normalize = function (vector) {
  700. var result = Vector3.Zero();
  701. Vector3.NormalizeToRef(vector, result);
  702. return result;
  703. };
  704. Vector3.NormalizeToRef = function (vector, result) {
  705. result.copyFrom(vector);
  706. result.normalize();
  707. };
  708. Vector3.Project = function (vector, world, transform, viewport) {
  709. var cw = viewport.width;
  710. var ch = viewport.height;
  711. var cx = viewport.x;
  712. var cy = viewport.y;
  713. var viewportMatrix = 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);
  714. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  715. return Vector3.TransformCoordinates(vector, finalMatrix);
  716. };
  717. Vector3.UnprojectFromTransform = function (source, viewportWidth, viewportHeight, world, transform) {
  718. var matrix = world.multiply(transform);
  719. matrix.invert();
  720. source.x = source.x / viewportWidth * 2 - 1;
  721. source.y = -(source.y / viewportHeight * 2 - 1);
  722. var vector = Vector3.TransformCoordinates(source, matrix);
  723. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  724. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  725. vector = vector.scale(1.0 / num);
  726. }
  727. return vector;
  728. };
  729. Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  730. var matrix = world.multiply(view).multiply(projection);
  731. matrix.invert();
  732. source.x = source.x / viewportWidth * 2 - 1;
  733. source.y = -(source.y / viewportHeight * 2 - 1);
  734. var vector = Vector3.TransformCoordinates(source, matrix);
  735. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  736. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  737. vector = vector.scale(1.0 / num);
  738. }
  739. return vector;
  740. };
  741. Vector3.Minimize = function (left, right) {
  742. var min = left.clone();
  743. min.MinimizeInPlace(right);
  744. return min;
  745. };
  746. Vector3.Maximize = function (left, right) {
  747. var max = left.clone();
  748. max.MaximizeInPlace(right);
  749. return max;
  750. };
  751. Vector3.Distance = function (value1, value2) {
  752. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  753. };
  754. Vector3.DistanceSquared = function (value1, value2) {
  755. var x = value1.x - value2.x;
  756. var y = value1.y - value2.y;
  757. var z = value1.z - value2.z;
  758. return (x * x) + (y * y) + (z * z);
  759. };
  760. Vector3.Center = function (value1, value2) {
  761. var center = value1.add(value2);
  762. center.scaleInPlace(0.5);
  763. return center;
  764. };
  765. return Vector3;
  766. })();
  767. BABYLON.Vector3 = Vector3;
  768. //Vector4 class created for EulerAngle class conversion to Quaternion
  769. var Vector4 = (function () {
  770. function Vector4(x, y, z, w) {
  771. this.x = x;
  772. this.y = y;
  773. this.z = z;
  774. this.w = w;
  775. }
  776. Vector4.prototype.toString = function () {
  777. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "W:" + this.w + "}";
  778. };
  779. // Operators
  780. Vector4.prototype.asArray = function () {
  781. var result = [];
  782. this.toArray(result, 0);
  783. return result;
  784. };
  785. Vector4.prototype.toArray = function (array, index) {
  786. if (index === undefined) {
  787. index = 0;
  788. }
  789. array[index] = this.x;
  790. array[index + 1] = this.y;
  791. array[index + 2] = this.z;
  792. array[index + 3] = this.w;
  793. return this;
  794. };
  795. Vector4.prototype.addInPlace = function (otherVector) {
  796. this.x += otherVector.x;
  797. this.y += otherVector.y;
  798. this.z += otherVector.z;
  799. this.w += otherVector.w;
  800. return this;
  801. };
  802. Vector4.prototype.add = function (otherVector) {
  803. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  804. };
  805. Vector4.prototype.addToRef = function (otherVector, result) {
  806. result.x = this.x + otherVector.x;
  807. result.y = this.y + otherVector.y;
  808. result.z = this.z + otherVector.z;
  809. result.w = this.w + otherVector.w;
  810. return this;
  811. };
  812. Vector4.prototype.subtractInPlace = function (otherVector) {
  813. this.x -= otherVector.x;
  814. this.y -= otherVector.y;
  815. this.z -= otherVector.z;
  816. this.w -= otherVector.w;
  817. return this;
  818. };
  819. Vector4.prototype.subtract = function (otherVector) {
  820. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  821. };
  822. Vector4.prototype.subtractToRef = function (otherVector, result) {
  823. result.x = this.x - otherVector.x;
  824. result.y = this.y - otherVector.y;
  825. result.z = this.z - otherVector.z;
  826. result.w = this.w - otherVector.w;
  827. return this;
  828. };
  829. Vector4.prototype.subtractFromFloats = function (x, y, z, w) {
  830. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  831. };
  832. Vector4.prototype.subtractFromFloatsToRef = function (x, y, z, w, result) {
  833. result.x = this.x - x;
  834. result.y = this.y - y;
  835. result.z = this.z - z;
  836. result.w = this.w - w;
  837. return this;
  838. };
  839. Vector4.prototype.negate = function () {
  840. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  841. };
  842. Vector4.prototype.scaleInPlace = function (scale) {
  843. this.x *= scale;
  844. this.y *= scale;
  845. this.z *= scale;
  846. this.w *= scale;
  847. return this;
  848. };
  849. Vector4.prototype.scale = function (scale) {
  850. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  851. };
  852. Vector4.prototype.scaleToRef = function (scale, result) {
  853. result.x = this.x * scale;
  854. result.y = this.y * scale;
  855. result.z = this.z * scale;
  856. result.w = this.w * scale;
  857. };
  858. Vector4.prototype.equals = function (otherVector) {
  859. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  860. };
  861. Vector4.prototype.equalsWithEpsilon = function (otherVector) {
  862. 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 && Math.abs(this.w - otherVector.w) < BABYLON.Engine.Epsilon;
  863. };
  864. Vector4.prototype.equalsToFloats = function (x, y, z, w) {
  865. return this.x === x && this.y === y && this.z === z && this.w === w;
  866. };
  867. Vector4.prototype.multiplyInPlace = function (otherVector) {
  868. this.x *= otherVector.x;
  869. this.y *= otherVector.y;
  870. this.z *= otherVector.z;
  871. this.w *= otherVector.w;
  872. return this;
  873. };
  874. Vector4.prototype.multiply = function (otherVector) {
  875. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  876. };
  877. Vector4.prototype.multiplyToRef = function (otherVector, result) {
  878. result.x = this.x * otherVector.x;
  879. result.y = this.y * otherVector.y;
  880. result.z = this.z * otherVector.z;
  881. result.w = this.w * otherVector.w;
  882. return this;
  883. };
  884. Vector4.prototype.multiplyByFloats = function (x, y, z, w) {
  885. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  886. };
  887. Vector4.prototype.divide = function (otherVector) {
  888. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  889. };
  890. Vector4.prototype.divideToRef = function (otherVector, result) {
  891. result.x = this.x / otherVector.x;
  892. result.y = this.y / otherVector.y;
  893. result.z = this.z / otherVector.z;
  894. result.w = this.w / otherVector.w;
  895. return this;
  896. };
  897. Vector4.prototype.MinimizeInPlace = function (other) {
  898. if (other.x < this.x)
  899. this.x = other.x;
  900. if (other.y < this.y)
  901. this.y = other.y;
  902. if (other.z < this.z)
  903. this.z = other.z;
  904. if (other.w < this.w)
  905. this.w = other.w;
  906. return this;
  907. };
  908. Vector4.prototype.MaximizeInPlace = function (other) {
  909. if (other.x > this.x)
  910. this.x = other.x;
  911. if (other.y > this.y)
  912. this.y = other.y;
  913. if (other.z > this.z)
  914. this.z = other.z;
  915. if (other.w > this.w)
  916. this.w = other.w;
  917. return this;
  918. };
  919. // Properties
  920. Vector4.prototype.length = function () {
  921. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  922. };
  923. Vector4.prototype.lengthSquared = function () {
  924. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  925. };
  926. // Methods
  927. Vector4.prototype.normalize = function () {
  928. var len = this.length();
  929. if (len === 0)
  930. return this;
  931. var num = 1.0 / len;
  932. this.x *= num;
  933. this.y *= num;
  934. this.z *= num;
  935. this.w *= num;
  936. return this;
  937. };
  938. Vector4.prototype.clone = function () {
  939. return new Vector4(this.x, this.y, this.z, this.w);
  940. };
  941. Vector4.prototype.copyFrom = function (source) {
  942. this.x = source.x;
  943. this.y = source.y;
  944. this.z = source.z;
  945. this.w = source.w;
  946. return this;
  947. };
  948. Vector4.prototype.copyFromFloats = function (x, y, z, w) {
  949. this.x = x;
  950. this.y = y;
  951. this.z = z;
  952. this.w = w;
  953. return this;
  954. };
  955. // Statics
  956. Vector4.FromArray = function (array, offset) {
  957. if (!offset) {
  958. offset = 0;
  959. }
  960. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  961. };
  962. Vector4.FromArrayToRef = function (array, offset, result) {
  963. result.x = array[offset];
  964. result.y = array[offset + 1];
  965. result.z = array[offset + 2];
  966. result.w = array[offset + 3];
  967. };
  968. Vector4.FromFloatArrayToRef = function (array, offset, result) {
  969. result.x = array[offset];
  970. result.y = array[offset + 1];
  971. result.z = array[offset + 2];
  972. result.w = array[offset + 3];
  973. };
  974. Vector4.FromFloatsToRef = function (x, y, z, w, result) {
  975. result.x = x;
  976. result.y = y;
  977. result.z = z;
  978. result.w = w;
  979. };
  980. Vector4.Zero = function () {
  981. return new Vector4(0, 0, 0, 0);
  982. };
  983. Vector4.Normalize = function (vector) {
  984. var result = Vector4.Zero();
  985. Vector4.NormalizeToRef(vector, result);
  986. return result;
  987. };
  988. Vector4.NormalizeToRef = function (vector, result) {
  989. result.copyFrom(vector);
  990. result.normalize();
  991. };
  992. Vector4.Minimize = function (left, right) {
  993. var min = left.clone();
  994. min.MinimizeInPlace(right);
  995. return min;
  996. };
  997. Vector4.Maximize = function (left, right) {
  998. var max = left.clone();
  999. max.MaximizeInPlace(right);
  1000. return max;
  1001. };
  1002. Vector4.Distance = function (value1, value2) {
  1003. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1004. };
  1005. Vector4.DistanceSquared = function (value1, value2) {
  1006. var x = value1.x - value2.x;
  1007. var y = value1.y - value2.y;
  1008. var z = value1.z - value2.z;
  1009. var w = value1.w - value2.w;
  1010. return (x * x) + (y * y) + (z * z) + (w * w);
  1011. };
  1012. Vector4.Center = function (value1, value2) {
  1013. var center = value1.add(value2);
  1014. center.scaleInPlace(0.5);
  1015. return center;
  1016. };
  1017. return Vector4;
  1018. })();
  1019. BABYLON.Vector4 = Vector4;
  1020. var Quaternion = (function () {
  1021. function Quaternion(x, y, z, w) {
  1022. if (x === void 0) { x = 0; }
  1023. if (y === void 0) { y = 0; }
  1024. if (z === void 0) { z = 0; }
  1025. if (w === void 0) { w = 1; }
  1026. this.x = x;
  1027. this.y = y;
  1028. this.z = z;
  1029. this.w = w;
  1030. }
  1031. Quaternion.prototype.toString = function () {
  1032. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1033. };
  1034. Quaternion.prototype.asArray = function () {
  1035. return [this.x, this.y, this.z, this.w];
  1036. };
  1037. Quaternion.prototype.equals = function (otherQuaternion) {
  1038. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  1039. };
  1040. Quaternion.prototype.clone = function () {
  1041. return new Quaternion(this.x, this.y, this.z, this.w);
  1042. };
  1043. Quaternion.prototype.copyFrom = function (other) {
  1044. this.x = other.x;
  1045. this.y = other.y;
  1046. this.z = other.z;
  1047. this.w = other.w;
  1048. return this;
  1049. };
  1050. Quaternion.prototype.copyFromFloats = function (x, y, z, w) {
  1051. this.x = x;
  1052. this.y = y;
  1053. this.z = z;
  1054. this.w = w;
  1055. return this;
  1056. };
  1057. Quaternion.prototype.add = function (other) {
  1058. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  1059. };
  1060. Quaternion.prototype.subtract = function (other) {
  1061. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  1062. };
  1063. Quaternion.prototype.scale = function (value) {
  1064. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  1065. };
  1066. Quaternion.prototype.multiply = function (q1) {
  1067. var result = new Quaternion(0, 0, 0, 1.0);
  1068. this.multiplyToRef(q1, result);
  1069. return result;
  1070. };
  1071. Quaternion.prototype.multiplyToRef = function (q1, result) {
  1072. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1073. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1074. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1075. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1076. return this;
  1077. };
  1078. Quaternion.prototype.length = function () {
  1079. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1080. };
  1081. Quaternion.prototype.normalize = function () {
  1082. var length = 1.0 / this.length();
  1083. this.x *= length;
  1084. this.y *= length;
  1085. this.z *= length;
  1086. this.w *= length;
  1087. return this;
  1088. };
  1089. Quaternion.prototype.toEulerAngles = function () {
  1090. var result = Vector3.Zero();
  1091. this.toEulerAnglesToRef(result);
  1092. return result;
  1093. };
  1094. Quaternion.prototype.toEulerAnglesToRef = function (result) {
  1095. //result is an EulerAngles in the in the z-x-z convention
  1096. var qx = this.x;
  1097. var qy = this.y;
  1098. var qz = this.z;
  1099. var qw = this.w;
  1100. var qxy = qx * qy;
  1101. var qxz = qx * qz;
  1102. var qwy = qw * qy;
  1103. var qwz = qw * qz;
  1104. var qwx = qw * qx;
  1105. var qyz = qy * qz;
  1106. var sqx = qx * qx;
  1107. var sqy = qy * qy;
  1108. var determinant = sqx + sqy;
  1109. if (determinant !== 0.000 && determinant !== 1.000) {
  1110. result.x = Math.atan2(qxz + qwy, qwx - qyz);
  1111. result.y = Math.acos(1 - 2 * determinant);
  1112. result.z = Math.atan2(qxz - qwy, qwx + qyz);
  1113. }
  1114. else {
  1115. if (determinant === 0.0) {
  1116. result.x = 0.0;
  1117. result.y = 0.0;
  1118. result.z = Math.atan2(qxy - qwz, 0.5 - sqy - qz * qz); //actually, degeneracy gives us choice with x+z=Math.atan2(qxy-qwz,0.5-sqy-qz*qz)
  1119. }
  1120. else {
  1121. result.x = Math.atan2(qxy - qwz, 0.5 - sqy - qz * qz); //actually, degeneracy gives us choice with x-z=Math.atan2(qxy-qwz,0.5-sqy-qz*qz)
  1122. result.y = Math.PI;
  1123. result.z = 0.0;
  1124. }
  1125. }
  1126. return this;
  1127. };
  1128. Quaternion.prototype.toRotationMatrix = function (result) {
  1129. var xx = this.x * this.x;
  1130. var yy = this.y * this.y;
  1131. var zz = this.z * this.z;
  1132. var xy = this.x * this.y;
  1133. var zw = this.z * this.w;
  1134. var zx = this.z * this.x;
  1135. var yw = this.y * this.w;
  1136. var yz = this.y * this.z;
  1137. var xw = this.x * this.w;
  1138. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1139. result.m[1] = 2.0 * (xy + zw);
  1140. result.m[2] = 2.0 * (zx - yw);
  1141. result.m[3] = 0;
  1142. result.m[4] = 2.0 * (xy - zw);
  1143. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1144. result.m[6] = 2.0 * (yz + xw);
  1145. result.m[7] = 0;
  1146. result.m[8] = 2.0 * (zx + yw);
  1147. result.m[9] = 2.0 * (yz - xw);
  1148. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1149. result.m[11] = 0;
  1150. result.m[12] = 0;
  1151. result.m[13] = 0;
  1152. result.m[14] = 0;
  1153. result.m[15] = 1.0;
  1154. return this;
  1155. };
  1156. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  1157. Quaternion.FromRotationMatrixToRef(matrix, this);
  1158. return this;
  1159. };
  1160. // Statics
  1161. Quaternion.FromRotationMatrix = function (matrix) {
  1162. var result = new Quaternion();
  1163. Quaternion.FromRotationMatrixToRef(matrix, result);
  1164. return result;
  1165. };
  1166. Quaternion.FromRotationMatrixToRef = function (matrix, result) {
  1167. var data = matrix.m;
  1168. var m11 = data[0], m12 = data[4], m13 = data[8];
  1169. var m21 = data[1], m22 = data[5], m23 = data[9];
  1170. var m31 = data[2], m32 = data[6], m33 = data[10];
  1171. var trace = m11 + m22 + m33;
  1172. var s;
  1173. if (trace > 0) {
  1174. s = 0.5 / Math.sqrt(trace + 1.0);
  1175. result.w = 0.25 / s;
  1176. result.x = (m32 - m23) * s;
  1177. result.y = (m13 - m31) * s;
  1178. result.z = (m21 - m12) * s;
  1179. }
  1180. else if (m11 > m22 && m11 > m33) {
  1181. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1182. result.w = (m32 - m23) / s;
  1183. result.x = 0.25 * s;
  1184. result.y = (m12 + m21) / s;
  1185. result.z = (m13 + m31) / s;
  1186. }
  1187. else if (m22 > m33) {
  1188. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1189. result.w = (m13 - m31) / s;
  1190. result.x = (m12 + m21) / s;
  1191. result.y = 0.25 * s;
  1192. result.z = (m23 + m32) / s;
  1193. }
  1194. else {
  1195. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1196. result.w = (m21 - m12) / s;
  1197. result.x = (m13 + m31) / s;
  1198. result.y = (m23 + m32) / s;
  1199. result.z = 0.25 * s;
  1200. }
  1201. };
  1202. Quaternion.Inverse = function (q) {
  1203. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1204. };
  1205. Quaternion.Identity = function () {
  1206. return new Quaternion(0, 0, 0, 1);
  1207. };
  1208. Quaternion.RotationAxis = function (axis, angle) {
  1209. var result = new Quaternion();
  1210. var sin = Math.sin(angle / 2);
  1211. result.w = Math.cos(angle / 2);
  1212. result.x = axis.x * sin;
  1213. result.y = axis.y * sin;
  1214. result.z = axis.z * sin;
  1215. return result;
  1216. };
  1217. Quaternion.FromArray = function (array, offset) {
  1218. if (!offset) {
  1219. offset = 0;
  1220. }
  1221. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1222. };
  1223. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1224. var result = new Quaternion();
  1225. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1226. return result;
  1227. };
  1228. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1229. var halfRoll = roll * 0.5;
  1230. var halfPitch = pitch * 0.5;
  1231. var halfYaw = yaw * 0.5;
  1232. var sinRoll = Math.sin(halfRoll);
  1233. var cosRoll = Math.cos(halfRoll);
  1234. var sinPitch = Math.sin(halfPitch);
  1235. var cosPitch = Math.cos(halfPitch);
  1236. var sinYaw = Math.sin(halfYaw);
  1237. var cosYaw = Math.cos(halfYaw);
  1238. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1239. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1240. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1241. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1242. };
  1243. Quaternion.Slerp = function (left, right, amount) {
  1244. var num2;
  1245. var num3;
  1246. var num = amount;
  1247. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1248. var flag = false;
  1249. if (num4 < 0) {
  1250. flag = true;
  1251. num4 = -num4;
  1252. }
  1253. if (num4 > 0.999999) {
  1254. num3 = 1 - num;
  1255. num2 = flag ? -num : num;
  1256. }
  1257. else {
  1258. var num5 = Math.acos(num4);
  1259. var num6 = (1.0 / Math.sin(num5));
  1260. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1261. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1262. }
  1263. 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));
  1264. };
  1265. return Quaternion;
  1266. })();
  1267. BABYLON.Quaternion = Quaternion;
  1268. var Matrix = (function () {
  1269. function Matrix() {
  1270. this.m = new Float32Array(16);
  1271. }
  1272. // Properties
  1273. Matrix.prototype.isIdentity = function () {
  1274. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1275. return false;
  1276. 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)
  1277. return false;
  1278. return true;
  1279. };
  1280. Matrix.prototype.determinant = function () {
  1281. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1282. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1283. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1284. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1285. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1286. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1287. 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))));
  1288. };
  1289. // Methods
  1290. Matrix.prototype.toArray = function () {
  1291. return this.m;
  1292. };
  1293. Matrix.prototype.asArray = function () {
  1294. return this.toArray();
  1295. };
  1296. Matrix.prototype.invert = function () {
  1297. this.invertToRef(this);
  1298. return this;
  1299. };
  1300. Matrix.prototype.invertToRef = function (other) {
  1301. var l1 = this.m[0];
  1302. var l2 = this.m[1];
  1303. var l3 = this.m[2];
  1304. var l4 = this.m[3];
  1305. var l5 = this.m[4];
  1306. var l6 = this.m[5];
  1307. var l7 = this.m[6];
  1308. var l8 = this.m[7];
  1309. var l9 = this.m[8];
  1310. var l10 = this.m[9];
  1311. var l11 = this.m[10];
  1312. var l12 = this.m[11];
  1313. var l13 = this.m[12];
  1314. var l14 = this.m[13];
  1315. var l15 = this.m[14];
  1316. var l16 = this.m[15];
  1317. var l17 = (l11 * l16) - (l12 * l15);
  1318. var l18 = (l10 * l16) - (l12 * l14);
  1319. var l19 = (l10 * l15) - (l11 * l14);
  1320. var l20 = (l9 * l16) - (l12 * l13);
  1321. var l21 = (l9 * l15) - (l11 * l13);
  1322. var l22 = (l9 * l14) - (l10 * l13);
  1323. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1324. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1325. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1326. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1327. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1328. var l28 = (l7 * l16) - (l8 * l15);
  1329. var l29 = (l6 * l16) - (l8 * l14);
  1330. var l30 = (l6 * l15) - (l7 * l14);
  1331. var l31 = (l5 * l16) - (l8 * l13);
  1332. var l32 = (l5 * l15) - (l7 * l13);
  1333. var l33 = (l5 * l14) - (l6 * l13);
  1334. var l34 = (l7 * l12) - (l8 * l11);
  1335. var l35 = (l6 * l12) - (l8 * l10);
  1336. var l36 = (l6 * l11) - (l7 * l10);
  1337. var l37 = (l5 * l12) - (l8 * l9);
  1338. var l38 = (l5 * l11) - (l7 * l9);
  1339. var l39 = (l5 * l10) - (l6 * l9);
  1340. other.m[0] = l23 * l27;
  1341. other.m[4] = l24 * l27;
  1342. other.m[8] = l25 * l27;
  1343. other.m[12] = l26 * l27;
  1344. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1345. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1346. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1347. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1348. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1349. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1350. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1351. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1352. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1353. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1354. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1355. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1356. return this;
  1357. };
  1358. Matrix.prototype.setTranslation = function (vector3) {
  1359. this.m[12] = vector3.x;
  1360. this.m[13] = vector3.y;
  1361. this.m[14] = vector3.z;
  1362. return this;
  1363. };
  1364. Matrix.prototype.multiply = function (other) {
  1365. var result = new Matrix();
  1366. this.multiplyToRef(other, result);
  1367. return result;
  1368. };
  1369. Matrix.prototype.copyFrom = function (other) {
  1370. for (var index = 0; index < 16; index++) {
  1371. this.m[index] = other.m[index];
  1372. }
  1373. return this;
  1374. };
  1375. Matrix.prototype.copyToArray = function (array, offset) {
  1376. if (offset === void 0) { offset = 0; }
  1377. for (var index = 0; index < 16; index++) {
  1378. array[offset + index] = this.m[index];
  1379. }
  1380. return this;
  1381. };
  1382. Matrix.prototype.multiplyToRef = function (other, result) {
  1383. this.multiplyToArray(other, result.m, 0);
  1384. return this;
  1385. };
  1386. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1387. var tm0 = this.m[0];
  1388. var tm1 = this.m[1];
  1389. var tm2 = this.m[2];
  1390. var tm3 = this.m[3];
  1391. var tm4 = this.m[4];
  1392. var tm5 = this.m[5];
  1393. var tm6 = this.m[6];
  1394. var tm7 = this.m[7];
  1395. var tm8 = this.m[8];
  1396. var tm9 = this.m[9];
  1397. var tm10 = this.m[10];
  1398. var tm11 = this.m[11];
  1399. var tm12 = this.m[12];
  1400. var tm13 = this.m[13];
  1401. var tm14 = this.m[14];
  1402. var tm15 = this.m[15];
  1403. var om0 = other.m[0];
  1404. var om1 = other.m[1];
  1405. var om2 = other.m[2];
  1406. var om3 = other.m[3];
  1407. var om4 = other.m[4];
  1408. var om5 = other.m[5];
  1409. var om6 = other.m[6];
  1410. var om7 = other.m[7];
  1411. var om8 = other.m[8];
  1412. var om9 = other.m[9];
  1413. var om10 = other.m[10];
  1414. var om11 = other.m[11];
  1415. var om12 = other.m[12];
  1416. var om13 = other.m[13];
  1417. var om14 = other.m[14];
  1418. var om15 = other.m[15];
  1419. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1420. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1421. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1422. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1423. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1424. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1425. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1426. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1427. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1428. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1429. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1430. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1431. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1432. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1433. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1434. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1435. return this;
  1436. };
  1437. Matrix.prototype.equals = function (value) {
  1438. 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]);
  1439. };
  1440. Matrix.prototype.clone = function () {
  1441. 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]);
  1442. };
  1443. Matrix.prototype.decompose = function (scale, rotation, translation) {
  1444. translation.x = this.m[12];
  1445. translation.y = this.m[13];
  1446. translation.z = this.m[14];
  1447. var xs = BABYLON.Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1448. var ys = BABYLON.Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1449. var zs = BABYLON.Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1450. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1451. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1452. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1453. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  1454. rotation.x = 0;
  1455. rotation.y = 0;
  1456. rotation.z = 0;
  1457. rotation.w = 1;
  1458. return false;
  1459. }
  1460. var rotationMatrix = Matrix.FromValues(this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0, this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0, this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0, 0, 0, 0, 1);
  1461. Quaternion.FromRotationMatrixToRef(rotationMatrix, rotation);
  1462. return true;
  1463. };
  1464. // Statics
  1465. Matrix.FromArray = function (array, offset) {
  1466. var result = new Matrix();
  1467. if (!offset) {
  1468. offset = 0;
  1469. }
  1470. Matrix.FromArrayToRef(array, offset, result);
  1471. return result;
  1472. };
  1473. Matrix.FromArrayToRef = function (array, offset, result) {
  1474. for (var index = 0; index < 16; index++) {
  1475. result.m[index] = array[index + offset];
  1476. }
  1477. };
  1478. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1479. result.m[0] = initialM11;
  1480. result.m[1] = initialM12;
  1481. result.m[2] = initialM13;
  1482. result.m[3] = initialM14;
  1483. result.m[4] = initialM21;
  1484. result.m[5] = initialM22;
  1485. result.m[6] = initialM23;
  1486. result.m[7] = initialM24;
  1487. result.m[8] = initialM31;
  1488. result.m[9] = initialM32;
  1489. result.m[10] = initialM33;
  1490. result.m[11] = initialM34;
  1491. result.m[12] = initialM41;
  1492. result.m[13] = initialM42;
  1493. result.m[14] = initialM43;
  1494. result.m[15] = initialM44;
  1495. };
  1496. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  1497. var result = new Matrix();
  1498. result.m[0] = initialM11;
  1499. result.m[1] = initialM12;
  1500. result.m[2] = initialM13;
  1501. result.m[3] = initialM14;
  1502. result.m[4] = initialM21;
  1503. result.m[5] = initialM22;
  1504. result.m[6] = initialM23;
  1505. result.m[7] = initialM24;
  1506. result.m[8] = initialM31;
  1507. result.m[9] = initialM32;
  1508. result.m[10] = initialM33;
  1509. result.m[11] = initialM34;
  1510. result.m[12] = initialM41;
  1511. result.m[13] = initialM42;
  1512. result.m[14] = initialM43;
  1513. result.m[15] = initialM44;
  1514. return result;
  1515. };
  1516. Matrix.Compose = function (scale, rotation, translation) {
  1517. var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
  1518. var rotationMatrix = Matrix.Identity();
  1519. rotation.toRotationMatrix(rotationMatrix);
  1520. result = result.multiply(rotationMatrix);
  1521. result.setTranslation(translation);
  1522. return result;
  1523. };
  1524. Matrix.Identity = function () {
  1525. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  1526. };
  1527. Matrix.IdentityToRef = function (result) {
  1528. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  1529. };
  1530. Matrix.Zero = function () {
  1531. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1532. };
  1533. Matrix.RotationX = function (angle) {
  1534. var result = new Matrix();
  1535. Matrix.RotationXToRef(angle, result);
  1536. return result;
  1537. };
  1538. Matrix.Invert = function (source) {
  1539. var result = new Matrix();
  1540. source.invertToRef(result);
  1541. return result;
  1542. };
  1543. Matrix.RotationXToRef = function (angle, result) {
  1544. var s = Math.sin(angle);
  1545. var c = Math.cos(angle);
  1546. result.m[0] = 1.0;
  1547. result.m[15] = 1.0;
  1548. result.m[5] = c;
  1549. result.m[10] = c;
  1550. result.m[9] = -s;
  1551. result.m[6] = s;
  1552. result.m[1] = 0;
  1553. result.m[2] = 0;
  1554. result.m[3] = 0;
  1555. result.m[4] = 0;
  1556. result.m[7] = 0;
  1557. result.m[8] = 0;
  1558. result.m[11] = 0;
  1559. result.m[12] = 0;
  1560. result.m[13] = 0;
  1561. result.m[14] = 0;
  1562. };
  1563. Matrix.RotationY = function (angle) {
  1564. var result = new Matrix();
  1565. Matrix.RotationYToRef(angle, result);
  1566. return result;
  1567. };
  1568. Matrix.RotationYToRef = function (angle, result) {
  1569. var s = Math.sin(angle);
  1570. var c = Math.cos(angle);
  1571. result.m[5] = 1.0;
  1572. result.m[15] = 1.0;
  1573. result.m[0] = c;
  1574. result.m[2] = -s;
  1575. result.m[8] = s;
  1576. result.m[10] = c;
  1577. result.m[1] = 0;
  1578. result.m[3] = 0;
  1579. result.m[4] = 0;
  1580. result.m[6] = 0;
  1581. result.m[7] = 0;
  1582. result.m[9] = 0;
  1583. result.m[11] = 0;
  1584. result.m[12] = 0;
  1585. result.m[13] = 0;
  1586. result.m[14] = 0;
  1587. };
  1588. Matrix.RotationZ = function (angle) {
  1589. var result = new Matrix();
  1590. Matrix.RotationZToRef(angle, result);
  1591. return result;
  1592. };
  1593. Matrix.RotationZToRef = function (angle, result) {
  1594. var s = Math.sin(angle);
  1595. var c = Math.cos(angle);
  1596. result.m[10] = 1.0;
  1597. result.m[15] = 1.0;
  1598. result.m[0] = c;
  1599. result.m[1] = s;
  1600. result.m[4] = -s;
  1601. result.m[5] = c;
  1602. result.m[2] = 0;
  1603. result.m[3] = 0;
  1604. result.m[6] = 0;
  1605. result.m[7] = 0;
  1606. result.m[8] = 0;
  1607. result.m[9] = 0;
  1608. result.m[11] = 0;
  1609. result.m[12] = 0;
  1610. result.m[13] = 0;
  1611. result.m[14] = 0;
  1612. };
  1613. Matrix.RotationAxis = function (axis, angle) {
  1614. var s = Math.sin(-angle);
  1615. var c = Math.cos(-angle);
  1616. var c1 = 1 - c;
  1617. axis.normalize();
  1618. var result = Matrix.Zero();
  1619. result.m[0] = (axis.x * axis.x) * c1 + c;
  1620. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1621. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1622. result.m[3] = 0.0;
  1623. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1624. result.m[5] = (axis.y * axis.y) * c1 + c;
  1625. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1626. result.m[7] = 0.0;
  1627. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1628. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1629. result.m[10] = (axis.z * axis.z) * c1 + c;
  1630. result.m[11] = 0.0;
  1631. result.m[15] = 1.0;
  1632. return result;
  1633. };
  1634. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1635. var result = new Matrix();
  1636. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1637. return result;
  1638. };
  1639. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1640. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1641. this._tempQuaternion.toRotationMatrix(result);
  1642. };
  1643. Matrix.Scaling = function (x, y, z) {
  1644. var result = Matrix.Zero();
  1645. Matrix.ScalingToRef(x, y, z, result);
  1646. return result;
  1647. };
  1648. Matrix.ScalingToRef = function (x, y, z, result) {
  1649. result.m[0] = x;
  1650. result.m[1] = 0;
  1651. result.m[2] = 0;
  1652. result.m[3] = 0;
  1653. result.m[4] = 0;
  1654. result.m[5] = y;
  1655. result.m[6] = 0;
  1656. result.m[7] = 0;
  1657. result.m[8] = 0;
  1658. result.m[9] = 0;
  1659. result.m[10] = z;
  1660. result.m[11] = 0;
  1661. result.m[12] = 0;
  1662. result.m[13] = 0;
  1663. result.m[14] = 0;
  1664. result.m[15] = 1.0;
  1665. };
  1666. Matrix.Translation = function (x, y, z) {
  1667. var result = Matrix.Identity();
  1668. Matrix.TranslationToRef(x, y, z, result);
  1669. return result;
  1670. };
  1671. Matrix.TranslationToRef = function (x, y, z, result) {
  1672. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  1673. };
  1674. Matrix.LookAtLH = function (eye, target, up) {
  1675. var result = Matrix.Zero();
  1676. Matrix.LookAtLHToRef(eye, target, up, result);
  1677. return result;
  1678. };
  1679. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1680. // Z axis
  1681. target.subtractToRef(eye, this._zAxis);
  1682. this._zAxis.normalize();
  1683. // X axis
  1684. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1685. this._xAxis.normalize();
  1686. // Y axis
  1687. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1688. this._yAxis.normalize();
  1689. // Eye angles
  1690. var ex = -Vector3.Dot(this._xAxis, eye);
  1691. var ey = -Vector3.Dot(this._yAxis, eye);
  1692. var ez = -Vector3.Dot(this._zAxis, eye);
  1693. 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);
  1694. };
  1695. Matrix.OrthoLH = function (width, height, znear, zfar) {
  1696. var hw = 2.0 / width;
  1697. var hh = 2.0 / height;
  1698. var id = 1.0 / (zfar - znear);
  1699. var nid = znear / (znear - zfar);
  1700. return Matrix.FromValues(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1);
  1701. };
  1702. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1703. var matrix = Matrix.Zero();
  1704. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1705. return matrix;
  1706. };
  1707. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1708. result.m[0] = 2.0 / (right - left);
  1709. result.m[1] = result.m[2] = result.m[3] = 0;
  1710. result.m[5] = 2.0 / (top - bottom);
  1711. result.m[4] = result.m[6] = result.m[7] = 0;
  1712. result.m[10] = -1.0 / (znear - zfar);
  1713. result.m[8] = result.m[9] = result.m[11] = 0;
  1714. result.m[12] = (left + right) / (left - right);
  1715. result.m[13] = (top + bottom) / (bottom - top);
  1716. result.m[14] = znear / (znear - zfar);
  1717. result.m[15] = 1.0;
  1718. };
  1719. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1720. var matrix = Matrix.Zero();
  1721. matrix.m[0] = (2.0 * znear) / width;
  1722. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1723. matrix.m[5] = (2.0 * znear) / height;
  1724. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1725. matrix.m[10] = -zfar / (znear - zfar);
  1726. matrix.m[8] = matrix.m[9] = 0.0;
  1727. matrix.m[11] = 1.0;
  1728. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1729. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1730. return matrix;
  1731. };
  1732. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1733. var matrix = Matrix.Zero();
  1734. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1735. return matrix;
  1736. };
  1737. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result, fovMode) {
  1738. if (fovMode === void 0) { fovMode = BABYLON.Camera.FOVMODE_VERTICAL_FIXED; }
  1739. var tan = 1.0 / (Math.tan(fov * 0.5));
  1740. var v_fixed = (fovMode === BABYLON.Camera.FOVMODE_VERTICAL_FIXED);
  1741. if (v_fixed) {
  1742. result.m[0] = tan / aspect;
  1743. }
  1744. else {
  1745. result.m[0] = tan;
  1746. }
  1747. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1748. if (v_fixed) {
  1749. result.m[5] = tan;
  1750. }
  1751. else {
  1752. result.m[5] = tan * aspect;
  1753. }
  1754. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1755. result.m[8] = result.m[9] = 0.0;
  1756. result.m[10] = -zfar / (znear - zfar);
  1757. result.m[11] = 1.0;
  1758. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1759. result.m[14] = (znear * zfar) / (znear - zfar);
  1760. };
  1761. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  1762. var cw = viewport.width;
  1763. var ch = viewport.height;
  1764. var cx = viewport.x;
  1765. var cy = viewport.y;
  1766. 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);
  1767. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1768. };
  1769. Matrix.Transpose = function (matrix) {
  1770. var result = new Matrix();
  1771. result.m[0] = matrix.m[0];
  1772. result.m[1] = matrix.m[4];
  1773. result.m[2] = matrix.m[8];
  1774. result.m[3] = matrix.m[12];
  1775. result.m[4] = matrix.m[1];
  1776. result.m[5] = matrix.m[5];
  1777. result.m[6] = matrix.m[9];
  1778. result.m[7] = matrix.m[13];
  1779. result.m[8] = matrix.m[2];
  1780. result.m[9] = matrix.m[6];
  1781. result.m[10] = matrix.m[10];
  1782. result.m[11] = matrix.m[14];
  1783. result.m[12] = matrix.m[3];
  1784. result.m[13] = matrix.m[7];
  1785. result.m[14] = matrix.m[11];
  1786. result.m[15] = matrix.m[15];
  1787. return result;
  1788. };
  1789. Matrix.Reflection = function (plane) {
  1790. var matrix = new Matrix();
  1791. Matrix.ReflectionToRef(plane, matrix);
  1792. return matrix;
  1793. };
  1794. Matrix.ReflectionToRef = function (plane, result) {
  1795. plane.normalize();
  1796. var x = plane.normal.x;
  1797. var y = plane.normal.y;
  1798. var z = plane.normal.z;
  1799. var temp = -2 * x;
  1800. var temp2 = -2 * y;
  1801. var temp3 = -2 * z;
  1802. result.m[0] = (temp * x) + 1;
  1803. result.m[1] = temp2 * x;
  1804. result.m[2] = temp3 * x;
  1805. result.m[3] = 0.0;
  1806. result.m[4] = temp * y;
  1807. result.m[5] = (temp2 * y) + 1;
  1808. result.m[6] = temp3 * y;
  1809. result.m[7] = 0.0;
  1810. result.m[8] = temp * z;
  1811. result.m[9] = temp2 * z;
  1812. result.m[10] = (temp3 * z) + 1;
  1813. result.m[11] = 0.0;
  1814. result.m[12] = temp * plane.d;
  1815. result.m[13] = temp2 * plane.d;
  1816. result.m[14] = temp3 * plane.d;
  1817. result.m[15] = 1.0;
  1818. };
  1819. Matrix._tempQuaternion = new Quaternion();
  1820. Matrix._xAxis = Vector3.Zero();
  1821. Matrix._yAxis = Vector3.Zero();
  1822. Matrix._zAxis = Vector3.Zero();
  1823. return Matrix;
  1824. })();
  1825. BABYLON.Matrix = Matrix;
  1826. var Plane = (function () {
  1827. function Plane(a, b, c, d) {
  1828. this.normal = new Vector3(a, b, c);
  1829. this.d = d;
  1830. }
  1831. Plane.prototype.asArray = function () {
  1832. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  1833. };
  1834. // Methods
  1835. Plane.prototype.clone = function () {
  1836. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  1837. };
  1838. Plane.prototype.normalize = function () {
  1839. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1840. var magnitude = 0;
  1841. if (norm !== 0) {
  1842. magnitude = 1.0 / norm;
  1843. }
  1844. this.normal.x *= magnitude;
  1845. this.normal.y *= magnitude;
  1846. this.normal.z *= magnitude;
  1847. this.d *= magnitude;
  1848. return this;
  1849. };
  1850. Plane.prototype.transform = function (transformation) {
  1851. var transposedMatrix = Matrix.Transpose(transformation);
  1852. var x = this.normal.x;
  1853. var y = this.normal.y;
  1854. var z = this.normal.z;
  1855. var d = this.d;
  1856. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1857. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1858. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1859. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1860. return new Plane(normalX, normalY, normalZ, finalD);
  1861. };
  1862. Plane.prototype.dotCoordinate = function (point) {
  1863. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1864. };
  1865. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  1866. var x1 = point2.x - point1.x;
  1867. var y1 = point2.y - point1.y;
  1868. var z1 = point2.z - point1.z;
  1869. var x2 = point3.x - point1.x;
  1870. var y2 = point3.y - point1.y;
  1871. var z2 = point3.z - point1.z;
  1872. var yz = (y1 * z2) - (z1 * y2);
  1873. var xz = (z1 * x2) - (x1 * z2);
  1874. var xy = (x1 * y2) - (y1 * x2);
  1875. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1876. var invPyth;
  1877. if (pyth !== 0) {
  1878. invPyth = 1.0 / pyth;
  1879. }
  1880. else {
  1881. invPyth = 0;
  1882. }
  1883. this.normal.x = yz * invPyth;
  1884. this.normal.y = xz * invPyth;
  1885. this.normal.z = xy * invPyth;
  1886. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1887. return this;
  1888. };
  1889. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  1890. var dot = Vector3.Dot(this.normal, direction);
  1891. return (dot <= epsilon);
  1892. };
  1893. Plane.prototype.signedDistanceTo = function (point) {
  1894. return Vector3.Dot(point, this.normal) + this.d;
  1895. };
  1896. // Statics
  1897. Plane.FromArray = function (array) {
  1898. return new Plane(array[0], array[1], array[2], array[3]);
  1899. };
  1900. Plane.FromPoints = function (point1, point2, point3) {
  1901. var result = new Plane(0, 0, 0, 0);
  1902. result.copyFromPoints(point1, point2, point3);
  1903. return result;
  1904. };
  1905. Plane.FromPositionAndNormal = function (origin, normal) {
  1906. var result = new Plane(0, 0, 0, 0);
  1907. normal.normalize();
  1908. result.normal = normal;
  1909. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1910. return result;
  1911. };
  1912. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  1913. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1914. return Vector3.Dot(point, normal) + d;
  1915. };
  1916. return Plane;
  1917. })();
  1918. BABYLON.Plane = Plane;
  1919. var Viewport = (function () {
  1920. function Viewport(x, y, width, height) {
  1921. this.x = x;
  1922. this.y = y;
  1923. this.width = width;
  1924. this.height = height;
  1925. }
  1926. Viewport.prototype.toGlobal = function (engine) {
  1927. var width = engine.getRenderWidth();
  1928. var height = engine.getRenderHeight();
  1929. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1930. };
  1931. return Viewport;
  1932. })();
  1933. BABYLON.Viewport = Viewport;
  1934. var Frustum = (function () {
  1935. function Frustum() {
  1936. }
  1937. Frustum.GetPlanes = function (transform) {
  1938. var frustumPlanes = [];
  1939. for (var index = 0; index < 6; index++) {
  1940. frustumPlanes.push(new Plane(0, 0, 0, 0));
  1941. }
  1942. Frustum.GetPlanesToRef(transform, frustumPlanes);
  1943. return frustumPlanes;
  1944. };
  1945. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  1946. // Near
  1947. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1948. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1949. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1950. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1951. frustumPlanes[0].normalize();
  1952. // Far
  1953. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1954. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1955. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1956. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1957. frustumPlanes[1].normalize();
  1958. // Left
  1959. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1960. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1961. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1962. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1963. frustumPlanes[2].normalize();
  1964. // Right
  1965. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1966. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1967. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1968. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1969. frustumPlanes[3].normalize();
  1970. // Top
  1971. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1972. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1973. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1974. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1975. frustumPlanes[4].normalize();
  1976. // Bottom
  1977. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1978. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1979. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1980. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1981. frustumPlanes[5].normalize();
  1982. };
  1983. return Frustum;
  1984. })();
  1985. BABYLON.Frustum = Frustum;
  1986. var Ray = (function () {
  1987. function Ray(origin, direction, length) {
  1988. if (length === void 0) { length = Number.MAX_VALUE; }
  1989. this.origin = origin;
  1990. this.direction = direction;
  1991. this.length = length;
  1992. }
  1993. // Methods
  1994. Ray.prototype.intersectsBoxMinMax = function (minimum, maximum) {
  1995. var d = 0.0;
  1996. var maxValue = Number.MAX_VALUE;
  1997. if (Math.abs(this.direction.x) < 0.0000001) {
  1998. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  1999. return false;
  2000. }
  2001. }
  2002. else {
  2003. var inv = 1.0 / this.direction.x;
  2004. var min = (minimum.x - this.origin.x) * inv;
  2005. var max = (maximum.x - this.origin.x) * inv;
  2006. if (max === -Infinity) {
  2007. max = Infinity;
  2008. }
  2009. if (min > max) {
  2010. var temp = min;
  2011. min = max;
  2012. max = temp;
  2013. }
  2014. d = Math.max(min, d);
  2015. maxValue = Math.min(max, maxValue);
  2016. if (d > maxValue) {
  2017. return false;
  2018. }
  2019. }
  2020. if (Math.abs(this.direction.y) < 0.0000001) {
  2021. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  2022. return false;
  2023. }
  2024. }
  2025. else {
  2026. inv = 1.0 / this.direction.y;
  2027. min = (minimum.y - this.origin.y) * inv;
  2028. max = (maximum.y - this.origin.y) * inv;
  2029. if (max === -Infinity) {
  2030. max = Infinity;
  2031. }
  2032. if (min > max) {
  2033. temp = min;
  2034. min = max;
  2035. max = temp;
  2036. }
  2037. d = Math.max(min, d);
  2038. maxValue = Math.min(max, maxValue);
  2039. if (d > maxValue) {
  2040. return false;
  2041. }
  2042. }
  2043. if (Math.abs(this.direction.z) < 0.0000001) {
  2044. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  2045. return false;
  2046. }
  2047. }
  2048. else {
  2049. inv = 1.0 / this.direction.z;
  2050. min = (minimum.z - this.origin.z) * inv;
  2051. max = (maximum.z - this.origin.z) * inv;
  2052. if (max === -Infinity) {
  2053. max = Infinity;
  2054. }
  2055. if (min > max) {
  2056. temp = min;
  2057. min = max;
  2058. max = temp;
  2059. }
  2060. d = Math.max(min, d);
  2061. maxValue = Math.min(max, maxValue);
  2062. if (d > maxValue) {
  2063. return false;
  2064. }
  2065. }
  2066. return true;
  2067. };
  2068. Ray.prototype.intersectsBox = function (box) {
  2069. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  2070. };
  2071. Ray.prototype.intersectsSphere = function (sphere) {
  2072. var x = sphere.center.x - this.origin.x;
  2073. var y = sphere.center.y - this.origin.y;
  2074. var z = sphere.center.z - this.origin.z;
  2075. var pyth = (x * x) + (y * y) + (z * z);
  2076. var rr = sphere.radius * sphere.radius;
  2077. if (pyth <= rr) {
  2078. return true;
  2079. }
  2080. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  2081. if (dot < 0.0) {
  2082. return false;
  2083. }
  2084. var temp = pyth - (dot * dot);
  2085. return temp <= rr;
  2086. };
  2087. Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  2088. if (!this._edge1) {
  2089. this._edge1 = Vector3.Zero();
  2090. this._edge2 = Vector3.Zero();
  2091. this._pvec = Vector3.Zero();
  2092. this._tvec = Vector3.Zero();
  2093. this._qvec = Vector3.Zero();
  2094. }
  2095. vertex1.subtractToRef(vertex0, this._edge1);
  2096. vertex2.subtractToRef(vertex0, this._edge2);
  2097. Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  2098. var det = Vector3.Dot(this._edge1, this._pvec);
  2099. if (det === 0) {
  2100. return null;
  2101. }
  2102. var invdet = 1 / det;
  2103. this.origin.subtractToRef(vertex0, this._tvec);
  2104. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  2105. if (bu < 0 || bu > 1.0) {
  2106. return null;
  2107. }
  2108. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  2109. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  2110. if (bv < 0 || bu + bv > 1.0) {
  2111. return null;
  2112. }
  2113. //check if the distance is longer than the predefined length.
  2114. var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
  2115. if (distance > this.length) {
  2116. return null;
  2117. }
  2118. return new BABYLON.IntersectionInfo(bu, bv, distance);
  2119. };
  2120. // Statics
  2121. Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  2122. var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  2123. var end = Vector3.Unproject(new Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  2124. var direction = end.subtract(start);
  2125. direction.normalize();
  2126. return new Ray(start, direction);
  2127. };
  2128. /**
  2129. * Function will create a new transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be
  2130. * transformed to the given world matrix.
  2131. * @param origin The origin point
  2132. * @param end The end point
  2133. * @param world a matrix to transform the ray to. Default is the identity matrix.
  2134. */
  2135. Ray.CreateNewFromTo = function (origin, end, world) {
  2136. if (world === void 0) { world = Matrix.Identity(); }
  2137. var direction = end.subtract(origin);
  2138. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  2139. direction.normalize();
  2140. return Ray.Transform(new Ray(origin, direction, length), world);
  2141. };
  2142. Ray.Transform = function (ray, matrix) {
  2143. var newOrigin = Vector3.TransformCoordinates(ray.origin, matrix);
  2144. var newDirection = Vector3.TransformNormal(ray.direction, matrix);
  2145. return new Ray(newOrigin, newDirection, ray.length);
  2146. };
  2147. return Ray;
  2148. })();
  2149. BABYLON.Ray = Ray;
  2150. (function (Space) {
  2151. Space[Space["LOCAL"] = 0] = "LOCAL";
  2152. Space[Space["WORLD"] = 1] = "WORLD";
  2153. })(BABYLON.Space || (BABYLON.Space = {}));
  2154. var Space = BABYLON.Space;
  2155. var Axis = (function () {
  2156. function Axis() {
  2157. }
  2158. Axis.X = new Vector3(1, 0, 0);
  2159. Axis.Y = new Vector3(0, 1, 0);
  2160. Axis.Z = new Vector3(0, 0, 1);
  2161. return Axis;
  2162. })();
  2163. BABYLON.Axis = Axis;
  2164. ;
  2165. var BezierCurve = (function () {
  2166. function BezierCurve() {
  2167. }
  2168. BezierCurve.interpolate = function (t, x1, y1, x2, y2) {
  2169. // Extract X (which is equal to time here)
  2170. var f0 = 1 - 3 * x2 + 3 * x1;
  2171. var f1 = 3 * x2 - 6 * x1;
  2172. var f2 = 3 * x1;
  2173. var refinedT = t;
  2174. for (var i = 0; i < 5; i++) {
  2175. var refinedT2 = refinedT * refinedT;
  2176. var refinedT3 = refinedT2 * refinedT;
  2177. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2178. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2179. refinedT -= (x - t) * slope;
  2180. refinedT = Math.min(1, Math.max(0, refinedT));
  2181. }
  2182. // Resolve cubic bezier for the given x
  2183. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 + 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 + Math.pow(refinedT, 3);
  2184. };
  2185. return BezierCurve;
  2186. })();
  2187. BABYLON.BezierCurve = BezierCurve;
  2188. (function (Orientation) {
  2189. Orientation[Orientation["CW"] = 0] = "CW";
  2190. Orientation[Orientation["CCW"] = 1] = "CCW";
  2191. })(BABYLON.Orientation || (BABYLON.Orientation = {}));
  2192. var Orientation = BABYLON.Orientation;
  2193. var Angle = (function () {
  2194. function Angle(radians) {
  2195. var _this = this;
  2196. this.degrees = function () { return _this._radians * 180 / Math.PI; };
  2197. this.radians = function () { return _this._radians; };
  2198. this._radians = radians;
  2199. if (this._radians < 0)
  2200. this._radians += (2 * Math.PI);
  2201. }
  2202. Angle.BetweenTwoPoints = function (a, b) {
  2203. var delta = b.subtract(a);
  2204. var theta = Math.atan2(delta.y, delta.x);
  2205. return new Angle(theta);
  2206. };
  2207. Angle.FromRadians = function (radians) {
  2208. return new Angle(radians);
  2209. };
  2210. Angle.FromDegrees = function (degrees) {
  2211. return new Angle(degrees * Math.PI / 180);
  2212. };
  2213. return Angle;
  2214. })();
  2215. BABYLON.Angle = Angle;
  2216. var Arc2 = (function () {
  2217. function Arc2(startPoint, midPoint, endPoint) {
  2218. this.startPoint = startPoint;
  2219. this.midPoint = midPoint;
  2220. this.endPoint = endPoint;
  2221. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2222. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2223. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2224. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2225. this.centerPoint = new Vector2((startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det, ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det);
  2226. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2227. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2228. var a1 = this.startAngle.degrees();
  2229. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2230. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2231. // angles correction
  2232. if (a2 - a1 > +180.0)
  2233. a2 -= 360.0;
  2234. if (a2 - a1 < -180.0)
  2235. a2 += 360.0;
  2236. if (a3 - a2 > +180.0)
  2237. a3 -= 360.0;
  2238. if (a3 - a2 < -180.0)
  2239. a3 += 360.0;
  2240. this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
  2241. this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
  2242. }
  2243. return Arc2;
  2244. })();
  2245. BABYLON.Arc2 = Arc2;
  2246. var PathCursor = (function () {
  2247. function PathCursor(path) {
  2248. this.path = path;
  2249. this._onchange = new Array();
  2250. this.value = 0;
  2251. this.animations = new Array();
  2252. }
  2253. PathCursor.prototype.getPoint = function () {
  2254. var point = this.path.getPointAtLengthPosition(this.value);
  2255. return new Vector3(point.x, 0, point.y);
  2256. };
  2257. PathCursor.prototype.moveAhead = function (step) {
  2258. if (step === void 0) { step = 0.002; }
  2259. this.move(step);
  2260. return this;
  2261. };
  2262. PathCursor.prototype.moveBack = function (step) {
  2263. if (step === void 0) { step = 0.002; }
  2264. this.move(-step);
  2265. return this;
  2266. };
  2267. PathCursor.prototype.move = function (step) {
  2268. if (Math.abs(step) > 1) {
  2269. throw "step size should be less than 1.";
  2270. }
  2271. this.value += step;
  2272. this.ensureLimits();
  2273. this.raiseOnChange();
  2274. return this;
  2275. };
  2276. PathCursor.prototype.ensureLimits = function () {
  2277. while (this.value > 1) {
  2278. this.value -= 1;
  2279. }
  2280. while (this.value < 0) {
  2281. this.value += 1;
  2282. }
  2283. return this;
  2284. };
  2285. // used by animation engine
  2286. PathCursor.prototype.markAsDirty = function (propertyName) {
  2287. this.ensureLimits();
  2288. this.raiseOnChange();
  2289. return this;
  2290. };
  2291. PathCursor.prototype.raiseOnChange = function () {
  2292. var _this = this;
  2293. this._onchange.forEach(function (f) { return f(_this); });
  2294. return this;
  2295. };
  2296. PathCursor.prototype.onchange = function (f) {
  2297. this._onchange.push(f);
  2298. return this;
  2299. };
  2300. return PathCursor;
  2301. })();
  2302. BABYLON.PathCursor = PathCursor;
  2303. var Path2 = (function () {
  2304. function Path2(x, y) {
  2305. this._points = [];
  2306. this._length = 0;
  2307. this.closed = false;
  2308. this._points.push(new Vector2(x, y));
  2309. }
  2310. Path2.prototype.addLineTo = function (x, y) {
  2311. if (closed) {
  2312. BABYLON.Tools.Error("cannot add lines to closed paths");
  2313. return this;
  2314. }
  2315. var newPoint = new Vector2(x, y);
  2316. var previousPoint = this._points[this._points.length - 1];
  2317. this._points.push(newPoint);
  2318. this._length += newPoint.subtract(previousPoint).length();
  2319. return this;
  2320. };
  2321. Path2.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
  2322. if (numberOfSegments === void 0) { numberOfSegments = 36; }
  2323. if (closed) {
  2324. BABYLON.Tools.Error("cannot add arcs to closed paths");
  2325. return this;
  2326. }
  2327. var startPoint = this._points[this._points.length - 1];
  2328. var midPoint = new Vector2(midX, midY);
  2329. var endPoint = new Vector2(endX, endY);
  2330. var arc = new Arc2(startPoint, midPoint, endPoint);
  2331. var increment = arc.angle.radians() / numberOfSegments;
  2332. if (arc.orientation === 0 /* CW */)
  2333. increment *= -1;
  2334. var currentAngle = arc.startAngle.radians() + increment;
  2335. for (var i = 0; i < numberOfSegments; i++) {
  2336. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2337. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2338. this.addLineTo(x, y);
  2339. currentAngle += increment;
  2340. }
  2341. return this;
  2342. };
  2343. Path2.prototype.close = function () {
  2344. this.closed = true;
  2345. return this;
  2346. };
  2347. Path2.prototype.length = function () {
  2348. var result = this._length;
  2349. if (!this.closed) {
  2350. var lastPoint = this._points[this._points.length - 1];
  2351. var firstPoint = this._points[0];
  2352. result += (firstPoint.subtract(lastPoint).length());
  2353. }
  2354. return result;
  2355. };
  2356. Path2.prototype.getPoints = function () {
  2357. return this._points;
  2358. };
  2359. Path2.prototype.getPointAtLengthPosition = function (normalizedLengthPosition) {
  2360. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2361. BABYLON.Tools.Error("normalized length position should be between 0 and 1.");
  2362. return Vector2.Zero();
  2363. }
  2364. var lengthPosition = normalizedLengthPosition * this.length();
  2365. var previousOffset = 0;
  2366. for (var i = 0; i < this._points.length; i++) {
  2367. var j = (i + 1) % this._points.length;
  2368. var a = this._points[i];
  2369. var b = this._points[j];
  2370. var bToA = b.subtract(a);
  2371. var nextOffset = (bToA.length() + previousOffset);
  2372. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2373. var dir = bToA.normalize();
  2374. var localOffset = lengthPosition - previousOffset;
  2375. return new Vector2(a.x + (dir.x * localOffset), a.y + (dir.y * localOffset));
  2376. }
  2377. previousOffset = nextOffset;
  2378. }
  2379. BABYLON.Tools.Error("internal error");
  2380. return Vector2.Zero();
  2381. };
  2382. Path2.StartingAt = function (x, y) {
  2383. return new Path2(x, y);
  2384. };
  2385. return Path2;
  2386. })();
  2387. BABYLON.Path2 = Path2;
  2388. })(BABYLON || (BABYLON = {}));
  2389. //# sourceMappingURL=babylon.math.js.map