babylon.math.js 140 KB

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