babylon.math.js 130 KB

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