babylon.math.js 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Color3 = (function () {
  4. function Color3(r, g, b) {
  5. if (typeof r === "undefined") { r = 0; }
  6. if (typeof g === "undefined") { g = 0; }
  7. if (typeof b === "undefined") { b = 0; }
  8. this.r = r;
  9. this.g = g;
  10. this.b = b;
  11. }
  12. Color3.prototype.toString = function () {
  13. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  14. };
  15. // Operators
  16. Color3.prototype.toArray = function (array, index) {
  17. if (index === undefined) {
  18. index = 0;
  19. }
  20. array[index] = this.r;
  21. array[index + 1] = this.g;
  22. array[index + 2] = this.b;
  23. };
  24. Color3.prototype.toColor4 = function (alpha) {
  25. if (typeof alpha === "undefined") { alpha = 1; }
  26. return new Color4(this.r, this.g, this.b, alpha);
  27. };
  28. Color3.prototype.asArray = function () {
  29. var result = [];
  30. this.toArray(result, 0);
  31. return result;
  32. };
  33. Color3.prototype.toLuminance = function () {
  34. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  35. };
  36. Color3.prototype.multiply = function (otherColor) {
  37. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  38. };
  39. Color3.prototype.multiplyToRef = function (otherColor, result) {
  40. result.r = this.r * otherColor.r;
  41. result.g = this.g * otherColor.g;
  42. result.b = this.b * otherColor.b;
  43. };
  44. Color3.prototype.equals = function (otherColor) {
  45. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  46. };
  47. Color3.prototype.scale = function (scale) {
  48. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  49. };
  50. Color3.prototype.scaleToRef = function (scale, result) {
  51. result.r = this.r * scale;
  52. result.g = this.g * scale;
  53. result.b = this.b * scale;
  54. };
  55. Color3.prototype.add = function (otherColor) {
  56. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  57. };
  58. Color3.prototype.addToRef = function (otherColor, result) {
  59. result.r = this.r + otherColor.r;
  60. result.g = this.g + otherColor.g;
  61. result.b = this.b + otherColor.b;
  62. };
  63. Color3.prototype.subtract = function (otherColor) {
  64. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  65. };
  66. Color3.prototype.subtractToRef = function (otherColor, result) {
  67. result.r = this.r - otherColor.r;
  68. result.g = this.g - otherColor.g;
  69. result.b = this.b - otherColor.b;
  70. };
  71. Color3.prototype.clone = function () {
  72. return new Color3(this.r, this.g, this.b);
  73. };
  74. Color3.prototype.copyFrom = function (source) {
  75. this.r = source.r;
  76. this.g = source.g;
  77. this.b = source.b;
  78. };
  79. Color3.prototype.copyFromFloats = function (r, g, b) {
  80. this.r = r;
  81. this.g = g;
  82. this.b = b;
  83. };
  84. // Statics
  85. Color3.FromArray = function (array) {
  86. return new Color3(array[0], array[1], array[2]);
  87. };
  88. Color3.FromInts = function (r, g, b) {
  89. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  90. };
  91. Color3.Lerp = function (start, end, amount) {
  92. var r = start.r + ((end.r - start.r) * amount);
  93. var g = start.g + ((end.g - start.g) * amount);
  94. var b = start.b + ((end.b - start.b) * amount);
  95. return new Color3(r, g, b);
  96. };
  97. Color3.Red = function () {
  98. return new Color3(1, 0, 0);
  99. };
  100. Color3.Green = function () {
  101. return new Color3(0, 1, 0);
  102. };
  103. Color3.Blue = function () {
  104. return new Color3(0, 0, 1);
  105. };
  106. Color3.Black = function () {
  107. return new Color3(0, 0, 0);
  108. };
  109. Color3.White = function () {
  110. return new Color3(1, 1, 1);
  111. };
  112. Color3.Purple = function () {
  113. return new Color3(0.5, 0, 0.5);
  114. };
  115. Color3.Magenta = function () {
  116. return new Color3(1, 0, 1);
  117. };
  118. Color3.Yellow = function () {
  119. return new Color3(1, 1, 0);
  120. };
  121. Color3.Gray = function () {
  122. return new Color3(0.5, 0.5, 0.5);
  123. };
  124. return Color3;
  125. })();
  126. BABYLON.Color3 = Color3;
  127. var Color4 = (function () {
  128. function Color4(r, g, b, a) {
  129. this.r = r;
  130. this.g = g;
  131. this.b = b;
  132. this.a = a;
  133. }
  134. // Operators
  135. Color4.prototype.addInPlace = function (right) {
  136. this.r += right.r;
  137. this.g += right.g;
  138. this.b += right.b;
  139. this.a += right.a;
  140. };
  141. Color4.prototype.asArray = function () {
  142. var result = [];
  143. this.toArray(result, 0);
  144. return result;
  145. };
  146. Color4.prototype.toArray = function (array, index) {
  147. if (index === undefined) {
  148. index = 0;
  149. }
  150. array[index] = this.r;
  151. array[index + 1] = this.g;
  152. array[index + 2] = this.b;
  153. array[index + 3] = this.a;
  154. };
  155. Color4.prototype.add = function (right) {
  156. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  157. };
  158. Color4.prototype.subtract = function (right) {
  159. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  160. };
  161. Color4.prototype.subtractToRef = function (right, result) {
  162. result.r = this.r - right.r;
  163. result.g = this.g - right.g;
  164. result.b = this.b - right.b;
  165. result.a = this.a - right.a;
  166. };
  167. Color4.prototype.scale = function (scale) {
  168. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  169. };
  170. Color4.prototype.scaleToRef = function (scale, result) {
  171. result.r = this.r * scale;
  172. result.g = this.g * scale;
  173. result.b = this.b * scale;
  174. result.a = this.a * scale;
  175. };
  176. Color4.prototype.toString = function () {
  177. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  178. };
  179. Color4.prototype.clone = function () {
  180. return new Color4(this.r, this.g, this.b, this.a);
  181. };
  182. // Statics
  183. Color4.Lerp = function (left, right, amount) {
  184. var result = new Color4(0, 0, 0, 0);
  185. Color4.LerpToRef(left, right, amount, result);
  186. return result;
  187. };
  188. Color4.LerpToRef = function (left, right, amount, result) {
  189. result.r = left.r + (right.r - left.r) * amount;
  190. result.g = left.g + (right.g - left.g) * amount;
  191. result.b = left.b + (right.b - left.b) * amount;
  192. result.a = left.a + (right.a - left.a) * amount;
  193. };
  194. Color4.FromArray = function (array, offset) {
  195. if (typeof offset === "undefined") { offset = 0; }
  196. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  197. };
  198. Color4.FromInts = function (r, g, b, a) {
  199. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  200. };
  201. return Color4;
  202. })();
  203. BABYLON.Color4 = Color4;
  204. var Vector2 = (function () {
  205. function Vector2(x, y) {
  206. this.x = x;
  207. this.y = y;
  208. }
  209. Vector2.prototype.toString = function () {
  210. return "{X: " + this.x + " Y:" + this.y + "}";
  211. };
  212. // Operators
  213. Vector2.prototype.toArray = function (array, index) {
  214. if (index === undefined) {
  215. index = 0;
  216. }
  217. array[index] = this.x;
  218. array[index + 1] = this.y;
  219. };
  220. Vector2.prototype.asArray = function () {
  221. var result = [];
  222. this.toArray(result, 0);
  223. return result;
  224. };
  225. Vector2.prototype.copyFrom = function (source) {
  226. this.x = source.x;
  227. this.y = source.y;
  228. };
  229. Vector2.prototype.copyFromFloats = function (x, y) {
  230. this.x = x;
  231. this.y = y;
  232. };
  233. Vector2.prototype.add = function (otherVector) {
  234. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  235. };
  236. Vector2.prototype.addVector3 = function (otherVector) {
  237. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  238. };
  239. Vector2.prototype.subtract = function (otherVector) {
  240. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  241. };
  242. Vector2.prototype.subtractInPlace = function (otherVector) {
  243. this.x -= otherVector.x;
  244. this.y -= otherVector.y;
  245. };
  246. Vector2.prototype.multiplyInPlace = function (otherVector) {
  247. this.x *= otherVector.x;
  248. this.y *= otherVector.y;
  249. };
  250. Vector2.prototype.multiply = function (otherVector) {
  251. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  252. };
  253. Vector2.prototype.multiplyToRef = function (otherVector, result) {
  254. result.x = this.x * otherVector.x;
  255. result.y = this.y * otherVector.y;
  256. };
  257. Vector2.prototype.multiplyByFloats = function (x, y) {
  258. return new Vector2(this.x * x, this.y * y);
  259. };
  260. Vector2.prototype.divide = function (otherVector) {
  261. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  262. };
  263. Vector2.prototype.divideToRef = function (otherVector, result) {
  264. result.x = this.x / otherVector.x;
  265. result.y = this.y / otherVector.y;
  266. };
  267. Vector2.prototype.negate = function () {
  268. return new Vector2(-this.x, -this.y);
  269. };
  270. Vector2.prototype.scaleInPlace = function (scale) {
  271. this.x *= scale;
  272. this.y *= scale;
  273. return this;
  274. };
  275. Vector2.prototype.scale = function (scale) {
  276. return new Vector2(this.x * scale, this.y * scale);
  277. };
  278. Vector2.prototype.equals = function (otherVector) {
  279. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  280. };
  281. // Properties
  282. Vector2.prototype.length = function () {
  283. return Math.sqrt(this.x * this.x + this.y * this.y);
  284. };
  285. Vector2.prototype.lengthSquared = function () {
  286. return (this.x * this.x + this.y * this.y);
  287. };
  288. // Methods
  289. Vector2.prototype.normalize = function () {
  290. var len = this.length();
  291. if (len === 0)
  292. return this;
  293. var num = 1.0 / len;
  294. this.x *= num;
  295. this.y *= num;
  296. return this;
  297. };
  298. Vector2.prototype.clone = function () {
  299. return new Vector2(this.x, this.y);
  300. };
  301. // Statics
  302. Vector2.Zero = function () {
  303. return new Vector2(0, 0);
  304. };
  305. Vector2.FromArray = function (array, offset) {
  306. if (!offset) {
  307. offset = 0;
  308. }
  309. return new Vector2(array[offset], array[offset + 1]);
  310. };
  311. Vector2.FromArrayToRef = function (array, offset, result) {
  312. result.x = array[offset];
  313. result.y = array[offset + 1];
  314. };
  315. Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  316. var squared = amount * amount;
  317. var cubed = amount * squared;
  318. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) + (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) + ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  319. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) + (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) + ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  320. return new Vector2(x, y);
  321. };
  322. Vector2.Clamp = function (value, min, max) {
  323. var x = value.x;
  324. x = (x > max.x) ? max.x : x;
  325. x = (x < min.x) ? min.x : x;
  326. var y = value.y;
  327. y = (y > max.y) ? max.y : y;
  328. y = (y < min.y) ? min.y : y;
  329. return new Vector2(x, y);
  330. };
  331. Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  332. var squared = amount * amount;
  333. var cubed = amount * squared;
  334. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  335. var part2 = (-2.0 * cubed) + (3.0 * squared);
  336. var part3 = (cubed - (2.0 * squared)) + amount;
  337. var part4 = cubed - squared;
  338. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  339. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  340. return new Vector2(x, y);
  341. };
  342. Vector2.Lerp = function (start, end, amount) {
  343. var x = start.x + ((end.x - start.x) * amount);
  344. var y = start.y + ((end.y - start.y) * amount);
  345. return new Vector2(x, y);
  346. };
  347. Vector2.Dot = function (left, right) {
  348. return left.x * right.x + left.y * right.y;
  349. };
  350. Vector2.Normalize = function (vector) {
  351. var newVector = vector.clone();
  352. newVector.normalize();
  353. return newVector;
  354. };
  355. Vector2.Minimize = function (left, right) {
  356. var x = (left.x < right.x) ? left.x : right.x;
  357. var y = (left.y < right.y) ? left.y : right.y;
  358. return new Vector2(x, y);
  359. };
  360. Vector2.Maximize = function (left, right) {
  361. var x = (left.x > right.x) ? left.x : right.x;
  362. var y = (left.y > right.y) ? left.y : right.y;
  363. return new Vector2(x, y);
  364. };
  365. Vector2.Transform = function (vector, transformation) {
  366. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  367. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  368. return new Vector2(x, y);
  369. };
  370. Vector2.Distance = function (value1, value2) {
  371. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  372. };
  373. Vector2.DistanceSquared = function (value1, value2) {
  374. var x = value1.x - value2.x;
  375. var y = value1.y - value2.y;
  376. return (x * x) + (y * y);
  377. };
  378. return Vector2;
  379. })();
  380. BABYLON.Vector2 = Vector2;
  381. var Vector3 = (function () {
  382. function Vector3(x, y, z) {
  383. this.x = x;
  384. this.y = y;
  385. this.z = z;
  386. }
  387. Vector3.prototype.toString = function () {
  388. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  389. };
  390. // Operators
  391. Vector3.prototype.asArray = function () {
  392. var result = [];
  393. this.toArray(result, 0);
  394. return result;
  395. };
  396. Vector3.prototype.toArray = function (array, index) {
  397. if (index === undefined) {
  398. index = 0;
  399. }
  400. array[index] = this.x;
  401. array[index + 1] = this.y;
  402. array[index + 2] = this.z;
  403. };
  404. Vector3.prototype.addInPlace = function (otherVector) {
  405. this.x += otherVector.x;
  406. this.y += otherVector.y;
  407. this.z += otherVector.z;
  408. };
  409. Vector3.prototype.add = function (otherVector) {
  410. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  411. };
  412. Vector3.prototype.addToRef = function (otherVector, result) {
  413. result.x = this.x + otherVector.x;
  414. result.y = this.y + otherVector.y;
  415. result.z = this.z + otherVector.z;
  416. };
  417. Vector3.prototype.subtractInPlace = function (otherVector) {
  418. this.x -= otherVector.x;
  419. this.y -= otherVector.y;
  420. this.z -= otherVector.z;
  421. };
  422. Vector3.prototype.subtract = function (otherVector) {
  423. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  424. };
  425. Vector3.prototype.subtractToRef = function (otherVector, result) {
  426. result.x = this.x - otherVector.x;
  427. result.y = this.y - otherVector.y;
  428. result.z = this.z - otherVector.z;
  429. };
  430. Vector3.prototype.subtractFromFloats = function (x, y, z) {
  431. return new Vector3(this.x - x, this.y - y, this.z - z);
  432. };
  433. Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  434. result.x = this.x - x;
  435. result.y = this.y - y;
  436. result.z = this.z - z;
  437. };
  438. Vector3.prototype.negate = function () {
  439. return new Vector3(-this.x, -this.y, -this.z);
  440. };
  441. Vector3.prototype.scaleInPlace = function (scale) {
  442. this.x *= scale;
  443. this.y *= scale;
  444. this.z *= scale;
  445. return this;
  446. };
  447. Vector3.prototype.scale = function (scale) {
  448. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  449. };
  450. Vector3.prototype.scaleToRef = function (scale, result) {
  451. result.x = this.x * scale;
  452. result.y = this.y * scale;
  453. result.z = this.z * scale;
  454. };
  455. Vector3.prototype.equals = function (otherVector) {
  456. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  457. };
  458. Vector3.prototype.equalsWithEpsilon = function (otherVector) {
  459. return Math.abs(this.x - otherVector.x) < BABYLON.Engine.Epsilon && Math.abs(this.y - otherVector.y) < BABYLON.Engine.Epsilon && Math.abs(this.z - otherVector.z) < BABYLON.Engine.Epsilon;
  460. };
  461. Vector3.prototype.equalsToFloats = function (x, y, z) {
  462. return this.x === x && this.y === y && this.z === z;
  463. };
  464. Vector3.prototype.multiplyInPlace = function (otherVector) {
  465. this.x *= otherVector.x;
  466. this.y *= otherVector.y;
  467. this.z *= otherVector.z;
  468. };
  469. Vector3.prototype.multiply = function (otherVector) {
  470. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  471. };
  472. Vector3.prototype.multiplyToRef = function (otherVector, result) {
  473. result.x = this.x * otherVector.x;
  474. result.y = this.y * otherVector.y;
  475. result.z = this.z * otherVector.z;
  476. };
  477. Vector3.prototype.multiplyByFloats = function (x, y, z) {
  478. return new Vector3(this.x * x, this.y * y, this.z * z);
  479. };
  480. Vector3.prototype.divide = function (otherVector) {
  481. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  482. };
  483. Vector3.prototype.divideToRef = function (otherVector, result) {
  484. result.x = this.x / otherVector.x;
  485. result.y = this.y / otherVector.y;
  486. result.z = this.z / otherVector.z;
  487. };
  488. Vector3.prototype.MinimizeInPlace = function (other) {
  489. if (other.x < this.x)
  490. this.x = other.x;
  491. if (other.y < this.y)
  492. this.y = other.y;
  493. if (other.z < this.z)
  494. this.z = other.z;
  495. };
  496. Vector3.prototype.MaximizeInPlace = function (other) {
  497. if (other.x > this.x)
  498. this.x = other.x;
  499. if (other.y > this.y)
  500. this.y = other.y;
  501. if (other.z > this.z)
  502. this.z = other.z;
  503. };
  504. // Properties
  505. Vector3.prototype.length = function () {
  506. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  507. };
  508. Vector3.prototype.lengthSquared = function () {
  509. return (this.x * this.x + this.y * this.y + this.z * this.z);
  510. };
  511. // Methods
  512. Vector3.prototype.normalize = function () {
  513. var len = this.length();
  514. if (len === 0)
  515. return this;
  516. var num = 1.0 / len;
  517. this.x *= num;
  518. this.y *= num;
  519. this.z *= num;
  520. return this;
  521. };
  522. Vector3.prototype.clone = function () {
  523. return new Vector3(this.x, this.y, this.z);
  524. };
  525. Vector3.prototype.copyFrom = function (source) {
  526. this.x = source.x;
  527. this.y = source.y;
  528. this.z = source.z;
  529. };
  530. Vector3.prototype.copyFromFloats = function (x, y, z) {
  531. this.x = x;
  532. this.y = y;
  533. this.z = z;
  534. };
  535. // Statics
  536. Vector3.FromArray = function (array, offset) {
  537. if (!offset) {
  538. offset = 0;
  539. }
  540. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  541. };
  542. Vector3.FromArrayToRef = function (array, offset, result) {
  543. result.x = array[offset];
  544. result.y = array[offset + 1];
  545. result.z = array[offset + 2];
  546. };
  547. Vector3.FromFloatArrayToRef = function (array, offset, result) {
  548. result.x = array[offset];
  549. result.y = array[offset + 1];
  550. result.z = array[offset + 2];
  551. };
  552. Vector3.FromFloatsToRef = function (x, y, z, result) {
  553. result.x = x;
  554. result.y = y;
  555. result.z = z;
  556. };
  557. Vector3.Zero = function () {
  558. return new Vector3(0, 0, 0);
  559. };
  560. Vector3.Up = function () {
  561. return new Vector3(0, 1.0, 0);
  562. };
  563. Vector3.TransformCoordinates = function (vector, transformation) {
  564. var result = Vector3.Zero();
  565. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  566. return result;
  567. };
  568. Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  569. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  570. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  571. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  572. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  573. result.x = x / w;
  574. result.y = y / w;
  575. result.z = z / w;
  576. };
  577. Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  578. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  579. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  580. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  581. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  582. result.x = rx / rw;
  583. result.y = ry / rw;
  584. result.z = rz / rw;
  585. };
  586. Vector3.TransformNormal = function (vector, transformation) {
  587. var result = Vector3.Zero();
  588. Vector3.TransformNormalToRef(vector, transformation, result);
  589. return result;
  590. };
  591. Vector3.TransformNormalToRef = function (vector, transformation, result) {
  592. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  593. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  594. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  595. };
  596. Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  597. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  598. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  599. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  600. };
  601. Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  602. var squared = amount * amount;
  603. var cubed = amount * squared;
  604. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) + (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) + ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  605. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) + (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) + ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  606. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) + (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) + ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  607. return new Vector3(x, y, z);
  608. };
  609. Vector3.Clamp = function (value, min, max) {
  610. var x = value.x;
  611. x = (x > max.x) ? max.x : x;
  612. x = (x < min.x) ? min.x : x;
  613. var y = value.y;
  614. y = (y > max.y) ? max.y : y;
  615. y = (y < min.y) ? min.y : y;
  616. var z = value.z;
  617. z = (z > max.z) ? max.z : z;
  618. z = (z < min.z) ? min.z : z;
  619. return new Vector3(x, y, z);
  620. };
  621. Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  622. var squared = amount * amount;
  623. var cubed = amount * squared;
  624. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  625. var part2 = (-2.0 * cubed) + (3.0 * squared);
  626. var part3 = (cubed - (2.0 * squared)) + amount;
  627. var part4 = cubed - squared;
  628. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  629. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  630. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  631. return new Vector3(x, y, z);
  632. };
  633. Vector3.Lerp = function (start, end, amount) {
  634. var x = start.x + ((end.x - start.x) * amount);
  635. var y = start.y + ((end.y - start.y) * amount);
  636. var z = start.z + ((end.z - start.z) * amount);
  637. return new Vector3(x, y, z);
  638. };
  639. Vector3.Dot = function (left, right) {
  640. return (left.x * right.x + left.y * right.y + left.z * right.z);
  641. };
  642. Vector3.Cross = function (left, right) {
  643. var result = Vector3.Zero();
  644. Vector3.CrossToRef(left, right, result);
  645. return result;
  646. };
  647. Vector3.CrossToRef = function (left, right, result) {
  648. result.x = left.y * right.z - left.z * right.y;
  649. result.y = left.z * right.x - left.x * right.z;
  650. result.z = left.x * right.y - left.y * right.x;
  651. };
  652. Vector3.Normalize = function (vector) {
  653. var result = Vector3.Zero();
  654. Vector3.NormalizeToRef(vector, result);
  655. return result;
  656. };
  657. Vector3.NormalizeToRef = function (vector, result) {
  658. result.copyFrom(vector);
  659. result.normalize();
  660. };
  661. Vector3.Project = function (vector, world, transform, viewport) {
  662. var cw = viewport.width;
  663. var ch = viewport.height;
  664. var cx = viewport.x;
  665. var cy = viewport.y;
  666. 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);
  667. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  668. return Vector3.TransformCoordinates(vector, finalMatrix);
  669. };
  670. Vector3.UnprojectFromTransform = function (source, viewportWidth, viewportHeight, world, transform) {
  671. var matrix = world.multiply(transform);
  672. matrix.invert();
  673. source.x = source.x / viewportWidth * 2 - 1;
  674. source.y = -(source.y / viewportHeight * 2 - 1);
  675. var vector = Vector3.TransformCoordinates(source, matrix);
  676. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  677. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  678. vector = vector.scale(1.0 / num);
  679. }
  680. return vector;
  681. };
  682. Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  683. var matrix = world.multiply(view).multiply(projection);
  684. matrix.invert();
  685. source.x = source.x / viewportWidth * 2 - 1;
  686. source.y = -(source.y / viewportHeight * 2 - 1);
  687. var vector = Vector3.TransformCoordinates(source, matrix);
  688. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  689. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  690. vector = vector.scale(1.0 / num);
  691. }
  692. return vector;
  693. };
  694. Vector3.Minimize = function (left, right) {
  695. var min = left.clone();
  696. min.MinimizeInPlace(right);
  697. return min;
  698. };
  699. Vector3.Maximize = function (left, right) {
  700. var max = left.clone();
  701. max.MaximizeInPlace(right);
  702. return max;
  703. };
  704. Vector3.Distance = function (value1, value2) {
  705. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  706. };
  707. Vector3.DistanceSquared = function (value1, value2) {
  708. var x = value1.x - value2.x;
  709. var y = value1.y - value2.y;
  710. var z = value1.z - value2.z;
  711. return (x * x) + (y * y) + (z * z);
  712. };
  713. Vector3.Center = function (value1, value2) {
  714. var center = value1.add(value2);
  715. center.scaleInPlace(0.5);
  716. return center;
  717. };
  718. return Vector3;
  719. })();
  720. BABYLON.Vector3 = Vector3;
  721. //Vector4 class created for EulerAngle class conversion to Quaternion
  722. var Vector4 = (function () {
  723. function Vector4(x, y, z, w) {
  724. this.x = x;
  725. this.y = y;
  726. this.z = z;
  727. this.w = w;
  728. }
  729. Vector4.prototype.toString = function () {
  730. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "W:" + this.w + "}";
  731. };
  732. // Operators
  733. Vector4.prototype.asArray = function () {
  734. var result = [];
  735. this.toArray(result, 0);
  736. return result;
  737. };
  738. Vector4.prototype.toArray = function (array, index) {
  739. if (index === undefined) {
  740. index = 0;
  741. }
  742. array[index] = this.x;
  743. array[index + 1] = this.y;
  744. array[index + 2] = this.z;
  745. array[index + 3] = this.w;
  746. };
  747. Vector4.prototype.addInPlace = function (otherVector) {
  748. this.x += otherVector.x;
  749. this.y += otherVector.y;
  750. this.z += otherVector.z;
  751. this.w += otherVector.w;
  752. };
  753. Vector4.prototype.add = function (otherVector) {
  754. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  755. };
  756. Vector4.prototype.addToRef = function (otherVector, result) {
  757. result.x = this.x + otherVector.x;
  758. result.y = this.y + otherVector.y;
  759. result.z = this.z + otherVector.z;
  760. result.w = this.w + otherVector.w;
  761. };
  762. Vector4.prototype.subtractInPlace = function (otherVector) {
  763. this.x -= otherVector.x;
  764. this.y -= otherVector.y;
  765. this.z -= otherVector.z;
  766. this.w -= otherVector.w;
  767. };
  768. Vector4.prototype.subtract = function (otherVector) {
  769. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  770. };
  771. Vector4.prototype.subtractToRef = function (otherVector, result) {
  772. result.x = this.x - otherVector.x;
  773. result.y = this.y - otherVector.y;
  774. result.z = this.z - otherVector.z;
  775. result.w = this.w - otherVector.w;
  776. };
  777. Vector4.prototype.subtractFromFloats = function (x, y, z, w) {
  778. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  779. };
  780. Vector4.prototype.subtractFromFloatsToRef = function (x, y, z, w, result) {
  781. result.x = this.x - x;
  782. result.y = this.y - y;
  783. result.z = this.z - z;
  784. result.w = this.w - w;
  785. };
  786. Vector4.prototype.negate = function () {
  787. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  788. };
  789. Vector4.prototype.scaleInPlace = function (scale) {
  790. this.x *= scale;
  791. this.y *= scale;
  792. this.z *= scale;
  793. this.w *= scale;
  794. return this;
  795. };
  796. Vector4.prototype.scale = function (scale) {
  797. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  798. };
  799. Vector4.prototype.scaleToRef = function (scale, result) {
  800. result.x = this.x * scale;
  801. result.y = this.y * scale;
  802. result.z = this.z * scale;
  803. result.w = this.w * scale;
  804. };
  805. Vector4.prototype.equals = function (otherVector) {
  806. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  807. };
  808. Vector4.prototype.equalsWithEpsilon = function (otherVector) {
  809. return Math.abs(this.x - otherVector.x) < BABYLON.Engine.Epsilon && Math.abs(this.y - otherVector.y) < BABYLON.Engine.Epsilon && Math.abs(this.z - otherVector.z) < BABYLON.Engine.Epsilon && Math.abs(this.w - otherVector.w) < BABYLON.Engine.Epsilon;
  810. };
  811. Vector4.prototype.equalsToFloats = function (x, y, z, w) {
  812. return this.x === x && this.y === y && this.z === z && this.w === w;
  813. };
  814. Vector4.prototype.multiplyInPlace = function (otherVector) {
  815. this.x *= otherVector.x;
  816. this.y *= otherVector.y;
  817. this.z *= otherVector.z;
  818. this.w *= otherVector.w;
  819. };
  820. Vector4.prototype.multiply = function (otherVector) {
  821. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  822. };
  823. Vector4.prototype.multiplyToRef = function (otherVector, result) {
  824. result.x = this.x * otherVector.x;
  825. result.y = this.y * otherVector.y;
  826. result.z = this.z * otherVector.z;
  827. result.w = this.w * otherVector.w;
  828. };
  829. Vector4.prototype.multiplyByFloats = function (x, y, z, w) {
  830. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  831. };
  832. Vector4.prototype.divide = function (otherVector) {
  833. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  834. };
  835. Vector4.prototype.divideToRef = function (otherVector, result) {
  836. result.x = this.x / otherVector.x;
  837. result.y = this.y / otherVector.y;
  838. result.z = this.z / otherVector.z;
  839. result.w = this.w / otherVector.w;
  840. };
  841. Vector4.prototype.MinimizeInPlace = function (other) {
  842. if (other.x < this.x)
  843. this.x = other.x;
  844. if (other.y < this.y)
  845. this.y = other.y;
  846. if (other.z < this.z)
  847. this.z = other.z;
  848. if (other.w < this.w)
  849. this.w = other.w;
  850. };
  851. Vector4.prototype.MaximizeInPlace = function (other) {
  852. if (other.x > this.x)
  853. this.x = other.x;
  854. if (other.y > this.y)
  855. this.y = other.y;
  856. if (other.z > this.z)
  857. this.z = other.z;
  858. if (other.w > this.w)
  859. this.w = other.w;
  860. };
  861. // Properties
  862. Vector4.prototype.length = function () {
  863. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  864. };
  865. Vector4.prototype.lengthSquared = function () {
  866. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  867. };
  868. // Methods
  869. Vector4.prototype.normalize = function () {
  870. var len = this.length();
  871. if (len === 0)
  872. return this;
  873. var num = 1.0 / len;
  874. this.x *= num;
  875. this.y *= num;
  876. this.z *= num;
  877. this.w *= num;
  878. return this;
  879. };
  880. Vector4.prototype.clone = function () {
  881. return new Vector4(this.x, this.y, this.z, this.w);
  882. };
  883. Vector4.prototype.copyFrom = function (source) {
  884. this.x = source.x;
  885. this.y = source.y;
  886. this.z = source.z;
  887. this.w = source.w;
  888. };
  889. Vector4.prototype.copyFromFloats = function (x, y, z, w) {
  890. this.x = x;
  891. this.y = y;
  892. this.z = z;
  893. this.w = w;
  894. };
  895. // Statics
  896. Vector4.FromArray = function (array, offset) {
  897. if (!offset) {
  898. offset = 0;
  899. }
  900. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  901. };
  902. Vector4.FromArrayToRef = function (array, offset, result) {
  903. result.x = array[offset];
  904. result.y = array[offset + 1];
  905. result.z = array[offset + 2];
  906. result.w = array[offset + 3];
  907. };
  908. Vector4.FromFloatArrayToRef = function (array, offset, result) {
  909. result.x = array[offset];
  910. result.y = array[offset + 1];
  911. result.z = array[offset + 2];
  912. result.w = array[offset + 3];
  913. };
  914. Vector4.FromFloatsToRef = function (x, y, z, w, result) {
  915. result.x = x;
  916. result.y = y;
  917. result.z = z;
  918. result.w = w;
  919. };
  920. Vector4.Zero = function () {
  921. return new Vector4(0, 0, 0, 0);
  922. };
  923. Vector4.Normalize = function (vector) {
  924. var result = Vector4.Zero();
  925. Vector4.NormalizeToRef(vector, result);
  926. return result;
  927. };
  928. Vector4.NormalizeToRef = function (vector, result) {
  929. result.copyFrom(vector);
  930. result.normalize();
  931. };
  932. Vector4.Minimize = function (left, right) {
  933. var min = left.clone();
  934. min.MinimizeInPlace(right);
  935. return min;
  936. };
  937. Vector4.Maximize = function (left, right) {
  938. var max = left.clone();
  939. max.MaximizeInPlace(right);
  940. return max;
  941. };
  942. Vector4.Distance = function (value1, value2) {
  943. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  944. };
  945. Vector4.DistanceSquared = function (value1, value2) {
  946. var x = value1.x - value2.x;
  947. var y = value1.y - value2.y;
  948. var z = value1.z - value2.z;
  949. var w = value1.w - value2.w;
  950. return (x * x) + (y * y) + (z * z) + (w * w);
  951. };
  952. Vector4.Center = function (value1, value2) {
  953. var center = value1.add(value2);
  954. center.scaleInPlace(0.5);
  955. return center;
  956. };
  957. return Vector4;
  958. })();
  959. BABYLON.Vector4 = Vector4;
  960. var Quaternion = (function () {
  961. function Quaternion(x, y, z, w) {
  962. if (typeof x === "undefined") { x = 0; }
  963. if (typeof y === "undefined") { y = 0; }
  964. if (typeof z === "undefined") { z = 0; }
  965. if (typeof w === "undefined") { w = 1; }
  966. this.x = x;
  967. this.y = y;
  968. this.z = z;
  969. this.w = w;
  970. }
  971. Quaternion.prototype.toString = function () {
  972. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  973. };
  974. Quaternion.prototype.asArray = function () {
  975. return [this.x, this.y, this.z, this.w];
  976. };
  977. Quaternion.prototype.equals = function (otherQuaternion) {
  978. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  979. };
  980. Quaternion.prototype.clone = function () {
  981. return new Quaternion(this.x, this.y, this.z, this.w);
  982. };
  983. Quaternion.prototype.copyFrom = function (other) {
  984. this.x = other.x;
  985. this.y = other.y;
  986. this.z = other.z;
  987. this.w = other.w;
  988. };
  989. Quaternion.prototype.copyFromFloats = function (x, y, z, w) {
  990. this.x = x;
  991. this.y = y;
  992. this.z = z;
  993. this.w = w;
  994. };
  995. Quaternion.prototype.add = function (other) {
  996. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  997. };
  998. Quaternion.prototype.subtract = function (other) {
  999. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  1000. };
  1001. Quaternion.prototype.scale = function (value) {
  1002. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  1003. };
  1004. Quaternion.prototype.multiply = function (q1) {
  1005. var result = new Quaternion(0, 0, 0, 1.0);
  1006. this.multiplyToRef(q1, result);
  1007. return result;
  1008. };
  1009. Quaternion.prototype.multiplyToRef = function (q1, result) {
  1010. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1011. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1012. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1013. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1014. };
  1015. Quaternion.prototype.length = function () {
  1016. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1017. };
  1018. Quaternion.prototype.normalize = function () {
  1019. var length = 1.0 / this.length();
  1020. this.x *= length;
  1021. this.y *= length;
  1022. this.z *= length;
  1023. this.w *= length;
  1024. };
  1025. Quaternion.prototype.toEulerAngles = function () {
  1026. var result = Vector3.Zero();
  1027. this.toEulerAnglesToRef(result);
  1028. return result;
  1029. };
  1030. Quaternion.prototype.toEulerAnglesToRef = function (result) {
  1031. //result is an EulerAngles in the in the z-x-z convention
  1032. var qx = this.x;
  1033. var qy = this.y;
  1034. var qz = this.z;
  1035. var qw = this.w;
  1036. var qxy = qx * qy;
  1037. var qxz = qx * qz;
  1038. var qwy = qw * qy;
  1039. var qwz = qw * qz;
  1040. var qwx = qw * qx;
  1041. var qyz = qy * qz;
  1042. var sqx = qx * qx;
  1043. var sqy = qy * qy;
  1044. var determinant = sqx + sqy;
  1045. if (determinant !== 0.000 && determinant !== 1.000) {
  1046. result.x = Math.atan2(qxz + qwy, qwx - qyz);
  1047. result.y = Math.acos(1 - 2 * determinant);
  1048. result.z = Math.atan2(qxz - qwy, qwx + qyz);
  1049. } else {
  1050. if (determinant === 0.0) {
  1051. result.x = 0.0;
  1052. result.y = 0.0;
  1053. result.z = Math.atan2(qxy - qwz, 0.5 - sqy - qz * qz); //actually, degeneracy gives us choice with x+z=Math.atan2(qxy-qwz,0.5-sqy-qz*qz)
  1054. } else {
  1055. result.x = Math.atan2(qxy - qwz, 0.5 - sqy - qz * qz); //actually, degeneracy gives us choice with x-z=Math.atan2(qxy-qwz,0.5-sqy-qz*qz)
  1056. result.y = Math.PI;
  1057. result.z = 0.0;
  1058. }
  1059. }
  1060. };
  1061. Quaternion.prototype.toRotationMatrix = function (result) {
  1062. var xx = this.x * this.x;
  1063. var yy = this.y * this.y;
  1064. var zz = this.z * this.z;
  1065. var xy = this.x * this.y;
  1066. var zw = this.z * this.w;
  1067. var zx = this.z * this.x;
  1068. var yw = this.y * this.w;
  1069. var yz = this.y * this.z;
  1070. var xw = this.x * this.w;
  1071. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1072. result.m[1] = 2.0 * (xy + zw);
  1073. result.m[2] = 2.0 * (zx - yw);
  1074. result.m[3] = 0;
  1075. result.m[4] = 2.0 * (xy - zw);
  1076. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1077. result.m[6] = 2.0 * (yz + xw);
  1078. result.m[7] = 0;
  1079. result.m[8] = 2.0 * (zx + yw);
  1080. result.m[9] = 2.0 * (yz - xw);
  1081. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1082. result.m[11] = 0;
  1083. result.m[12] = 0;
  1084. result.m[13] = 0;
  1085. result.m[14] = 0;
  1086. result.m[15] = 1.0;
  1087. };
  1088. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  1089. var data = matrix.m;
  1090. var m11 = data[0], m12 = data[4], m13 = data[8];
  1091. var m21 = data[1], m22 = data[5], m23 = data[9];
  1092. var m31 = data[2], m32 = data[6], m33 = data[10];
  1093. var trace = m11 + m22 + m33;
  1094. var s;
  1095. if (trace > 0) {
  1096. s = 0.5 / Math.sqrt(trace + 1.0);
  1097. this.w = 0.25 / s;
  1098. this.x = (m32 - m23) * s;
  1099. this.y = (m13 - m31) * s;
  1100. this.z = (m21 - m12) * s;
  1101. return;
  1102. }
  1103. if (m11 > m22 && m11 > m33) {
  1104. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1105. this.w = (m32 - m23) / s;
  1106. this.x = 0.25 * s;
  1107. this.y = (m12 + m21) / s;
  1108. this.z = (m13 + m31) / s;
  1109. return;
  1110. }
  1111. if (m22 > m33) {
  1112. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1113. this.w = (m13 - m31) / s;
  1114. this.x = (m12 + m21) / s;
  1115. this.y = 0.25 * s;
  1116. this.z = (m23 + m32) / s;
  1117. return;
  1118. }
  1119. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1120. this.w = (m21 - m12) / s;
  1121. this.x = (m13 + m31) / s;
  1122. this.y = (m23 + m32) / s;
  1123. this.z = 0.25 * s;
  1124. };
  1125. // Statics
  1126. Quaternion.Inverse = function (q) {
  1127. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1128. };
  1129. Quaternion.Identity = function () {
  1130. return new Quaternion(0, 0, 0, 1);
  1131. };
  1132. Quaternion.RotationAxis = function (axis, angle) {
  1133. var result = new Quaternion();
  1134. var sin = Math.sin(angle / 2);
  1135. result.w = Math.cos(angle / 2);
  1136. result.x = axis.x * sin;
  1137. result.y = axis.y * sin;
  1138. result.z = axis.z * sin;
  1139. return result;
  1140. };
  1141. Quaternion.FromArray = function (array, offset) {
  1142. if (!offset) {
  1143. offset = 0;
  1144. }
  1145. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1146. };
  1147. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1148. var result = new Quaternion();
  1149. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1150. return result;
  1151. };
  1152. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1153. var halfRoll = roll * 0.5;
  1154. var halfPitch = pitch * 0.5;
  1155. var halfYaw = yaw * 0.5;
  1156. var sinRoll = Math.sin(halfRoll);
  1157. var cosRoll = Math.cos(halfRoll);
  1158. var sinPitch = Math.sin(halfPitch);
  1159. var cosPitch = Math.cos(halfPitch);
  1160. var sinYaw = Math.sin(halfYaw);
  1161. var cosYaw = Math.cos(halfYaw);
  1162. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1163. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1164. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1165. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1166. };
  1167. Quaternion.Slerp = function (left, right, amount) {
  1168. var num2;
  1169. var num3;
  1170. var num = amount;
  1171. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1172. var flag = false;
  1173. if (num4 < 0) {
  1174. flag = true;
  1175. num4 = -num4;
  1176. }
  1177. if (num4 > 0.999999) {
  1178. num3 = 1 - num;
  1179. num2 = flag ? -num : num;
  1180. } else {
  1181. var num5 = Math.acos(num4);
  1182. var num6 = (1.0 / Math.sin(num5));
  1183. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1184. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1185. }
  1186. 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));
  1187. };
  1188. return Quaternion;
  1189. })();
  1190. BABYLON.Quaternion = Quaternion;
  1191. var Matrix = (function () {
  1192. function Matrix() {
  1193. this.m = new Float32Array(16);
  1194. }
  1195. // Properties
  1196. Matrix.prototype.isIdentity = function () {
  1197. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1198. return false;
  1199. if (this.m[1] !== 0.0 || this.m[2] !== 0.0 || this.m[3] !== 0.0 || this.m[4] !== 0.0 || this.m[6] !== 0.0 || this.m[7] !== 0.0 || this.m[8] !== 0.0 || this.m[9] !== 0.0 || this.m[11] !== 0.0 || this.m[12] !== 0.0 || this.m[13] !== 0.0 || this.m[14] !== 0.0)
  1200. return false;
  1201. return true;
  1202. };
  1203. Matrix.prototype.determinant = function () {
  1204. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1205. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1206. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1207. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1208. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1209. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1210. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) - (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) - (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  1211. };
  1212. // Methods
  1213. Matrix.prototype.toArray = function () {
  1214. return this.m;
  1215. };
  1216. Matrix.prototype.asArray = function () {
  1217. return this.toArray();
  1218. };
  1219. Matrix.prototype.invert = function () {
  1220. this.invertToRef(this);
  1221. };
  1222. Matrix.prototype.invertToRef = function (other) {
  1223. var l1 = this.m[0];
  1224. var l2 = this.m[1];
  1225. var l3 = this.m[2];
  1226. var l4 = this.m[3];
  1227. var l5 = this.m[4];
  1228. var l6 = this.m[5];
  1229. var l7 = this.m[6];
  1230. var l8 = this.m[7];
  1231. var l9 = this.m[8];
  1232. var l10 = this.m[9];
  1233. var l11 = this.m[10];
  1234. var l12 = this.m[11];
  1235. var l13 = this.m[12];
  1236. var l14 = this.m[13];
  1237. var l15 = this.m[14];
  1238. var l16 = this.m[15];
  1239. var l17 = (l11 * l16) - (l12 * l15);
  1240. var l18 = (l10 * l16) - (l12 * l14);
  1241. var l19 = (l10 * l15) - (l11 * l14);
  1242. var l20 = (l9 * l16) - (l12 * l13);
  1243. var l21 = (l9 * l15) - (l11 * l13);
  1244. var l22 = (l9 * l14) - (l10 * l13);
  1245. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1246. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1247. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1248. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1249. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1250. var l28 = (l7 * l16) - (l8 * l15);
  1251. var l29 = (l6 * l16) - (l8 * l14);
  1252. var l30 = (l6 * l15) - (l7 * l14);
  1253. var l31 = (l5 * l16) - (l8 * l13);
  1254. var l32 = (l5 * l15) - (l7 * l13);
  1255. var l33 = (l5 * l14) - (l6 * l13);
  1256. var l34 = (l7 * l12) - (l8 * l11);
  1257. var l35 = (l6 * l12) - (l8 * l10);
  1258. var l36 = (l6 * l11) - (l7 * l10);
  1259. var l37 = (l5 * l12) - (l8 * l9);
  1260. var l38 = (l5 * l11) - (l7 * l9);
  1261. var l39 = (l5 * l10) - (l6 * l9);
  1262. other.m[0] = l23 * l27;
  1263. other.m[4] = l24 * l27;
  1264. other.m[8] = l25 * l27;
  1265. other.m[12] = l26 * l27;
  1266. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1267. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1268. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1269. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1270. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1271. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1272. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1273. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1274. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1275. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1276. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1277. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1278. };
  1279. Matrix.prototype.setTranslation = function (vector3) {
  1280. this.m[12] = vector3.x;
  1281. this.m[13] = vector3.y;
  1282. this.m[14] = vector3.z;
  1283. };
  1284. Matrix.prototype.multiply = function (other) {
  1285. var result = new Matrix();
  1286. this.multiplyToRef(other, result);
  1287. return result;
  1288. };
  1289. Matrix.prototype.copyFrom = function (other) {
  1290. for (var index = 0; index < 16; index++) {
  1291. this.m[index] = other.m[index];
  1292. }
  1293. };
  1294. Matrix.prototype.copyToArray = function (array, offset) {
  1295. if (typeof offset === "undefined") { offset = 0; }
  1296. for (var index = 0; index < 16; index++) {
  1297. array[offset + index] = this.m[index];
  1298. }
  1299. };
  1300. Matrix.prototype.multiplyToRef = function (other, result) {
  1301. this.multiplyToArray(other, result.m, 0);
  1302. };
  1303. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1304. var tm0 = this.m[0];
  1305. var tm1 = this.m[1];
  1306. var tm2 = this.m[2];
  1307. var tm3 = this.m[3];
  1308. var tm4 = this.m[4];
  1309. var tm5 = this.m[5];
  1310. var tm6 = this.m[6];
  1311. var tm7 = this.m[7];
  1312. var tm8 = this.m[8];
  1313. var tm9 = this.m[9];
  1314. var tm10 = this.m[10];
  1315. var tm11 = this.m[11];
  1316. var tm12 = this.m[12];
  1317. var tm13 = this.m[13];
  1318. var tm14 = this.m[14];
  1319. var tm15 = this.m[15];
  1320. var om0 = other.m[0];
  1321. var om1 = other.m[1];
  1322. var om2 = other.m[2];
  1323. var om3 = other.m[3];
  1324. var om4 = other.m[4];
  1325. var om5 = other.m[5];
  1326. var om6 = other.m[6];
  1327. var om7 = other.m[7];
  1328. var om8 = other.m[8];
  1329. var om9 = other.m[9];
  1330. var om10 = other.m[10];
  1331. var om11 = other.m[11];
  1332. var om12 = other.m[12];
  1333. var om13 = other.m[13];
  1334. var om14 = other.m[14];
  1335. var om15 = other.m[15];
  1336. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1337. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1338. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1339. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1340. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1341. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1342. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1343. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1344. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1345. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1346. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1347. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1348. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1349. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1350. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1351. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1352. };
  1353. Matrix.prototype.equals = function (value) {
  1354. return value && (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] && this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] && this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] && this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1355. };
  1356. Matrix.prototype.clone = function () {
  1357. 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]);
  1358. };
  1359. Matrix.prototype.decompose = function (scale, rotation, translation) {
  1360. translation.x = this.m[12];
  1361. translation.y = this.m[13];
  1362. translation.z = this.m[14];
  1363. var xs = BABYLON.Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1364. var ys = BABYLON.Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1365. var zs = BABYLON.Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1366. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1367. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1368. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1369. if (scale.x == 0 || scale.y == 0 || scale.z == 0) {
  1370. rotation.x = 0;
  1371. rotation.y = 0;
  1372. rotation.z = 0;
  1373. rotation.w = 1;
  1374. return false;
  1375. }
  1376. var rotationMatrix = BABYLON.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);
  1377. rotation.fromRotationMatrix(rotationMatrix);
  1378. return true;
  1379. };
  1380. // Statics
  1381. Matrix.FromArray = function (array, offset) {
  1382. var result = new Matrix();
  1383. if (!offset) {
  1384. offset = 0;
  1385. }
  1386. Matrix.FromArrayToRef(array, offset, result);
  1387. return result;
  1388. };
  1389. Matrix.FromArrayToRef = function (array, offset, result) {
  1390. for (var index = 0; index < 16; index++) {
  1391. result.m[index] = array[index + offset];
  1392. }
  1393. };
  1394. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1395. result.m[0] = initialM11;
  1396. result.m[1] = initialM12;
  1397. result.m[2] = initialM13;
  1398. result.m[3] = initialM14;
  1399. result.m[4] = initialM21;
  1400. result.m[5] = initialM22;
  1401. result.m[6] = initialM23;
  1402. result.m[7] = initialM24;
  1403. result.m[8] = initialM31;
  1404. result.m[9] = initialM32;
  1405. result.m[10] = initialM33;
  1406. result.m[11] = initialM34;
  1407. result.m[12] = initialM41;
  1408. result.m[13] = initialM42;
  1409. result.m[14] = initialM43;
  1410. result.m[15] = initialM44;
  1411. };
  1412. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  1413. var result = new Matrix();
  1414. result.m[0] = initialM11;
  1415. result.m[1] = initialM12;
  1416. result.m[2] = initialM13;
  1417. result.m[3] = initialM14;
  1418. result.m[4] = initialM21;
  1419. result.m[5] = initialM22;
  1420. result.m[6] = initialM23;
  1421. result.m[7] = initialM24;
  1422. result.m[8] = initialM31;
  1423. result.m[9] = initialM32;
  1424. result.m[10] = initialM33;
  1425. result.m[11] = initialM34;
  1426. result.m[12] = initialM41;
  1427. result.m[13] = initialM42;
  1428. result.m[14] = initialM43;
  1429. result.m[15] = initialM44;
  1430. return result;
  1431. };
  1432. Matrix.Compose = function (scale, rotation, translation) {
  1433. var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
  1434. var rotationMatrix = Matrix.Identity();
  1435. rotation.toRotationMatrix(rotationMatrix);
  1436. result = result.multiply(rotationMatrix);
  1437. result.setTranslation(translation);
  1438. return result;
  1439. };
  1440. Matrix.Identity = function () {
  1441. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  1442. };
  1443. Matrix.IdentityToRef = function (result) {
  1444. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  1445. };
  1446. Matrix.Zero = function () {
  1447. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1448. };
  1449. Matrix.RotationX = function (angle) {
  1450. var result = new Matrix();
  1451. Matrix.RotationXToRef(angle, result);
  1452. return result;
  1453. };
  1454. Matrix.Invert = function (source) {
  1455. var result = new Matrix();
  1456. source.invertToRef(result);
  1457. return result;
  1458. };
  1459. Matrix.RotationXToRef = function (angle, result) {
  1460. var s = Math.sin(angle);
  1461. var c = Math.cos(angle);
  1462. result.m[0] = 1.0;
  1463. result.m[15] = 1.0;
  1464. result.m[5] = c;
  1465. result.m[10] = c;
  1466. result.m[9] = -s;
  1467. result.m[6] = s;
  1468. result.m[1] = 0;
  1469. result.m[2] = 0;
  1470. result.m[3] = 0;
  1471. result.m[4] = 0;
  1472. result.m[7] = 0;
  1473. result.m[8] = 0;
  1474. result.m[11] = 0;
  1475. result.m[12] = 0;
  1476. result.m[13] = 0;
  1477. result.m[14] = 0;
  1478. };
  1479. Matrix.RotationY = function (angle) {
  1480. var result = new Matrix();
  1481. Matrix.RotationYToRef(angle, result);
  1482. return result;
  1483. };
  1484. Matrix.RotationYToRef = function (angle, result) {
  1485. var s = Math.sin(angle);
  1486. var c = Math.cos(angle);
  1487. result.m[5] = 1.0;
  1488. result.m[15] = 1.0;
  1489. result.m[0] = c;
  1490. result.m[2] = -s;
  1491. result.m[8] = s;
  1492. result.m[10] = c;
  1493. result.m[1] = 0;
  1494. result.m[3] = 0;
  1495. result.m[4] = 0;
  1496. result.m[6] = 0;
  1497. result.m[7] = 0;
  1498. result.m[9] = 0;
  1499. result.m[11] = 0;
  1500. result.m[12] = 0;
  1501. result.m[13] = 0;
  1502. result.m[14] = 0;
  1503. };
  1504. Matrix.RotationZ = function (angle) {
  1505. var result = new Matrix();
  1506. Matrix.RotationZToRef(angle, result);
  1507. return result;
  1508. };
  1509. Matrix.RotationZToRef = function (angle, result) {
  1510. var s = Math.sin(angle);
  1511. var c = Math.cos(angle);
  1512. result.m[10] = 1.0;
  1513. result.m[15] = 1.0;
  1514. result.m[0] = c;
  1515. result.m[1] = s;
  1516. result.m[4] = -s;
  1517. result.m[5] = c;
  1518. result.m[2] = 0;
  1519. result.m[3] = 0;
  1520. result.m[6] = 0;
  1521. result.m[7] = 0;
  1522. result.m[8] = 0;
  1523. result.m[9] = 0;
  1524. result.m[11] = 0;
  1525. result.m[12] = 0;
  1526. result.m[13] = 0;
  1527. result.m[14] = 0;
  1528. };
  1529. Matrix.RotationAxis = function (axis, angle) {
  1530. var s = Math.sin(-angle);
  1531. var c = Math.cos(-angle);
  1532. var c1 = 1 - c;
  1533. axis.normalize();
  1534. var result = Matrix.Zero();
  1535. result.m[0] = (axis.x * axis.x) * c1 + c;
  1536. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1537. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1538. result.m[3] = 0.0;
  1539. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1540. result.m[5] = (axis.y * axis.y) * c1 + c;
  1541. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1542. result.m[7] = 0.0;
  1543. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1544. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1545. result.m[10] = (axis.z * axis.z) * c1 + c;
  1546. result.m[11] = 0.0;
  1547. result.m[15] = 1.0;
  1548. return result;
  1549. };
  1550. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1551. var result = new Matrix();
  1552. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1553. return result;
  1554. };
  1555. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1556. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1557. this._tempQuaternion.toRotationMatrix(result);
  1558. };
  1559. Matrix.Scaling = function (x, y, z) {
  1560. var result = Matrix.Zero();
  1561. Matrix.ScalingToRef(x, y, z, result);
  1562. return result;
  1563. };
  1564. Matrix.ScalingToRef = function (x, y, z, result) {
  1565. result.m[0] = x;
  1566. result.m[1] = 0;
  1567. result.m[2] = 0;
  1568. result.m[3] = 0;
  1569. result.m[4] = 0;
  1570. result.m[5] = y;
  1571. result.m[6] = 0;
  1572. result.m[7] = 0;
  1573. result.m[8] = 0;
  1574. result.m[9] = 0;
  1575. result.m[10] = z;
  1576. result.m[11] = 0;
  1577. result.m[12] = 0;
  1578. result.m[13] = 0;
  1579. result.m[14] = 0;
  1580. result.m[15] = 1.0;
  1581. };
  1582. Matrix.Translation = function (x, y, z) {
  1583. var result = Matrix.Identity();
  1584. Matrix.TranslationToRef(x, y, z, result);
  1585. return result;
  1586. };
  1587. Matrix.TranslationToRef = function (x, y, z, result) {
  1588. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  1589. };
  1590. Matrix.LookAtLH = function (eye, target, up) {
  1591. var result = Matrix.Zero();
  1592. Matrix.LookAtLHToRef(eye, target, up, result);
  1593. return result;
  1594. };
  1595. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1596. // Z axis
  1597. target.subtractToRef(eye, this._zAxis);
  1598. this._zAxis.normalize();
  1599. // X axis
  1600. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1601. this._xAxis.normalize();
  1602. // Y axis
  1603. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1604. this._yAxis.normalize();
  1605. // Eye angles
  1606. var ex = -Vector3.Dot(this._xAxis, eye);
  1607. var ey = -Vector3.Dot(this._yAxis, eye);
  1608. var ez = -Vector3.Dot(this._zAxis, eye);
  1609. 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);
  1610. };
  1611. Matrix.OrthoLH = function (width, height, znear, zfar) {
  1612. var hw = 2.0 / width;
  1613. var hh = 2.0 / height;
  1614. var id = 1.0 / (zfar - znear);
  1615. var nid = znear / (znear - zfar);
  1616. return Matrix.FromValues(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1);
  1617. };
  1618. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1619. var matrix = Matrix.Zero();
  1620. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1621. return matrix;
  1622. };
  1623. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1624. result.m[0] = 2.0 / (right - left);
  1625. result.m[1] = result.m[2] = result.m[3] = 0;
  1626. result.m[5] = 2.0 / (top - bottom);
  1627. result.m[4] = result.m[6] = result.m[7] = 0;
  1628. result.m[10] = -1.0 / (znear - zfar);
  1629. result.m[8] = result.m[9] = result.m[11] = 0;
  1630. result.m[12] = (left + right) / (left - right);
  1631. result.m[13] = (top + bottom) / (bottom - top);
  1632. result.m[14] = znear / (znear - zfar);
  1633. result.m[15] = 1.0;
  1634. };
  1635. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1636. var matrix = Matrix.Zero();
  1637. matrix.m[0] = (2.0 * znear) / width;
  1638. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1639. matrix.m[5] = (2.0 * znear) / height;
  1640. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1641. matrix.m[10] = -zfar / (znear - zfar);
  1642. matrix.m[8] = matrix.m[9] = 0.0;
  1643. matrix.m[11] = 1.0;
  1644. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1645. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1646. return matrix;
  1647. };
  1648. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1649. var matrix = Matrix.Zero();
  1650. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1651. return matrix;
  1652. };
  1653. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result) {
  1654. var tan = 1.0 / (Math.tan(fov * 0.5));
  1655. result.m[0] = tan / aspect;
  1656. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1657. result.m[5] = tan;
  1658. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1659. result.m[8] = result.m[9] = 0.0;
  1660. result.m[10] = -zfar / (znear - zfar);
  1661. result.m[11] = 1.0;
  1662. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1663. result.m[14] = (znear * zfar) / (znear - zfar);
  1664. };
  1665. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  1666. var cw = viewport.width;
  1667. var ch = viewport.height;
  1668. var cx = viewport.x;
  1669. var cy = viewport.y;
  1670. 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);
  1671. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1672. };
  1673. Matrix.Transpose = function (matrix) {
  1674. var result = new Matrix();
  1675. result.m[0] = matrix.m[0];
  1676. result.m[1] = matrix.m[4];
  1677. result.m[2] = matrix.m[8];
  1678. result.m[3] = matrix.m[12];
  1679. result.m[4] = matrix.m[1];
  1680. result.m[5] = matrix.m[5];
  1681. result.m[6] = matrix.m[9];
  1682. result.m[7] = matrix.m[13];
  1683. result.m[8] = matrix.m[2];
  1684. result.m[9] = matrix.m[6];
  1685. result.m[10] = matrix.m[10];
  1686. result.m[11] = matrix.m[14];
  1687. result.m[12] = matrix.m[3];
  1688. result.m[13] = matrix.m[7];
  1689. result.m[14] = matrix.m[11];
  1690. result.m[15] = matrix.m[15];
  1691. return result;
  1692. };
  1693. Matrix.Reflection = function (plane) {
  1694. var matrix = new Matrix();
  1695. Matrix.ReflectionToRef(plane, matrix);
  1696. return matrix;
  1697. };
  1698. Matrix.ReflectionToRef = function (plane, result) {
  1699. plane.normalize();
  1700. var x = plane.normal.x;
  1701. var y = plane.normal.y;
  1702. var z = plane.normal.z;
  1703. var temp = -2 * x;
  1704. var temp2 = -2 * y;
  1705. var temp3 = -2 * z;
  1706. result.m[0] = (temp * x) + 1;
  1707. result.m[1] = temp2 * x;
  1708. result.m[2] = temp3 * x;
  1709. result.m[3] = 0.0;
  1710. result.m[4] = temp * y;
  1711. result.m[5] = (temp2 * y) + 1;
  1712. result.m[6] = temp3 * y;
  1713. result.m[7] = 0.0;
  1714. result.m[8] = temp * z;
  1715. result.m[9] = temp2 * z;
  1716. result.m[10] = (temp3 * z) + 1;
  1717. result.m[11] = 0.0;
  1718. result.m[12] = temp * plane.d;
  1719. result.m[13] = temp2 * plane.d;
  1720. result.m[14] = temp3 * plane.d;
  1721. result.m[15] = 1.0;
  1722. };
  1723. Matrix._tempQuaternion = new Quaternion();
  1724. Matrix._xAxis = Vector3.Zero();
  1725. Matrix._yAxis = Vector3.Zero();
  1726. Matrix._zAxis = Vector3.Zero();
  1727. return Matrix;
  1728. })();
  1729. BABYLON.Matrix = Matrix;
  1730. var Plane = (function () {
  1731. function Plane(a, b, c, d) {
  1732. this.normal = new Vector3(a, b, c);
  1733. this.d = d;
  1734. }
  1735. Plane.prototype.asArray = function () {
  1736. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  1737. };
  1738. // Methods
  1739. Plane.prototype.clone = function () {
  1740. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  1741. };
  1742. Plane.prototype.normalize = function () {
  1743. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1744. var magnitude = 0;
  1745. if (norm !== 0) {
  1746. magnitude = 1.0 / norm;
  1747. }
  1748. this.normal.x *= magnitude;
  1749. this.normal.y *= magnitude;
  1750. this.normal.z *= magnitude;
  1751. this.d *= magnitude;
  1752. };
  1753. Plane.prototype.transform = function (transformation) {
  1754. var transposedMatrix = Matrix.Transpose(transformation);
  1755. var x = this.normal.x;
  1756. var y = this.normal.y;
  1757. var z = this.normal.z;
  1758. var d = this.d;
  1759. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1760. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1761. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1762. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1763. return new Plane(normalX, normalY, normalZ, finalD);
  1764. };
  1765. Plane.prototype.dotCoordinate = function (point) {
  1766. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1767. };
  1768. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  1769. var x1 = point2.x - point1.x;
  1770. var y1 = point2.y - point1.y;
  1771. var z1 = point2.z - point1.z;
  1772. var x2 = point3.x - point1.x;
  1773. var y2 = point3.y - point1.y;
  1774. var z2 = point3.z - point1.z;
  1775. var yz = (y1 * z2) - (z1 * y2);
  1776. var xz = (z1 * x2) - (x1 * z2);
  1777. var xy = (x1 * y2) - (y1 * x2);
  1778. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1779. var invPyth;
  1780. if (pyth !== 0) {
  1781. invPyth = 1.0 / pyth;
  1782. } else {
  1783. invPyth = 0;
  1784. }
  1785. this.normal.x = yz * invPyth;
  1786. this.normal.y = xz * invPyth;
  1787. this.normal.z = xy * invPyth;
  1788. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1789. };
  1790. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  1791. var dot = Vector3.Dot(this.normal, direction);
  1792. return (dot <= epsilon);
  1793. };
  1794. Plane.prototype.signedDistanceTo = function (point) {
  1795. return Vector3.Dot(point, this.normal) + this.d;
  1796. };
  1797. // Statics
  1798. Plane.FromArray = function (array) {
  1799. return new Plane(array[0], array[1], array[2], array[3]);
  1800. };
  1801. Plane.FromPoints = function (point1, point2, point3) {
  1802. var result = new Plane(0, 0, 0, 0);
  1803. result.copyFromPoints(point1, point2, point3);
  1804. return result;
  1805. };
  1806. Plane.FromPositionAndNormal = function (origin, normal) {
  1807. var result = new Plane(0, 0, 0, 0);
  1808. normal.normalize();
  1809. result.normal = normal;
  1810. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1811. return result;
  1812. };
  1813. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  1814. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1815. return Vector3.Dot(point, normal) + d;
  1816. };
  1817. return Plane;
  1818. })();
  1819. BABYLON.Plane = Plane;
  1820. var Viewport = (function () {
  1821. function Viewport(x, y, width, height) {
  1822. this.x = x;
  1823. this.y = y;
  1824. this.width = width;
  1825. this.height = height;
  1826. }
  1827. Viewport.prototype.toGlobal = function (engine) {
  1828. var width = engine.getRenderWidth();
  1829. var height = engine.getRenderHeight();
  1830. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1831. };
  1832. return Viewport;
  1833. })();
  1834. BABYLON.Viewport = Viewport;
  1835. var Frustum = (function () {
  1836. function Frustum() {
  1837. }
  1838. Frustum.GetPlanes = function (transform) {
  1839. var frustumPlanes = [];
  1840. for (var index = 0; index < 6; index++) {
  1841. frustumPlanes.push(new Plane(0, 0, 0, 0));
  1842. }
  1843. Frustum.GetPlanesToRef(transform, frustumPlanes);
  1844. return frustumPlanes;
  1845. };
  1846. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  1847. // Near
  1848. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1849. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1850. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1851. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1852. frustumPlanes[0].normalize();
  1853. // Far
  1854. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1855. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1856. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1857. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1858. frustumPlanes[1].normalize();
  1859. // Left
  1860. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1861. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1862. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1863. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1864. frustumPlanes[2].normalize();
  1865. // Right
  1866. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1867. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1868. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1869. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1870. frustumPlanes[3].normalize();
  1871. // Top
  1872. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1873. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1874. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1875. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1876. frustumPlanes[4].normalize();
  1877. // Bottom
  1878. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1879. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1880. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1881. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1882. frustumPlanes[5].normalize();
  1883. };
  1884. return Frustum;
  1885. })();
  1886. BABYLON.Frustum = Frustum;
  1887. var Ray = (function () {
  1888. function Ray(origin, direction, length) {
  1889. if (typeof length === "undefined") { length = Number.MAX_VALUE; }
  1890. this.origin = origin;
  1891. this.direction = direction;
  1892. this.length = length;
  1893. }
  1894. // Methods
  1895. Ray.prototype.intersectsBoxMinMax = function (minimum, maximum) {
  1896. var d = 0.0;
  1897. var maxValue = Number.MAX_VALUE;
  1898. if (Math.abs(this.direction.x) < 0.0000001) {
  1899. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  1900. return false;
  1901. }
  1902. } else {
  1903. var inv = 1.0 / this.direction.x;
  1904. var min = (minimum.x - this.origin.x) * inv;
  1905. var max = (maximum.x - this.origin.x) * inv;
  1906. if (max === -Infinity) {
  1907. max = Infinity;
  1908. }
  1909. if (min > max) {
  1910. var temp = min;
  1911. min = max;
  1912. max = temp;
  1913. }
  1914. d = Math.max(min, d);
  1915. maxValue = Math.min(max, maxValue);
  1916. if (d > maxValue) {
  1917. return false;
  1918. }
  1919. }
  1920. if (Math.abs(this.direction.y) < 0.0000001) {
  1921. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  1922. return false;
  1923. }
  1924. } else {
  1925. inv = 1.0 / this.direction.y;
  1926. min = (minimum.y - this.origin.y) * inv;
  1927. max = (maximum.y - this.origin.y) * inv;
  1928. if (max === -Infinity) {
  1929. max = Infinity;
  1930. }
  1931. if (min > max) {
  1932. temp = min;
  1933. min = max;
  1934. max = temp;
  1935. }
  1936. d = Math.max(min, d);
  1937. maxValue = Math.min(max, maxValue);
  1938. if (d > maxValue) {
  1939. return false;
  1940. }
  1941. }
  1942. if (Math.abs(this.direction.z) < 0.0000001) {
  1943. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  1944. return false;
  1945. }
  1946. } else {
  1947. inv = 1.0 / this.direction.z;
  1948. min = (minimum.z - this.origin.z) * inv;
  1949. max = (maximum.z - this.origin.z) * inv;
  1950. if (max === -Infinity) {
  1951. max = Infinity;
  1952. }
  1953. if (min > max) {
  1954. temp = min;
  1955. min = max;
  1956. max = temp;
  1957. }
  1958. d = Math.max(min, d);
  1959. maxValue = Math.min(max, maxValue);
  1960. if (d > maxValue) {
  1961. return false;
  1962. }
  1963. }
  1964. return true;
  1965. };
  1966. Ray.prototype.intersectsBox = function (box) {
  1967. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  1968. };
  1969. Ray.prototype.intersectsSphere = function (sphere) {
  1970. var x = sphere.center.x - this.origin.x;
  1971. var y = sphere.center.y - this.origin.y;
  1972. var z = sphere.center.z - this.origin.z;
  1973. var pyth = (x * x) + (y * y) + (z * z);
  1974. var rr = sphere.radius * sphere.radius;
  1975. if (pyth <= rr) {
  1976. return true;
  1977. }
  1978. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  1979. if (dot < 0.0) {
  1980. return false;
  1981. }
  1982. var temp = pyth - (dot * dot);
  1983. return temp <= rr;
  1984. };
  1985. Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  1986. if (!this._edge1) {
  1987. this._edge1 = Vector3.Zero();
  1988. this._edge2 = Vector3.Zero();
  1989. this._pvec = Vector3.Zero();
  1990. this._tvec = Vector3.Zero();
  1991. this._qvec = Vector3.Zero();
  1992. }
  1993. vertex1.subtractToRef(vertex0, this._edge1);
  1994. vertex2.subtractToRef(vertex0, this._edge2);
  1995. Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  1996. var det = Vector3.Dot(this._edge1, this._pvec);
  1997. if (det === 0) {
  1998. return null;
  1999. }
  2000. var invdet = 1 / det;
  2001. this.origin.subtractToRef(vertex0, this._tvec);
  2002. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  2003. if (bu < 0 || bu > 1.0) {
  2004. return null;
  2005. }
  2006. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  2007. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  2008. if (bv < 0 || bu + bv > 1.0) {
  2009. return null;
  2010. }
  2011. //check if the distance is longer than the predefined length.
  2012. var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
  2013. if (distance > this.length) {
  2014. return null;
  2015. }
  2016. return new BABYLON.IntersectionInfo(bu, bv, distance);
  2017. };
  2018. // Statics
  2019. Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  2020. var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  2021. var end = Vector3.Unproject(new Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  2022. var direction = end.subtract(start);
  2023. direction.normalize();
  2024. return new Ray(start, direction);
  2025. };
  2026. /**
  2027. * 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
  2028. * transformed to the given world matrix.
  2029. * @param origin The origin point
  2030. * @param end The end point
  2031. * @param world a matrix to transform the ray to. Default is the identity matrix.
  2032. */
  2033. Ray.CreateNewFromTo = function (origin, end, world) {
  2034. if (typeof world === "undefined") { world = Matrix.Identity(); }
  2035. var direction = end.subtract(origin);
  2036. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  2037. direction.normalize();
  2038. return Ray.Transform(new Ray(origin, direction, length), world);
  2039. };
  2040. Ray.Transform = function (ray, matrix) {
  2041. var newOrigin = Vector3.TransformCoordinates(ray.origin, matrix);
  2042. var newDirection = Vector3.TransformNormal(ray.direction, matrix);
  2043. return new Ray(newOrigin, newDirection, ray.length);
  2044. };
  2045. return Ray;
  2046. })();
  2047. BABYLON.Ray = Ray;
  2048. (function (Space) {
  2049. Space[Space["LOCAL"] = 0] = "LOCAL";
  2050. Space[Space["WORLD"] = 1] = "WORLD";
  2051. })(BABYLON.Space || (BABYLON.Space = {}));
  2052. var Space = BABYLON.Space;
  2053. var Axis = (function () {
  2054. function Axis() {
  2055. }
  2056. Axis.X = new Vector3(1, 0, 0);
  2057. Axis.Y = new Vector3(0, 1, 0);
  2058. Axis.Z = new Vector3(0, 0, 1);
  2059. return Axis;
  2060. })();
  2061. BABYLON.Axis = Axis;
  2062. ;
  2063. var BezierCurve = (function () {
  2064. function BezierCurve() {
  2065. }
  2066. BezierCurve.interpolate = function (t, x1, y1, x2, y2) {
  2067. // Extract X (which is equal to time here)
  2068. var f0 = 1 - 3 * x2 + 3 * x1;
  2069. var f1 = 3 * x2 - 6 * x1;
  2070. var f2 = 3 * x1;
  2071. var refinedT = t;
  2072. for (var i = 0; i < 5; i++) {
  2073. var refinedT2 = refinedT * refinedT;
  2074. var refinedT3 = refinedT2 * refinedT;
  2075. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2076. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2077. refinedT -= (x - t) * slope;
  2078. refinedT = Math.min(1, Math.max(0, refinedT));
  2079. }
  2080. // Resolve cubic bezier for the given x
  2081. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 + 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 + Math.pow(refinedT, 3);
  2082. };
  2083. return BezierCurve;
  2084. })();
  2085. BABYLON.BezierCurve = BezierCurve;
  2086. (function (Orientation) {
  2087. Orientation[Orientation["CW"] = 0] = "CW";
  2088. Orientation[Orientation["CCW"] = 1] = "CCW";
  2089. })(BABYLON.Orientation || (BABYLON.Orientation = {}));
  2090. var Orientation = BABYLON.Orientation;
  2091. var Angle = (function () {
  2092. function Angle(radians) {
  2093. var _this = this;
  2094. this.degrees = function () {
  2095. return _this._radians * 180 / Math.PI;
  2096. };
  2097. this.radians = function () {
  2098. return _this._radians;
  2099. };
  2100. this._radians = radians;
  2101. if (this._radians < 0)
  2102. this._radians += (2 * Math.PI);
  2103. }
  2104. Angle.BetweenTwoPoints = function (a, b) {
  2105. var delta = b.subtract(a);
  2106. var theta = Math.atan2(delta.y, delta.x);
  2107. return new Angle(theta);
  2108. };
  2109. Angle.FromRadians = function (radians) {
  2110. return new Angle(radians);
  2111. };
  2112. Angle.FromDegrees = function (degrees) {
  2113. return new Angle(degrees * Math.PI / 180);
  2114. };
  2115. return Angle;
  2116. })();
  2117. BABYLON.Angle = Angle;
  2118. var Arc2 = (function () {
  2119. function Arc2(startPoint, midPoint, endPoint) {
  2120. this.startPoint = startPoint;
  2121. this.midPoint = midPoint;
  2122. this.endPoint = endPoint;
  2123. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2124. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2125. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2126. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2127. 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);
  2128. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2129. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2130. var a1 = this.startAngle.degrees();
  2131. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2132. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2133. // angles correction
  2134. if (a2 - a1 > +180.0)
  2135. a2 -= 360.0;
  2136. if (a2 - a1 < -180.0)
  2137. a2 += 360.0;
  2138. if (a3 - a2 > +180.0)
  2139. a3 -= 360.0;
  2140. if (a3 - a2 < -180.0)
  2141. a3 += 360.0;
  2142. this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
  2143. this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
  2144. }
  2145. return Arc2;
  2146. })();
  2147. BABYLON.Arc2 = Arc2;
  2148. var PathCursor = (function () {
  2149. function PathCursor(path) {
  2150. this.path = path;
  2151. this._onchange = new Array();
  2152. this.value = 0;
  2153. this.animations = new Array();
  2154. }
  2155. PathCursor.prototype.getPoint = function () {
  2156. var point = this.path.getPointAtLengthPosition(this.value);
  2157. return new Vector3(point.x, 0, point.y);
  2158. };
  2159. PathCursor.prototype.moveAhead = function (step) {
  2160. if (typeof step === "undefined") { step = 0.002; }
  2161. this.move(step);
  2162. };
  2163. PathCursor.prototype.moveBack = function (step) {
  2164. if (typeof step === "undefined") { step = 0.002; }
  2165. this.move(-step);
  2166. };
  2167. PathCursor.prototype.move = function (step) {
  2168. if (Math.abs(step) > 1) {
  2169. throw "step size should be less than 1.";
  2170. }
  2171. this.value += step;
  2172. this.ensureLimits();
  2173. this.raiseOnChange();
  2174. };
  2175. PathCursor.prototype.ensureLimits = function () {
  2176. while (this.value > 1) {
  2177. this.value -= 1;
  2178. }
  2179. while (this.value < 0) {
  2180. this.value += 1;
  2181. }
  2182. };
  2183. // used by animation engine
  2184. PathCursor.prototype.markAsDirty = function (propertyName) {
  2185. this.ensureLimits();
  2186. this.raiseOnChange();
  2187. };
  2188. PathCursor.prototype.raiseOnChange = function () {
  2189. var _this = this;
  2190. this._onchange.forEach(function (f) {
  2191. return f(_this);
  2192. });
  2193. };
  2194. PathCursor.prototype.onchange = function (f) {
  2195. this._onchange.push(f);
  2196. };
  2197. return PathCursor;
  2198. })();
  2199. BABYLON.PathCursor = PathCursor;
  2200. var Path2 = (function () {
  2201. function Path2(x, y) {
  2202. this._points = [];
  2203. this._length = 0;
  2204. this.closed = false;
  2205. this._points.push(new Vector2(x, y));
  2206. }
  2207. Path2.prototype.addLineTo = function (x, y) {
  2208. if (closed) {
  2209. BABYLON.Tools.Error("cannot add lines to closed paths");
  2210. return this;
  2211. }
  2212. var newPoint = new Vector2(x, y);
  2213. var previousPoint = this._points[this._points.length - 1];
  2214. this._points.push(newPoint);
  2215. this._length += newPoint.subtract(previousPoint).length();
  2216. return this;
  2217. };
  2218. Path2.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
  2219. if (typeof numberOfSegments === "undefined") { numberOfSegments = 36; }
  2220. if (closed) {
  2221. BABYLON.Tools.Error("cannot add arcs to closed paths");
  2222. return this;
  2223. }
  2224. var startPoint = this._points[this._points.length - 1];
  2225. var midPoint = new Vector2(midX, midY);
  2226. var endPoint = new Vector2(endX, endY);
  2227. var arc = new Arc2(startPoint, midPoint, endPoint);
  2228. var increment = arc.angle.radians() / numberOfSegments;
  2229. if (arc.orientation === 0 /* CW */)
  2230. increment *= -1;
  2231. var currentAngle = arc.startAngle.radians() + increment;
  2232. for (var i = 0; i < numberOfSegments; i++) {
  2233. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2234. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2235. this.addLineTo(x, y);
  2236. currentAngle += increment;
  2237. }
  2238. return this;
  2239. };
  2240. Path2.prototype.close = function () {
  2241. this.closed = true;
  2242. return this;
  2243. };
  2244. Path2.prototype.length = function () {
  2245. var result = this._length;
  2246. if (!this.closed) {
  2247. var lastPoint = this._points[this._points.length - 1];
  2248. var firstPoint = this._points[0];
  2249. result += (firstPoint.subtract(lastPoint).length());
  2250. }
  2251. return result;
  2252. };
  2253. Path2.prototype.getPoints = function () {
  2254. return this._points;
  2255. };
  2256. Path2.prototype.getPointAtLengthPosition = function (normalizedLengthPosition) {
  2257. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2258. BABYLON.Tools.Error("normalized length position should be between 0 and 1.");
  2259. return;
  2260. }
  2261. var lengthPosition = normalizedLengthPosition * this.length();
  2262. var previousOffset = 0;
  2263. for (var i = 0; i < this._points.length; i++) {
  2264. var j = (i + 1) % this._points.length;
  2265. var a = this._points[i];
  2266. var b = this._points[j];
  2267. var bToA = b.subtract(a);
  2268. var nextOffset = (bToA.length() + previousOffset);
  2269. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2270. var dir = bToA.normalize();
  2271. var localOffset = lengthPosition - previousOffset;
  2272. return new Vector2(a.x + (dir.x * localOffset), a.y + (dir.y * localOffset));
  2273. }
  2274. previousOffset = nextOffset;
  2275. }
  2276. BABYLON.Tools.Error("internal error");
  2277. };
  2278. Path2.StartingAt = function (x, y) {
  2279. return new Path2(x, y);
  2280. };
  2281. return Path2;
  2282. })();
  2283. BABYLON.Path2 = Path2;
  2284. })(BABYLON || (BABYLON = {}));
  2285. //# sourceMappingURL=babylon.math.js.map