babylon.math.js 134 KB

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