babylon.math.js 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009
  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. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1205. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1206. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1207. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1208. return this;
  1209. };
  1210. Quaternion.prototype.length = function () {
  1211. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1212. };
  1213. Quaternion.prototype.normalize = function () {
  1214. var length = 1.0 / this.length();
  1215. this.x *= length;
  1216. this.y *= length;
  1217. this.z *= length;
  1218. this.w *= length;
  1219. return this;
  1220. };
  1221. Quaternion.prototype.toEulerAngles = function () {
  1222. var result = Vector3.Zero();
  1223. this.toEulerAnglesToRef(result);
  1224. return result;
  1225. };
  1226. Quaternion.prototype.toEulerAnglesToRef = function (result) {
  1227. //result is an EulerAngles in the in the z-x-z convention
  1228. var qx = this.x;
  1229. var qy = this.y;
  1230. var qz = this.z;
  1231. var qw = this.w;
  1232. var qxy = qx * qy;
  1233. var qxz = qx * qz;
  1234. var qwy = qw * qy;
  1235. var qwz = qw * qz;
  1236. var qwx = qw * qx;
  1237. var qyz = qy * qz;
  1238. var sqx = qx * qx;
  1239. var sqy = qy * qy;
  1240. var determinant = sqx + sqy;
  1241. if (determinant !== 0.000 && determinant !== 1.000) {
  1242. result.x = Math.atan2(qxz + qwy, qwx - qyz);
  1243. result.y = Math.acos(1 - 2 * determinant);
  1244. result.z = Math.atan2(qxz - qwy, qwx + qyz);
  1245. }
  1246. else {
  1247. if (determinant === 0.0) {
  1248. result.x = 0.0;
  1249. result.y = 0.0;
  1250. 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)
  1251. }
  1252. else {
  1253. 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)
  1254. result.y = Math.PI;
  1255. result.z = 0.0;
  1256. }
  1257. }
  1258. return this;
  1259. };
  1260. Quaternion.prototype.toRotationMatrix = function (result) {
  1261. var xx = this.x * this.x;
  1262. var yy = this.y * this.y;
  1263. var zz = this.z * this.z;
  1264. var xy = this.x * this.y;
  1265. var zw = this.z * this.w;
  1266. var zx = this.z * this.x;
  1267. var yw = this.y * this.w;
  1268. var yz = this.y * this.z;
  1269. var xw = this.x * this.w;
  1270. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1271. result.m[1] = 2.0 * (xy + zw);
  1272. result.m[2] = 2.0 * (zx - yw);
  1273. result.m[3] = 0;
  1274. result.m[4] = 2.0 * (xy - zw);
  1275. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1276. result.m[6] = 2.0 * (yz + xw);
  1277. result.m[7] = 0;
  1278. result.m[8] = 2.0 * (zx + yw);
  1279. result.m[9] = 2.0 * (yz - xw);
  1280. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1281. result.m[11] = 0;
  1282. result.m[12] = 0;
  1283. result.m[13] = 0;
  1284. result.m[14] = 0;
  1285. result.m[15] = 1.0;
  1286. return this;
  1287. };
  1288. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  1289. Quaternion.FromRotationMatrixToRef(matrix, this);
  1290. return this;
  1291. };
  1292. // Statics
  1293. Quaternion.FromRotationMatrix = function (matrix) {
  1294. var result = new Quaternion();
  1295. Quaternion.FromRotationMatrixToRef(matrix, result);
  1296. return result;
  1297. };
  1298. Quaternion.FromRotationMatrixToRef = function (matrix, result) {
  1299. var data = matrix.m;
  1300. var m11 = data[0], m12 = data[4], m13 = data[8];
  1301. var m21 = data[1], m22 = data[5], m23 = data[9];
  1302. var m31 = data[2], m32 = data[6], m33 = data[10];
  1303. var trace = m11 + m22 + m33;
  1304. var s;
  1305. if (trace > 0) {
  1306. s = 0.5 / Math.sqrt(trace + 1.0);
  1307. result.w = 0.25 / s;
  1308. result.x = (m32 - m23) * s;
  1309. result.y = (m13 - m31) * s;
  1310. result.z = (m21 - m12) * s;
  1311. }
  1312. else if (m11 > m22 && m11 > m33) {
  1313. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1314. result.w = (m32 - m23) / s;
  1315. result.x = 0.25 * s;
  1316. result.y = (m12 + m21) / s;
  1317. result.z = (m13 + m31) / s;
  1318. }
  1319. else if (m22 > m33) {
  1320. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1321. result.w = (m13 - m31) / s;
  1322. result.x = (m12 + m21) / s;
  1323. result.y = 0.25 * s;
  1324. result.z = (m23 + m32) / s;
  1325. }
  1326. else {
  1327. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1328. result.w = (m21 - m12) / s;
  1329. result.x = (m13 + m31) / s;
  1330. result.y = (m23 + m32) / s;
  1331. result.z = 0.25 * s;
  1332. }
  1333. };
  1334. Quaternion.Inverse = function (q) {
  1335. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1336. };
  1337. Quaternion.Identity = function () {
  1338. return new Quaternion(0, 0, 0, 1);
  1339. };
  1340. Quaternion.RotationAxis = function (axis, angle) {
  1341. var result = new Quaternion();
  1342. var sin = Math.sin(angle / 2);
  1343. result.w = Math.cos(angle / 2);
  1344. result.x = axis.x * sin;
  1345. result.y = axis.y * sin;
  1346. result.z = axis.z * sin;
  1347. return result;
  1348. };
  1349. Quaternion.FromArray = function (array, offset) {
  1350. if (!offset) {
  1351. offset = 0;
  1352. }
  1353. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1354. };
  1355. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1356. var result = new Quaternion();
  1357. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1358. return result;
  1359. };
  1360. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1361. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  1362. var halfRoll = roll * 0.5;
  1363. var halfPitch = pitch * 0.5;
  1364. var halfYaw = yaw * 0.5;
  1365. var sinRoll = Math.sin(halfRoll);
  1366. var cosRoll = Math.cos(halfRoll);
  1367. var sinPitch = Math.sin(halfPitch);
  1368. var cosPitch = Math.cos(halfPitch);
  1369. var sinYaw = Math.sin(halfYaw);
  1370. var cosYaw = Math.cos(halfYaw);
  1371. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1372. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1373. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1374. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1375. };
  1376. Quaternion.RotationAlphaBetaGamma = function (alpha, beta, gamma) {
  1377. var result = new Quaternion();
  1378. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  1379. return result;
  1380. };
  1381. Quaternion.RotationAlphaBetaGammaToRef = function (alpha, beta, gamma, result) {
  1382. // Produces a quaternion from Euler angles in the z-x-z orientation
  1383. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  1384. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  1385. var halfBeta = beta * 0.5;
  1386. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1387. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1388. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1389. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1390. };
  1391. Quaternion.Slerp = function (left, right, amount) {
  1392. var num2;
  1393. var num3;
  1394. var num = amount;
  1395. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1396. var flag = false;
  1397. if (num4 < 0) {
  1398. flag = true;
  1399. num4 = -num4;
  1400. }
  1401. if (num4 > 0.999999) {
  1402. num3 = 1 - num;
  1403. num2 = flag ? -num : num;
  1404. }
  1405. else {
  1406. var num5 = Math.acos(num4);
  1407. var num6 = (1.0 / Math.sin(num5));
  1408. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1409. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1410. }
  1411. 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));
  1412. };
  1413. return Quaternion;
  1414. })();
  1415. BABYLON.Quaternion = Quaternion;
  1416. var Matrix = (function () {
  1417. function Matrix() {
  1418. this.m = new Float32Array(16);
  1419. }
  1420. // Properties
  1421. Matrix.prototype.isIdentity = function () {
  1422. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1423. return false;
  1424. 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)
  1425. return false;
  1426. return true;
  1427. };
  1428. Matrix.prototype.determinant = function () {
  1429. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1430. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1431. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1432. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1433. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1434. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1435. 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))));
  1436. };
  1437. // Methods
  1438. Matrix.prototype.toArray = function () {
  1439. return this.m;
  1440. };
  1441. Matrix.prototype.asArray = function () {
  1442. return this.toArray();
  1443. };
  1444. Matrix.prototype.invert = function () {
  1445. this.invertToRef(this);
  1446. return this;
  1447. };
  1448. Matrix.prototype.invertToRef = function (other) {
  1449. var l1 = this.m[0];
  1450. var l2 = this.m[1];
  1451. var l3 = this.m[2];
  1452. var l4 = this.m[3];
  1453. var l5 = this.m[4];
  1454. var l6 = this.m[5];
  1455. var l7 = this.m[6];
  1456. var l8 = this.m[7];
  1457. var l9 = this.m[8];
  1458. var l10 = this.m[9];
  1459. var l11 = this.m[10];
  1460. var l12 = this.m[11];
  1461. var l13 = this.m[12];
  1462. var l14 = this.m[13];
  1463. var l15 = this.m[14];
  1464. var l16 = this.m[15];
  1465. var l17 = (l11 * l16) - (l12 * l15);
  1466. var l18 = (l10 * l16) - (l12 * l14);
  1467. var l19 = (l10 * l15) - (l11 * l14);
  1468. var l20 = (l9 * l16) - (l12 * l13);
  1469. var l21 = (l9 * l15) - (l11 * l13);
  1470. var l22 = (l9 * l14) - (l10 * l13);
  1471. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1472. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1473. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1474. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1475. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1476. var l28 = (l7 * l16) - (l8 * l15);
  1477. var l29 = (l6 * l16) - (l8 * l14);
  1478. var l30 = (l6 * l15) - (l7 * l14);
  1479. var l31 = (l5 * l16) - (l8 * l13);
  1480. var l32 = (l5 * l15) - (l7 * l13);
  1481. var l33 = (l5 * l14) - (l6 * l13);
  1482. var l34 = (l7 * l12) - (l8 * l11);
  1483. var l35 = (l6 * l12) - (l8 * l10);
  1484. var l36 = (l6 * l11) - (l7 * l10);
  1485. var l37 = (l5 * l12) - (l8 * l9);
  1486. var l38 = (l5 * l11) - (l7 * l9);
  1487. var l39 = (l5 * l10) - (l6 * l9);
  1488. other.m[0] = l23 * l27;
  1489. other.m[4] = l24 * l27;
  1490. other.m[8] = l25 * l27;
  1491. other.m[12] = l26 * l27;
  1492. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1493. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1494. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1495. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1496. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1497. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1498. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1499. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1500. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1501. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1502. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1503. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1504. return this;
  1505. };
  1506. Matrix.prototype.invertToRefSIMD = function (other) {
  1507. var src = this.m;
  1508. var dest = other.m;
  1509. var row0, row1, row2, row3;
  1510. var tmp1;
  1511. var minor0, minor1, minor2, minor3;
  1512. var det;
  1513. // Load the 4 rows
  1514. var src0 = SIMD.float32x4.load(src, 0);
  1515. var src1 = SIMD.float32x4.load(src, 4);
  1516. var src2 = SIMD.float32x4.load(src, 8);
  1517. var src3 = SIMD.float32x4.load(src, 12);
  1518. // Transpose the source matrix. Sort of. Not a true transpose operation
  1519. tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1520. row1 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1521. row0 = SIMD.float32x4.shuffle(tmp1, row1, 0, 2, 4, 6);
  1522. row1 = SIMD.float32x4.shuffle(row1, tmp1, 1, 3, 5, 7);
  1523. tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1524. row3 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1525. row2 = SIMD.float32x4.shuffle(tmp1, row3, 0, 2, 4, 6);
  1526. row3 = SIMD.float32x4.shuffle(row3, tmp1, 1, 3, 5, 7);
  1527. // This is a true transposition, but it will lead to an incorrect result
  1528. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1529. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1530. //row0 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1531. //row1 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1532. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1533. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1534. //row2 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1535. //row3 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1536. // ----
  1537. tmp1 = SIMD.float32x4.mul(row2, row3);
  1538. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1539. minor0 = SIMD.float32x4.mul(row1, tmp1);
  1540. minor1 = SIMD.float32x4.mul(row0, tmp1);
  1541. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1542. minor0 = SIMD.float32x4.sub(SIMD.float32x4.mul(row1, tmp1), minor0);
  1543. minor1 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor1);
  1544. minor1 = SIMD.float32x4.swizzle(minor1, 2, 3, 0, 1); // 0x4E = 01001110
  1545. // ----
  1546. tmp1 = SIMD.float32x4.mul(row1, row2);
  1547. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1548. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor0);
  1549. minor3 = SIMD.float32x4.mul(row0, tmp1);
  1550. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1551. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row3, tmp1));
  1552. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor3);
  1553. minor3 = SIMD.float32x4.swizzle(minor3, 2, 3, 0, 1); // 0x4E = 01001110
  1554. // ----
  1555. tmp1 = SIMD.float32x4.mul(SIMD.float32x4.swizzle(row1, 2, 3, 0, 1), row3); // 0x4E = 01001110
  1556. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1557. row2 = SIMD.float32x4.swizzle(row2, 2, 3, 0, 1); // 0x4E = 01001110
  1558. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor0);
  1559. minor2 = SIMD.float32x4.mul(row0, tmp1);
  1560. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1561. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row2, tmp1));
  1562. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor2);
  1563. minor2 = SIMD.float32x4.swizzle(minor2, 2, 3, 0, 1); // 0x4E = 01001110
  1564. // ----
  1565. tmp1 = SIMD.float32x4.mul(row0, row1);
  1566. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1567. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor2);
  1568. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row2, tmp1), minor3);
  1569. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1570. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row3, tmp1), minor2);
  1571. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row2, tmp1));
  1572. // ----
  1573. tmp1 = SIMD.float32x4.mul(row0, row3);
  1574. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1575. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row2, tmp1));
  1576. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor2);
  1577. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1578. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor1);
  1579. minor2 = SIMD.float32x4.sub(minor2, SIMD.float32x4.mul(row1, tmp1));
  1580. // ----
  1581. tmp1 = SIMD.float32x4.mul(row0, row2);
  1582. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1583. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor1);
  1584. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row1, tmp1));
  1585. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1586. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row3, tmp1));
  1587. minor3 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor3);
  1588. // Compute determinant
  1589. det = SIMD.float32x4.mul(row0, minor0);
  1590. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 2, 3, 0, 1), det); // 0x4E = 01001110
  1591. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 1, 0, 3, 2), det); // 0xB1 = 10110001
  1592. tmp1 = SIMD.float32x4.reciprocalApproximation(det);
  1593. det = SIMD.float32x4.sub(SIMD.float32x4.add(tmp1, tmp1), SIMD.float32x4.mul(det, SIMD.float32x4.mul(tmp1, tmp1)));
  1594. det = SIMD.float32x4.swizzle(det, 0, 0, 0, 0);
  1595. // These shuffles aren't necessary if the faulty transposition is done
  1596. // up at the top of this function.
  1597. //minor0 = SIMD.float32x4.swizzle(minor0, 2, 1, 0, 3);
  1598. //minor1 = SIMD.float32x4.swizzle(minor1, 2, 1, 0, 3);
  1599. //minor2 = SIMD.float32x4.swizzle(minor2, 2, 1, 0, 3);
  1600. //minor3 = SIMD.float32x4.swizzle(minor3, 2, 1, 0, 3);
  1601. // Compute final values by multiplying with 1/det
  1602. minor0 = SIMD.float32x4.mul(det, minor0);
  1603. minor1 = SIMD.float32x4.mul(det, minor1);
  1604. minor2 = SIMD.float32x4.mul(det, minor2);
  1605. minor3 = SIMD.float32x4.mul(det, minor3);
  1606. SIMD.float32x4.store(dest, 0, minor0);
  1607. SIMD.float32x4.store(dest, 4, minor1);
  1608. SIMD.float32x4.store(dest, 8, minor2);
  1609. SIMD.float32x4.store(dest, 12, minor3);
  1610. return this;
  1611. };
  1612. Matrix.prototype.setTranslation = function (vector3) {
  1613. this.m[12] = vector3.x;
  1614. this.m[13] = vector3.y;
  1615. this.m[14] = vector3.z;
  1616. return this;
  1617. };
  1618. Matrix.prototype.multiply = function (other) {
  1619. var result = new Matrix();
  1620. this.multiplyToRef(other, result);
  1621. return result;
  1622. };
  1623. Matrix.prototype.copyFrom = function (other) {
  1624. for (var index = 0; index < 16; index++) {
  1625. this.m[index] = other.m[index];
  1626. }
  1627. return this;
  1628. };
  1629. Matrix.prototype.copyToArray = function (array, offset) {
  1630. if (offset === void 0) { offset = 0; }
  1631. for (var index = 0; index < 16; index++) {
  1632. array[offset + index] = this.m[index];
  1633. }
  1634. return this;
  1635. };
  1636. Matrix.prototype.multiplyToRef = function (other, result) {
  1637. this.multiplyToArray(other, result.m, 0);
  1638. return this;
  1639. };
  1640. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1641. var tm0 = this.m[0];
  1642. var tm1 = this.m[1];
  1643. var tm2 = this.m[2];
  1644. var tm3 = this.m[3];
  1645. var tm4 = this.m[4];
  1646. var tm5 = this.m[5];
  1647. var tm6 = this.m[6];
  1648. var tm7 = this.m[7];
  1649. var tm8 = this.m[8];
  1650. var tm9 = this.m[9];
  1651. var tm10 = this.m[10];
  1652. var tm11 = this.m[11];
  1653. var tm12 = this.m[12];
  1654. var tm13 = this.m[13];
  1655. var tm14 = this.m[14];
  1656. var tm15 = this.m[15];
  1657. var om0 = other.m[0];
  1658. var om1 = other.m[1];
  1659. var om2 = other.m[2];
  1660. var om3 = other.m[3];
  1661. var om4 = other.m[4];
  1662. var om5 = other.m[5];
  1663. var om6 = other.m[6];
  1664. var om7 = other.m[7];
  1665. var om8 = other.m[8];
  1666. var om9 = other.m[9];
  1667. var om10 = other.m[10];
  1668. var om11 = other.m[11];
  1669. var om12 = other.m[12];
  1670. var om13 = other.m[13];
  1671. var om14 = other.m[14];
  1672. var om15 = other.m[15];
  1673. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1674. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1675. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1676. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1677. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1678. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1679. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1680. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1681. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1682. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1683. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1684. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1685. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1686. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1687. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1688. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1689. return this;
  1690. };
  1691. Matrix.prototype.multiplyToArraySIMD = function (other, result, offset) {
  1692. if (offset === void 0) { offset = 0; }
  1693. var tm = this.m;
  1694. var om = other.m;
  1695. var om0 = SIMD.float32x4.load(om, 0);
  1696. var om1 = SIMD.float32x4.load(om, 4);
  1697. var om2 = SIMD.float32x4.load(om, 8);
  1698. var om3 = SIMD.float32x4.load(om, 12);
  1699. var tm0 = SIMD.float32x4.load(tm, 0);
  1700. 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)))));
  1701. var tm1 = SIMD.float32x4.load(tm, 4);
  1702. 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)))));
  1703. var tm2 = SIMD.float32x4.load(tm, 8);
  1704. 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)))));
  1705. var tm3 = SIMD.float32x4.load(tm, 12);
  1706. 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)))));
  1707. };
  1708. Matrix.prototype.equals = function (value) {
  1709. 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]);
  1710. };
  1711. Matrix.prototype.clone = function () {
  1712. 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]);
  1713. };
  1714. Matrix.prototype.decompose = function (scale, rotation, translation) {
  1715. translation.x = this.m[12];
  1716. translation.y = this.m[13];
  1717. translation.z = this.m[14];
  1718. var xs = BABYLON.Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1719. var ys = BABYLON.Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1720. var zs = BABYLON.Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1721. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1722. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1723. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1724. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  1725. rotation.x = 0;
  1726. rotation.y = 0;
  1727. rotation.z = 0;
  1728. rotation.w = 1;
  1729. return false;
  1730. }
  1731. 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);
  1732. Quaternion.FromRotationMatrixToRef(rotationMatrix, rotation);
  1733. return true;
  1734. };
  1735. // Statics
  1736. Matrix.FromArray = function (array, offset) {
  1737. var result = new Matrix();
  1738. if (!offset) {
  1739. offset = 0;
  1740. }
  1741. Matrix.FromArrayToRef(array, offset, result);
  1742. return result;
  1743. };
  1744. Matrix.FromArrayToRef = function (array, offset, result) {
  1745. for (var index = 0; index < 16; index++) {
  1746. result.m[index] = array[index + offset];
  1747. }
  1748. };
  1749. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1750. result.m[0] = initialM11;
  1751. result.m[1] = initialM12;
  1752. result.m[2] = initialM13;
  1753. result.m[3] = initialM14;
  1754. result.m[4] = initialM21;
  1755. result.m[5] = initialM22;
  1756. result.m[6] = initialM23;
  1757. result.m[7] = initialM24;
  1758. result.m[8] = initialM31;
  1759. result.m[9] = initialM32;
  1760. result.m[10] = initialM33;
  1761. result.m[11] = initialM34;
  1762. result.m[12] = initialM41;
  1763. result.m[13] = initialM42;
  1764. result.m[14] = initialM43;
  1765. result.m[15] = initialM44;
  1766. };
  1767. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  1768. var result = new Matrix();
  1769. result.m[0] = initialM11;
  1770. result.m[1] = initialM12;
  1771. result.m[2] = initialM13;
  1772. result.m[3] = initialM14;
  1773. result.m[4] = initialM21;
  1774. result.m[5] = initialM22;
  1775. result.m[6] = initialM23;
  1776. result.m[7] = initialM24;
  1777. result.m[8] = initialM31;
  1778. result.m[9] = initialM32;
  1779. result.m[10] = initialM33;
  1780. result.m[11] = initialM34;
  1781. result.m[12] = initialM41;
  1782. result.m[13] = initialM42;
  1783. result.m[14] = initialM43;
  1784. result.m[15] = initialM44;
  1785. return result;
  1786. };
  1787. Matrix.Compose = function (scale, rotation, translation) {
  1788. var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
  1789. var rotationMatrix = Matrix.Identity();
  1790. rotation.toRotationMatrix(rotationMatrix);
  1791. result = result.multiply(rotationMatrix);
  1792. result.setTranslation(translation);
  1793. return result;
  1794. };
  1795. Matrix.Identity = function () {
  1796. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  1797. };
  1798. Matrix.IdentityToRef = function (result) {
  1799. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  1800. };
  1801. Matrix.Zero = function () {
  1802. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1803. };
  1804. Matrix.RotationX = function (angle) {
  1805. var result = new Matrix();
  1806. Matrix.RotationXToRef(angle, result);
  1807. return result;
  1808. };
  1809. Matrix.Invert = function (source) {
  1810. var result = new Matrix();
  1811. source.invertToRef(result);
  1812. return result;
  1813. };
  1814. Matrix.RotationXToRef = function (angle, result) {
  1815. var s = Math.sin(angle);
  1816. var c = Math.cos(angle);
  1817. result.m[0] = 1.0;
  1818. result.m[15] = 1.0;
  1819. result.m[5] = c;
  1820. result.m[10] = c;
  1821. result.m[9] = -s;
  1822. result.m[6] = s;
  1823. result.m[1] = 0;
  1824. result.m[2] = 0;
  1825. result.m[3] = 0;
  1826. result.m[4] = 0;
  1827. result.m[7] = 0;
  1828. result.m[8] = 0;
  1829. result.m[11] = 0;
  1830. result.m[12] = 0;
  1831. result.m[13] = 0;
  1832. result.m[14] = 0;
  1833. };
  1834. Matrix.RotationY = function (angle) {
  1835. var result = new Matrix();
  1836. Matrix.RotationYToRef(angle, result);
  1837. return result;
  1838. };
  1839. Matrix.RotationYToRef = function (angle, result) {
  1840. var s = Math.sin(angle);
  1841. var c = Math.cos(angle);
  1842. result.m[5] = 1.0;
  1843. result.m[15] = 1.0;
  1844. result.m[0] = c;
  1845. result.m[2] = -s;
  1846. result.m[8] = s;
  1847. result.m[10] = c;
  1848. result.m[1] = 0;
  1849. result.m[3] = 0;
  1850. result.m[4] = 0;
  1851. result.m[6] = 0;
  1852. result.m[7] = 0;
  1853. result.m[9] = 0;
  1854. result.m[11] = 0;
  1855. result.m[12] = 0;
  1856. result.m[13] = 0;
  1857. result.m[14] = 0;
  1858. };
  1859. Matrix.RotationZ = function (angle) {
  1860. var result = new Matrix();
  1861. Matrix.RotationZToRef(angle, result);
  1862. return result;
  1863. };
  1864. Matrix.RotationZToRef = function (angle, result) {
  1865. var s = Math.sin(angle);
  1866. var c = Math.cos(angle);
  1867. result.m[10] = 1.0;
  1868. result.m[15] = 1.0;
  1869. result.m[0] = c;
  1870. result.m[1] = s;
  1871. result.m[4] = -s;
  1872. result.m[5] = c;
  1873. result.m[2] = 0;
  1874. result.m[3] = 0;
  1875. result.m[6] = 0;
  1876. result.m[7] = 0;
  1877. result.m[8] = 0;
  1878. result.m[9] = 0;
  1879. result.m[11] = 0;
  1880. result.m[12] = 0;
  1881. result.m[13] = 0;
  1882. result.m[14] = 0;
  1883. };
  1884. Matrix.RotationAxis = function (axis, angle) {
  1885. var s = Math.sin(-angle);
  1886. var c = Math.cos(-angle);
  1887. var c1 = 1 - c;
  1888. axis.normalize();
  1889. var result = Matrix.Zero();
  1890. result.m[0] = (axis.x * axis.x) * c1 + c;
  1891. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1892. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1893. result.m[3] = 0.0;
  1894. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1895. result.m[5] = (axis.y * axis.y) * c1 + c;
  1896. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1897. result.m[7] = 0.0;
  1898. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1899. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1900. result.m[10] = (axis.z * axis.z) * c1 + c;
  1901. result.m[11] = 0.0;
  1902. result.m[15] = 1.0;
  1903. return result;
  1904. };
  1905. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1906. var result = new Matrix();
  1907. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1908. return result;
  1909. };
  1910. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1911. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1912. this._tempQuaternion.toRotationMatrix(result);
  1913. };
  1914. Matrix.Scaling = function (x, y, z) {
  1915. var result = Matrix.Zero();
  1916. Matrix.ScalingToRef(x, y, z, result);
  1917. return result;
  1918. };
  1919. Matrix.ScalingToRef = function (x, y, z, result) {
  1920. result.m[0] = x;
  1921. result.m[1] = 0;
  1922. result.m[2] = 0;
  1923. result.m[3] = 0;
  1924. result.m[4] = 0;
  1925. result.m[5] = y;
  1926. result.m[6] = 0;
  1927. result.m[7] = 0;
  1928. result.m[8] = 0;
  1929. result.m[9] = 0;
  1930. result.m[10] = z;
  1931. result.m[11] = 0;
  1932. result.m[12] = 0;
  1933. result.m[13] = 0;
  1934. result.m[14] = 0;
  1935. result.m[15] = 1.0;
  1936. };
  1937. Matrix.Translation = function (x, y, z) {
  1938. var result = Matrix.Identity();
  1939. Matrix.TranslationToRef(x, y, z, result);
  1940. return result;
  1941. };
  1942. Matrix.TranslationToRef = function (x, y, z, result) {
  1943. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  1944. };
  1945. Matrix.LookAtLH = function (eye, target, up) {
  1946. var result = Matrix.Zero();
  1947. Matrix.LookAtLHToRef(eye, target, up, result);
  1948. return result;
  1949. };
  1950. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1951. // Z axis
  1952. target.subtractToRef(eye, this._zAxis);
  1953. this._zAxis.normalize();
  1954. // X axis
  1955. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1956. this._xAxis.normalize();
  1957. // Y axis
  1958. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1959. this._yAxis.normalize();
  1960. // Eye angles
  1961. var ex = -Vector3.Dot(this._xAxis, eye);
  1962. var ey = -Vector3.Dot(this._yAxis, eye);
  1963. var ez = -Vector3.Dot(this._zAxis, eye);
  1964. 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);
  1965. };
  1966. Matrix.LookAtLHToRefSIMD = function (eyeRef, targetRef, upRef, result) {
  1967. var out = result.m;
  1968. var center = SIMD.float32x4(targetRef.x, targetRef.y, targetRef.z, 0);
  1969. var eye = SIMD.float32x4(eyeRef.x, eyeRef.y, eyeRef.z, 0);
  1970. var up = SIMD.float32x4(upRef.x, upRef.y, upRef.z, 0);
  1971. // cc.kmVec3Subtract(f, pCenter, pEye);
  1972. var f = SIMD.float32x4.sub(center, eye);
  1973. // cc.kmVec3Normalize(f, f);
  1974. var tmp = SIMD.float32x4.mul(f, f);
  1975. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1976. f = SIMD.float32x4.mul(f, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1977. // cc.kmVec3Assign(up, pUp);
  1978. // cc.kmVec3Normalize(up, up);
  1979. tmp = SIMD.float32x4.mul(up, up);
  1980. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1981. up = SIMD.float32x4.mul(up, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1982. // cc.kmVec3Cross(s, f, up);
  1983. 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)));
  1984. // cc.kmVec3Normalize(s, s);
  1985. tmp = SIMD.float32x4.mul(s, s);
  1986. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1987. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1988. // cc.kmVec3Cross(u, s, f);
  1989. 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)));
  1990. // cc.kmVec3Normalize(s, s);
  1991. tmp = SIMD.float32x4.mul(s, s);
  1992. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1993. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1994. var zero = SIMD.float32x4.splat(0.0);
  1995. s = SIMD.float32x4.neg(s);
  1996. var tmp01 = SIMD.float32x4.shuffle(s, u, 0, 1, 4, 5);
  1997. var tmp23 = SIMD.float32x4.shuffle(f, zero, 0, 1, 4, 5);
  1998. var a0 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  1999. var a1 = SIMD.float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7);
  2000. tmp01 = SIMD.float32x4.shuffle(s, u, 2, 3, 6, 7);
  2001. tmp23 = SIMD.float32x4.shuffle(f, zero, 2, 3, 6, 7);
  2002. var a2 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  2003. var a3 = SIMD.float32x4(0.0, 0.0, 0.0, 1.0);
  2004. var b0 = SIMD.float32x4(1.0, 0.0, 0.0, 0.0);
  2005. var b1 = SIMD.float32x4(0.0, 1.0, 0.0, 0.0);
  2006. var b2 = SIMD.float32x4(0.0, 0.0, 1.0, 0.0);
  2007. var b3 = SIMD.float32x4.neg(eye);
  2008. b3 = SIMD.float32x4.withW(b3, 1.0);
  2009. 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)))));
  2010. 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)))));
  2011. 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)))));
  2012. 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)))));
  2013. };
  2014. Matrix.OrthoLH = function (width, height, znear, zfar) {
  2015. var matrix = Matrix.Zero();
  2016. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  2017. return matrix;
  2018. };
  2019. Matrix.OrthoLHToRef = function (width, height, znear, zfar, result) {
  2020. var hw = 2.0 / width;
  2021. var hh = 2.0 / height;
  2022. var id = 1.0 / (zfar - znear);
  2023. var nid = znear / (znear - zfar);
  2024. Matrix.FromValuesToRef(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1, result);
  2025. };
  2026. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  2027. var matrix = Matrix.Zero();
  2028. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  2029. return matrix;
  2030. };
  2031. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  2032. result.m[0] = 2.0 / (right - left);
  2033. result.m[1] = result.m[2] = result.m[3] = 0;
  2034. result.m[5] = 2.0 / (top - bottom);
  2035. result.m[4] = result.m[6] = result.m[7] = 0;
  2036. result.m[10] = -1.0 / (znear - zfar);
  2037. result.m[8] = result.m[9] = result.m[11] = 0;
  2038. result.m[12] = (left + right) / (left - right);
  2039. result.m[13] = (top + bottom) / (bottom - top);
  2040. result.m[14] = znear / (znear - zfar);
  2041. result.m[15] = 1.0;
  2042. };
  2043. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  2044. var matrix = Matrix.Zero();
  2045. matrix.m[0] = (2.0 * znear) / width;
  2046. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  2047. matrix.m[5] = (2.0 * znear) / height;
  2048. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  2049. matrix.m[10] = -zfar / (znear - zfar);
  2050. matrix.m[8] = matrix.m[9] = 0.0;
  2051. matrix.m[11] = 1.0;
  2052. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  2053. matrix.m[14] = (znear * zfar) / (znear - zfar);
  2054. return matrix;
  2055. };
  2056. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  2057. var matrix = Matrix.Zero();
  2058. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  2059. return matrix;
  2060. };
  2061. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result, fovMode) {
  2062. if (fovMode === void 0) { fovMode = BABYLON.Camera.FOVMODE_VERTICAL_FIXED; }
  2063. var tan = 1.0 / (Math.tan(fov * 0.5));
  2064. var v_fixed = (fovMode === BABYLON.Camera.FOVMODE_VERTICAL_FIXED);
  2065. if (v_fixed) {
  2066. result.m[0] = tan / aspect;
  2067. }
  2068. else {
  2069. result.m[0] = tan;
  2070. }
  2071. result.m[1] = result.m[2] = result.m[3] = 0.0;
  2072. if (v_fixed) {
  2073. result.m[5] = tan;
  2074. }
  2075. else {
  2076. result.m[5] = tan * aspect;
  2077. }
  2078. result.m[4] = result.m[6] = result.m[7] = 0.0;
  2079. result.m[8] = result.m[9] = 0.0;
  2080. result.m[10] = -zfar / (znear - zfar);
  2081. result.m[11] = 1.0;
  2082. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2083. result.m[14] = (znear * zfar) / (znear - zfar);
  2084. };
  2085. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  2086. var cw = viewport.width;
  2087. var ch = viewport.height;
  2088. var cx = viewport.x;
  2089. var cy = viewport.y;
  2090. 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);
  2091. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  2092. };
  2093. Matrix.Transpose = function (matrix) {
  2094. var result = new Matrix();
  2095. result.m[0] = matrix.m[0];
  2096. result.m[1] = matrix.m[4];
  2097. result.m[2] = matrix.m[8];
  2098. result.m[3] = matrix.m[12];
  2099. result.m[4] = matrix.m[1];
  2100. result.m[5] = matrix.m[5];
  2101. result.m[6] = matrix.m[9];
  2102. result.m[7] = matrix.m[13];
  2103. result.m[8] = matrix.m[2];
  2104. result.m[9] = matrix.m[6];
  2105. result.m[10] = matrix.m[10];
  2106. result.m[11] = matrix.m[14];
  2107. result.m[12] = matrix.m[3];
  2108. result.m[13] = matrix.m[7];
  2109. result.m[14] = matrix.m[11];
  2110. result.m[15] = matrix.m[15];
  2111. return result;
  2112. };
  2113. Matrix.Reflection = function (plane) {
  2114. var matrix = new Matrix();
  2115. Matrix.ReflectionToRef(plane, matrix);
  2116. return matrix;
  2117. };
  2118. Matrix.ReflectionToRef = function (plane, result) {
  2119. plane.normalize();
  2120. var x = plane.normal.x;
  2121. var y = plane.normal.y;
  2122. var z = plane.normal.z;
  2123. var temp = -2 * x;
  2124. var temp2 = -2 * y;
  2125. var temp3 = -2 * z;
  2126. result.m[0] = (temp * x) + 1;
  2127. result.m[1] = temp2 * x;
  2128. result.m[2] = temp3 * x;
  2129. result.m[3] = 0.0;
  2130. result.m[4] = temp * y;
  2131. result.m[5] = (temp2 * y) + 1;
  2132. result.m[6] = temp3 * y;
  2133. result.m[7] = 0.0;
  2134. result.m[8] = temp * z;
  2135. result.m[9] = temp2 * z;
  2136. result.m[10] = (temp3 * z) + 1;
  2137. result.m[11] = 0.0;
  2138. result.m[12] = temp * plane.d;
  2139. result.m[13] = temp2 * plane.d;
  2140. result.m[14] = temp3 * plane.d;
  2141. result.m[15] = 1.0;
  2142. };
  2143. Matrix._tempQuaternion = new Quaternion();
  2144. Matrix._xAxis = Vector3.Zero();
  2145. Matrix._yAxis = Vector3.Zero();
  2146. Matrix._zAxis = Vector3.Zero();
  2147. return Matrix;
  2148. })();
  2149. BABYLON.Matrix = Matrix;
  2150. var Plane = (function () {
  2151. function Plane(a, b, c, d) {
  2152. this.normal = new Vector3(a, b, c);
  2153. this.d = d;
  2154. }
  2155. Plane.prototype.asArray = function () {
  2156. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  2157. };
  2158. // Methods
  2159. Plane.prototype.clone = function () {
  2160. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  2161. };
  2162. Plane.prototype.normalize = function () {
  2163. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  2164. var magnitude = 0;
  2165. if (norm !== 0) {
  2166. magnitude = 1.0 / norm;
  2167. }
  2168. this.normal.x *= magnitude;
  2169. this.normal.y *= magnitude;
  2170. this.normal.z *= magnitude;
  2171. this.d *= magnitude;
  2172. return this;
  2173. };
  2174. Plane.prototype.transform = function (transformation) {
  2175. var transposedMatrix = Matrix.Transpose(transformation);
  2176. var x = this.normal.x;
  2177. var y = this.normal.y;
  2178. var z = this.normal.z;
  2179. var d = this.d;
  2180. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  2181. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  2182. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  2183. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  2184. return new Plane(normalX, normalY, normalZ, finalD);
  2185. };
  2186. Plane.prototype.dotCoordinate = function (point) {
  2187. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  2188. };
  2189. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  2190. var x1 = point2.x - point1.x;
  2191. var y1 = point2.y - point1.y;
  2192. var z1 = point2.z - point1.z;
  2193. var x2 = point3.x - point1.x;
  2194. var y2 = point3.y - point1.y;
  2195. var z2 = point3.z - point1.z;
  2196. var yz = (y1 * z2) - (z1 * y2);
  2197. var xz = (z1 * x2) - (x1 * z2);
  2198. var xy = (x1 * y2) - (y1 * x2);
  2199. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  2200. var invPyth;
  2201. if (pyth !== 0) {
  2202. invPyth = 1.0 / pyth;
  2203. }
  2204. else {
  2205. invPyth = 0;
  2206. }
  2207. this.normal.x = yz * invPyth;
  2208. this.normal.y = xz * invPyth;
  2209. this.normal.z = xy * invPyth;
  2210. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  2211. return this;
  2212. };
  2213. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  2214. var dot = Vector3.Dot(this.normal, direction);
  2215. return (dot <= epsilon);
  2216. };
  2217. Plane.prototype.signedDistanceTo = function (point) {
  2218. return Vector3.Dot(point, this.normal) + this.d;
  2219. };
  2220. // Statics
  2221. Plane.FromArray = function (array) {
  2222. return new Plane(array[0], array[1], array[2], array[3]);
  2223. };
  2224. Plane.FromPoints = function (point1, point2, point3) {
  2225. var result = new Plane(0, 0, 0, 0);
  2226. result.copyFromPoints(point1, point2, point3);
  2227. return result;
  2228. };
  2229. Plane.FromPositionAndNormal = function (origin, normal) {
  2230. var result = new Plane(0, 0, 0, 0);
  2231. normal.normalize();
  2232. result.normal = normal;
  2233. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2234. return result;
  2235. };
  2236. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  2237. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2238. return Vector3.Dot(point, normal) + d;
  2239. };
  2240. return Plane;
  2241. })();
  2242. BABYLON.Plane = Plane;
  2243. var Viewport = (function () {
  2244. function Viewport(x, y, width, height) {
  2245. this.x = x;
  2246. this.y = y;
  2247. this.width = width;
  2248. this.height = height;
  2249. }
  2250. Viewport.prototype.toGlobal = function (engine) {
  2251. var width = engine.getRenderWidth();
  2252. var height = engine.getRenderHeight();
  2253. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  2254. };
  2255. return Viewport;
  2256. })();
  2257. BABYLON.Viewport = Viewport;
  2258. var Frustum = (function () {
  2259. function Frustum() {
  2260. }
  2261. Frustum.GetPlanes = function (transform) {
  2262. var frustumPlanes = [];
  2263. for (var index = 0; index < 6; index++) {
  2264. frustumPlanes.push(new Plane(0, 0, 0, 0));
  2265. }
  2266. Frustum.GetPlanesToRef(transform, frustumPlanes);
  2267. return frustumPlanes;
  2268. };
  2269. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  2270. // Near
  2271. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  2272. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  2273. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  2274. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  2275. frustumPlanes[0].normalize();
  2276. // Far
  2277. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  2278. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  2279. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  2280. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  2281. frustumPlanes[1].normalize();
  2282. // Left
  2283. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  2284. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  2285. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  2286. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  2287. frustumPlanes[2].normalize();
  2288. // Right
  2289. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  2290. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  2291. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  2292. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  2293. frustumPlanes[3].normalize();
  2294. // Top
  2295. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  2296. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  2297. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  2298. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  2299. frustumPlanes[4].normalize();
  2300. // Bottom
  2301. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  2302. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  2303. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  2304. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  2305. frustumPlanes[5].normalize();
  2306. };
  2307. return Frustum;
  2308. })();
  2309. BABYLON.Frustum = Frustum;
  2310. var Ray = (function () {
  2311. function Ray(origin, direction, length) {
  2312. if (length === void 0) { length = Number.MAX_VALUE; }
  2313. this.origin = origin;
  2314. this.direction = direction;
  2315. this.length = length;
  2316. }
  2317. // Methods
  2318. Ray.prototype.intersectsBoxMinMax = function (minimum, maximum) {
  2319. var d = 0.0;
  2320. var maxValue = Number.MAX_VALUE;
  2321. if (Math.abs(this.direction.x) < 0.0000001) {
  2322. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  2323. return false;
  2324. }
  2325. }
  2326. else {
  2327. var inv = 1.0 / this.direction.x;
  2328. var min = (minimum.x - this.origin.x) * inv;
  2329. var max = (maximum.x - this.origin.x) * inv;
  2330. if (max === -Infinity) {
  2331. max = Infinity;
  2332. }
  2333. if (min > max) {
  2334. var temp = min;
  2335. min = max;
  2336. max = temp;
  2337. }
  2338. d = Math.max(min, d);
  2339. maxValue = Math.min(max, maxValue);
  2340. if (d > maxValue) {
  2341. return false;
  2342. }
  2343. }
  2344. if (Math.abs(this.direction.y) < 0.0000001) {
  2345. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  2346. return false;
  2347. }
  2348. }
  2349. else {
  2350. inv = 1.0 / this.direction.y;
  2351. min = (minimum.y - this.origin.y) * inv;
  2352. max = (maximum.y - this.origin.y) * inv;
  2353. if (max === -Infinity) {
  2354. max = Infinity;
  2355. }
  2356. if (min > max) {
  2357. temp = min;
  2358. min = max;
  2359. max = temp;
  2360. }
  2361. d = Math.max(min, d);
  2362. maxValue = Math.min(max, maxValue);
  2363. if (d > maxValue) {
  2364. return false;
  2365. }
  2366. }
  2367. if (Math.abs(this.direction.z) < 0.0000001) {
  2368. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  2369. return false;
  2370. }
  2371. }
  2372. else {
  2373. inv = 1.0 / this.direction.z;
  2374. min = (minimum.z - this.origin.z) * inv;
  2375. max = (maximum.z - this.origin.z) * inv;
  2376. if (max === -Infinity) {
  2377. max = Infinity;
  2378. }
  2379. if (min > max) {
  2380. temp = min;
  2381. min = max;
  2382. max = temp;
  2383. }
  2384. d = Math.max(min, d);
  2385. maxValue = Math.min(max, maxValue);
  2386. if (d > maxValue) {
  2387. return false;
  2388. }
  2389. }
  2390. return true;
  2391. };
  2392. Ray.prototype.intersectsBox = function (box) {
  2393. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  2394. };
  2395. Ray.prototype.intersectsSphere = function (sphere) {
  2396. var x = sphere.center.x - this.origin.x;
  2397. var y = sphere.center.y - this.origin.y;
  2398. var z = sphere.center.z - this.origin.z;
  2399. var pyth = (x * x) + (y * y) + (z * z);
  2400. var rr = sphere.radius * sphere.radius;
  2401. if (pyth <= rr) {
  2402. return true;
  2403. }
  2404. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  2405. if (dot < 0.0) {
  2406. return false;
  2407. }
  2408. var temp = pyth - (dot * dot);
  2409. return temp <= rr;
  2410. };
  2411. Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  2412. if (!this._edge1) {
  2413. this._edge1 = Vector3.Zero();
  2414. this._edge2 = Vector3.Zero();
  2415. this._pvec = Vector3.Zero();
  2416. this._tvec = Vector3.Zero();
  2417. this._qvec = Vector3.Zero();
  2418. }
  2419. vertex1.subtractToRef(vertex0, this._edge1);
  2420. vertex2.subtractToRef(vertex0, this._edge2);
  2421. Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  2422. var det = Vector3.Dot(this._edge1, this._pvec);
  2423. if (det === 0) {
  2424. return null;
  2425. }
  2426. var invdet = 1 / det;
  2427. this.origin.subtractToRef(vertex0, this._tvec);
  2428. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  2429. if (bu < 0 || bu > 1.0) {
  2430. return null;
  2431. }
  2432. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  2433. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  2434. if (bv < 0 || bu + bv > 1.0) {
  2435. return null;
  2436. }
  2437. //check if the distance is longer than the predefined length.
  2438. var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
  2439. if (distance > this.length) {
  2440. return null;
  2441. }
  2442. return new BABYLON.IntersectionInfo(bu, bv, distance);
  2443. };
  2444. // Statics
  2445. Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  2446. var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  2447. var end = Vector3.Unproject(new Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  2448. var direction = end.subtract(start);
  2449. direction.normalize();
  2450. return new Ray(start, direction);
  2451. };
  2452. /**
  2453. * 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
  2454. * transformed to the given world matrix.
  2455. * @param origin The origin point
  2456. * @param end The end point
  2457. * @param world a matrix to transform the ray to. Default is the identity matrix.
  2458. */
  2459. Ray.CreateNewFromTo = function (origin, end, world) {
  2460. if (world === void 0) { world = Matrix.Identity(); }
  2461. var direction = end.subtract(origin);
  2462. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  2463. direction.normalize();
  2464. return Ray.Transform(new Ray(origin, direction, length), world);
  2465. };
  2466. Ray.Transform = function (ray, matrix) {
  2467. var newOrigin = Vector3.TransformCoordinates(ray.origin, matrix);
  2468. var newDirection = Vector3.TransformNormal(ray.direction, matrix);
  2469. return new Ray(newOrigin, newDirection, ray.length);
  2470. };
  2471. return Ray;
  2472. })();
  2473. BABYLON.Ray = Ray;
  2474. (function (Space) {
  2475. Space[Space["LOCAL"] = 0] = "LOCAL";
  2476. Space[Space["WORLD"] = 1] = "WORLD";
  2477. })(BABYLON.Space || (BABYLON.Space = {}));
  2478. var Space = BABYLON.Space;
  2479. var Axis = (function () {
  2480. function Axis() {
  2481. }
  2482. Axis.X = new Vector3(1, 0, 0);
  2483. Axis.Y = new Vector3(0, 1, 0);
  2484. Axis.Z = new Vector3(0, 0, 1);
  2485. return Axis;
  2486. })();
  2487. BABYLON.Axis = Axis;
  2488. ;
  2489. var BezierCurve = (function () {
  2490. function BezierCurve() {
  2491. }
  2492. BezierCurve.interpolate = function (t, x1, y1, x2, y2) {
  2493. // Extract X (which is equal to time here)
  2494. var f0 = 1 - 3 * x2 + 3 * x1;
  2495. var f1 = 3 * x2 - 6 * x1;
  2496. var f2 = 3 * x1;
  2497. var refinedT = t;
  2498. for (var i = 0; i < 5; i++) {
  2499. var refinedT2 = refinedT * refinedT;
  2500. var refinedT3 = refinedT2 * refinedT;
  2501. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2502. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2503. refinedT -= (x - t) * slope;
  2504. refinedT = Math.min(1, Math.max(0, refinedT));
  2505. }
  2506. // Resolve cubic bezier for the given x
  2507. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 + 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 + Math.pow(refinedT, 3);
  2508. };
  2509. return BezierCurve;
  2510. })();
  2511. BABYLON.BezierCurve = BezierCurve;
  2512. (function (Orientation) {
  2513. Orientation[Orientation["CW"] = 0] = "CW";
  2514. Orientation[Orientation["CCW"] = 1] = "CCW";
  2515. })(BABYLON.Orientation || (BABYLON.Orientation = {}));
  2516. var Orientation = BABYLON.Orientation;
  2517. var Angle = (function () {
  2518. function Angle(radians) {
  2519. var _this = this;
  2520. this.degrees = function () { return _this._radians * 180 / Math.PI; };
  2521. this.radians = function () { return _this._radians; };
  2522. this._radians = radians;
  2523. if (this._radians < 0)
  2524. this._radians += (2 * Math.PI);
  2525. }
  2526. Angle.BetweenTwoPoints = function (a, b) {
  2527. var delta = b.subtract(a);
  2528. var theta = Math.atan2(delta.y, delta.x);
  2529. return new Angle(theta);
  2530. };
  2531. Angle.FromRadians = function (radians) {
  2532. return new Angle(radians);
  2533. };
  2534. Angle.FromDegrees = function (degrees) {
  2535. return new Angle(degrees * Math.PI / 180);
  2536. };
  2537. return Angle;
  2538. })();
  2539. BABYLON.Angle = Angle;
  2540. var Arc2 = (function () {
  2541. function Arc2(startPoint, midPoint, endPoint) {
  2542. this.startPoint = startPoint;
  2543. this.midPoint = midPoint;
  2544. this.endPoint = endPoint;
  2545. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2546. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2547. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2548. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2549. 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);
  2550. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2551. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2552. var a1 = this.startAngle.degrees();
  2553. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2554. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2555. // angles correction
  2556. if (a2 - a1 > +180.0)
  2557. a2 -= 360.0;
  2558. if (a2 - a1 < -180.0)
  2559. a2 += 360.0;
  2560. if (a3 - a2 > +180.0)
  2561. a3 -= 360.0;
  2562. if (a3 - a2 < -180.0)
  2563. a3 += 360.0;
  2564. this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
  2565. this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
  2566. }
  2567. return Arc2;
  2568. })();
  2569. BABYLON.Arc2 = Arc2;
  2570. var PathCursor = (function () {
  2571. function PathCursor(path) {
  2572. this.path = path;
  2573. this._onchange = new Array();
  2574. this.value = 0;
  2575. this.animations = new Array();
  2576. }
  2577. PathCursor.prototype.getPoint = function () {
  2578. var point = this.path.getPointAtLengthPosition(this.value);
  2579. return new Vector3(point.x, 0, point.y);
  2580. };
  2581. PathCursor.prototype.moveAhead = function (step) {
  2582. if (step === void 0) { step = 0.002; }
  2583. this.move(step);
  2584. return this;
  2585. };
  2586. PathCursor.prototype.moveBack = function (step) {
  2587. if (step === void 0) { step = 0.002; }
  2588. this.move(-step);
  2589. return this;
  2590. };
  2591. PathCursor.prototype.move = function (step) {
  2592. if (Math.abs(step) > 1) {
  2593. throw "step size should be less than 1.";
  2594. }
  2595. this.value += step;
  2596. this.ensureLimits();
  2597. this.raiseOnChange();
  2598. return this;
  2599. };
  2600. PathCursor.prototype.ensureLimits = function () {
  2601. while (this.value > 1) {
  2602. this.value -= 1;
  2603. }
  2604. while (this.value < 0) {
  2605. this.value += 1;
  2606. }
  2607. return this;
  2608. };
  2609. // used by animation engine
  2610. PathCursor.prototype.markAsDirty = function (propertyName) {
  2611. this.ensureLimits();
  2612. this.raiseOnChange();
  2613. return this;
  2614. };
  2615. PathCursor.prototype.raiseOnChange = function () {
  2616. var _this = this;
  2617. this._onchange.forEach(function (f) { return f(_this); });
  2618. return this;
  2619. };
  2620. PathCursor.prototype.onchange = function (f) {
  2621. this._onchange.push(f);
  2622. return this;
  2623. };
  2624. return PathCursor;
  2625. })();
  2626. BABYLON.PathCursor = PathCursor;
  2627. var Path2 = (function () {
  2628. function Path2(x, y) {
  2629. this._points = new Array();
  2630. this._length = 0;
  2631. this.closed = false;
  2632. this._points.push(new Vector2(x, y));
  2633. }
  2634. Path2.prototype.addLineTo = function (x, y) {
  2635. if (closed) {
  2636. BABYLON.Tools.Error("cannot add lines to closed paths");
  2637. return this;
  2638. }
  2639. var newPoint = new Vector2(x, y);
  2640. var previousPoint = this._points[this._points.length - 1];
  2641. this._points.push(newPoint);
  2642. this._length += newPoint.subtract(previousPoint).length();
  2643. return this;
  2644. };
  2645. Path2.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
  2646. if (numberOfSegments === void 0) { numberOfSegments = 36; }
  2647. if (closed) {
  2648. BABYLON.Tools.Error("cannot add arcs to closed paths");
  2649. return this;
  2650. }
  2651. var startPoint = this._points[this._points.length - 1];
  2652. var midPoint = new Vector2(midX, midY);
  2653. var endPoint = new Vector2(endX, endY);
  2654. var arc = new Arc2(startPoint, midPoint, endPoint);
  2655. var increment = arc.angle.radians() / numberOfSegments;
  2656. if (arc.orientation === 0 /* CW */)
  2657. increment *= -1;
  2658. var currentAngle = arc.startAngle.radians() + increment;
  2659. for (var i = 0; i < numberOfSegments; i++) {
  2660. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2661. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2662. this.addLineTo(x, y);
  2663. currentAngle += increment;
  2664. }
  2665. return this;
  2666. };
  2667. Path2.prototype.close = function () {
  2668. this.closed = true;
  2669. return this;
  2670. };
  2671. Path2.prototype.length = function () {
  2672. var result = this._length;
  2673. if (!this.closed) {
  2674. var lastPoint = this._points[this._points.length - 1];
  2675. var firstPoint = this._points[0];
  2676. result += (firstPoint.subtract(lastPoint).length());
  2677. }
  2678. return result;
  2679. };
  2680. Path2.prototype.getPoints = function () {
  2681. return this._points;
  2682. };
  2683. Path2.prototype.getPointAtLengthPosition = function (normalizedLengthPosition) {
  2684. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2685. BABYLON.Tools.Error("normalized length position should be between 0 and 1.");
  2686. return Vector2.Zero();
  2687. }
  2688. var lengthPosition = normalizedLengthPosition * this.length();
  2689. var previousOffset = 0;
  2690. for (var i = 0; i < this._points.length; i++) {
  2691. var j = (i + 1) % this._points.length;
  2692. var a = this._points[i];
  2693. var b = this._points[j];
  2694. var bToA = b.subtract(a);
  2695. var nextOffset = (bToA.length() + previousOffset);
  2696. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2697. var dir = bToA.normalize();
  2698. var localOffset = lengthPosition - previousOffset;
  2699. return new Vector2(a.x + (dir.x * localOffset), a.y + (dir.y * localOffset));
  2700. }
  2701. previousOffset = nextOffset;
  2702. }
  2703. BABYLON.Tools.Error("internal error");
  2704. return Vector2.Zero();
  2705. };
  2706. Path2.StartingAt = function (x, y) {
  2707. return new Path2(x, y);
  2708. };
  2709. return Path2;
  2710. })();
  2711. BABYLON.Path2 = Path2;
  2712. var Path3D = (function () {
  2713. function Path3D(path, firstNormal) {
  2714. this.path = path;
  2715. this._curve = new Array();
  2716. this._distances = new Array();
  2717. this._tangents = new Array();
  2718. this._normals = new Array();
  2719. this._binormals = new Array();
  2720. for (var p = 0; p < path.length; p++) {
  2721. this._curve[p] = path[p].clone(); // hard copy
  2722. }
  2723. this._compute(firstNormal);
  2724. }
  2725. Path3D.prototype.getCurve = function () {
  2726. return this._curve;
  2727. };
  2728. Path3D.prototype.getTangents = function () {
  2729. return this._tangents;
  2730. };
  2731. Path3D.prototype.getNormals = function () {
  2732. return this._normals;
  2733. };
  2734. Path3D.prototype.getBinormals = function () {
  2735. return this._binormals;
  2736. };
  2737. Path3D.prototype.getDistances = function () {
  2738. return this._distances;
  2739. };
  2740. Path3D.prototype.update = function (path, firstNormal) {
  2741. for (var p = 0; p < path.length; p++) {
  2742. this._curve[p].x = path[p].x;
  2743. this._curve[p].y = path[p].y;
  2744. this._curve[p].z = path[p].z;
  2745. }
  2746. this._compute(firstNormal);
  2747. return this;
  2748. };
  2749. // private function compute() : computes tangents, normals and binormals
  2750. Path3D.prototype._compute = function (firstNormal) {
  2751. var l = this._curve.length;
  2752. // first and last tangents
  2753. this._tangents[0] = this._getFirstNonNullVector(0);
  2754. this._tangents[0].normalize();
  2755. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  2756. this._tangents[l - 1].normalize();
  2757. // normals and binormals at first point : arbitrary vector with _normalVector()
  2758. var tg0 = this._tangents[0];
  2759. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  2760. this._normals[0] = pp0;
  2761. this._normals[0].normalize();
  2762. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  2763. this._binormals[0].normalize();
  2764. this._distances[0] = 0;
  2765. // normals and binormals : next points
  2766. var prev; // previous vector (segment)
  2767. var cur; // current vector (segment)
  2768. var curTang; // current tangent
  2769. var prevNorm; // previous normal
  2770. var prevBinor; // previous binormal
  2771. for (var i = 1; i < l; i++) {
  2772. // tangents
  2773. prev = this._getLastNonNullVector(i);
  2774. if (i < l - 1) {
  2775. cur = this._getFirstNonNullVector(i);
  2776. this._tangents[i] = prev.add(cur);
  2777. this._tangents[i].normalize();
  2778. }
  2779. this._distances[i] = this._distances[i - 1] + prev.length();
  2780. // normals and binormals
  2781. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  2782. curTang = this._tangents[i];
  2783. prevNorm = this._normals[i - 1];
  2784. prevBinor = this._binormals[i - 1];
  2785. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  2786. this._normals[i].normalize();
  2787. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  2788. this._binormals[i].normalize();
  2789. }
  2790. };
  2791. // private function getFirstNonNullVector(index)
  2792. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  2793. Path3D.prototype._getFirstNonNullVector = function (index) {
  2794. var i = 1;
  2795. var nNVector = this._curve[index + i].subtract(this._curve[index]);
  2796. while (nNVector.length() == 0 && index + i + 1 < this._curve.length) {
  2797. i++;
  2798. nNVector = this._curve[index + i].subtract(this._curve[index]);
  2799. }
  2800. return nNVector;
  2801. };
  2802. // private function getLastNonNullVector(index)
  2803. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  2804. Path3D.prototype._getLastNonNullVector = function (index) {
  2805. var i = 1;
  2806. var nLVector = this._curve[index].subtract(this._curve[index - i]);
  2807. while (nLVector.length() == 0 && index > i + 1) {
  2808. i++;
  2809. nLVector = this._curve[index].subtract(this._curve[index - i]);
  2810. }
  2811. return nLVector;
  2812. };
  2813. // private function normalVector(v0, vt, va) :
  2814. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  2815. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  2816. Path3D.prototype._normalVector = function (v0, vt, va) {
  2817. var normal0;
  2818. if (va === undefined || va === null) {
  2819. var point;
  2820. if (vt.y !== 1) {
  2821. point = new Vector3(0, -1, 0);
  2822. }
  2823. else if (vt.x !== 1) {
  2824. point = new Vector3(1, 0, 0);
  2825. }
  2826. else if (vt.z !== 1) {
  2827. point = new Vector3(0, 0, 1);
  2828. }
  2829. normal0 = Vector3.Cross(vt, point);
  2830. }
  2831. else {
  2832. normal0 = Vector3.Cross(vt, va);
  2833. Vector3.CrossToRef(normal0, vt, normal0);
  2834. }
  2835. normal0.normalize();
  2836. return normal0;
  2837. };
  2838. return Path3D;
  2839. })();
  2840. BABYLON.Path3D = Path3D;
  2841. var Curve3 = (function () {
  2842. function Curve3(points) {
  2843. this._length = 0;
  2844. this._points = points;
  2845. this._length = this._computeLength(points);
  2846. }
  2847. // QuadraticBezier(origin_V3, control_V3, destination_V3, nbPoints)
  2848. Curve3.CreateQuadraticBezier = function (v0, v1, v2, nbPoints) {
  2849. nbPoints = nbPoints > 2 ? nbPoints : 3;
  2850. var bez = new Array();
  2851. var equation = function (t, val0, val1, val2) {
  2852. var res = (1 - t) * (1 - t) * val0 + 2 * t * (1 - t) * val1 + t * t * val2;
  2853. return res;
  2854. };
  2855. for (var i = 0; i <= nbPoints; i++) {
  2856. 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)));
  2857. }
  2858. return new Curve3(bez);
  2859. };
  2860. // CubicBezier(origin_V3, control1_V3, control2_V3, destination_V3, nbPoints)
  2861. Curve3.CreateCubicBezier = function (v0, v1, v2, v3, nbPoints) {
  2862. nbPoints = nbPoints > 3 ? nbPoints : 4;
  2863. var bez = new Array();
  2864. var equation = function (t, val0, val1, val2, val3) {
  2865. 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;
  2866. return res;
  2867. };
  2868. for (var i = 0; i <= nbPoints; i++) {
  2869. 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)));
  2870. }
  2871. return new Curve3(bez);
  2872. };
  2873. // HermiteSpline(origin_V3, originTangent_V3, destination_V3, destinationTangent_V3, nbPoints)
  2874. Curve3.CreateHermiteSpline = function (p1, t1, p2, t2, nbPoints) {
  2875. var hermite = new Array();
  2876. var step = 1 / nbPoints;
  2877. for (var i = 0; i <= nbPoints; i++) {
  2878. hermite.push(BABYLON.Vector3.Hermite(p1, t1, p2, t2, i * step));
  2879. }
  2880. return new Curve3(hermite);
  2881. };
  2882. Curve3.prototype.getPoints = function () {
  2883. return this._points;
  2884. };
  2885. Curve3.prototype.length = function () {
  2886. return this._length;
  2887. };
  2888. Curve3.prototype.continue = function (curve) {
  2889. var lastPoint = this._points[this._points.length - 1];
  2890. var continuedPoints = this._points.slice();
  2891. var curvePoints = curve.getPoints();
  2892. for (var i = 1; i < curvePoints.length; i++) {
  2893. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  2894. }
  2895. var continuedCurve = new Curve3(continuedPoints);
  2896. return continuedCurve;
  2897. };
  2898. Curve3.prototype._computeLength = function (path) {
  2899. var l = 0;
  2900. for (var i = 1; i < path.length; i++) {
  2901. l += (path[i].subtract(path[i - 1])).length();
  2902. }
  2903. return l;
  2904. };
  2905. return Curve3;
  2906. })();
  2907. BABYLON.Curve3 = Curve3;
  2908. // Vertex formats
  2909. var PositionNormalVertex = (function () {
  2910. function PositionNormalVertex(position, normal) {
  2911. if (position === void 0) { position = Vector3.Zero(); }
  2912. if (normal === void 0) { normal = Vector3.Up(); }
  2913. this.position = position;
  2914. this.normal = normal;
  2915. }
  2916. PositionNormalVertex.prototype.clone = function () {
  2917. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  2918. };
  2919. return PositionNormalVertex;
  2920. })();
  2921. BABYLON.PositionNormalVertex = PositionNormalVertex;
  2922. var PositionNormalTextureVertex = (function () {
  2923. function PositionNormalTextureVertex(position, normal, uv) {
  2924. if (position === void 0) { position = Vector3.Zero(); }
  2925. if (normal === void 0) { normal = Vector3.Up(); }
  2926. if (uv === void 0) { uv = Vector2.Zero(); }
  2927. this.position = position;
  2928. this.normal = normal;
  2929. this.uv = uv;
  2930. }
  2931. PositionNormalTextureVertex.prototype.clone = function () {
  2932. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  2933. };
  2934. return PositionNormalTextureVertex;
  2935. })();
  2936. BABYLON.PositionNormalTextureVertex = PositionNormalTextureVertex;
  2937. // SIMD
  2938. var previousMultiplyToArray = Matrix.prototype.multiplyToArray;
  2939. var previousInvertToRef = Matrix.prototype.invertToRef;
  2940. var previousLookAtLHToRef = Matrix.LookAtLHToRef;
  2941. var previousTransformCoordinatesToRef = Vector3.TransformCoordinatesToRef;
  2942. var previousTransformCoordinatesFromFloatsToRef = Vector3.TransformCoordinatesFromFloatsToRef;
  2943. var SIMDHelper = (function () {
  2944. function SIMDHelper() {
  2945. }
  2946. Object.defineProperty(SIMDHelper, "IsEnabled", {
  2947. get: function () {
  2948. return SIMDHelper._isEnabled;
  2949. },
  2950. enumerable: true,
  2951. configurable: true
  2952. });
  2953. SIMDHelper.DisableSIMD = function () {
  2954. // Replace functions
  2955. Matrix.prototype.multiplyToArray = previousMultiplyToArray;
  2956. Matrix.prototype.invertToRef = previousInvertToRef;
  2957. Matrix.LookAtLHToRef = previousLookAtLHToRef;
  2958. Vector3.TransformCoordinatesToRef = previousTransformCoordinatesToRef;
  2959. Vector3.TransformCoordinatesFromFloatsToRef = previousTransformCoordinatesFromFloatsToRef;
  2960. SIMDHelper._isEnabled = false;
  2961. };
  2962. SIMDHelper.EnableSIMD = function () {
  2963. if (window.SIMD === undefined) {
  2964. return;
  2965. }
  2966. // Replace functions
  2967. Matrix.prototype.multiplyToArray = Matrix.prototype.multiplyToArraySIMD;
  2968. Matrix.prototype.invertToRef = Matrix.prototype.invertToRefSIMD;
  2969. Matrix.LookAtLHToRef = Matrix.LookAtLHToRefSIMD;
  2970. Vector3.TransformCoordinatesToRef = Vector3.TransformCoordinatesToRefSIMD;
  2971. Vector3.TransformCoordinatesFromFloatsToRef = Vector3.TransformCoordinatesFromFloatsToRefSIMD;
  2972. Object.defineProperty(BABYLON.Vector3.prototype, "x", {
  2973. get: function () {
  2974. return this._data[0];
  2975. },
  2976. set: function (value) {
  2977. if (!this._data) {
  2978. this._data = new Float32Array(3);
  2979. }
  2980. this._data[0] = value;
  2981. }
  2982. });
  2983. Object.defineProperty(BABYLON.Vector3.prototype, "y", {
  2984. get: function () {
  2985. return this._data[1];
  2986. },
  2987. set: function (value) {
  2988. this._data[1] = value;
  2989. }
  2990. });
  2991. Object.defineProperty(BABYLON.Vector3.prototype, "z", {
  2992. get: function () {
  2993. return this._data[2];
  2994. },
  2995. set: function (value) {
  2996. this._data[2] = value;
  2997. }
  2998. });
  2999. SIMDHelper._isEnabled = true;
  3000. };
  3001. SIMDHelper._isEnabled = false;
  3002. return SIMDHelper;
  3003. })();
  3004. BABYLON.SIMDHelper = SIMDHelper;
  3005. if (window.SIMD !== undefined && window.SIMD.float32x4 && window.SIMD.float32x4.swizzle) {
  3006. SIMDHelper.EnableSIMD();
  3007. }
  3008. })(BABYLON || (BABYLON = {}));
  3009. //# sourceMappingURL=babylon.math.js.map