babylon.math.js 124 KB

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