babylon.math.js 130 KB

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