babylon.math.js 126 KB

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