babylon.math.js 134 KB

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