babylon.math.js 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  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. BABYLON.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.multiplyInPlace = function (otherVector) {
  243. this.x *= otherVector.x;
  244. this.y *= otherVector.y;
  245. };
  246. Vector2.prototype.multiply = function (otherVector) {
  247. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  248. };
  249. Vector2.prototype.multiplyToRef = function (otherVector, result) {
  250. result.x = this.x * otherVector.x;
  251. result.y = this.y * otherVector.y;
  252. };
  253. Vector2.prototype.multiplyByFloats = function (x, y) {
  254. return new Vector2(this.x * x, this.y * y);
  255. };
  256. Vector2.prototype.divide = function (otherVector) {
  257. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  258. };
  259. Vector2.prototype.divideToRef = function (otherVector, result) {
  260. result.x = this.x / otherVector.x;
  261. result.y = this.y / otherVector.y;
  262. };
  263. Vector2.prototype.negate = function () {
  264. return new Vector2(-this.x, -this.y);
  265. };
  266. Vector2.prototype.scaleInPlace = function (scale) {
  267. this.x *= scale;
  268. this.y *= scale;
  269. return this;
  270. };
  271. Vector2.prototype.scale = function (scale) {
  272. return new Vector2(this.x * scale, this.y * scale);
  273. };
  274. Vector2.prototype.equals = function (otherVector) {
  275. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  276. };
  277. // Properties
  278. Vector2.prototype.length = function () {
  279. return Math.sqrt(this.x * this.x + this.y * this.y);
  280. };
  281. Vector2.prototype.lengthSquared = function () {
  282. return (this.x * this.x + this.y * this.y);
  283. };
  284. // Methods
  285. Vector2.prototype.normalize = function () {
  286. var len = this.length();
  287. if (len === 0)
  288. return this;
  289. var num = 1.0 / len;
  290. this.x *= num;
  291. this.y *= num;
  292. return this;
  293. };
  294. Vector2.prototype.clone = function () {
  295. return new Vector2(this.x, this.y);
  296. };
  297. // Statics
  298. Vector2.Zero = function () {
  299. return new Vector2(0, 0);
  300. };
  301. Vector2.FromArray = function (array, offset) {
  302. if (!offset) {
  303. offset = 0;
  304. }
  305. return new Vector2(array[offset], array[offset + 1]);
  306. };
  307. Vector2.FromArrayToRef = function (array, offset, result) {
  308. result.x = array[offset];
  309. result.y = array[offset + 1];
  310. };
  311. Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  312. var squared = amount * amount;
  313. var cubed = amount * squared;
  314. 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));
  315. 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));
  316. return new Vector2(x, y);
  317. };
  318. Vector2.Clamp = function (value, min, max) {
  319. var x = value.x;
  320. x = (x > max.x) ? max.x : x;
  321. x = (x < min.x) ? min.x : x;
  322. var y = value.y;
  323. y = (y > max.y) ? max.y : y;
  324. y = (y < min.y) ? min.y : y;
  325. return new Vector2(x, y);
  326. };
  327. Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  328. var squared = amount * amount;
  329. var cubed = amount * squared;
  330. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  331. var part2 = (-2.0 * cubed) + (3.0 * squared);
  332. var part3 = (cubed - (2.0 * squared)) + amount;
  333. var part4 = cubed - squared;
  334. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  335. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  336. return new Vector2(x, y);
  337. };
  338. Vector2.Lerp = function (start, end, amount) {
  339. var x = start.x + ((end.x - start.x) * amount);
  340. var y = start.y + ((end.y - start.y) * amount);
  341. return new Vector2(x, y);
  342. };
  343. Vector2.Dot = function (left, right) {
  344. return left.x * right.x + left.y * right.y;
  345. };
  346. Vector2.Normalize = function (vector) {
  347. var newVector = vector.clone();
  348. newVector.normalize();
  349. return newVector;
  350. };
  351. Vector2.Minimize = function (left, right) {
  352. var x = (left.x < right.x) ? left.x : right.x;
  353. var y = (left.y < right.y) ? left.y : right.y;
  354. return new Vector2(x, y);
  355. };
  356. Vector2.Maximize = function (left, right) {
  357. var x = (left.x > right.x) ? left.x : right.x;
  358. var y = (left.y > right.y) ? left.y : right.y;
  359. return new Vector2(x, y);
  360. };
  361. Vector2.Transform = function (vector, transformation) {
  362. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  363. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  364. return new Vector2(x, y);
  365. };
  366. Vector2.Distance = function (value1, value2) {
  367. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  368. };
  369. Vector2.DistanceSquared = function (value1, value2) {
  370. var x = value1.x - value2.x;
  371. var y = value1.y - value2.y;
  372. return (x * x) + (y * y);
  373. };
  374. return Vector2;
  375. })();
  376. BABYLON.Vector2 = Vector2;
  377. var Vector3 = (function () {
  378. function Vector3(x, y, z) {
  379. this.x = x;
  380. this.y = y;
  381. this.z = z;
  382. }
  383. Vector3.prototype.toString = function () {
  384. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  385. };
  386. // Operators
  387. Vector3.prototype.asArray = function () {
  388. var result = [];
  389. this.toArray(result, 0);
  390. return result;
  391. };
  392. Vector3.prototype.toArray = function (array, index) {
  393. if (index === undefined) {
  394. index = 0;
  395. }
  396. array[index] = this.x;
  397. array[index + 1] = this.y;
  398. array[index + 2] = this.z;
  399. };
  400. Vector3.prototype.addInPlace = function (otherVector) {
  401. this.x += otherVector.x;
  402. this.y += otherVector.y;
  403. this.z += otherVector.z;
  404. };
  405. Vector3.prototype.add = function (otherVector) {
  406. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  407. };
  408. Vector3.prototype.addToRef = function (otherVector, result) {
  409. result.x = this.x + otherVector.x;
  410. result.y = this.y + otherVector.y;
  411. result.z = this.z + otherVector.z;
  412. };
  413. Vector3.prototype.subtractInPlace = function (otherVector) {
  414. this.x -= otherVector.x;
  415. this.y -= otherVector.y;
  416. this.z -= otherVector.z;
  417. };
  418. Vector3.prototype.subtract = function (otherVector) {
  419. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  420. };
  421. Vector3.prototype.subtractToRef = function (otherVector, result) {
  422. result.x = this.x - otherVector.x;
  423. result.y = this.y - otherVector.y;
  424. result.z = this.z - otherVector.z;
  425. };
  426. Vector3.prototype.subtractFromFloats = function (x, y, z) {
  427. return new Vector3(this.x - x, this.y - y, this.z - z);
  428. };
  429. Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  430. result.x = this.x - x;
  431. result.y = this.y - y;
  432. result.z = this.z - z;
  433. };
  434. Vector3.prototype.negate = function () {
  435. return new Vector3(-this.x, -this.y, -this.z);
  436. };
  437. Vector3.prototype.scaleInPlace = function (scale) {
  438. this.x *= scale;
  439. this.y *= scale;
  440. this.z *= scale;
  441. return this;
  442. };
  443. Vector3.prototype.scale = function (scale) {
  444. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  445. };
  446. Vector3.prototype.scaleToRef = function (scale, result) {
  447. result.x = this.x * scale;
  448. result.y = this.y * scale;
  449. result.z = this.z * scale;
  450. };
  451. Vector3.prototype.equals = function (otherVector) {
  452. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  453. };
  454. Vector3.prototype.equalsWithEpsilon = function (otherVector) {
  455. 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;
  456. };
  457. Vector3.prototype.equalsToFloats = function (x, y, z) {
  458. return this.x === x && this.y === y && this.z === z;
  459. };
  460. Vector3.prototype.multiplyInPlace = function (otherVector) {
  461. this.x *= otherVector.x;
  462. this.y *= otherVector.y;
  463. this.z *= otherVector.z;
  464. };
  465. Vector3.prototype.multiply = function (otherVector) {
  466. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  467. };
  468. Vector3.prototype.multiplyToRef = function (otherVector, result) {
  469. result.x = this.x * otherVector.x;
  470. result.y = this.y * otherVector.y;
  471. result.z = this.z * otherVector.z;
  472. };
  473. Vector3.prototype.multiplyByFloats = function (x, y, z) {
  474. return new Vector3(this.x * x, this.y * y, this.z * z);
  475. };
  476. Vector3.prototype.divide = function (otherVector) {
  477. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  478. };
  479. Vector3.prototype.divideToRef = function (otherVector, result) {
  480. result.x = this.x / otherVector.x;
  481. result.y = this.y / otherVector.y;
  482. result.z = this.z / otherVector.z;
  483. };
  484. Vector3.prototype.MinimizeInPlace = function (other) {
  485. if (other.x < this.x)
  486. this.x = other.x;
  487. if (other.y < this.y)
  488. this.y = other.y;
  489. if (other.z < this.z)
  490. this.z = other.z;
  491. };
  492. Vector3.prototype.MaximizeInPlace = function (other) {
  493. if (other.x > this.x)
  494. this.x = other.x;
  495. if (other.y > this.y)
  496. this.y = other.y;
  497. if (other.z > this.z)
  498. this.z = other.z;
  499. };
  500. // Properties
  501. Vector3.prototype.length = function () {
  502. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  503. };
  504. Vector3.prototype.lengthSquared = function () {
  505. return (this.x * this.x + this.y * this.y + this.z * this.z);
  506. };
  507. // Methods
  508. Vector3.prototype.normalize = function () {
  509. var len = this.length();
  510. if (len === 0)
  511. return this;
  512. var num = 1.0 / len;
  513. this.x *= num;
  514. this.y *= num;
  515. this.z *= num;
  516. return this;
  517. };
  518. Vector3.prototype.clone = function () {
  519. return new Vector3(this.x, this.y, this.z);
  520. };
  521. Vector3.prototype.copyFrom = function (source) {
  522. this.x = source.x;
  523. this.y = source.y;
  524. this.z = source.z;
  525. };
  526. Vector3.prototype.copyFromFloats = function (x, y, z) {
  527. this.x = x;
  528. this.y = y;
  529. this.z = z;
  530. };
  531. // Statics
  532. Vector3.FromArray = function (array, offset) {
  533. if (!offset) {
  534. offset = 0;
  535. }
  536. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  537. };
  538. Vector3.FromArrayToRef = function (array, offset, result) {
  539. result.x = array[offset];
  540. result.y = array[offset + 1];
  541. result.z = array[offset + 2];
  542. };
  543. Vector3.FromFloatArrayToRef = function (array, offset, result) {
  544. result.x = array[offset];
  545. result.y = array[offset + 1];
  546. result.z = array[offset + 2];
  547. };
  548. Vector3.FromFloatsToRef = function (x, y, z, result) {
  549. result.x = x;
  550. result.y = y;
  551. result.z = z;
  552. };
  553. Vector3.Zero = function () {
  554. return new Vector3(0, 0, 0);
  555. };
  556. Vector3.Up = function () {
  557. return new Vector3(0, 1.0, 0);
  558. };
  559. Vector3.TransformCoordinates = function (vector, transformation) {
  560. var result = Vector3.Zero();
  561. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  562. return result;
  563. };
  564. Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  565. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  566. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  567. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  568. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  569. result.x = x / w;
  570. result.y = y / w;
  571. result.z = z / w;
  572. };
  573. Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  574. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  575. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  576. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  577. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  578. result.x = rx / rw;
  579. result.y = ry / rw;
  580. result.z = rz / rw;
  581. };
  582. Vector3.TransformNormal = function (vector, transformation) {
  583. var result = Vector3.Zero();
  584. Vector3.TransformNormalToRef(vector, transformation, result);
  585. return result;
  586. };
  587. Vector3.TransformNormalToRef = function (vector, transformation, result) {
  588. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  589. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  590. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  591. };
  592. Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  593. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  594. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  595. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  596. };
  597. Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  598. var squared = amount * amount;
  599. var cubed = amount * squared;
  600. 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));
  601. 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));
  602. 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));
  603. return new Vector3(x, y, z);
  604. };
  605. Vector3.Clamp = function (value, min, max) {
  606. var x = value.x;
  607. x = (x > max.x) ? max.x : x;
  608. x = (x < min.x) ? min.x : x;
  609. var y = value.y;
  610. y = (y > max.y) ? max.y : y;
  611. y = (y < min.y) ? min.y : y;
  612. var z = value.z;
  613. z = (z > max.z) ? max.z : z;
  614. z = (z < min.z) ? min.z : z;
  615. return new Vector3(x, y, z);
  616. };
  617. Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  618. var squared = amount * amount;
  619. var cubed = amount * squared;
  620. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  621. var part2 = (-2.0 * cubed) + (3.0 * squared);
  622. var part3 = (cubed - (2.0 * squared)) + amount;
  623. var part4 = cubed - squared;
  624. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  625. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  626. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  627. return new Vector3(x, y, z);
  628. };
  629. Vector3.Lerp = function (start, end, amount) {
  630. var x = start.x + ((end.x - start.x) * amount);
  631. var y = start.y + ((end.y - start.y) * amount);
  632. var z = start.z + ((end.z - start.z) * amount);
  633. return new Vector3(x, y, z);
  634. };
  635. Vector3.Dot = function (left, right) {
  636. return (left.x * right.x + left.y * right.y + left.z * right.z);
  637. };
  638. Vector3.Cross = function (left, right) {
  639. var result = Vector3.Zero();
  640. Vector3.CrossToRef(left, right, result);
  641. return result;
  642. };
  643. Vector3.CrossToRef = function (left, right, result) {
  644. result.x = left.y * right.z - left.z * right.y;
  645. result.y = left.z * right.x - left.x * right.z;
  646. result.z = left.x * right.y - left.y * right.x;
  647. };
  648. Vector3.Normalize = function (vector) {
  649. var result = Vector3.Zero();
  650. Vector3.NormalizeToRef(vector, result);
  651. return result;
  652. };
  653. Vector3.NormalizeToRef = function (vector, result) {
  654. result.copyFrom(vector);
  655. result.normalize();
  656. };
  657. Vector3.Project = function (vector, world, transform, viewport) {
  658. var cw = viewport.width;
  659. var ch = viewport.height;
  660. var cx = viewport.x;
  661. var cy = viewport.y;
  662. var viewportMatrix = BABYLON.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);
  663. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  664. return Vector3.TransformCoordinates(vector, finalMatrix);
  665. };
  666. Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  667. var matrix = world.multiply(view).multiply(projection);
  668. matrix.invert();
  669. source.x = source.x / viewportWidth * 2 - 1;
  670. source.y = -(source.y / viewportHeight * 2 - 1);
  671. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  672. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  673. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  674. vector = vector.scale(1.0 / num);
  675. }
  676. return vector;
  677. };
  678. Vector3.Minimize = function (left, right) {
  679. var min = left.clone();
  680. min.MinimizeInPlace(right);
  681. return min;
  682. };
  683. Vector3.Maximize = function (left, right) {
  684. var max = left.clone();
  685. max.MaximizeInPlace(right);
  686. return max;
  687. };
  688. Vector3.Distance = function (value1, value2) {
  689. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  690. };
  691. Vector3.DistanceSquared = function (value1, value2) {
  692. var x = value1.x - value2.x;
  693. var y = value1.y - value2.y;
  694. var z = value1.z - value2.z;
  695. return (x * x) + (y * y) + (z * z);
  696. };
  697. Vector3.Center = function (value1, value2) {
  698. var center = value1.add(value2);
  699. center.scaleInPlace(0.5);
  700. return center;
  701. };
  702. return Vector3;
  703. })();
  704. BABYLON.Vector3 = Vector3;
  705. var Quaternion = (function () {
  706. function Quaternion(x, y, z, w) {
  707. if (typeof x === "undefined") { x = 0; }
  708. if (typeof y === "undefined") { y = 0; }
  709. if (typeof z === "undefined") { z = 0; }
  710. if (typeof w === "undefined") { w = 1; }
  711. this.x = x;
  712. this.y = y;
  713. this.z = z;
  714. this.w = w;
  715. }
  716. Quaternion.prototype.toString = function () {
  717. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  718. };
  719. Quaternion.prototype.asArray = function () {
  720. return [this.x, this.y, this.z, this.w];
  721. };
  722. Quaternion.prototype.equals = function (otherQuaternion) {
  723. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  724. };
  725. Quaternion.prototype.clone = function () {
  726. return new Quaternion(this.x, this.y, this.z, this.w);
  727. };
  728. Quaternion.prototype.copyFrom = function (other) {
  729. this.x = other.x;
  730. this.y = other.y;
  731. this.z = other.z;
  732. this.w = other.w;
  733. };
  734. Quaternion.prototype.copyFromFloats = function (x, y, z, w) {
  735. this.x = x;
  736. this.y = y;
  737. this.z = z;
  738. this.w = w;
  739. };
  740. Quaternion.prototype.add = function (other) {
  741. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  742. };
  743. Quaternion.prototype.subtract = function (other) {
  744. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  745. };
  746. Quaternion.prototype.scale = function (value) {
  747. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  748. };
  749. Quaternion.prototype.multiply = function (q1) {
  750. var result = new Quaternion(0, 0, 0, 1.0);
  751. this.multiplyToRef(q1, result);
  752. return result;
  753. };
  754. Quaternion.prototype.multiplyToRef = function (q1, result) {
  755. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  756. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  757. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  758. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  759. };
  760. Quaternion.prototype.length = function () {
  761. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  762. };
  763. Quaternion.prototype.normalize = function () {
  764. var length = 1.0 / this.length();
  765. this.x *= length;
  766. this.y *= length;
  767. this.z *= length;
  768. this.w *= length;
  769. };
  770. Quaternion.prototype.toEulerAngles = function () {
  771. var result = Vector3.Zero();
  772. this.toEulerAnglesToRef(result);
  773. return result;
  774. };
  775. Quaternion.prototype.toEulerAnglesToRef = function (result) {
  776. var qx = this.x;
  777. var qy = this.y;
  778. var qz = this.z;
  779. var qw = this.w;
  780. var sqx = qx * qx;
  781. var sqy = qy * qy;
  782. var sqz = qz * qz;
  783. var yaw = Math.atan2(2.0 * (qy * qw - qx * qz), 1.0 - 2.0 * (sqy + sqz));
  784. var pitch = Math.asin(2.0 * (qx * qy + qz * qw));
  785. var roll = Math.atan2(2.0 * (qx * qw - qy * qz), 1.0 - 2.0 * (sqx + sqz));
  786. var gimbaLockTest = qx * qy + qz * qw;
  787. if (gimbaLockTest > 0.499) {
  788. yaw = 2.0 * Math.atan2(qx, qw);
  789. roll = 0;
  790. } else if (gimbaLockTest < -0.499) {
  791. yaw = -2.0 * Math.atan2(qx, qw);
  792. roll = 0;
  793. }
  794. result.x = pitch;
  795. result.y = yaw;
  796. result.z = roll;
  797. };
  798. Quaternion.prototype.toRotationMatrix = function (result) {
  799. var xx = this.x * this.x;
  800. var yy = this.y * this.y;
  801. var zz = this.z * this.z;
  802. var xy = this.x * this.y;
  803. var zw = this.z * this.w;
  804. var zx = this.z * this.x;
  805. var yw = this.y * this.w;
  806. var yz = this.y * this.z;
  807. var xw = this.x * this.w;
  808. result.m[0] = 1.0 - (2.0 * (yy + zz));
  809. result.m[1] = 2.0 * (xy + zw);
  810. result.m[2] = 2.0 * (zx - yw);
  811. result.m[3] = 0;
  812. result.m[4] = 2.0 * (xy - zw);
  813. result.m[5] = 1.0 - (2.0 * (zz + xx));
  814. result.m[6] = 2.0 * (yz + xw);
  815. result.m[7] = 0;
  816. result.m[8] = 2.0 * (zx + yw);
  817. result.m[9] = 2.0 * (yz - xw);
  818. result.m[10] = 1.0 - (2.0 * (yy + xx));
  819. result.m[11] = 0;
  820. result.m[12] = 0;
  821. result.m[13] = 0;
  822. result.m[14] = 0;
  823. result.m[15] = 1.0;
  824. };
  825. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  826. var data = matrix.m;
  827. var m11 = data[0], m12 = data[4], m13 = data[8];
  828. var m21 = data[1], m22 = data[5], m23 = data[9];
  829. var m31 = data[2], m32 = data[6], m33 = data[10];
  830. var trace = m11 + m22 + m33;
  831. var s;
  832. if (trace > 0) {
  833. s = 0.5 / Math.sqrt(trace + 1.0);
  834. this.w = 0.25 / s;
  835. this.x = (m32 - m23) * s;
  836. this.y = (m13 - m31) * s;
  837. this.z = (m21 - m12) * s;
  838. return;
  839. }
  840. if (m11 > m22 && m11 > m33) {
  841. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  842. this.w = (m32 - m23) / s;
  843. this.x = 0.25 * s;
  844. this.y = (m12 + m21) / s;
  845. this.z = (m13 + m31) / s;
  846. return;
  847. }
  848. if (m22 > m33) {
  849. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  850. this.w = (m13 - m31) / s;
  851. this.x = (m12 + m21) / s;
  852. this.y = 0.25 * s;
  853. this.z = (m23 + m32) / s;
  854. return;
  855. }
  856. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  857. this.w = (m21 - m12) / s;
  858. this.x = (m13 + m31) / s;
  859. this.y = (m23 + m32) / s;
  860. this.z = 0.25 * s;
  861. };
  862. // Statics
  863. Quaternion.Inverse = function (q) {
  864. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  865. };
  866. Quaternion.RotationAxis = function (axis, angle) {
  867. var result = new Quaternion();
  868. var sin = Math.sin(angle / 2);
  869. result.w = Math.cos(angle / 2);
  870. result.x = axis.x * sin;
  871. result.y = axis.y * sin;
  872. result.z = axis.z * sin;
  873. return result;
  874. };
  875. Quaternion.FromArray = function (array, offset) {
  876. if (!offset) {
  877. offset = 0;
  878. }
  879. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  880. };
  881. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  882. var result = new Quaternion();
  883. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  884. return result;
  885. };
  886. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  887. var halfRoll = roll * 0.5;
  888. var halfPitch = pitch * 0.5;
  889. var halfYaw = yaw * 0.5;
  890. var sinRoll = Math.sin(halfRoll);
  891. var cosRoll = Math.cos(halfRoll);
  892. var sinPitch = Math.sin(halfPitch);
  893. var cosPitch = Math.cos(halfPitch);
  894. var sinYaw = Math.sin(halfYaw);
  895. var cosYaw = Math.cos(halfYaw);
  896. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  897. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  898. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  899. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  900. };
  901. Quaternion.Slerp = function (left, right, amount) {
  902. var num2;
  903. var num3;
  904. var num = amount;
  905. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  906. var flag = false;
  907. if (num4 < 0) {
  908. flag = true;
  909. num4 = -num4;
  910. }
  911. if (num4 > 0.999999) {
  912. num3 = 1 - num;
  913. num2 = flag ? -num : num;
  914. } else {
  915. var num5 = Math.acos(num4);
  916. var num6 = (1.0 / Math.sin(num5));
  917. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  918. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  919. }
  920. 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));
  921. };
  922. return Quaternion;
  923. })();
  924. BABYLON.Quaternion = Quaternion;
  925. var Matrix = (function () {
  926. function Matrix() {
  927. this.m = new Float32Array(16);
  928. }
  929. // Properties
  930. Matrix.prototype.isIdentity = function () {
  931. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  932. return false;
  933. 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)
  934. return false;
  935. return true;
  936. };
  937. Matrix.prototype.determinant = function () {
  938. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  939. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  940. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  941. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  942. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  943. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  944. 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))));
  945. };
  946. // Methods
  947. Matrix.prototype.toArray = function () {
  948. return this.m;
  949. };
  950. Matrix.prototype.asArray = function () {
  951. return this.toArray();
  952. };
  953. Matrix.prototype.invert = function () {
  954. this.invertToRef(this);
  955. };
  956. Matrix.prototype.invertToRef = function (other) {
  957. var l1 = this.m[0];
  958. var l2 = this.m[1];
  959. var l3 = this.m[2];
  960. var l4 = this.m[3];
  961. var l5 = this.m[4];
  962. var l6 = this.m[5];
  963. var l7 = this.m[6];
  964. var l8 = this.m[7];
  965. var l9 = this.m[8];
  966. var l10 = this.m[9];
  967. var l11 = this.m[10];
  968. var l12 = this.m[11];
  969. var l13 = this.m[12];
  970. var l14 = this.m[13];
  971. var l15 = this.m[14];
  972. var l16 = this.m[15];
  973. var l17 = (l11 * l16) - (l12 * l15);
  974. var l18 = (l10 * l16) - (l12 * l14);
  975. var l19 = (l10 * l15) - (l11 * l14);
  976. var l20 = (l9 * l16) - (l12 * l13);
  977. var l21 = (l9 * l15) - (l11 * l13);
  978. var l22 = (l9 * l14) - (l10 * l13);
  979. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  980. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  981. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  982. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  983. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  984. var l28 = (l7 * l16) - (l8 * l15);
  985. var l29 = (l6 * l16) - (l8 * l14);
  986. var l30 = (l6 * l15) - (l7 * l14);
  987. var l31 = (l5 * l16) - (l8 * l13);
  988. var l32 = (l5 * l15) - (l7 * l13);
  989. var l33 = (l5 * l14) - (l6 * l13);
  990. var l34 = (l7 * l12) - (l8 * l11);
  991. var l35 = (l6 * l12) - (l8 * l10);
  992. var l36 = (l6 * l11) - (l7 * l10);
  993. var l37 = (l5 * l12) - (l8 * l9);
  994. var l38 = (l5 * l11) - (l7 * l9);
  995. var l39 = (l5 * l10) - (l6 * l9);
  996. other.m[0] = l23 * l27;
  997. other.m[4] = l24 * l27;
  998. other.m[8] = l25 * l27;
  999. other.m[12] = l26 * l27;
  1000. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1001. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1002. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1003. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1004. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1005. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1006. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1007. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1008. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1009. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1010. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1011. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1012. };
  1013. Matrix.prototype.setTranslation = function (vector3) {
  1014. this.m[12] = vector3.x;
  1015. this.m[13] = vector3.y;
  1016. this.m[14] = vector3.z;
  1017. };
  1018. Matrix.prototype.multiply = function (other) {
  1019. var result = new Matrix();
  1020. this.multiplyToRef(other, result);
  1021. return result;
  1022. };
  1023. Matrix.prototype.copyFrom = function (other) {
  1024. for (var index = 0; index < 16; index++) {
  1025. this.m[index] = other.m[index];
  1026. }
  1027. };
  1028. Matrix.prototype.copyToArray = function (array, offset) {
  1029. if (typeof offset === "undefined") { offset = 0; }
  1030. for (var index = 0; index < 16; index++) {
  1031. array[offset + index] = this.m[index];
  1032. }
  1033. };
  1034. Matrix.prototype.multiplyToRef = function (other, result) {
  1035. this.multiplyToArray(other, result.m, 0);
  1036. };
  1037. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1038. var tm0 = this.m[0];
  1039. var tm1 = this.m[1];
  1040. var tm2 = this.m[2];
  1041. var tm3 = this.m[3];
  1042. var tm4 = this.m[4];
  1043. var tm5 = this.m[5];
  1044. var tm6 = this.m[6];
  1045. var tm7 = this.m[7];
  1046. var tm8 = this.m[8];
  1047. var tm9 = this.m[9];
  1048. var tm10 = this.m[10];
  1049. var tm11 = this.m[11];
  1050. var tm12 = this.m[12];
  1051. var tm13 = this.m[13];
  1052. var tm14 = this.m[14];
  1053. var tm15 = this.m[15];
  1054. var om0 = other.m[0];
  1055. var om1 = other.m[1];
  1056. var om2 = other.m[2];
  1057. var om3 = other.m[3];
  1058. var om4 = other.m[4];
  1059. var om5 = other.m[5];
  1060. var om6 = other.m[6];
  1061. var om7 = other.m[7];
  1062. var om8 = other.m[8];
  1063. var om9 = other.m[9];
  1064. var om10 = other.m[10];
  1065. var om11 = other.m[11];
  1066. var om12 = other.m[12];
  1067. var om13 = other.m[13];
  1068. var om14 = other.m[14];
  1069. var om15 = other.m[15];
  1070. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1071. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1072. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1073. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1074. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1075. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1076. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1077. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1078. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1079. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1080. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1081. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1082. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1083. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1084. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1085. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1086. };
  1087. Matrix.prototype.equals = function (value) {
  1088. 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]);
  1089. };
  1090. Matrix.prototype.clone = function () {
  1091. 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]);
  1092. };
  1093. // Statics
  1094. Matrix.FromArray = function (array, offset) {
  1095. var result = new Matrix();
  1096. if (!offset) {
  1097. offset = 0;
  1098. }
  1099. Matrix.FromArrayToRef(array, offset, result);
  1100. return result;
  1101. };
  1102. Matrix.FromArrayToRef = function (array, offset, result) {
  1103. for (var index = 0; index < 16; index++) {
  1104. result.m[index] = array[index + offset];
  1105. }
  1106. };
  1107. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1108. result.m[0] = initialM11;
  1109. result.m[1] = initialM12;
  1110. result.m[2] = initialM13;
  1111. result.m[3] = initialM14;
  1112. result.m[4] = initialM21;
  1113. result.m[5] = initialM22;
  1114. result.m[6] = initialM23;
  1115. result.m[7] = initialM24;
  1116. result.m[8] = initialM31;
  1117. result.m[9] = initialM32;
  1118. result.m[10] = initialM33;
  1119. result.m[11] = initialM34;
  1120. result.m[12] = initialM41;
  1121. result.m[13] = initialM42;
  1122. result.m[14] = initialM43;
  1123. result.m[15] = initialM44;
  1124. };
  1125. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  1126. var result = new Matrix();
  1127. result.m[0] = initialM11;
  1128. result.m[1] = initialM12;
  1129. result.m[2] = initialM13;
  1130. result.m[3] = initialM14;
  1131. result.m[4] = initialM21;
  1132. result.m[5] = initialM22;
  1133. result.m[6] = initialM23;
  1134. result.m[7] = initialM24;
  1135. result.m[8] = initialM31;
  1136. result.m[9] = initialM32;
  1137. result.m[10] = initialM33;
  1138. result.m[11] = initialM34;
  1139. result.m[12] = initialM41;
  1140. result.m[13] = initialM42;
  1141. result.m[14] = initialM43;
  1142. result.m[15] = initialM44;
  1143. return result;
  1144. };
  1145. Matrix.Identity = function () {
  1146. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  1147. };
  1148. Matrix.IdentityToRef = function (result) {
  1149. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  1150. };
  1151. Matrix.Zero = function () {
  1152. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1153. };
  1154. Matrix.RotationX = function (angle) {
  1155. var result = new Matrix();
  1156. Matrix.RotationXToRef(angle, result);
  1157. return result;
  1158. };
  1159. Matrix.RotationXToRef = function (angle, result) {
  1160. var s = Math.sin(angle);
  1161. var c = Math.cos(angle);
  1162. result.m[0] = 1.0;
  1163. result.m[15] = 1.0;
  1164. result.m[5] = c;
  1165. result.m[10] = c;
  1166. result.m[9] = -s;
  1167. result.m[6] = s;
  1168. result.m[1] = 0;
  1169. result.m[2] = 0;
  1170. result.m[3] = 0;
  1171. result.m[4] = 0;
  1172. result.m[7] = 0;
  1173. result.m[8] = 0;
  1174. result.m[11] = 0;
  1175. result.m[12] = 0;
  1176. result.m[13] = 0;
  1177. result.m[14] = 0;
  1178. };
  1179. Matrix.RotationY = function (angle) {
  1180. var result = new Matrix();
  1181. Matrix.RotationYToRef(angle, result);
  1182. return result;
  1183. };
  1184. Matrix.RotationYToRef = function (angle, result) {
  1185. var s = Math.sin(angle);
  1186. var c = Math.cos(angle);
  1187. result.m[5] = 1.0;
  1188. result.m[15] = 1.0;
  1189. result.m[0] = c;
  1190. result.m[2] = -s;
  1191. result.m[8] = s;
  1192. result.m[10] = c;
  1193. result.m[1] = 0;
  1194. result.m[3] = 0;
  1195. result.m[4] = 0;
  1196. result.m[6] = 0;
  1197. result.m[7] = 0;
  1198. result.m[9] = 0;
  1199. result.m[11] = 0;
  1200. result.m[12] = 0;
  1201. result.m[13] = 0;
  1202. result.m[14] = 0;
  1203. };
  1204. Matrix.RotationZ = function (angle) {
  1205. var result = new Matrix();
  1206. Matrix.RotationZToRef(angle, result);
  1207. return result;
  1208. };
  1209. Matrix.RotationZToRef = function (angle, result) {
  1210. var s = Math.sin(angle);
  1211. var c = Math.cos(angle);
  1212. result.m[10] = 1.0;
  1213. result.m[15] = 1.0;
  1214. result.m[0] = c;
  1215. result.m[1] = s;
  1216. result.m[4] = -s;
  1217. result.m[5] = c;
  1218. result.m[2] = 0;
  1219. result.m[3] = 0;
  1220. result.m[6] = 0;
  1221. result.m[7] = 0;
  1222. result.m[8] = 0;
  1223. result.m[9] = 0;
  1224. result.m[11] = 0;
  1225. result.m[12] = 0;
  1226. result.m[13] = 0;
  1227. result.m[14] = 0;
  1228. };
  1229. Matrix.RotationAxis = function (axis, angle) {
  1230. var s = Math.sin(-angle);
  1231. var c = Math.cos(-angle);
  1232. var c1 = 1 - c;
  1233. axis.normalize();
  1234. var result = Matrix.Zero();
  1235. result.m[0] = (axis.x * axis.x) * c1 + c;
  1236. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1237. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1238. result.m[3] = 0.0;
  1239. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1240. result.m[5] = (axis.y * axis.y) * c1 + c;
  1241. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1242. result.m[7] = 0.0;
  1243. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1244. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1245. result.m[10] = (axis.z * axis.z) * c1 + c;
  1246. result.m[11] = 0.0;
  1247. result.m[15] = 1.0;
  1248. return result;
  1249. };
  1250. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1251. var result = new Matrix();
  1252. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1253. return result;
  1254. };
  1255. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1256. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1257. this._tempQuaternion.toRotationMatrix(result);
  1258. };
  1259. Matrix.Scaling = function (x, y, z) {
  1260. var result = Matrix.Zero();
  1261. Matrix.ScalingToRef(x, y, z, result);
  1262. return result;
  1263. };
  1264. Matrix.ScalingToRef = function (x, y, z, result) {
  1265. result.m[0] = x;
  1266. result.m[1] = 0;
  1267. result.m[2] = 0;
  1268. result.m[3] = 0;
  1269. result.m[4] = 0;
  1270. result.m[5] = y;
  1271. result.m[6] = 0;
  1272. result.m[7] = 0;
  1273. result.m[8] = 0;
  1274. result.m[9] = 0;
  1275. result.m[10] = z;
  1276. result.m[11] = 0;
  1277. result.m[12] = 0;
  1278. result.m[13] = 0;
  1279. result.m[14] = 0;
  1280. result.m[15] = 1.0;
  1281. };
  1282. Matrix.Translation = function (x, y, z) {
  1283. var result = Matrix.Identity();
  1284. Matrix.TranslationToRef(x, y, z, result);
  1285. return result;
  1286. };
  1287. Matrix.TranslationToRef = function (x, y, z, result) {
  1288. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  1289. };
  1290. Matrix.LookAtLH = function (eye, target, up) {
  1291. var result = Matrix.Zero();
  1292. Matrix.LookAtLHToRef(eye, target, up, result);
  1293. return result;
  1294. };
  1295. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1296. // Z axis
  1297. target.subtractToRef(eye, this._zAxis);
  1298. this._zAxis.normalize();
  1299. // X axis
  1300. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1301. this._xAxis.normalize();
  1302. // Y axis
  1303. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1304. this._yAxis.normalize();
  1305. // Eye angles
  1306. var ex = -Vector3.Dot(this._xAxis, eye);
  1307. var ey = -Vector3.Dot(this._yAxis, eye);
  1308. var ez = -Vector3.Dot(this._zAxis, eye);
  1309. 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);
  1310. };
  1311. Matrix.OrthoLH = function (width, height, znear, zfar) {
  1312. var hw = 2.0 / width;
  1313. var hh = 2.0 / height;
  1314. var id = 1.0 / (zfar - znear);
  1315. var nid = znear / (znear - zfar);
  1316. return Matrix.FromValues(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1);
  1317. };
  1318. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1319. var matrix = Matrix.Zero();
  1320. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1321. return matrix;
  1322. };
  1323. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1324. result.m[0] = 2.0 / (right - left);
  1325. result.m[1] = result.m[2] = result.m[3] = 0;
  1326. result.m[5] = 2.0 / (top - bottom);
  1327. result.m[4] = result.m[6] = result.m[7] = 0;
  1328. result.m[10] = -1.0 / (znear - zfar);
  1329. result.m[8] = result.m[9] = result.m[11] = 0;
  1330. result.m[12] = (left + right) / (left - right);
  1331. result.m[13] = (top + bottom) / (bottom - top);
  1332. result.m[14] = znear / (znear - zfar);
  1333. result.m[15] = 1.0;
  1334. };
  1335. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1336. var matrix = Matrix.Zero();
  1337. matrix.m[0] = (2.0 * znear) / width;
  1338. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1339. matrix.m[5] = (2.0 * znear) / height;
  1340. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1341. matrix.m[10] = -zfar / (znear - zfar);
  1342. matrix.m[8] = matrix.m[9] = 0.0;
  1343. matrix.m[11] = 1.0;
  1344. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1345. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1346. return matrix;
  1347. };
  1348. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1349. var matrix = Matrix.Zero();
  1350. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1351. return matrix;
  1352. };
  1353. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result) {
  1354. var tan = 1.0 / (Math.tan(fov * 0.5));
  1355. result.m[0] = tan / aspect;
  1356. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1357. result.m[5] = tan;
  1358. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1359. result.m[8] = result.m[9] = 0.0;
  1360. result.m[10] = -zfar / (znear - zfar);
  1361. result.m[11] = 1.0;
  1362. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1363. result.m[14] = (znear * zfar) / (znear - zfar);
  1364. };
  1365. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  1366. var cw = viewport.width;
  1367. var ch = viewport.height;
  1368. var cx = viewport.x;
  1369. var cy = viewport.y;
  1370. 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);
  1371. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1372. };
  1373. Matrix.Transpose = function (matrix) {
  1374. var result = new Matrix();
  1375. result.m[0] = matrix.m[0];
  1376. result.m[1] = matrix.m[4];
  1377. result.m[2] = matrix.m[8];
  1378. result.m[3] = matrix.m[12];
  1379. result.m[4] = matrix.m[1];
  1380. result.m[5] = matrix.m[5];
  1381. result.m[6] = matrix.m[9];
  1382. result.m[7] = matrix.m[13];
  1383. result.m[8] = matrix.m[2];
  1384. result.m[9] = matrix.m[6];
  1385. result.m[10] = matrix.m[10];
  1386. result.m[11] = matrix.m[14];
  1387. result.m[12] = matrix.m[3];
  1388. result.m[13] = matrix.m[7];
  1389. result.m[14] = matrix.m[11];
  1390. result.m[15] = matrix.m[15];
  1391. return result;
  1392. };
  1393. Matrix.Reflection = function (plane) {
  1394. var matrix = new Matrix();
  1395. Matrix.ReflectionToRef(plane, matrix);
  1396. return matrix;
  1397. };
  1398. Matrix.ReflectionToRef = function (plane, result) {
  1399. plane.normalize();
  1400. var x = plane.normal.x;
  1401. var y = plane.normal.y;
  1402. var z = plane.normal.z;
  1403. var temp = -2 * x;
  1404. var temp2 = -2 * y;
  1405. var temp3 = -2 * z;
  1406. result.m[0] = (temp * x) + 1;
  1407. result.m[1] = temp2 * x;
  1408. result.m[2] = temp3 * x;
  1409. result.m[3] = 0.0;
  1410. result.m[4] = temp * y;
  1411. result.m[5] = (temp2 * y) + 1;
  1412. result.m[6] = temp3 * y;
  1413. result.m[7] = 0.0;
  1414. result.m[8] = temp * z;
  1415. result.m[9] = temp2 * z;
  1416. result.m[10] = (temp3 * z) + 1;
  1417. result.m[11] = 0.0;
  1418. result.m[12] = temp * plane.d;
  1419. result.m[13] = temp2 * plane.d;
  1420. result.m[14] = temp3 * plane.d;
  1421. result.m[15] = 1.0;
  1422. };
  1423. Matrix._tempQuaternion = new Quaternion();
  1424. Matrix._xAxis = Vector3.Zero();
  1425. Matrix._yAxis = Vector3.Zero();
  1426. Matrix._zAxis = Vector3.Zero();
  1427. return Matrix;
  1428. })();
  1429. BABYLON.Matrix = Matrix;
  1430. var Plane = (function () {
  1431. function Plane(a, b, c, d) {
  1432. this.normal = new Vector3(a, b, c);
  1433. this.d = d;
  1434. }
  1435. Plane.prototype.asArray = function () {
  1436. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  1437. };
  1438. // Methods
  1439. Plane.prototype.clone = function () {
  1440. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  1441. };
  1442. Plane.prototype.normalize = function () {
  1443. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1444. var magnitude = 0;
  1445. if (norm != 0) {
  1446. magnitude = 1.0 / norm;
  1447. }
  1448. this.normal.x *= magnitude;
  1449. this.normal.y *= magnitude;
  1450. this.normal.z *= magnitude;
  1451. this.d *= magnitude;
  1452. };
  1453. Plane.prototype.transform = function (transformation) {
  1454. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1455. var x = this.normal.x;
  1456. var y = this.normal.y;
  1457. var z = this.normal.z;
  1458. var d = this.d;
  1459. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1460. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1461. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1462. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1463. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1464. };
  1465. Plane.prototype.dotCoordinate = function (point) {
  1466. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1467. };
  1468. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  1469. var x1 = point2.x - point1.x;
  1470. var y1 = point2.y - point1.y;
  1471. var z1 = point2.z - point1.z;
  1472. var x2 = point3.x - point1.x;
  1473. var y2 = point3.y - point1.y;
  1474. var z2 = point3.z - point1.z;
  1475. var yz = (y1 * z2) - (z1 * y2);
  1476. var xz = (z1 * x2) - (x1 * z2);
  1477. var xy = (x1 * y2) - (y1 * x2);
  1478. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1479. var invPyth;
  1480. if (pyth != 0) {
  1481. invPyth = 1.0 / pyth;
  1482. } else {
  1483. invPyth = 0;
  1484. }
  1485. this.normal.x = yz * invPyth;
  1486. this.normal.y = xz * invPyth;
  1487. this.normal.z = xy * invPyth;
  1488. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1489. };
  1490. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  1491. var dot = Vector3.Dot(this.normal, direction);
  1492. return (dot <= epsilon);
  1493. };
  1494. Plane.prototype.signedDistanceTo = function (point) {
  1495. return Vector3.Dot(point, this.normal) + this.d;
  1496. };
  1497. // Statics
  1498. Plane.FromArray = function (array) {
  1499. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1500. };
  1501. Plane.FromPoints = function (point1, point2, point3) {
  1502. var result = new BABYLON.Plane(0, 0, 0, 0);
  1503. result.copyFromPoints(point1, point2, point3);
  1504. return result;
  1505. };
  1506. Plane.FromPositionAndNormal = function (origin, normal) {
  1507. var result = new BABYLON.Plane(0, 0, 0, 0);
  1508. normal.normalize();
  1509. result.normal = normal;
  1510. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1511. return result;
  1512. };
  1513. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  1514. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1515. return Vector3.Dot(point, normal) + d;
  1516. };
  1517. return Plane;
  1518. })();
  1519. BABYLON.Plane = Plane;
  1520. var Viewport = (function () {
  1521. function Viewport(x, y, width, height) {
  1522. this.x = x;
  1523. this.y = y;
  1524. this.width = width;
  1525. this.height = height;
  1526. }
  1527. Viewport.prototype.toGlobal = function (engine) {
  1528. var width = engine.getRenderWidth();
  1529. var height = engine.getRenderHeight();
  1530. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1531. };
  1532. return Viewport;
  1533. })();
  1534. BABYLON.Viewport = Viewport;
  1535. var Frustum = (function () {
  1536. function Frustum() {
  1537. }
  1538. Frustum.GetPlanes = function (transform) {
  1539. var frustumPlanes = [];
  1540. for (var index = 0; index < 6; index++) {
  1541. frustumPlanes.push(new Plane(0, 0, 0, 0));
  1542. }
  1543. Frustum.GetPlanesToRef(transform, frustumPlanes);
  1544. return frustumPlanes;
  1545. };
  1546. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  1547. // Near
  1548. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1549. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1550. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1551. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1552. frustumPlanes[0].normalize();
  1553. // Far
  1554. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1555. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1556. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1557. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1558. frustumPlanes[1].normalize();
  1559. // Left
  1560. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1561. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1562. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1563. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1564. frustumPlanes[2].normalize();
  1565. // Right
  1566. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1567. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1568. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1569. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1570. frustumPlanes[3].normalize();
  1571. // Top
  1572. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1573. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1574. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1575. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1576. frustumPlanes[4].normalize();
  1577. // Bottom
  1578. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1579. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1580. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1581. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1582. frustumPlanes[5].normalize();
  1583. };
  1584. return Frustum;
  1585. })();
  1586. BABYLON.Frustum = Frustum;
  1587. var Ray = (function () {
  1588. function Ray(origin, direction) {
  1589. this.origin = origin;
  1590. this.direction = direction;
  1591. }
  1592. // Methods
  1593. Ray.prototype.intersectsBoxMinMax = function (minimum, maximum) {
  1594. var d = 0.0;
  1595. var maxValue = Number.MAX_VALUE;
  1596. if (Math.abs(this.direction.x) < 0.0000001) {
  1597. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  1598. return false;
  1599. }
  1600. } else {
  1601. var inv = 1.0 / this.direction.x;
  1602. var min = (minimum.x - this.origin.x) * inv;
  1603. var max = (maximum.x - this.origin.x) * inv;
  1604. if (min > max) {
  1605. var temp = min;
  1606. min = max;
  1607. max = temp;
  1608. }
  1609. d = Math.max(min, d);
  1610. maxValue = Math.min(max, maxValue);
  1611. if (d > maxValue) {
  1612. return false;
  1613. }
  1614. }
  1615. if (Math.abs(this.direction.y) < 0.0000001) {
  1616. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  1617. return false;
  1618. }
  1619. } else {
  1620. inv = 1.0 / this.direction.y;
  1621. min = (minimum.y - this.origin.y) * inv;
  1622. max = (maximum.y - this.origin.y) * inv;
  1623. if (min > max) {
  1624. temp = min;
  1625. min = max;
  1626. max = temp;
  1627. }
  1628. d = Math.max(min, d);
  1629. maxValue = Math.min(max, maxValue);
  1630. if (d > maxValue) {
  1631. return false;
  1632. }
  1633. }
  1634. if (Math.abs(this.direction.z) < 0.0000001) {
  1635. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  1636. return false;
  1637. }
  1638. } else {
  1639. inv = 1.0 / this.direction.z;
  1640. min = (minimum.z - this.origin.z) * inv;
  1641. max = (maximum.z - this.origin.z) * inv;
  1642. if (min > max) {
  1643. temp = min;
  1644. min = max;
  1645. max = temp;
  1646. }
  1647. d = Math.max(min, d);
  1648. maxValue = Math.min(max, maxValue);
  1649. if (d > maxValue) {
  1650. return false;
  1651. }
  1652. }
  1653. return true;
  1654. };
  1655. Ray.prototype.intersectsBox = function (box) {
  1656. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  1657. };
  1658. Ray.prototype.intersectsSphere = function (sphere) {
  1659. var x = sphere.center.x - this.origin.x;
  1660. var y = sphere.center.y - this.origin.y;
  1661. var z = sphere.center.z - this.origin.z;
  1662. var pyth = (x * x) + (y * y) + (z * z);
  1663. var rr = sphere.radius * sphere.radius;
  1664. if (pyth <= rr) {
  1665. return true;
  1666. }
  1667. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  1668. if (dot < 0.0) {
  1669. return false;
  1670. }
  1671. var temp = pyth - (dot * dot);
  1672. return temp <= rr;
  1673. };
  1674. Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  1675. if (!this._edge1) {
  1676. this._edge1 = BABYLON.Vector3.Zero();
  1677. this._edge2 = BABYLON.Vector3.Zero();
  1678. this._pvec = BABYLON.Vector3.Zero();
  1679. this._tvec = BABYLON.Vector3.Zero();
  1680. this._qvec = BABYLON.Vector3.Zero();
  1681. }
  1682. vertex1.subtractToRef(vertex0, this._edge1);
  1683. vertex2.subtractToRef(vertex0, this._edge2);
  1684. BABYLON.Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  1685. var det = Vector3.Dot(this._edge1, this._pvec);
  1686. if (det === 0) {
  1687. return null;
  1688. }
  1689. var invdet = 1 / det;
  1690. this.origin.subtractToRef(vertex0, this._tvec);
  1691. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  1692. if (bu < 0 || bu > 1.0) {
  1693. return null;
  1694. }
  1695. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  1696. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  1697. if (bv < 0 || bu + bv > 1.0) {
  1698. return null;
  1699. }
  1700. return new BABYLON.IntersectionInfo(bu, bv, Vector3.Dot(this._edge2, this._qvec) * invdet);
  1701. };
  1702. // Statics
  1703. Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  1704. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  1705. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  1706. var direction = end.subtract(start);
  1707. direction.normalize();
  1708. return new Ray(start, direction);
  1709. };
  1710. Ray.Transform = function (ray, matrix) {
  1711. var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
  1712. var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
  1713. return new Ray(newOrigin, newDirection);
  1714. };
  1715. return Ray;
  1716. })();
  1717. BABYLON.Ray = Ray;
  1718. (function (Space) {
  1719. Space[Space["LOCAL"] = 0] = "LOCAL";
  1720. Space[Space["WORLD"] = 1] = "WORLD";
  1721. })(BABYLON.Space || (BABYLON.Space = {}));
  1722. var Space = BABYLON.Space;
  1723. var Axis = (function () {
  1724. function Axis() {
  1725. }
  1726. Axis.X = new BABYLON.Vector3(1, 0, 0);
  1727. Axis.Y = new BABYLON.Vector3(0, 1, 0);
  1728. Axis.Z = new BABYLON.Vector3(0, 0, 1);
  1729. return Axis;
  1730. })();
  1731. BABYLON.Axis = Axis;
  1732. ;
  1733. })(BABYLON || (BABYLON = {}));
  1734. //# sourceMappingURL=babylon.math.js.map