babylon.math.js 131 KB

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