babylon.math.js 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010
  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. Vector2.prototype.equalsWithEpsilon = function (otherVector) {
  306. return otherVector && BABYLON.Tools.WithinEpsilon(this.x, otherVector.x) && BABYLON.Tools.WithinEpsilon(this.y, otherVector.y);
  307. };
  308. // Properties
  309. Vector2.prototype.length = function () {
  310. return Math.sqrt(this.x * this.x + this.y * this.y);
  311. };
  312. Vector2.prototype.lengthSquared = function () {
  313. return (this.x * this.x + this.y * this.y);
  314. };
  315. // Methods
  316. Vector2.prototype.normalize = function () {
  317. var len = this.length();
  318. if (len === 0)
  319. return this;
  320. var num = 1.0 / len;
  321. this.x *= num;
  322. this.y *= num;
  323. return this;
  324. };
  325. Vector2.prototype.clone = function () {
  326. return new Vector2(this.x, this.y);
  327. };
  328. // Statics
  329. Vector2.Zero = function () {
  330. return new Vector2(0, 0);
  331. };
  332. Vector2.FromArray = function (array, offset) {
  333. if (offset === void 0) { offset = 0; }
  334. return new Vector2(array[offset], array[offset + 1]);
  335. };
  336. Vector2.FromArrayToRef = function (array, offset, result) {
  337. result.x = array[offset];
  338. result.y = array[offset + 1];
  339. };
  340. Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  341. var squared = amount * amount;
  342. var cubed = amount * squared;
  343. 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));
  344. 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));
  345. return new Vector2(x, y);
  346. };
  347. Vector2.Clamp = function (value, min, max) {
  348. var x = value.x;
  349. x = (x > max.x) ? max.x : x;
  350. x = (x < min.x) ? min.x : x;
  351. var y = value.y;
  352. y = (y > max.y) ? max.y : y;
  353. y = (y < min.y) ? min.y : y;
  354. return new Vector2(x, y);
  355. };
  356. Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  357. var squared = amount * amount;
  358. var cubed = amount * squared;
  359. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  360. var part2 = (-2.0 * cubed) + (3.0 * squared);
  361. var part3 = (cubed - (2.0 * squared)) + amount;
  362. var part4 = cubed - squared;
  363. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  364. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  365. return new Vector2(x, y);
  366. };
  367. Vector2.Lerp = function (start, end, amount) {
  368. var x = start.x + ((end.x - start.x) * amount);
  369. var y = start.y + ((end.y - start.y) * amount);
  370. return new Vector2(x, y);
  371. };
  372. Vector2.Dot = function (left, right) {
  373. return left.x * right.x + left.y * right.y;
  374. };
  375. Vector2.Normalize = function (vector) {
  376. var newVector = vector.clone();
  377. newVector.normalize();
  378. return newVector;
  379. };
  380. Vector2.Minimize = function (left, right) {
  381. var x = (left.x < right.x) ? left.x : right.x;
  382. var y = (left.y < right.y) ? left.y : right.y;
  383. return new Vector2(x, y);
  384. };
  385. Vector2.Maximize = function (left, right) {
  386. var x = (left.x > right.x) ? left.x : right.x;
  387. var y = (left.y > right.y) ? left.y : right.y;
  388. return new Vector2(x, y);
  389. };
  390. Vector2.Transform = function (vector, transformation) {
  391. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  392. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  393. return new Vector2(x, y);
  394. };
  395. Vector2.Distance = function (value1, value2) {
  396. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  397. };
  398. Vector2.DistanceSquared = function (value1, value2) {
  399. var x = value1.x - value2.x;
  400. var y = value1.y - value2.y;
  401. return (x * x) + (y * y);
  402. };
  403. return Vector2;
  404. })();
  405. BABYLON.Vector2 = Vector2;
  406. var Vector3 = (function () {
  407. function Vector3(x, y, z) {
  408. this.x = x;
  409. this.y = y;
  410. this.z = z;
  411. }
  412. Vector3.prototype.toString = function () {
  413. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  414. };
  415. // Operators
  416. Vector3.prototype.asArray = function () {
  417. var result = [];
  418. this.toArray(result, 0);
  419. return result;
  420. };
  421. Vector3.prototype.toArray = function (array, index) {
  422. if (index === void 0) { index = 0; }
  423. array[index] = this.x;
  424. array[index + 1] = this.y;
  425. array[index + 2] = this.z;
  426. return this;
  427. };
  428. Vector3.prototype.toQuaternion = function () {
  429. var result = new Quaternion(0, 0, 0, 1);
  430. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  431. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  432. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  433. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  434. var cosy = Math.cos(this.y * 0.5);
  435. var siny = Math.sin(this.y * 0.5);
  436. result.x = coszMinusx * siny;
  437. result.y = -sinzMinusx * siny;
  438. result.z = sinxPlusz * cosy;
  439. result.w = cosxPlusz * cosy;
  440. return result;
  441. };
  442. Vector3.prototype.addInPlace = function (otherVector) {
  443. this.x += otherVector.x;
  444. this.y += otherVector.y;
  445. this.z += otherVector.z;
  446. return this;
  447. };
  448. Vector3.prototype.add = function (otherVector) {
  449. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  450. };
  451. Vector3.prototype.addToRef = function (otherVector, result) {
  452. result.x = this.x + otherVector.x;
  453. result.y = this.y + otherVector.y;
  454. result.z = this.z + otherVector.z;
  455. return this;
  456. };
  457. Vector3.prototype.subtractInPlace = function (otherVector) {
  458. this.x -= otherVector.x;
  459. this.y -= otherVector.y;
  460. this.z -= otherVector.z;
  461. return this;
  462. };
  463. Vector3.prototype.subtract = function (otherVector) {
  464. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  465. };
  466. Vector3.prototype.subtractToRef = function (otherVector, result) {
  467. result.x = this.x - otherVector.x;
  468. result.y = this.y - otherVector.y;
  469. result.z = this.z - otherVector.z;
  470. return this;
  471. };
  472. Vector3.prototype.subtractFromFloats = function (x, y, z) {
  473. return new Vector3(this.x - x, this.y - y, this.z - z);
  474. };
  475. Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  476. result.x = this.x - x;
  477. result.y = this.y - y;
  478. result.z = this.z - z;
  479. return this;
  480. };
  481. Vector3.prototype.negate = function () {
  482. return new Vector3(-this.x, -this.y, -this.z);
  483. };
  484. Vector3.prototype.scaleInPlace = function (scale) {
  485. this.x *= scale;
  486. this.y *= scale;
  487. this.z *= scale;
  488. return this;
  489. };
  490. Vector3.prototype.scale = function (scale) {
  491. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  492. };
  493. Vector3.prototype.scaleToRef = function (scale, result) {
  494. result.x = this.x * scale;
  495. result.y = this.y * scale;
  496. result.z = this.z * scale;
  497. };
  498. Vector3.prototype.equals = function (otherVector) {
  499. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  500. };
  501. Vector3.prototype.equalsWithEpsilon = function (otherVector) {
  502. return otherVector && BABYLON.Tools.WithinEpsilon(this.x, otherVector.x) && BABYLON.Tools.WithinEpsilon(this.y, otherVector.y) && BABYLON.Tools.WithinEpsilon(this.z, otherVector.z);
  503. };
  504. Vector3.prototype.equalsToFloats = function (x, y, z) {
  505. return this.x === x && this.y === y && this.z === z;
  506. };
  507. Vector3.prototype.multiplyInPlace = function (otherVector) {
  508. this.x *= otherVector.x;
  509. this.y *= otherVector.y;
  510. this.z *= otherVector.z;
  511. return this;
  512. };
  513. Vector3.prototype.multiply = function (otherVector) {
  514. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  515. };
  516. Vector3.prototype.multiplyToRef = function (otherVector, result) {
  517. result.x = this.x * otherVector.x;
  518. result.y = this.y * otherVector.y;
  519. result.z = this.z * otherVector.z;
  520. return this;
  521. };
  522. Vector3.prototype.multiplyByFloats = function (x, y, z) {
  523. return new Vector3(this.x * x, this.y * y, this.z * z);
  524. };
  525. Vector3.prototype.divide = function (otherVector) {
  526. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  527. };
  528. Vector3.prototype.divideToRef = function (otherVector, result) {
  529. result.x = this.x / otherVector.x;
  530. result.y = this.y / otherVector.y;
  531. result.z = this.z / otherVector.z;
  532. return this;
  533. };
  534. Vector3.prototype.MinimizeInPlace = function (other) {
  535. if (other.x < this.x)
  536. this.x = other.x;
  537. if (other.y < this.y)
  538. this.y = other.y;
  539. if (other.z < this.z)
  540. this.z = other.z;
  541. return this;
  542. };
  543. Vector3.prototype.MaximizeInPlace = function (other) {
  544. if (other.x > this.x)
  545. this.x = other.x;
  546. if (other.y > this.y)
  547. this.y = other.y;
  548. if (other.z > this.z)
  549. this.z = other.z;
  550. return this;
  551. };
  552. // Properties
  553. Vector3.prototype.length = function () {
  554. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  555. };
  556. Vector3.prototype.lengthSquared = function () {
  557. return (this.x * this.x + this.y * this.y + this.z * this.z);
  558. };
  559. // Methods
  560. Vector3.prototype.normalize = function () {
  561. var len = this.length();
  562. if (len === 0)
  563. return this;
  564. var num = 1.0 / len;
  565. this.x *= num;
  566. this.y *= num;
  567. this.z *= num;
  568. return this;
  569. };
  570. Vector3.prototype.clone = function () {
  571. return new Vector3(this.x, this.y, this.z);
  572. };
  573. Vector3.prototype.copyFrom = function (source) {
  574. this.x = source.x;
  575. this.y = source.y;
  576. this.z = source.z;
  577. return this;
  578. };
  579. Vector3.prototype.copyFromFloats = function (x, y, z) {
  580. this.x = x;
  581. this.y = y;
  582. this.z = z;
  583. return this;
  584. };
  585. // Statics
  586. Vector3.GetClipFactor = function (vector0, vector1, axis, size) {
  587. var d0 = Vector3.Dot(vector0, axis) - size;
  588. var d1 = Vector3.Dot(vector1, axis) - size;
  589. var s = d0 / (d0 - d1);
  590. return s;
  591. };
  592. Vector3.FromArray = function (array, offset) {
  593. if (!offset) {
  594. offset = 0;
  595. }
  596. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  597. };
  598. Vector3.FromArrayToRef = function (array, offset, result) {
  599. result.x = array[offset];
  600. result.y = array[offset + 1];
  601. result.z = array[offset + 2];
  602. };
  603. Vector3.FromFloatArrayToRef = function (array, offset, result) {
  604. result.x = array[offset];
  605. result.y = array[offset + 1];
  606. result.z = array[offset + 2];
  607. };
  608. Vector3.FromFloatsToRef = function (x, y, z, result) {
  609. result.x = x;
  610. result.y = y;
  611. result.z = z;
  612. };
  613. Vector3.Zero = function () {
  614. return new Vector3(0, 0, 0);
  615. };
  616. Vector3.Up = function () {
  617. return new Vector3(0, 1.0, 0);
  618. };
  619. Vector3.TransformCoordinates = function (vector, transformation) {
  620. var result = Vector3.Zero();
  621. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  622. return result;
  623. };
  624. Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  625. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  626. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  627. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  628. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  629. result.x = x / w;
  630. result.y = y / w;
  631. result.z = z / w;
  632. };
  633. Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  634. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  635. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  636. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  637. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  638. result.x = rx / rw;
  639. result.y = ry / rw;
  640. result.z = rz / rw;
  641. };
  642. Vector3.TransformCoordinatesToRefSIMD = function (vector, transformation, result) {
  643. var v = SIMD.float32x4.loadXYZ(vector._data, 0);
  644. var m0 = SIMD.float32x4.load(transformation.m, 0);
  645. var m1 = SIMD.float32x4.load(transformation.m, 4);
  646. var m2 = SIMD.float32x4.load(transformation.m, 8);
  647. var m3 = SIMD.float32x4.load(transformation.m, 12);
  648. var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 0, 0, 0, 0), m0), SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 1, 1, 1, 1), m1)), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 2, 2, 2, 2), m2), m3));
  649. r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
  650. SIMD.float32x4.storeXYZ(result._data, 0, r);
  651. };
  652. Vector3.TransformCoordinatesFromFloatsToRefSIMD = function (x, y, z, transformation, result) {
  653. var v0 = SIMD.float32x4.splat(x);
  654. var v1 = SIMD.float32x4.splat(y);
  655. var v2 = SIMD.float32x4.splat(z);
  656. var m0 = SIMD.float32x4.load(transformation.m, 0);
  657. var m1 = SIMD.float32x4.load(transformation.m, 4);
  658. var m2 = SIMD.float32x4.load(transformation.m, 8);
  659. var m3 = SIMD.float32x4.load(transformation.m, 12);
  660. var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(v0, m0), SIMD.float32x4.mul(v1, m1)), SIMD.float32x4.add(SIMD.float32x4.mul(v2, m2), m3));
  661. r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
  662. SIMD.float32x4.storeXYZ(result._data, 0, r);
  663. };
  664. Vector3.TransformNormal = function (vector, transformation) {
  665. var result = Vector3.Zero();
  666. Vector3.TransformNormalToRef(vector, transformation, result);
  667. return result;
  668. };
  669. Vector3.TransformNormalToRef = function (vector, transformation, result) {
  670. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  671. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  672. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  673. };
  674. Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  675. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  676. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  677. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  678. };
  679. Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  680. var squared = amount * amount;
  681. var cubed = amount * squared;
  682. 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));
  683. 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));
  684. 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));
  685. return new Vector3(x, y, z);
  686. };
  687. Vector3.Clamp = function (value, min, max) {
  688. var x = value.x;
  689. x = (x > max.x) ? max.x : x;
  690. x = (x < min.x) ? min.x : x;
  691. var y = value.y;
  692. y = (y > max.y) ? max.y : y;
  693. y = (y < min.y) ? min.y : y;
  694. var z = value.z;
  695. z = (z > max.z) ? max.z : z;
  696. z = (z < min.z) ? min.z : z;
  697. return new Vector3(x, y, z);
  698. };
  699. Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  700. var squared = amount * amount;
  701. var cubed = amount * squared;
  702. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  703. var part2 = (-2.0 * cubed) + (3.0 * squared);
  704. var part3 = (cubed - (2.0 * squared)) + amount;
  705. var part4 = cubed - squared;
  706. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  707. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  708. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  709. return new Vector3(x, y, z);
  710. };
  711. Vector3.Lerp = function (start, end, amount) {
  712. var x = start.x + ((end.x - start.x) * amount);
  713. var y = start.y + ((end.y - start.y) * amount);
  714. var z = start.z + ((end.z - start.z) * amount);
  715. return new Vector3(x, y, z);
  716. };
  717. Vector3.Dot = function (left, right) {
  718. return (left.x * right.x + left.y * right.y + left.z * right.z);
  719. };
  720. Vector3.Cross = function (left, right) {
  721. var result = Vector3.Zero();
  722. Vector3.CrossToRef(left, right, result);
  723. return result;
  724. };
  725. Vector3.CrossToRef = function (left, right, result) {
  726. result.x = left.y * right.z - left.z * right.y;
  727. result.y = left.z * right.x - left.x * right.z;
  728. result.z = left.x * right.y - left.y * right.x;
  729. };
  730. Vector3.Normalize = function (vector) {
  731. var result = Vector3.Zero();
  732. Vector3.NormalizeToRef(vector, result);
  733. return result;
  734. };
  735. Vector3.NormalizeToRef = function (vector, result) {
  736. result.copyFrom(vector);
  737. result.normalize();
  738. };
  739. Vector3.Project = function (vector, world, transform, viewport) {
  740. var cw = viewport.width;
  741. var ch = viewport.height;
  742. var cx = viewport.x;
  743. var cy = viewport.y;
  744. 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);
  745. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  746. return Vector3.TransformCoordinates(vector, finalMatrix);
  747. };
  748. Vector3.UnprojectFromTransform = function (source, viewportWidth, viewportHeight, world, transform) {
  749. var matrix = world.multiply(transform);
  750. matrix.invert();
  751. source.x = source.x / viewportWidth * 2 - 1;
  752. source.y = -(source.y / viewportHeight * 2 - 1);
  753. var vector = Vector3.TransformCoordinates(source, matrix);
  754. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  755. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  756. vector = vector.scale(1.0 / num);
  757. }
  758. return vector;
  759. };
  760. Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  761. var matrix = world.multiply(view).multiply(projection);
  762. matrix.invert();
  763. source.x = source.x / viewportWidth * 2 - 1;
  764. source.y = -(source.y / viewportHeight * 2 - 1);
  765. var vector = Vector3.TransformCoordinates(source, matrix);
  766. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  767. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  768. vector = vector.scale(1.0 / num);
  769. }
  770. return vector;
  771. };
  772. Vector3.Minimize = function (left, right) {
  773. var min = left.clone();
  774. min.MinimizeInPlace(right);
  775. return min;
  776. };
  777. Vector3.Maximize = function (left, right) {
  778. var max = left.clone();
  779. max.MaximizeInPlace(right);
  780. return max;
  781. };
  782. Vector3.Distance = function (value1, value2) {
  783. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  784. };
  785. Vector3.DistanceSquared = function (value1, value2) {
  786. var x = value1.x - value2.x;
  787. var y = value1.y - value2.y;
  788. var z = value1.z - value2.z;
  789. return (x * x) + (y * y) + (z * z);
  790. };
  791. Vector3.Center = function (value1, value2) {
  792. var center = value1.add(value2);
  793. center.scaleInPlace(0.5);
  794. return center;
  795. };
  796. /**
  797. * Given three orthogonal left-handed oriented Vector3 axis in space (target system),
  798. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  799. * to something in order to rotate it from its local system to the given target system.
  800. */
  801. Vector3.RotationFromAxis = function (axis1, axis2, axis3) {
  802. var u = Vector3.Normalize(axis1);
  803. var v = Vector3.Normalize(axis2);
  804. var w = Vector3.Normalize(axis3);
  805. // world axis
  806. var X = Axis.X;
  807. var Y = Axis.Y;
  808. var Z = Axis.Z;
  809. // equation unknowns and vars
  810. var yaw = 0.0;
  811. var pitch = 0.0;
  812. var roll = 0.0;
  813. var x = 0.0;
  814. var y = 0.0;
  815. var z = 0.0;
  816. var t = 0.0;
  817. var sign = -1.0;
  818. var pi = Math.PI;
  819. var nbRevert = 0;
  820. var cross;
  821. var dot = 0.0;
  822. // step 1 : rotation around w
  823. // Rv3(u) = u1, and u1 belongs to plane xOz
  824. // Rv3(w) = w1 = w invariant
  825. var u1;
  826. var v1;
  827. if (w.z == 0) {
  828. z = 1.0;
  829. }
  830. else if (w.x == 0) {
  831. x = 1.0;
  832. }
  833. else {
  834. t = w.z / w.x;
  835. x = -t * Math.sqrt(1 / (1 + t * t));
  836. z = Math.sqrt(1 / (1 + t * t));
  837. }
  838. u1 = new Vector3(x, y, z);
  839. v1 = Vector3.Cross(w, u1); // v1 image of v through rotation around w
  840. cross = Vector3.Cross(u, u1); // returns same direction as w (=local z) if positive angle : cross(source, image)
  841. if (Vector3.Dot(w, cross) < 0) {
  842. sign = 1;
  843. }
  844. dot = Vector3.Dot(u, u1);
  845. roll = Math.acos(dot) * sign;
  846. if (Vector3.Dot(u1, X) < 0) {
  847. roll = Math.PI + roll;
  848. u1 = u1.scaleInPlace(-1);
  849. v1 = v1.scaleInPlace(-1);
  850. nbRevert++;
  851. }
  852. // step 2 : rotate around u1
  853. // Ru1(w1) = Ru1(w) = w2, and w2 belongs to plane xOz
  854. // u1 is yet in xOz and invariant by Ru1, so after this step u1 and w2 will be in xOz
  855. var w2;
  856. var v2;
  857. x = 0.0;
  858. y = 0.0;
  859. z = 0.0;
  860. sign = -1;
  861. if (w.z == 0) {
  862. x = 1.0;
  863. }
  864. else {
  865. t = u1.z / u1.x;
  866. x = -t * Math.sqrt(1 / (1 + t * t));
  867. z = Math.sqrt(1 / (1 + t * t));
  868. }
  869. w2 = new BABYLON.Vector3(x, y, z);
  870. v2 = BABYLON.Vector3.Cross(w2, u1); // v2 image of v1 through rotation around u1
  871. cross = BABYLON.Vector3.Cross(w, w2); // returns same direction as u1 (=local x) if positive angle : cross(source, image)
  872. if (BABYLON.Vector3.Dot(u1, cross) < 0) {
  873. sign = 1;
  874. }
  875. dot = BABYLON.Vector3.Dot(w, w2);
  876. pitch = Math.acos(dot) * sign;
  877. if (BABYLON.Vector3.Dot(v2, Y) < 0) {
  878. pitch = Math.PI + pitch;
  879. v2 = v2.scaleInPlace(-1);
  880. w2 = w2.scaleInPlace(-1);
  881. nbRevert++;
  882. }
  883. // step 3 : rotate around v2
  884. // Rv2(u1) = X, same as Rv2(w2) = Z, with X=(1,0,0) and Z=(0,0,1)
  885. sign = -1;
  886. cross = BABYLON.Vector3.Cross(X, u1); // returns same direction as Y if positive angle : cross(source, image)
  887. if (BABYLON.Vector3.Dot(cross, Y) < 0) {
  888. sign = 1;
  889. }
  890. dot = BABYLON.Vector3.Dot(u1, X);
  891. yaw = -Math.acos(dot) * sign; // negative : plane zOx oriented clockwise
  892. if (dot < 0 && nbRevert < 2) {
  893. yaw = Math.PI + yaw;
  894. }
  895. return new BABYLON.Vector3(pitch, yaw, roll);
  896. };
  897. return Vector3;
  898. })();
  899. BABYLON.Vector3 = Vector3;
  900. //Vector4 class created for EulerAngle class conversion to Quaternion
  901. var Vector4 = (function () {
  902. function Vector4(x, y, z, w) {
  903. this.x = x;
  904. this.y = y;
  905. this.z = z;
  906. this.w = w;
  907. }
  908. Vector4.prototype.toString = function () {
  909. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "W:" + this.w + "}";
  910. };
  911. // Operators
  912. Vector4.prototype.asArray = function () {
  913. var result = [];
  914. this.toArray(result, 0);
  915. return result;
  916. };
  917. Vector4.prototype.toArray = function (array, index) {
  918. if (index === undefined) {
  919. index = 0;
  920. }
  921. array[index] = this.x;
  922. array[index + 1] = this.y;
  923. array[index + 2] = this.z;
  924. array[index + 3] = this.w;
  925. return this;
  926. };
  927. Vector4.prototype.addInPlace = function (otherVector) {
  928. this.x += otherVector.x;
  929. this.y += otherVector.y;
  930. this.z += otherVector.z;
  931. this.w += otherVector.w;
  932. return this;
  933. };
  934. Vector4.prototype.add = function (otherVector) {
  935. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  936. };
  937. Vector4.prototype.addToRef = function (otherVector, result) {
  938. result.x = this.x + otherVector.x;
  939. result.y = this.y + otherVector.y;
  940. result.z = this.z + otherVector.z;
  941. result.w = this.w + otherVector.w;
  942. return this;
  943. };
  944. Vector4.prototype.subtractInPlace = function (otherVector) {
  945. this.x -= otherVector.x;
  946. this.y -= otherVector.y;
  947. this.z -= otherVector.z;
  948. this.w -= otherVector.w;
  949. return this;
  950. };
  951. Vector4.prototype.subtract = function (otherVector) {
  952. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  953. };
  954. Vector4.prototype.subtractToRef = function (otherVector, result) {
  955. result.x = this.x - otherVector.x;
  956. result.y = this.y - otherVector.y;
  957. result.z = this.z - otherVector.z;
  958. result.w = this.w - otherVector.w;
  959. return this;
  960. };
  961. Vector4.prototype.subtractFromFloats = function (x, y, z, w) {
  962. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  963. };
  964. Vector4.prototype.subtractFromFloatsToRef = function (x, y, z, w, result) {
  965. result.x = this.x - x;
  966. result.y = this.y - y;
  967. result.z = this.z - z;
  968. result.w = this.w - w;
  969. return this;
  970. };
  971. Vector4.prototype.negate = function () {
  972. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  973. };
  974. Vector4.prototype.scaleInPlace = function (scale) {
  975. this.x *= scale;
  976. this.y *= scale;
  977. this.z *= scale;
  978. this.w *= scale;
  979. return this;
  980. };
  981. Vector4.prototype.scale = function (scale) {
  982. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  983. };
  984. Vector4.prototype.scaleToRef = function (scale, result) {
  985. result.x = this.x * scale;
  986. result.y = this.y * scale;
  987. result.z = this.z * scale;
  988. result.w = this.w * scale;
  989. };
  990. Vector4.prototype.equals = function (otherVector) {
  991. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  992. };
  993. Vector4.prototype.equalsWithEpsilon = function (otherVector) {
  994. 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;
  995. };
  996. Vector4.prototype.equalsToFloats = function (x, y, z, w) {
  997. return this.x === x && this.y === y && this.z === z && this.w === w;
  998. };
  999. Vector4.prototype.multiplyInPlace = function (otherVector) {
  1000. this.x *= otherVector.x;
  1001. this.y *= otherVector.y;
  1002. this.z *= otherVector.z;
  1003. this.w *= otherVector.w;
  1004. return this;
  1005. };
  1006. Vector4.prototype.multiply = function (otherVector) {
  1007. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  1008. };
  1009. Vector4.prototype.multiplyToRef = function (otherVector, result) {
  1010. result.x = this.x * otherVector.x;
  1011. result.y = this.y * otherVector.y;
  1012. result.z = this.z * otherVector.z;
  1013. result.w = this.w * otherVector.w;
  1014. return this;
  1015. };
  1016. Vector4.prototype.multiplyByFloats = function (x, y, z, w) {
  1017. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  1018. };
  1019. Vector4.prototype.divide = function (otherVector) {
  1020. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  1021. };
  1022. Vector4.prototype.divideToRef = function (otherVector, result) {
  1023. result.x = this.x / otherVector.x;
  1024. result.y = this.y / otherVector.y;
  1025. result.z = this.z / otherVector.z;
  1026. result.w = this.w / otherVector.w;
  1027. return this;
  1028. };
  1029. Vector4.prototype.MinimizeInPlace = function (other) {
  1030. if (other.x < this.x)
  1031. this.x = other.x;
  1032. if (other.y < this.y)
  1033. this.y = other.y;
  1034. if (other.z < this.z)
  1035. this.z = other.z;
  1036. if (other.w < this.w)
  1037. this.w = other.w;
  1038. return this;
  1039. };
  1040. Vector4.prototype.MaximizeInPlace = function (other) {
  1041. if (other.x > this.x)
  1042. this.x = other.x;
  1043. if (other.y > this.y)
  1044. this.y = other.y;
  1045. if (other.z > this.z)
  1046. this.z = other.z;
  1047. if (other.w > this.w)
  1048. this.w = other.w;
  1049. return this;
  1050. };
  1051. // Properties
  1052. Vector4.prototype.length = function () {
  1053. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1054. };
  1055. Vector4.prototype.lengthSquared = function () {
  1056. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1057. };
  1058. // Methods
  1059. Vector4.prototype.normalize = function () {
  1060. var len = this.length();
  1061. if (len === 0)
  1062. return this;
  1063. var num = 1.0 / len;
  1064. this.x *= num;
  1065. this.y *= num;
  1066. this.z *= num;
  1067. this.w *= num;
  1068. return this;
  1069. };
  1070. Vector4.prototype.clone = function () {
  1071. return new Vector4(this.x, this.y, this.z, this.w);
  1072. };
  1073. Vector4.prototype.copyFrom = function (source) {
  1074. this.x = source.x;
  1075. this.y = source.y;
  1076. this.z = source.z;
  1077. this.w = source.w;
  1078. return this;
  1079. };
  1080. Vector4.prototype.copyFromFloats = function (x, y, z, w) {
  1081. this.x = x;
  1082. this.y = y;
  1083. this.z = z;
  1084. this.w = w;
  1085. return this;
  1086. };
  1087. // Statics
  1088. Vector4.FromArray = function (array, offset) {
  1089. if (!offset) {
  1090. offset = 0;
  1091. }
  1092. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1093. };
  1094. Vector4.FromArrayToRef = function (array, offset, result) {
  1095. result.x = array[offset];
  1096. result.y = array[offset + 1];
  1097. result.z = array[offset + 2];
  1098. result.w = array[offset + 3];
  1099. };
  1100. Vector4.FromFloatArrayToRef = function (array, offset, result) {
  1101. result.x = array[offset];
  1102. result.y = array[offset + 1];
  1103. result.z = array[offset + 2];
  1104. result.w = array[offset + 3];
  1105. };
  1106. Vector4.FromFloatsToRef = function (x, y, z, w, result) {
  1107. result.x = x;
  1108. result.y = y;
  1109. result.z = z;
  1110. result.w = w;
  1111. };
  1112. Vector4.Zero = function () {
  1113. return new Vector4(0, 0, 0, 0);
  1114. };
  1115. Vector4.Normalize = function (vector) {
  1116. var result = Vector4.Zero();
  1117. Vector4.NormalizeToRef(vector, result);
  1118. return result;
  1119. };
  1120. Vector4.NormalizeToRef = function (vector, result) {
  1121. result.copyFrom(vector);
  1122. result.normalize();
  1123. };
  1124. Vector4.Minimize = function (left, right) {
  1125. var min = left.clone();
  1126. min.MinimizeInPlace(right);
  1127. return min;
  1128. };
  1129. Vector4.Maximize = function (left, right) {
  1130. var max = left.clone();
  1131. max.MaximizeInPlace(right);
  1132. return max;
  1133. };
  1134. Vector4.Distance = function (value1, value2) {
  1135. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1136. };
  1137. Vector4.DistanceSquared = function (value1, value2) {
  1138. var x = value1.x - value2.x;
  1139. var y = value1.y - value2.y;
  1140. var z = value1.z - value2.z;
  1141. var w = value1.w - value2.w;
  1142. return (x * x) + (y * y) + (z * z) + (w * w);
  1143. };
  1144. Vector4.Center = function (value1, value2) {
  1145. var center = value1.add(value2);
  1146. center.scaleInPlace(0.5);
  1147. return center;
  1148. };
  1149. return Vector4;
  1150. })();
  1151. BABYLON.Vector4 = Vector4;
  1152. var Quaternion = (function () {
  1153. function Quaternion(x, y, z, w) {
  1154. if (x === void 0) { x = 0; }
  1155. if (y === void 0) { y = 0; }
  1156. if (z === void 0) { z = 0; }
  1157. if (w === void 0) { w = 1; }
  1158. this.x = x;
  1159. this.y = y;
  1160. this.z = z;
  1161. this.w = w;
  1162. }
  1163. Quaternion.prototype.toString = function () {
  1164. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1165. };
  1166. Quaternion.prototype.asArray = function () {
  1167. return [this.x, this.y, this.z, this.w];
  1168. };
  1169. Quaternion.prototype.equals = function (otherQuaternion) {
  1170. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  1171. };
  1172. Quaternion.prototype.clone = function () {
  1173. return new Quaternion(this.x, this.y, this.z, this.w);
  1174. };
  1175. Quaternion.prototype.copyFrom = function (other) {
  1176. this.x = other.x;
  1177. this.y = other.y;
  1178. this.z = other.z;
  1179. this.w = other.w;
  1180. return this;
  1181. };
  1182. Quaternion.prototype.copyFromFloats = function (x, y, z, w) {
  1183. this.x = x;
  1184. this.y = y;
  1185. this.z = z;
  1186. this.w = w;
  1187. return this;
  1188. };
  1189. Quaternion.prototype.add = function (other) {
  1190. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  1191. };
  1192. Quaternion.prototype.subtract = function (other) {
  1193. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  1194. };
  1195. Quaternion.prototype.scale = function (value) {
  1196. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  1197. };
  1198. Quaternion.prototype.multiply = function (q1) {
  1199. var result = new Quaternion(0, 0, 0, 1.0);
  1200. this.multiplyToRef(q1, result);
  1201. return result;
  1202. };
  1203. Quaternion.prototype.multiplyToRef = function (q1, result) {
  1204. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1205. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1206. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1207. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1208. result.copyFromFloats(x, y, z, w);
  1209. return this;
  1210. };
  1211. Quaternion.prototype.length = function () {
  1212. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1213. };
  1214. Quaternion.prototype.normalize = function () {
  1215. var length = 1.0 / this.length();
  1216. this.x *= length;
  1217. this.y *= length;
  1218. this.z *= length;
  1219. this.w *= length;
  1220. return this;
  1221. };
  1222. Quaternion.prototype.toEulerAngles = function () {
  1223. var result = Vector3.Zero();
  1224. this.toEulerAnglesToRef(result);
  1225. return result;
  1226. };
  1227. Quaternion.prototype.toEulerAnglesToRef = function (result) {
  1228. //result is an EulerAngles in the in the z-x-z convention
  1229. var qx = this.x;
  1230. var qy = this.y;
  1231. var qz = this.z;
  1232. var qw = this.w;
  1233. var qxy = qx * qy;
  1234. var qxz = qx * qz;
  1235. var qwy = qw * qy;
  1236. var qwz = qw * qz;
  1237. var qwx = qw * qx;
  1238. var qyz = qy * qz;
  1239. var sqx = qx * qx;
  1240. var sqy = qy * qy;
  1241. var determinant = sqx + sqy;
  1242. if (determinant !== 0.000 && determinant !== 1.000) {
  1243. result.x = Math.atan2(qxz + qwy, qwx - qyz);
  1244. result.y = Math.acos(1 - 2 * determinant);
  1245. result.z = Math.atan2(qxz - qwy, qwx + qyz);
  1246. }
  1247. else {
  1248. if (determinant === 0.0) {
  1249. result.x = 0.0;
  1250. result.y = 0.0;
  1251. 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)
  1252. }
  1253. else {
  1254. 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)
  1255. result.y = Math.PI;
  1256. result.z = 0.0;
  1257. }
  1258. }
  1259. return this;
  1260. };
  1261. Quaternion.prototype.toRotationMatrix = function (result) {
  1262. var xx = this.x * this.x;
  1263. var yy = this.y * this.y;
  1264. var zz = this.z * this.z;
  1265. var xy = this.x * this.y;
  1266. var zw = this.z * this.w;
  1267. var zx = this.z * this.x;
  1268. var yw = this.y * this.w;
  1269. var yz = this.y * this.z;
  1270. var xw = this.x * this.w;
  1271. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1272. result.m[1] = 2.0 * (xy + zw);
  1273. result.m[2] = 2.0 * (zx - yw);
  1274. result.m[3] = 0;
  1275. result.m[4] = 2.0 * (xy - zw);
  1276. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1277. result.m[6] = 2.0 * (yz + xw);
  1278. result.m[7] = 0;
  1279. result.m[8] = 2.0 * (zx + yw);
  1280. result.m[9] = 2.0 * (yz - xw);
  1281. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1282. result.m[11] = 0;
  1283. result.m[12] = 0;
  1284. result.m[13] = 0;
  1285. result.m[14] = 0;
  1286. result.m[15] = 1.0;
  1287. return this;
  1288. };
  1289. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  1290. Quaternion.FromRotationMatrixToRef(matrix, this);
  1291. return this;
  1292. };
  1293. // Statics
  1294. Quaternion.FromRotationMatrix = function (matrix) {
  1295. var result = new Quaternion();
  1296. Quaternion.FromRotationMatrixToRef(matrix, result);
  1297. return result;
  1298. };
  1299. Quaternion.FromRotationMatrixToRef = function (matrix, result) {
  1300. var data = matrix.m;
  1301. var m11 = data[0], m12 = data[4], m13 = data[8];
  1302. var m21 = data[1], m22 = data[5], m23 = data[9];
  1303. var m31 = data[2], m32 = data[6], m33 = data[10];
  1304. var trace = m11 + m22 + m33;
  1305. var s;
  1306. if (trace > 0) {
  1307. s = 0.5 / Math.sqrt(trace + 1.0);
  1308. result.w = 0.25 / s;
  1309. result.x = (m32 - m23) * s;
  1310. result.y = (m13 - m31) * s;
  1311. result.z = (m21 - m12) * s;
  1312. }
  1313. else if (m11 > m22 && m11 > m33) {
  1314. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1315. result.w = (m32 - m23) / s;
  1316. result.x = 0.25 * s;
  1317. result.y = (m12 + m21) / s;
  1318. result.z = (m13 + m31) / s;
  1319. }
  1320. else if (m22 > m33) {
  1321. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1322. result.w = (m13 - m31) / s;
  1323. result.x = (m12 + m21) / s;
  1324. result.y = 0.25 * s;
  1325. result.z = (m23 + m32) / s;
  1326. }
  1327. else {
  1328. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1329. result.w = (m21 - m12) / s;
  1330. result.x = (m13 + m31) / s;
  1331. result.y = (m23 + m32) / s;
  1332. result.z = 0.25 * s;
  1333. }
  1334. };
  1335. Quaternion.Inverse = function (q) {
  1336. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1337. };
  1338. Quaternion.Identity = function () {
  1339. return new Quaternion(0, 0, 0, 1);
  1340. };
  1341. Quaternion.RotationAxis = function (axis, angle) {
  1342. var result = new Quaternion();
  1343. var sin = Math.sin(angle / 2);
  1344. result.w = Math.cos(angle / 2);
  1345. result.x = axis.x * sin;
  1346. result.y = axis.y * sin;
  1347. result.z = axis.z * sin;
  1348. return result;
  1349. };
  1350. Quaternion.FromArray = function (array, offset) {
  1351. if (!offset) {
  1352. offset = 0;
  1353. }
  1354. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1355. };
  1356. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1357. var result = new Quaternion();
  1358. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1359. return result;
  1360. };
  1361. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1362. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  1363. var halfRoll = roll * 0.5;
  1364. var halfPitch = pitch * 0.5;
  1365. var halfYaw = yaw * 0.5;
  1366. var sinRoll = Math.sin(halfRoll);
  1367. var cosRoll = Math.cos(halfRoll);
  1368. var sinPitch = Math.sin(halfPitch);
  1369. var cosPitch = Math.cos(halfPitch);
  1370. var sinYaw = Math.sin(halfYaw);
  1371. var cosYaw = Math.cos(halfYaw);
  1372. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1373. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1374. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1375. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1376. };
  1377. Quaternion.RotationAlphaBetaGamma = function (alpha, beta, gamma) {
  1378. var result = new Quaternion();
  1379. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  1380. return result;
  1381. };
  1382. Quaternion.RotationAlphaBetaGammaToRef = function (alpha, beta, gamma, result) {
  1383. // Produces a quaternion from Euler angles in the z-x-z orientation
  1384. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  1385. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  1386. var halfBeta = beta * 0.5;
  1387. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1388. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1389. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1390. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1391. };
  1392. Quaternion.Slerp = function (left, right, amount) {
  1393. var num2;
  1394. var num3;
  1395. var num = amount;
  1396. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1397. var flag = false;
  1398. if (num4 < 0) {
  1399. flag = true;
  1400. num4 = -num4;
  1401. }
  1402. if (num4 > 0.999999) {
  1403. num3 = 1 - num;
  1404. num2 = flag ? -num : num;
  1405. }
  1406. else {
  1407. var num5 = Math.acos(num4);
  1408. var num6 = (1.0 / Math.sin(num5));
  1409. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1410. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1411. }
  1412. 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));
  1413. };
  1414. return Quaternion;
  1415. })();
  1416. BABYLON.Quaternion = Quaternion;
  1417. var Matrix = (function () {
  1418. function Matrix() {
  1419. this.m = new Float32Array(16);
  1420. }
  1421. // Properties
  1422. Matrix.prototype.isIdentity = function () {
  1423. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1424. return false;
  1425. 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)
  1426. return false;
  1427. return true;
  1428. };
  1429. Matrix.prototype.determinant = function () {
  1430. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1431. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1432. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1433. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1434. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1435. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1436. 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))));
  1437. };
  1438. // Methods
  1439. Matrix.prototype.toArray = function () {
  1440. return this.m;
  1441. };
  1442. Matrix.prototype.asArray = function () {
  1443. return this.toArray();
  1444. };
  1445. Matrix.prototype.invert = function () {
  1446. this.invertToRef(this);
  1447. return this;
  1448. };
  1449. Matrix.prototype.invertToRef = function (other) {
  1450. var l1 = this.m[0];
  1451. var l2 = this.m[1];
  1452. var l3 = this.m[2];
  1453. var l4 = this.m[3];
  1454. var l5 = this.m[4];
  1455. var l6 = this.m[5];
  1456. var l7 = this.m[6];
  1457. var l8 = this.m[7];
  1458. var l9 = this.m[8];
  1459. var l10 = this.m[9];
  1460. var l11 = this.m[10];
  1461. var l12 = this.m[11];
  1462. var l13 = this.m[12];
  1463. var l14 = this.m[13];
  1464. var l15 = this.m[14];
  1465. var l16 = this.m[15];
  1466. var l17 = (l11 * l16) - (l12 * l15);
  1467. var l18 = (l10 * l16) - (l12 * l14);
  1468. var l19 = (l10 * l15) - (l11 * l14);
  1469. var l20 = (l9 * l16) - (l12 * l13);
  1470. var l21 = (l9 * l15) - (l11 * l13);
  1471. var l22 = (l9 * l14) - (l10 * l13);
  1472. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1473. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1474. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1475. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1476. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1477. var l28 = (l7 * l16) - (l8 * l15);
  1478. var l29 = (l6 * l16) - (l8 * l14);
  1479. var l30 = (l6 * l15) - (l7 * l14);
  1480. var l31 = (l5 * l16) - (l8 * l13);
  1481. var l32 = (l5 * l15) - (l7 * l13);
  1482. var l33 = (l5 * l14) - (l6 * l13);
  1483. var l34 = (l7 * l12) - (l8 * l11);
  1484. var l35 = (l6 * l12) - (l8 * l10);
  1485. var l36 = (l6 * l11) - (l7 * l10);
  1486. var l37 = (l5 * l12) - (l8 * l9);
  1487. var l38 = (l5 * l11) - (l7 * l9);
  1488. var l39 = (l5 * l10) - (l6 * l9);
  1489. other.m[0] = l23 * l27;
  1490. other.m[4] = l24 * l27;
  1491. other.m[8] = l25 * l27;
  1492. other.m[12] = l26 * l27;
  1493. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1494. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1495. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1496. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1497. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1498. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1499. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1500. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1501. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1502. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1503. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1504. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1505. return this;
  1506. };
  1507. Matrix.prototype.invertToRefSIMD = function (other) {
  1508. var src = this.m;
  1509. var dest = other.m;
  1510. var row0, row1, row2, row3;
  1511. var tmp1;
  1512. var minor0, minor1, minor2, minor3;
  1513. var det;
  1514. // Load the 4 rows
  1515. var src0 = SIMD.float32x4.load(src, 0);
  1516. var src1 = SIMD.float32x4.load(src, 4);
  1517. var src2 = SIMD.float32x4.load(src, 8);
  1518. var src3 = SIMD.float32x4.load(src, 12);
  1519. // Transpose the source matrix. Sort of. Not a true transpose operation
  1520. tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1521. row1 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1522. row0 = SIMD.float32x4.shuffle(tmp1, row1, 0, 2, 4, 6);
  1523. row1 = SIMD.float32x4.shuffle(row1, tmp1, 1, 3, 5, 7);
  1524. tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1525. row3 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1526. row2 = SIMD.float32x4.shuffle(tmp1, row3, 0, 2, 4, 6);
  1527. row3 = SIMD.float32x4.shuffle(row3, tmp1, 1, 3, 5, 7);
  1528. // This is a true transposition, but it will lead to an incorrect result
  1529. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1530. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1531. //row0 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1532. //row1 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1533. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1534. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1535. //row2 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1536. //row3 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1537. // ----
  1538. tmp1 = SIMD.float32x4.mul(row2, row3);
  1539. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1540. minor0 = SIMD.float32x4.mul(row1, tmp1);
  1541. minor1 = SIMD.float32x4.mul(row0, tmp1);
  1542. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1543. minor0 = SIMD.float32x4.sub(SIMD.float32x4.mul(row1, tmp1), minor0);
  1544. minor1 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor1);
  1545. minor1 = SIMD.float32x4.swizzle(minor1, 2, 3, 0, 1); // 0x4E = 01001110
  1546. // ----
  1547. tmp1 = SIMD.float32x4.mul(row1, row2);
  1548. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1549. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor0);
  1550. minor3 = SIMD.float32x4.mul(row0, tmp1);
  1551. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1552. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row3, tmp1));
  1553. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor3);
  1554. minor3 = SIMD.float32x4.swizzle(minor3, 2, 3, 0, 1); // 0x4E = 01001110
  1555. // ----
  1556. tmp1 = SIMD.float32x4.mul(SIMD.float32x4.swizzle(row1, 2, 3, 0, 1), row3); // 0x4E = 01001110
  1557. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1558. row2 = SIMD.float32x4.swizzle(row2, 2, 3, 0, 1); // 0x4E = 01001110
  1559. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor0);
  1560. minor2 = SIMD.float32x4.mul(row0, tmp1);
  1561. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1562. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row2, tmp1));
  1563. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor2);
  1564. minor2 = SIMD.float32x4.swizzle(minor2, 2, 3, 0, 1); // 0x4E = 01001110
  1565. // ----
  1566. tmp1 = SIMD.float32x4.mul(row0, row1);
  1567. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1568. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor2);
  1569. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row2, tmp1), minor3);
  1570. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1571. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row3, tmp1), minor2);
  1572. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row2, tmp1));
  1573. // ----
  1574. tmp1 = SIMD.float32x4.mul(row0, row3);
  1575. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1576. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row2, tmp1));
  1577. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor2);
  1578. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1579. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor1);
  1580. minor2 = SIMD.float32x4.sub(minor2, SIMD.float32x4.mul(row1, tmp1));
  1581. // ----
  1582. tmp1 = SIMD.float32x4.mul(row0, row2);
  1583. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1584. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor1);
  1585. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row1, tmp1));
  1586. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1587. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row3, tmp1));
  1588. minor3 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor3);
  1589. // Compute determinant
  1590. det = SIMD.float32x4.mul(row0, minor0);
  1591. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 2, 3, 0, 1), det); // 0x4E = 01001110
  1592. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 1, 0, 3, 2), det); // 0xB1 = 10110001
  1593. tmp1 = SIMD.float32x4.reciprocalApproximation(det);
  1594. det = SIMD.float32x4.sub(SIMD.float32x4.add(tmp1, tmp1), SIMD.float32x4.mul(det, SIMD.float32x4.mul(tmp1, tmp1)));
  1595. det = SIMD.float32x4.swizzle(det, 0, 0, 0, 0);
  1596. // These shuffles aren't necessary if the faulty transposition is done
  1597. // up at the top of this function.
  1598. //minor0 = SIMD.float32x4.swizzle(minor0, 2, 1, 0, 3);
  1599. //minor1 = SIMD.float32x4.swizzle(minor1, 2, 1, 0, 3);
  1600. //minor2 = SIMD.float32x4.swizzle(minor2, 2, 1, 0, 3);
  1601. //minor3 = SIMD.float32x4.swizzle(minor3, 2, 1, 0, 3);
  1602. // Compute final values by multiplying with 1/det
  1603. minor0 = SIMD.float32x4.mul(det, minor0);
  1604. minor1 = SIMD.float32x4.mul(det, minor1);
  1605. minor2 = SIMD.float32x4.mul(det, minor2);
  1606. minor3 = SIMD.float32x4.mul(det, minor3);
  1607. SIMD.float32x4.store(dest, 0, minor0);
  1608. SIMD.float32x4.store(dest, 4, minor1);
  1609. SIMD.float32x4.store(dest, 8, minor2);
  1610. SIMD.float32x4.store(dest, 12, minor3);
  1611. return this;
  1612. };
  1613. Matrix.prototype.setTranslation = function (vector3) {
  1614. this.m[12] = vector3.x;
  1615. this.m[13] = vector3.y;
  1616. this.m[14] = vector3.z;
  1617. return this;
  1618. };
  1619. Matrix.prototype.multiply = function (other) {
  1620. var result = new Matrix();
  1621. this.multiplyToRef(other, result);
  1622. return result;
  1623. };
  1624. Matrix.prototype.copyFrom = function (other) {
  1625. for (var index = 0; index < 16; index++) {
  1626. this.m[index] = other.m[index];
  1627. }
  1628. return this;
  1629. };
  1630. Matrix.prototype.copyToArray = function (array, offset) {
  1631. if (offset === void 0) { offset = 0; }
  1632. for (var index = 0; index < 16; index++) {
  1633. array[offset + index] = this.m[index];
  1634. }
  1635. return this;
  1636. };
  1637. Matrix.prototype.multiplyToRef = function (other, result) {
  1638. this.multiplyToArray(other, result.m, 0);
  1639. return this;
  1640. };
  1641. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1642. var tm0 = this.m[0];
  1643. var tm1 = this.m[1];
  1644. var tm2 = this.m[2];
  1645. var tm3 = this.m[3];
  1646. var tm4 = this.m[4];
  1647. var tm5 = this.m[5];
  1648. var tm6 = this.m[6];
  1649. var tm7 = this.m[7];
  1650. var tm8 = this.m[8];
  1651. var tm9 = this.m[9];
  1652. var tm10 = this.m[10];
  1653. var tm11 = this.m[11];
  1654. var tm12 = this.m[12];
  1655. var tm13 = this.m[13];
  1656. var tm14 = this.m[14];
  1657. var tm15 = this.m[15];
  1658. var om0 = other.m[0];
  1659. var om1 = other.m[1];
  1660. var om2 = other.m[2];
  1661. var om3 = other.m[3];
  1662. var om4 = other.m[4];
  1663. var om5 = other.m[5];
  1664. var om6 = other.m[6];
  1665. var om7 = other.m[7];
  1666. var om8 = other.m[8];
  1667. var om9 = other.m[9];
  1668. var om10 = other.m[10];
  1669. var om11 = other.m[11];
  1670. var om12 = other.m[12];
  1671. var om13 = other.m[13];
  1672. var om14 = other.m[14];
  1673. var om15 = other.m[15];
  1674. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1675. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1676. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1677. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1678. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1679. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1680. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1681. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1682. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1683. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1684. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1685. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1686. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1687. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1688. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1689. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1690. return this;
  1691. };
  1692. Matrix.prototype.multiplyToArraySIMD = function (other, result, offset) {
  1693. if (offset === void 0) { offset = 0; }
  1694. var tm = this.m;
  1695. var om = other.m;
  1696. var om0 = SIMD.float32x4.load(om, 0);
  1697. var om1 = SIMD.float32x4.load(om, 4);
  1698. var om2 = SIMD.float32x4.load(om, 8);
  1699. var om3 = SIMD.float32x4.load(om, 12);
  1700. var tm0 = SIMD.float32x4.load(tm, 0);
  1701. SIMD.float32x4.store(result, offset + 0, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 3, 3, 3, 3), om3)))));
  1702. var tm1 = SIMD.float32x4.load(tm, 4);
  1703. SIMD.float32x4.store(result, offset + 4, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 3, 3, 3, 3), om3)))));
  1704. var tm2 = SIMD.float32x4.load(tm, 8);
  1705. SIMD.float32x4.store(result, offset + 8, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 3, 3, 3, 3), om3)))));
  1706. var tm3 = SIMD.float32x4.load(tm, 12);
  1707. SIMD.float32x4.store(result, offset + 12, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 3, 3, 3, 3), om3)))));
  1708. };
  1709. Matrix.prototype.equals = function (value) {
  1710. 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]);
  1711. };
  1712. Matrix.prototype.clone = function () {
  1713. 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]);
  1714. };
  1715. Matrix.prototype.decompose = function (scale, rotation, translation) {
  1716. translation.x = this.m[12];
  1717. translation.y = this.m[13];
  1718. translation.z = this.m[14];
  1719. var xs = BABYLON.Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1720. var ys = BABYLON.Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1721. var zs = BABYLON.Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1722. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1723. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1724. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1725. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  1726. rotation.x = 0;
  1727. rotation.y = 0;
  1728. rotation.z = 0;
  1729. rotation.w = 1;
  1730. return false;
  1731. }
  1732. 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);
  1733. Quaternion.FromRotationMatrixToRef(rotationMatrix, rotation);
  1734. return true;
  1735. };
  1736. // Statics
  1737. Matrix.FromArray = function (array, offset) {
  1738. var result = new Matrix();
  1739. if (!offset) {
  1740. offset = 0;
  1741. }
  1742. Matrix.FromArrayToRef(array, offset, result);
  1743. return result;
  1744. };
  1745. Matrix.FromArrayToRef = function (array, offset, result) {
  1746. for (var index = 0; index < 16; index++) {
  1747. result.m[index] = array[index + offset];
  1748. }
  1749. };
  1750. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1751. result.m[0] = initialM11;
  1752. result.m[1] = initialM12;
  1753. result.m[2] = initialM13;
  1754. result.m[3] = initialM14;
  1755. result.m[4] = initialM21;
  1756. result.m[5] = initialM22;
  1757. result.m[6] = initialM23;
  1758. result.m[7] = initialM24;
  1759. result.m[8] = initialM31;
  1760. result.m[9] = initialM32;
  1761. result.m[10] = initialM33;
  1762. result.m[11] = initialM34;
  1763. result.m[12] = initialM41;
  1764. result.m[13] = initialM42;
  1765. result.m[14] = initialM43;
  1766. result.m[15] = initialM44;
  1767. };
  1768. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  1769. var result = new Matrix();
  1770. result.m[0] = initialM11;
  1771. result.m[1] = initialM12;
  1772. result.m[2] = initialM13;
  1773. result.m[3] = initialM14;
  1774. result.m[4] = initialM21;
  1775. result.m[5] = initialM22;
  1776. result.m[6] = initialM23;
  1777. result.m[7] = initialM24;
  1778. result.m[8] = initialM31;
  1779. result.m[9] = initialM32;
  1780. result.m[10] = initialM33;
  1781. result.m[11] = initialM34;
  1782. result.m[12] = initialM41;
  1783. result.m[13] = initialM42;
  1784. result.m[14] = initialM43;
  1785. result.m[15] = initialM44;
  1786. return result;
  1787. };
  1788. Matrix.Compose = function (scale, rotation, translation) {
  1789. var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
  1790. var rotationMatrix = Matrix.Identity();
  1791. rotation.toRotationMatrix(rotationMatrix);
  1792. result = result.multiply(rotationMatrix);
  1793. result.setTranslation(translation);
  1794. return result;
  1795. };
  1796. Matrix.Identity = function () {
  1797. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  1798. };
  1799. Matrix.IdentityToRef = function (result) {
  1800. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  1801. };
  1802. Matrix.Zero = function () {
  1803. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1804. };
  1805. Matrix.RotationX = function (angle) {
  1806. var result = new Matrix();
  1807. Matrix.RotationXToRef(angle, result);
  1808. return result;
  1809. };
  1810. Matrix.Invert = function (source) {
  1811. var result = new Matrix();
  1812. source.invertToRef(result);
  1813. return result;
  1814. };
  1815. Matrix.RotationXToRef = function (angle, result) {
  1816. var s = Math.sin(angle);
  1817. var c = Math.cos(angle);
  1818. result.m[0] = 1.0;
  1819. result.m[15] = 1.0;
  1820. result.m[5] = c;
  1821. result.m[10] = c;
  1822. result.m[9] = -s;
  1823. result.m[6] = s;
  1824. result.m[1] = 0;
  1825. result.m[2] = 0;
  1826. result.m[3] = 0;
  1827. result.m[4] = 0;
  1828. result.m[7] = 0;
  1829. result.m[8] = 0;
  1830. result.m[11] = 0;
  1831. result.m[12] = 0;
  1832. result.m[13] = 0;
  1833. result.m[14] = 0;
  1834. };
  1835. Matrix.RotationY = function (angle) {
  1836. var result = new Matrix();
  1837. Matrix.RotationYToRef(angle, result);
  1838. return result;
  1839. };
  1840. Matrix.RotationYToRef = function (angle, result) {
  1841. var s = Math.sin(angle);
  1842. var c = Math.cos(angle);
  1843. result.m[5] = 1.0;
  1844. result.m[15] = 1.0;
  1845. result.m[0] = c;
  1846. result.m[2] = -s;
  1847. result.m[8] = s;
  1848. result.m[10] = c;
  1849. result.m[1] = 0;
  1850. result.m[3] = 0;
  1851. result.m[4] = 0;
  1852. result.m[6] = 0;
  1853. result.m[7] = 0;
  1854. result.m[9] = 0;
  1855. result.m[11] = 0;
  1856. result.m[12] = 0;
  1857. result.m[13] = 0;
  1858. result.m[14] = 0;
  1859. };
  1860. Matrix.RotationZ = function (angle) {
  1861. var result = new Matrix();
  1862. Matrix.RotationZToRef(angle, result);
  1863. return result;
  1864. };
  1865. Matrix.RotationZToRef = function (angle, result) {
  1866. var s = Math.sin(angle);
  1867. var c = Math.cos(angle);
  1868. result.m[10] = 1.0;
  1869. result.m[15] = 1.0;
  1870. result.m[0] = c;
  1871. result.m[1] = s;
  1872. result.m[4] = -s;
  1873. result.m[5] = c;
  1874. result.m[2] = 0;
  1875. result.m[3] = 0;
  1876. result.m[6] = 0;
  1877. result.m[7] = 0;
  1878. result.m[8] = 0;
  1879. result.m[9] = 0;
  1880. result.m[11] = 0;
  1881. result.m[12] = 0;
  1882. result.m[13] = 0;
  1883. result.m[14] = 0;
  1884. };
  1885. Matrix.RotationAxis = function (axis, angle) {
  1886. var s = Math.sin(-angle);
  1887. var c = Math.cos(-angle);
  1888. var c1 = 1 - c;
  1889. axis.normalize();
  1890. var result = Matrix.Zero();
  1891. result.m[0] = (axis.x * axis.x) * c1 + c;
  1892. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1893. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1894. result.m[3] = 0.0;
  1895. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1896. result.m[5] = (axis.y * axis.y) * c1 + c;
  1897. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1898. result.m[7] = 0.0;
  1899. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1900. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1901. result.m[10] = (axis.z * axis.z) * c1 + c;
  1902. result.m[11] = 0.0;
  1903. result.m[15] = 1.0;
  1904. return result;
  1905. };
  1906. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1907. var result = new Matrix();
  1908. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1909. return result;
  1910. };
  1911. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1912. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1913. this._tempQuaternion.toRotationMatrix(result);
  1914. };
  1915. Matrix.Scaling = function (x, y, z) {
  1916. var result = Matrix.Zero();
  1917. Matrix.ScalingToRef(x, y, z, result);
  1918. return result;
  1919. };
  1920. Matrix.ScalingToRef = function (x, y, z, result) {
  1921. result.m[0] = x;
  1922. result.m[1] = 0;
  1923. result.m[2] = 0;
  1924. result.m[3] = 0;
  1925. result.m[4] = 0;
  1926. result.m[5] = y;
  1927. result.m[6] = 0;
  1928. result.m[7] = 0;
  1929. result.m[8] = 0;
  1930. result.m[9] = 0;
  1931. result.m[10] = z;
  1932. result.m[11] = 0;
  1933. result.m[12] = 0;
  1934. result.m[13] = 0;
  1935. result.m[14] = 0;
  1936. result.m[15] = 1.0;
  1937. };
  1938. Matrix.Translation = function (x, y, z) {
  1939. var result = Matrix.Identity();
  1940. Matrix.TranslationToRef(x, y, z, result);
  1941. return result;
  1942. };
  1943. Matrix.TranslationToRef = function (x, y, z, result) {
  1944. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  1945. };
  1946. Matrix.LookAtLH = function (eye, target, up) {
  1947. var result = Matrix.Zero();
  1948. Matrix.LookAtLHToRef(eye, target, up, result);
  1949. return result;
  1950. };
  1951. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1952. // Z axis
  1953. target.subtractToRef(eye, this._zAxis);
  1954. this._zAxis.normalize();
  1955. // X axis
  1956. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1957. this._xAxis.normalize();
  1958. // Y axis
  1959. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1960. this._yAxis.normalize();
  1961. // Eye angles
  1962. var ex = -Vector3.Dot(this._xAxis, eye);
  1963. var ey = -Vector3.Dot(this._yAxis, eye);
  1964. var ez = -Vector3.Dot(this._zAxis, eye);
  1965. 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);
  1966. };
  1967. Matrix.LookAtLHToRefSIMD = function (eyeRef, targetRef, upRef, result) {
  1968. var out = result.m;
  1969. var center = SIMD.float32x4(targetRef.x, targetRef.y, targetRef.z, 0);
  1970. var eye = SIMD.float32x4(eyeRef.x, eyeRef.y, eyeRef.z, 0);
  1971. var up = SIMD.float32x4(upRef.x, upRef.y, upRef.z, 0);
  1972. // cc.kmVec3Subtract(f, pCenter, pEye);
  1973. var f = SIMD.float32x4.sub(center, eye);
  1974. // cc.kmVec3Normalize(f, f);
  1975. var tmp = SIMD.float32x4.mul(f, f);
  1976. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1977. f = SIMD.float32x4.mul(f, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1978. // cc.kmVec3Assign(up, pUp);
  1979. // cc.kmVec3Normalize(up, up);
  1980. tmp = SIMD.float32x4.mul(up, up);
  1981. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1982. up = SIMD.float32x4.mul(up, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1983. // cc.kmVec3Cross(s, f, up);
  1984. var s = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 1, 2, 0, 3), SIMD.float32x4.swizzle(up, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 2, 0, 1, 3), SIMD.float32x4.swizzle(up, 1, 2, 0, 3)));
  1985. // cc.kmVec3Normalize(s, s);
  1986. tmp = SIMD.float32x4.mul(s, s);
  1987. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1988. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1989. // cc.kmVec3Cross(u, s, f);
  1990. var u = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 1, 2, 0, 3), SIMD.float32x4.swizzle(f, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 2, 0, 1, 3), SIMD.float32x4.swizzle(f, 1, 2, 0, 3)));
  1991. // cc.kmVec3Normalize(s, s);
  1992. tmp = SIMD.float32x4.mul(s, s);
  1993. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1994. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1995. var zero = SIMD.float32x4.splat(0.0);
  1996. s = SIMD.float32x4.neg(s);
  1997. var tmp01 = SIMD.float32x4.shuffle(s, u, 0, 1, 4, 5);
  1998. var tmp23 = SIMD.float32x4.shuffle(f, zero, 0, 1, 4, 5);
  1999. var a0 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  2000. var a1 = SIMD.float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7);
  2001. tmp01 = SIMD.float32x4.shuffle(s, u, 2, 3, 6, 7);
  2002. tmp23 = SIMD.float32x4.shuffle(f, zero, 2, 3, 6, 7);
  2003. var a2 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  2004. var a3 = SIMD.float32x4(0.0, 0.0, 0.0, 1.0);
  2005. var b0 = SIMD.float32x4(1.0, 0.0, 0.0, 0.0);
  2006. var b1 = SIMD.float32x4(0.0, 1.0, 0.0, 0.0);
  2007. var b2 = SIMD.float32x4(0.0, 0.0, 1.0, 0.0);
  2008. var b3 = SIMD.float32x4.neg(eye);
  2009. b3 = SIMD.float32x4.withW(b3, 1.0);
  2010. SIMD.float32x4.store(out, 0, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 3, 3, 3, 3), a3)))));
  2011. SIMD.float32x4.store(out, 4, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 3, 3, 3, 3), a3)))));
  2012. SIMD.float32x4.store(out, 8, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 3, 3, 3, 3), a3)))));
  2013. SIMD.float32x4.store(out, 12, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 3, 3, 3, 3), a3)))));
  2014. };
  2015. Matrix.OrthoLH = function (width, height, znear, zfar) {
  2016. var matrix = Matrix.Zero();
  2017. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  2018. return matrix;
  2019. };
  2020. Matrix.OrthoLHToRef = function (width, height, znear, zfar, result) {
  2021. var hw = 2.0 / width;
  2022. var hh = 2.0 / height;
  2023. var id = 1.0 / (zfar - znear);
  2024. var nid = znear / (znear - zfar);
  2025. Matrix.FromValuesToRef(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1, result);
  2026. };
  2027. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  2028. var matrix = Matrix.Zero();
  2029. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  2030. return matrix;
  2031. };
  2032. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  2033. result.m[0] = 2.0 / (right - left);
  2034. result.m[1] = result.m[2] = result.m[3] = 0;
  2035. result.m[5] = 2.0 / (top - bottom);
  2036. result.m[4] = result.m[6] = result.m[7] = 0;
  2037. result.m[10] = -1.0 / (znear - zfar);
  2038. result.m[8] = result.m[9] = result.m[11] = 0;
  2039. result.m[12] = (left + right) / (left - right);
  2040. result.m[13] = (top + bottom) / (bottom - top);
  2041. result.m[14] = znear / (znear - zfar);
  2042. result.m[15] = 1.0;
  2043. };
  2044. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  2045. var matrix = Matrix.Zero();
  2046. matrix.m[0] = (2.0 * znear) / width;
  2047. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  2048. matrix.m[5] = (2.0 * znear) / height;
  2049. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  2050. matrix.m[10] = -zfar / (znear - zfar);
  2051. matrix.m[8] = matrix.m[9] = 0.0;
  2052. matrix.m[11] = 1.0;
  2053. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  2054. matrix.m[14] = (znear * zfar) / (znear - zfar);
  2055. return matrix;
  2056. };
  2057. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  2058. var matrix = Matrix.Zero();
  2059. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  2060. return matrix;
  2061. };
  2062. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result, fovMode) {
  2063. if (fovMode === void 0) { fovMode = BABYLON.Camera.FOVMODE_VERTICAL_FIXED; }
  2064. var tan = 1.0 / (Math.tan(fov * 0.5));
  2065. var v_fixed = (fovMode === BABYLON.Camera.FOVMODE_VERTICAL_FIXED);
  2066. if (v_fixed) {
  2067. result.m[0] = tan / aspect;
  2068. }
  2069. else {
  2070. result.m[0] = tan;
  2071. }
  2072. result.m[1] = result.m[2] = result.m[3] = 0.0;
  2073. if (v_fixed) {
  2074. result.m[5] = tan;
  2075. }
  2076. else {
  2077. result.m[5] = tan * aspect;
  2078. }
  2079. result.m[4] = result.m[6] = result.m[7] = 0.0;
  2080. result.m[8] = result.m[9] = 0.0;
  2081. result.m[10] = -zfar / (znear - zfar);
  2082. result.m[11] = 1.0;
  2083. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2084. result.m[14] = (znear * zfar) / (znear - zfar);
  2085. };
  2086. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  2087. var cw = viewport.width;
  2088. var ch = viewport.height;
  2089. var cx = viewport.x;
  2090. var cy = viewport.y;
  2091. 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);
  2092. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  2093. };
  2094. Matrix.Transpose = function (matrix) {
  2095. var result = new Matrix();
  2096. result.m[0] = matrix.m[0];
  2097. result.m[1] = matrix.m[4];
  2098. result.m[2] = matrix.m[8];
  2099. result.m[3] = matrix.m[12];
  2100. result.m[4] = matrix.m[1];
  2101. result.m[5] = matrix.m[5];
  2102. result.m[6] = matrix.m[9];
  2103. result.m[7] = matrix.m[13];
  2104. result.m[8] = matrix.m[2];
  2105. result.m[9] = matrix.m[6];
  2106. result.m[10] = matrix.m[10];
  2107. result.m[11] = matrix.m[14];
  2108. result.m[12] = matrix.m[3];
  2109. result.m[13] = matrix.m[7];
  2110. result.m[14] = matrix.m[11];
  2111. result.m[15] = matrix.m[15];
  2112. return result;
  2113. };
  2114. Matrix.Reflection = function (plane) {
  2115. var matrix = new Matrix();
  2116. Matrix.ReflectionToRef(plane, matrix);
  2117. return matrix;
  2118. };
  2119. Matrix.ReflectionToRef = function (plane, result) {
  2120. plane.normalize();
  2121. var x = plane.normal.x;
  2122. var y = plane.normal.y;
  2123. var z = plane.normal.z;
  2124. var temp = -2 * x;
  2125. var temp2 = -2 * y;
  2126. var temp3 = -2 * z;
  2127. result.m[0] = (temp * x) + 1;
  2128. result.m[1] = temp2 * x;
  2129. result.m[2] = temp3 * x;
  2130. result.m[3] = 0.0;
  2131. result.m[4] = temp * y;
  2132. result.m[5] = (temp2 * y) + 1;
  2133. result.m[6] = temp3 * y;
  2134. result.m[7] = 0.0;
  2135. result.m[8] = temp * z;
  2136. result.m[9] = temp2 * z;
  2137. result.m[10] = (temp3 * z) + 1;
  2138. result.m[11] = 0.0;
  2139. result.m[12] = temp * plane.d;
  2140. result.m[13] = temp2 * plane.d;
  2141. result.m[14] = temp3 * plane.d;
  2142. result.m[15] = 1.0;
  2143. };
  2144. Matrix._tempQuaternion = new Quaternion();
  2145. Matrix._xAxis = Vector3.Zero();
  2146. Matrix._yAxis = Vector3.Zero();
  2147. Matrix._zAxis = Vector3.Zero();
  2148. return Matrix;
  2149. })();
  2150. BABYLON.Matrix = Matrix;
  2151. var Plane = (function () {
  2152. function Plane(a, b, c, d) {
  2153. this.normal = new Vector3(a, b, c);
  2154. this.d = d;
  2155. }
  2156. Plane.prototype.asArray = function () {
  2157. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  2158. };
  2159. // Methods
  2160. Plane.prototype.clone = function () {
  2161. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  2162. };
  2163. Plane.prototype.normalize = function () {
  2164. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  2165. var magnitude = 0;
  2166. if (norm !== 0) {
  2167. magnitude = 1.0 / norm;
  2168. }
  2169. this.normal.x *= magnitude;
  2170. this.normal.y *= magnitude;
  2171. this.normal.z *= magnitude;
  2172. this.d *= magnitude;
  2173. return this;
  2174. };
  2175. Plane.prototype.transform = function (transformation) {
  2176. var transposedMatrix = Matrix.Transpose(transformation);
  2177. var x = this.normal.x;
  2178. var y = this.normal.y;
  2179. var z = this.normal.z;
  2180. var d = this.d;
  2181. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  2182. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  2183. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  2184. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  2185. return new Plane(normalX, normalY, normalZ, finalD);
  2186. };
  2187. Plane.prototype.dotCoordinate = function (point) {
  2188. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  2189. };
  2190. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  2191. var x1 = point2.x - point1.x;
  2192. var y1 = point2.y - point1.y;
  2193. var z1 = point2.z - point1.z;
  2194. var x2 = point3.x - point1.x;
  2195. var y2 = point3.y - point1.y;
  2196. var z2 = point3.z - point1.z;
  2197. var yz = (y1 * z2) - (z1 * y2);
  2198. var xz = (z1 * x2) - (x1 * z2);
  2199. var xy = (x1 * y2) - (y1 * x2);
  2200. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  2201. var invPyth;
  2202. if (pyth !== 0) {
  2203. invPyth = 1.0 / pyth;
  2204. }
  2205. else {
  2206. invPyth = 0;
  2207. }
  2208. this.normal.x = yz * invPyth;
  2209. this.normal.y = xz * invPyth;
  2210. this.normal.z = xy * invPyth;
  2211. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  2212. return this;
  2213. };
  2214. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  2215. var dot = Vector3.Dot(this.normal, direction);
  2216. return (dot <= epsilon);
  2217. };
  2218. Plane.prototype.signedDistanceTo = function (point) {
  2219. return Vector3.Dot(point, this.normal) + this.d;
  2220. };
  2221. // Statics
  2222. Plane.FromArray = function (array) {
  2223. return new Plane(array[0], array[1], array[2], array[3]);
  2224. };
  2225. Plane.FromPoints = function (point1, point2, point3) {
  2226. var result = new Plane(0, 0, 0, 0);
  2227. result.copyFromPoints(point1, point2, point3);
  2228. return result;
  2229. };
  2230. Plane.FromPositionAndNormal = function (origin, normal) {
  2231. var result = new Plane(0, 0, 0, 0);
  2232. normal.normalize();
  2233. result.normal = normal;
  2234. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2235. return result;
  2236. };
  2237. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  2238. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2239. return Vector3.Dot(point, normal) + d;
  2240. };
  2241. return Plane;
  2242. })();
  2243. BABYLON.Plane = Plane;
  2244. var Viewport = (function () {
  2245. function Viewport(x, y, width, height) {
  2246. this.x = x;
  2247. this.y = y;
  2248. this.width = width;
  2249. this.height = height;
  2250. }
  2251. Viewport.prototype.toGlobal = function (engine) {
  2252. var width = engine.getRenderWidth();
  2253. var height = engine.getRenderHeight();
  2254. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  2255. };
  2256. return Viewport;
  2257. })();
  2258. BABYLON.Viewport = Viewport;
  2259. var Frustum = (function () {
  2260. function Frustum() {
  2261. }
  2262. Frustum.GetPlanes = function (transform) {
  2263. var frustumPlanes = [];
  2264. for (var index = 0; index < 6; index++) {
  2265. frustumPlanes.push(new Plane(0, 0, 0, 0));
  2266. }
  2267. Frustum.GetPlanesToRef(transform, frustumPlanes);
  2268. return frustumPlanes;
  2269. };
  2270. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  2271. // Near
  2272. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  2273. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  2274. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  2275. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  2276. frustumPlanes[0].normalize();
  2277. // Far
  2278. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  2279. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  2280. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  2281. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  2282. frustumPlanes[1].normalize();
  2283. // Left
  2284. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  2285. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  2286. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  2287. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  2288. frustumPlanes[2].normalize();
  2289. // Right
  2290. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  2291. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  2292. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  2293. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  2294. frustumPlanes[3].normalize();
  2295. // Top
  2296. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  2297. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  2298. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  2299. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  2300. frustumPlanes[4].normalize();
  2301. // Bottom
  2302. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  2303. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  2304. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  2305. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  2306. frustumPlanes[5].normalize();
  2307. };
  2308. return Frustum;
  2309. })();
  2310. BABYLON.Frustum = Frustum;
  2311. var Ray = (function () {
  2312. function Ray(origin, direction, length) {
  2313. if (length === void 0) { length = Number.MAX_VALUE; }
  2314. this.origin = origin;
  2315. this.direction = direction;
  2316. this.length = length;
  2317. }
  2318. // Methods
  2319. Ray.prototype.intersectsBoxMinMax = function (minimum, maximum) {
  2320. var d = 0.0;
  2321. var maxValue = Number.MAX_VALUE;
  2322. if (Math.abs(this.direction.x) < 0.0000001) {
  2323. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  2324. return false;
  2325. }
  2326. }
  2327. else {
  2328. var inv = 1.0 / this.direction.x;
  2329. var min = (minimum.x - this.origin.x) * inv;
  2330. var max = (maximum.x - this.origin.x) * inv;
  2331. if (max === -Infinity) {
  2332. max = Infinity;
  2333. }
  2334. if (min > max) {
  2335. var temp = min;
  2336. min = max;
  2337. max = temp;
  2338. }
  2339. d = Math.max(min, d);
  2340. maxValue = Math.min(max, maxValue);
  2341. if (d > maxValue) {
  2342. return false;
  2343. }
  2344. }
  2345. if (Math.abs(this.direction.y) < 0.0000001) {
  2346. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  2347. return false;
  2348. }
  2349. }
  2350. else {
  2351. inv = 1.0 / this.direction.y;
  2352. min = (minimum.y - this.origin.y) * inv;
  2353. max = (maximum.y - this.origin.y) * inv;
  2354. if (max === -Infinity) {
  2355. max = Infinity;
  2356. }
  2357. if (min > max) {
  2358. temp = min;
  2359. min = max;
  2360. max = temp;
  2361. }
  2362. d = Math.max(min, d);
  2363. maxValue = Math.min(max, maxValue);
  2364. if (d > maxValue) {
  2365. return false;
  2366. }
  2367. }
  2368. if (Math.abs(this.direction.z) < 0.0000001) {
  2369. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  2370. return false;
  2371. }
  2372. }
  2373. else {
  2374. inv = 1.0 / this.direction.z;
  2375. min = (minimum.z - this.origin.z) * inv;
  2376. max = (maximum.z - this.origin.z) * inv;
  2377. if (max === -Infinity) {
  2378. max = Infinity;
  2379. }
  2380. if (min > max) {
  2381. temp = min;
  2382. min = max;
  2383. max = temp;
  2384. }
  2385. d = Math.max(min, d);
  2386. maxValue = Math.min(max, maxValue);
  2387. if (d > maxValue) {
  2388. return false;
  2389. }
  2390. }
  2391. return true;
  2392. };
  2393. Ray.prototype.intersectsBox = function (box) {
  2394. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  2395. };
  2396. Ray.prototype.intersectsSphere = function (sphere) {
  2397. var x = sphere.center.x - this.origin.x;
  2398. var y = sphere.center.y - this.origin.y;
  2399. var z = sphere.center.z - this.origin.z;
  2400. var pyth = (x * x) + (y * y) + (z * z);
  2401. var rr = sphere.radius * sphere.radius;
  2402. if (pyth <= rr) {
  2403. return true;
  2404. }
  2405. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  2406. if (dot < 0.0) {
  2407. return false;
  2408. }
  2409. var temp = pyth - (dot * dot);
  2410. return temp <= rr;
  2411. };
  2412. Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  2413. if (!this._edge1) {
  2414. this._edge1 = Vector3.Zero();
  2415. this._edge2 = Vector3.Zero();
  2416. this._pvec = Vector3.Zero();
  2417. this._tvec = Vector3.Zero();
  2418. this._qvec = Vector3.Zero();
  2419. }
  2420. vertex1.subtractToRef(vertex0, this._edge1);
  2421. vertex2.subtractToRef(vertex0, this._edge2);
  2422. Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  2423. var det = Vector3.Dot(this._edge1, this._pvec);
  2424. if (det === 0) {
  2425. return null;
  2426. }
  2427. var invdet = 1 / det;
  2428. this.origin.subtractToRef(vertex0, this._tvec);
  2429. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  2430. if (bu < 0 || bu > 1.0) {
  2431. return null;
  2432. }
  2433. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  2434. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  2435. if (bv < 0 || bu + bv > 1.0) {
  2436. return null;
  2437. }
  2438. //check if the distance is longer than the predefined length.
  2439. var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
  2440. if (distance > this.length) {
  2441. return null;
  2442. }
  2443. return new BABYLON.IntersectionInfo(bu, bv, distance);
  2444. };
  2445. // Statics
  2446. Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  2447. var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  2448. var end = Vector3.Unproject(new Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  2449. var direction = end.subtract(start);
  2450. direction.normalize();
  2451. return new Ray(start, direction);
  2452. };
  2453. /**
  2454. * 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
  2455. * transformed to the given world matrix.
  2456. * @param origin The origin point
  2457. * @param end The end point
  2458. * @param world a matrix to transform the ray to. Default is the identity matrix.
  2459. */
  2460. Ray.CreateNewFromTo = function (origin, end, world) {
  2461. if (world === void 0) { world = Matrix.Identity(); }
  2462. var direction = end.subtract(origin);
  2463. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  2464. direction.normalize();
  2465. return Ray.Transform(new Ray(origin, direction, length), world);
  2466. };
  2467. Ray.Transform = function (ray, matrix) {
  2468. var newOrigin = Vector3.TransformCoordinates(ray.origin, matrix);
  2469. var newDirection = Vector3.TransformNormal(ray.direction, matrix);
  2470. return new Ray(newOrigin, newDirection, ray.length);
  2471. };
  2472. return Ray;
  2473. })();
  2474. BABYLON.Ray = Ray;
  2475. (function (Space) {
  2476. Space[Space["LOCAL"] = 0] = "LOCAL";
  2477. Space[Space["WORLD"] = 1] = "WORLD";
  2478. })(BABYLON.Space || (BABYLON.Space = {}));
  2479. var Space = BABYLON.Space;
  2480. var Axis = (function () {
  2481. function Axis() {
  2482. }
  2483. Axis.X = new Vector3(1, 0, 0);
  2484. Axis.Y = new Vector3(0, 1, 0);
  2485. Axis.Z = new Vector3(0, 0, 1);
  2486. return Axis;
  2487. })();
  2488. BABYLON.Axis = Axis;
  2489. ;
  2490. var BezierCurve = (function () {
  2491. function BezierCurve() {
  2492. }
  2493. BezierCurve.interpolate = function (t, x1, y1, x2, y2) {
  2494. // Extract X (which is equal to time here)
  2495. var f0 = 1 - 3 * x2 + 3 * x1;
  2496. var f1 = 3 * x2 - 6 * x1;
  2497. var f2 = 3 * x1;
  2498. var refinedT = t;
  2499. for (var i = 0; i < 5; i++) {
  2500. var refinedT2 = refinedT * refinedT;
  2501. var refinedT3 = refinedT2 * refinedT;
  2502. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2503. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2504. refinedT -= (x - t) * slope;
  2505. refinedT = Math.min(1, Math.max(0, refinedT));
  2506. }
  2507. // Resolve cubic bezier for the given x
  2508. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 + 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 + Math.pow(refinedT, 3);
  2509. };
  2510. return BezierCurve;
  2511. })();
  2512. BABYLON.BezierCurve = BezierCurve;
  2513. (function (Orientation) {
  2514. Orientation[Orientation["CW"] = 0] = "CW";
  2515. Orientation[Orientation["CCW"] = 1] = "CCW";
  2516. })(BABYLON.Orientation || (BABYLON.Orientation = {}));
  2517. var Orientation = BABYLON.Orientation;
  2518. var Angle = (function () {
  2519. function Angle(radians) {
  2520. var _this = this;
  2521. this.degrees = function () { return _this._radians * 180 / Math.PI; };
  2522. this.radians = function () { return _this._radians; };
  2523. this._radians = radians;
  2524. if (this._radians < 0)
  2525. this._radians += (2 * Math.PI);
  2526. }
  2527. Angle.BetweenTwoPoints = function (a, b) {
  2528. var delta = b.subtract(a);
  2529. var theta = Math.atan2(delta.y, delta.x);
  2530. return new Angle(theta);
  2531. };
  2532. Angle.FromRadians = function (radians) {
  2533. return new Angle(radians);
  2534. };
  2535. Angle.FromDegrees = function (degrees) {
  2536. return new Angle(degrees * Math.PI / 180);
  2537. };
  2538. return Angle;
  2539. })();
  2540. BABYLON.Angle = Angle;
  2541. var Arc2 = (function () {
  2542. function Arc2(startPoint, midPoint, endPoint) {
  2543. this.startPoint = startPoint;
  2544. this.midPoint = midPoint;
  2545. this.endPoint = endPoint;
  2546. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2547. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2548. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2549. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2550. 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);
  2551. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2552. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2553. var a1 = this.startAngle.degrees();
  2554. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2555. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2556. // angles correction
  2557. if (a2 - a1 > +180.0)
  2558. a2 -= 360.0;
  2559. if (a2 - a1 < -180.0)
  2560. a2 += 360.0;
  2561. if (a3 - a2 > +180.0)
  2562. a3 -= 360.0;
  2563. if (a3 - a2 < -180.0)
  2564. a3 += 360.0;
  2565. this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
  2566. this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
  2567. }
  2568. return Arc2;
  2569. })();
  2570. BABYLON.Arc2 = Arc2;
  2571. var PathCursor = (function () {
  2572. function PathCursor(path) {
  2573. this.path = path;
  2574. this._onchange = new Array();
  2575. this.value = 0;
  2576. this.animations = new Array();
  2577. }
  2578. PathCursor.prototype.getPoint = function () {
  2579. var point = this.path.getPointAtLengthPosition(this.value);
  2580. return new Vector3(point.x, 0, point.y);
  2581. };
  2582. PathCursor.prototype.moveAhead = function (step) {
  2583. if (step === void 0) { step = 0.002; }
  2584. this.move(step);
  2585. return this;
  2586. };
  2587. PathCursor.prototype.moveBack = function (step) {
  2588. if (step === void 0) { step = 0.002; }
  2589. this.move(-step);
  2590. return this;
  2591. };
  2592. PathCursor.prototype.move = function (step) {
  2593. if (Math.abs(step) > 1) {
  2594. throw "step size should be less than 1.";
  2595. }
  2596. this.value += step;
  2597. this.ensureLimits();
  2598. this.raiseOnChange();
  2599. return this;
  2600. };
  2601. PathCursor.prototype.ensureLimits = function () {
  2602. while (this.value > 1) {
  2603. this.value -= 1;
  2604. }
  2605. while (this.value < 0) {
  2606. this.value += 1;
  2607. }
  2608. return this;
  2609. };
  2610. // used by animation engine
  2611. PathCursor.prototype.markAsDirty = function (propertyName) {
  2612. this.ensureLimits();
  2613. this.raiseOnChange();
  2614. return this;
  2615. };
  2616. PathCursor.prototype.raiseOnChange = function () {
  2617. var _this = this;
  2618. this._onchange.forEach(function (f) { return f(_this); });
  2619. return this;
  2620. };
  2621. PathCursor.prototype.onchange = function (f) {
  2622. this._onchange.push(f);
  2623. return this;
  2624. };
  2625. return PathCursor;
  2626. })();
  2627. BABYLON.PathCursor = PathCursor;
  2628. var Path2 = (function () {
  2629. function Path2(x, y) {
  2630. this._points = new Array();
  2631. this._length = 0;
  2632. this.closed = false;
  2633. this._points.push(new Vector2(x, y));
  2634. }
  2635. Path2.prototype.addLineTo = function (x, y) {
  2636. if (closed) {
  2637. BABYLON.Tools.Error("cannot add lines to closed paths");
  2638. return this;
  2639. }
  2640. var newPoint = new Vector2(x, y);
  2641. var previousPoint = this._points[this._points.length - 1];
  2642. this._points.push(newPoint);
  2643. this._length += newPoint.subtract(previousPoint).length();
  2644. return this;
  2645. };
  2646. Path2.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
  2647. if (numberOfSegments === void 0) { numberOfSegments = 36; }
  2648. if (closed) {
  2649. BABYLON.Tools.Error("cannot add arcs to closed paths");
  2650. return this;
  2651. }
  2652. var startPoint = this._points[this._points.length - 1];
  2653. var midPoint = new Vector2(midX, midY);
  2654. var endPoint = new Vector2(endX, endY);
  2655. var arc = new Arc2(startPoint, midPoint, endPoint);
  2656. var increment = arc.angle.radians() / numberOfSegments;
  2657. if (arc.orientation === 0 /* CW */)
  2658. increment *= -1;
  2659. var currentAngle = arc.startAngle.radians() + increment;
  2660. for (var i = 0; i < numberOfSegments; i++) {
  2661. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2662. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2663. this.addLineTo(x, y);
  2664. currentAngle += increment;
  2665. }
  2666. return this;
  2667. };
  2668. Path2.prototype.close = function () {
  2669. this.closed = true;
  2670. return this;
  2671. };
  2672. Path2.prototype.length = function () {
  2673. var result = this._length;
  2674. if (!this.closed) {
  2675. var lastPoint = this._points[this._points.length - 1];
  2676. var firstPoint = this._points[0];
  2677. result += (firstPoint.subtract(lastPoint).length());
  2678. }
  2679. return result;
  2680. };
  2681. Path2.prototype.getPoints = function () {
  2682. return this._points;
  2683. };
  2684. Path2.prototype.getPointAtLengthPosition = function (normalizedLengthPosition) {
  2685. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2686. BABYLON.Tools.Error("normalized length position should be between 0 and 1.");
  2687. return Vector2.Zero();
  2688. }
  2689. var lengthPosition = normalizedLengthPosition * this.length();
  2690. var previousOffset = 0;
  2691. for (var i = 0; i < this._points.length; i++) {
  2692. var j = (i + 1) % this._points.length;
  2693. var a = this._points[i];
  2694. var b = this._points[j];
  2695. var bToA = b.subtract(a);
  2696. var nextOffset = (bToA.length() + previousOffset);
  2697. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2698. var dir = bToA.normalize();
  2699. var localOffset = lengthPosition - previousOffset;
  2700. return new Vector2(a.x + (dir.x * localOffset), a.y + (dir.y * localOffset));
  2701. }
  2702. previousOffset = nextOffset;
  2703. }
  2704. BABYLON.Tools.Error("internal error");
  2705. return Vector2.Zero();
  2706. };
  2707. Path2.StartingAt = function (x, y) {
  2708. return new Path2(x, y);
  2709. };
  2710. return Path2;
  2711. })();
  2712. BABYLON.Path2 = Path2;
  2713. var Path3D = (function () {
  2714. function Path3D(path, firstNormal) {
  2715. this.path = path;
  2716. this._curve = new Array();
  2717. this._distances = new Array();
  2718. this._tangents = new Array();
  2719. this._normals = new Array();
  2720. this._binormals = new Array();
  2721. for (var p = 0; p < path.length; p++) {
  2722. this._curve[p] = path[p].clone(); // hard copy
  2723. }
  2724. this._compute(firstNormal);
  2725. }
  2726. Path3D.prototype.getCurve = function () {
  2727. return this._curve;
  2728. };
  2729. Path3D.prototype.getTangents = function () {
  2730. return this._tangents;
  2731. };
  2732. Path3D.prototype.getNormals = function () {
  2733. return this._normals;
  2734. };
  2735. Path3D.prototype.getBinormals = function () {
  2736. return this._binormals;
  2737. };
  2738. Path3D.prototype.getDistances = function () {
  2739. return this._distances;
  2740. };
  2741. Path3D.prototype.update = function (path, firstNormal) {
  2742. for (var p = 0; p < path.length; p++) {
  2743. this._curve[p].x = path[p].x;
  2744. this._curve[p].y = path[p].y;
  2745. this._curve[p].z = path[p].z;
  2746. }
  2747. this._compute(firstNormal);
  2748. return this;
  2749. };
  2750. // private function compute() : computes tangents, normals and binormals
  2751. Path3D.prototype._compute = function (firstNormal) {
  2752. var l = this._curve.length;
  2753. // first and last tangents
  2754. this._tangents[0] = this._getFirstNonNullVector(0);
  2755. this._tangents[0].normalize();
  2756. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  2757. this._tangents[l - 1].normalize();
  2758. // normals and binormals at first point : arbitrary vector with _normalVector()
  2759. var tg0 = this._tangents[0];
  2760. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  2761. this._normals[0] = pp0;
  2762. this._normals[0].normalize();
  2763. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  2764. this._binormals[0].normalize();
  2765. this._distances[0] = 0;
  2766. // normals and binormals : next points
  2767. var prev; // previous vector (segment)
  2768. var cur; // current vector (segment)
  2769. var curTang; // current tangent
  2770. var prevNorm; // previous normal
  2771. var prevBinor; // previous binormal
  2772. for (var i = 1; i < l; i++) {
  2773. // tangents
  2774. prev = this._getLastNonNullVector(i);
  2775. if (i < l - 1) {
  2776. cur = this._getFirstNonNullVector(i);
  2777. this._tangents[i] = prev.add(cur);
  2778. this._tangents[i].normalize();
  2779. }
  2780. this._distances[i] = this._distances[i - 1] + prev.length();
  2781. // normals and binormals
  2782. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  2783. curTang = this._tangents[i];
  2784. prevNorm = this._normals[i - 1];
  2785. prevBinor = this._binormals[i - 1];
  2786. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  2787. this._normals[i].normalize();
  2788. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  2789. this._binormals[i].normalize();
  2790. }
  2791. };
  2792. // private function getFirstNonNullVector(index)
  2793. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  2794. Path3D.prototype._getFirstNonNullVector = function (index) {
  2795. var i = 1;
  2796. var nNVector = this._curve[index + i].subtract(this._curve[index]);
  2797. while (nNVector.length() == 0 && index + i + 1 < this._curve.length) {
  2798. i++;
  2799. nNVector = this._curve[index + i].subtract(this._curve[index]);
  2800. }
  2801. return nNVector;
  2802. };
  2803. // private function getLastNonNullVector(index)
  2804. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  2805. Path3D.prototype._getLastNonNullVector = function (index) {
  2806. var i = 1;
  2807. var nLVector = this._curve[index].subtract(this._curve[index - i]);
  2808. while (nLVector.length() == 0 && index > i + 1) {
  2809. i++;
  2810. nLVector = this._curve[index].subtract(this._curve[index - i]);
  2811. }
  2812. return nLVector;
  2813. };
  2814. // private function normalVector(v0, vt, va) :
  2815. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  2816. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  2817. Path3D.prototype._normalVector = function (v0, vt, va) {
  2818. var normal0;
  2819. if (va === undefined || va === null) {
  2820. var point;
  2821. if (vt.y !== 1) {
  2822. point = new Vector3(0, -1, 0);
  2823. }
  2824. else if (vt.x !== 1) {
  2825. point = new Vector3(1, 0, 0);
  2826. }
  2827. else if (vt.z !== 1) {
  2828. point = new Vector3(0, 0, 1);
  2829. }
  2830. normal0 = Vector3.Cross(vt, point);
  2831. }
  2832. else {
  2833. normal0 = Vector3.Cross(vt, va);
  2834. Vector3.CrossToRef(normal0, vt, normal0);
  2835. }
  2836. normal0.normalize();
  2837. return normal0;
  2838. };
  2839. return Path3D;
  2840. })();
  2841. BABYLON.Path3D = Path3D;
  2842. var Curve3 = (function () {
  2843. function Curve3(points) {
  2844. this._length = 0;
  2845. this._points = points;
  2846. this._length = this._computeLength(points);
  2847. }
  2848. // QuadraticBezier(origin_V3, control_V3, destination_V3, nbPoints)
  2849. Curve3.CreateQuadraticBezier = function (v0, v1, v2, nbPoints) {
  2850. nbPoints = nbPoints > 2 ? nbPoints : 3;
  2851. var bez = new Array();
  2852. var equation = function (t, val0, val1, val2) {
  2853. var res = (1 - t) * (1 - t) * val0 + 2 * t * (1 - t) * val1 + t * t * val2;
  2854. return res;
  2855. };
  2856. for (var i = 0; i <= nbPoints; i++) {
  2857. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x), equation(i / nbPoints, v0.y, v1.y, v2.y), equation(i / nbPoints, v0.z, v1.z, v2.z)));
  2858. }
  2859. return new Curve3(bez);
  2860. };
  2861. // CubicBezier(origin_V3, control1_V3, control2_V3, destination_V3, nbPoints)
  2862. Curve3.CreateCubicBezier = function (v0, v1, v2, v3, nbPoints) {
  2863. nbPoints = nbPoints > 3 ? nbPoints : 4;
  2864. var bez = new Array();
  2865. var equation = function (t, val0, val1, val2, val3) {
  2866. var res = (1 - t) * (1 - t) * (1 - t) * val0 + 3 * t * (1 - t) * (1 - t) * val1 + 3 * t * t * (1 - t) * val2 + t * t * t * val3;
  2867. return res;
  2868. };
  2869. for (var i = 0; i <= nbPoints; i++) {
  2870. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x, v3.x), equation(i / nbPoints, v0.y, v1.y, v2.y, v3.y), equation(i / nbPoints, v0.z, v1.z, v2.z, v3.z)));
  2871. }
  2872. return new Curve3(bez);
  2873. };
  2874. // HermiteSpline(origin_V3, originTangent_V3, destination_V3, destinationTangent_V3, nbPoints)
  2875. Curve3.CreateHermiteSpline = function (p1, t1, p2, t2, nbPoints) {
  2876. var hermite = new Array();
  2877. var step = 1 / nbPoints;
  2878. for (var i = 0; i <= nbPoints; i++) {
  2879. hermite.push(BABYLON.Vector3.Hermite(p1, t1, p2, t2, i * step));
  2880. }
  2881. return new Curve3(hermite);
  2882. };
  2883. Curve3.prototype.getPoints = function () {
  2884. return this._points;
  2885. };
  2886. Curve3.prototype.length = function () {
  2887. return this._length;
  2888. };
  2889. Curve3.prototype.continue = function (curve) {
  2890. var lastPoint = this._points[this._points.length - 1];
  2891. var continuedPoints = this._points.slice();
  2892. var curvePoints = curve.getPoints();
  2893. for (var i = 1; i < curvePoints.length; i++) {
  2894. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  2895. }
  2896. var continuedCurve = new Curve3(continuedPoints);
  2897. return continuedCurve;
  2898. };
  2899. Curve3.prototype._computeLength = function (path) {
  2900. var l = 0;
  2901. for (var i = 1; i < path.length; i++) {
  2902. l += (path[i].subtract(path[i - 1])).length();
  2903. }
  2904. return l;
  2905. };
  2906. return Curve3;
  2907. })();
  2908. BABYLON.Curve3 = Curve3;
  2909. // Vertex formats
  2910. var PositionNormalVertex = (function () {
  2911. function PositionNormalVertex(position, normal) {
  2912. if (position === void 0) { position = Vector3.Zero(); }
  2913. if (normal === void 0) { normal = Vector3.Up(); }
  2914. this.position = position;
  2915. this.normal = normal;
  2916. }
  2917. PositionNormalVertex.prototype.clone = function () {
  2918. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  2919. };
  2920. return PositionNormalVertex;
  2921. })();
  2922. BABYLON.PositionNormalVertex = PositionNormalVertex;
  2923. var PositionNormalTextureVertex = (function () {
  2924. function PositionNormalTextureVertex(position, normal, uv) {
  2925. if (position === void 0) { position = Vector3.Zero(); }
  2926. if (normal === void 0) { normal = Vector3.Up(); }
  2927. if (uv === void 0) { uv = Vector2.Zero(); }
  2928. this.position = position;
  2929. this.normal = normal;
  2930. this.uv = uv;
  2931. }
  2932. PositionNormalTextureVertex.prototype.clone = function () {
  2933. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  2934. };
  2935. return PositionNormalTextureVertex;
  2936. })();
  2937. BABYLON.PositionNormalTextureVertex = PositionNormalTextureVertex;
  2938. // SIMD
  2939. var previousMultiplyToArray = Matrix.prototype.multiplyToArray;
  2940. var previousInvertToRef = Matrix.prototype.invertToRef;
  2941. var previousLookAtLHToRef = Matrix.LookAtLHToRef;
  2942. var previousTransformCoordinatesToRef = Vector3.TransformCoordinatesToRef;
  2943. var previousTransformCoordinatesFromFloatsToRef = Vector3.TransformCoordinatesFromFloatsToRef;
  2944. var SIMDHelper = (function () {
  2945. function SIMDHelper() {
  2946. }
  2947. Object.defineProperty(SIMDHelper, "IsEnabled", {
  2948. get: function () {
  2949. return SIMDHelper._isEnabled;
  2950. },
  2951. enumerable: true,
  2952. configurable: true
  2953. });
  2954. SIMDHelper.DisableSIMD = function () {
  2955. // Replace functions
  2956. Matrix.prototype.multiplyToArray = previousMultiplyToArray;
  2957. Matrix.prototype.invertToRef = previousInvertToRef;
  2958. Matrix.LookAtLHToRef = previousLookAtLHToRef;
  2959. Vector3.TransformCoordinatesToRef = previousTransformCoordinatesToRef;
  2960. Vector3.TransformCoordinatesFromFloatsToRef = previousTransformCoordinatesFromFloatsToRef;
  2961. SIMDHelper._isEnabled = false;
  2962. };
  2963. SIMDHelper.EnableSIMD = function () {
  2964. if (window.SIMD === undefined) {
  2965. return;
  2966. }
  2967. // Replace functions
  2968. Matrix.prototype.multiplyToArray = Matrix.prototype.multiplyToArraySIMD;
  2969. Matrix.prototype.invertToRef = Matrix.prototype.invertToRefSIMD;
  2970. Matrix.LookAtLHToRef = Matrix.LookAtLHToRefSIMD;
  2971. Vector3.TransformCoordinatesToRef = Vector3.TransformCoordinatesToRefSIMD;
  2972. Vector3.TransformCoordinatesFromFloatsToRef = Vector3.TransformCoordinatesFromFloatsToRefSIMD;
  2973. Object.defineProperty(BABYLON.Vector3.prototype, "x", {
  2974. get: function () {
  2975. return this._data[0];
  2976. },
  2977. set: function (value) {
  2978. if (!this._data) {
  2979. this._data = new Float32Array(3);
  2980. }
  2981. this._data[0] = value;
  2982. }
  2983. });
  2984. Object.defineProperty(BABYLON.Vector3.prototype, "y", {
  2985. get: function () {
  2986. return this._data[1];
  2987. },
  2988. set: function (value) {
  2989. this._data[1] = value;
  2990. }
  2991. });
  2992. Object.defineProperty(BABYLON.Vector3.prototype, "z", {
  2993. get: function () {
  2994. return this._data[2];
  2995. },
  2996. set: function (value) {
  2997. this._data[2] = value;
  2998. }
  2999. });
  3000. SIMDHelper._isEnabled = true;
  3001. };
  3002. SIMDHelper._isEnabled = false;
  3003. return SIMDHelper;
  3004. })();
  3005. BABYLON.SIMDHelper = SIMDHelper;
  3006. if (window.SIMD !== undefined && window.SIMD.float32x4 && window.SIMD.float32x4.swizzle) {
  3007. SIMDHelper.EnableSIMD();
  3008. }
  3009. })(BABYLON || (BABYLON = {}));
  3010. //# sourceMappingURL=babylon.math.js.map