babylon.math.js 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. ////////////////////////////////// Ray //////////////////////////////////
  5. BABYLON.Ray = function (origin, direction) {
  6. this.origin = origin;
  7. this.direction = direction;
  8. };
  9. // Methods
  10. BABYLON.Ray.prototype.intersectsBox = function (box) {
  11. var d = 0.0;
  12. var maxValue = Number.MAX_VALUE;
  13. if (Math.abs(this.direction.x) < 0.0000001) {
  14. if (this.origin.x < box.minimum.x || this.origin.x > box.maximum.x) {
  15. return false;
  16. }
  17. }
  18. else {
  19. var inv = 1.0 / this.direction.x;
  20. var min = (box.minimum.x - this.origin.x) * inv;
  21. var max = (box.maximum.x - this.origin.x) * inv;
  22. if (min > max) {
  23. var temp = min;
  24. min = max;
  25. max = temp;
  26. }
  27. d = Math.max(min, d);
  28. maxValue = Math.min(max, maxValue);
  29. if (d > maxValue) {
  30. return false;
  31. }
  32. }
  33. if (Math.abs(this.direction.y) < 0.0000001) {
  34. if (this.origin.y < box.minimum.y || this.origin.y > box.maximum.y) {
  35. return false;
  36. }
  37. }
  38. else {
  39. var inv = 1.0 / this.direction.y;
  40. var min = (box.minimum.y - this.origin.y) * inv;
  41. var max = (box.maximum.y - this.origin.y) * inv;
  42. if (min > max) {
  43. var temp = min;
  44. min = max;
  45. max = temp;
  46. }
  47. d = Math.max(min, d);
  48. maxValue = Math.min(max, maxValue);
  49. if (d > maxValue) {
  50. return false;
  51. }
  52. }
  53. if (Math.abs(this.direction.z) < 0.0000001) {
  54. if (this.origin.z < box.minimum.z || this.origin.z > box.maximum.z) {
  55. return false;
  56. }
  57. }
  58. else {
  59. var inv = 1.0 / this.direction.z;
  60. var min = (box.minimum.z - this.origin.z) * inv;
  61. var max = (box.maximum.z - this.origin.z) * inv;
  62. if (min > max) {
  63. var temp = min;
  64. min = max;
  65. max = temp;
  66. }
  67. d = Math.max(min, d);
  68. maxValue = Math.min(max, maxValue);
  69. if (d > maxValue) {
  70. return false;
  71. }
  72. }
  73. return true;
  74. };
  75. BABYLON.Ray.prototype.intersectsSphere = function (sphere) {
  76. var x = sphere.center.x - this.origin.x;
  77. var y = sphere.center.y - this.origin.y;
  78. var z = sphere.center.z - this.origin.z;
  79. var pyth = (x * x) + (y * y) + (z * z);
  80. var rr = sphere.radius * sphere.radius;
  81. if (pyth <= rr) {
  82. return true;
  83. }
  84. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  85. if (dot < 0.0) {
  86. return false;
  87. }
  88. var temp = pyth - (dot * dot);
  89. return temp <= rr;
  90. };
  91. BABYLON.Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  92. if (!this._edge1) {
  93. this._edge1 = BABYLON.Vector3.Zero();
  94. this._edge2 = BABYLON.Vector3.Zero();
  95. this._pvec = BABYLON.Vector3.Zero();
  96. this._tvec = BABYLON.Vector3.Zero();
  97. this._qvec = BABYLON.Vector3.Zero();
  98. }
  99. vertex1.subtractToRef(vertex0, this._edge1);
  100. vertex2.subtractToRef(vertex0, this._edge2);
  101. BABYLON.Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  102. var det = BABYLON.Vector3.Dot(this._edge1, this._pvec);
  103. if (det === 0) {
  104. return null;
  105. }
  106. var invdet = 1 / det;
  107. this.origin.subtractToRef(vertex0, this._tvec);
  108. var bu = BABYLON.Vector3.Dot(this._tvec, this._pvec) * invdet;
  109. if (bu < 0 || bu > 1.0) {
  110. return null;
  111. }
  112. BABYLON.Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  113. var bv = BABYLON.Vector3.Dot(this.direction, this._qvec) * invdet;
  114. if (bv < 0 || bu + bv > 1.0) {
  115. return null;
  116. }
  117. return {
  118. bu: bu,
  119. bv: bv,
  120. distance: BABYLON.Vector3.Dot(this._edge2, this._qvec) * invdet
  121. };
  122. };
  123. // Statics
  124. BABYLON.Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  125. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  126. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  127. var direction = end.subtract(start);
  128. direction.normalize();
  129. return new BABYLON.Ray(start, direction);
  130. };
  131. BABYLON.Ray.Transform = function (ray, matrix) {
  132. var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
  133. var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
  134. return new BABYLON.Ray(newOrigin, newDirection);
  135. };
  136. ////////////////////////////////// Color3 //////////////////////////////////
  137. BABYLON.Color3 = function (initialR, initialG, initialB) {
  138. this.r = initialR;
  139. this.g = initialG;
  140. this.b = initialB;
  141. };
  142. BABYLON.Color3.prototype.toString = function () {
  143. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  144. };
  145. // Operators
  146. BABYLON.Color3.prototype.asArray = function () {
  147. var result = [];
  148. this.toArray(result, 0);
  149. return result;
  150. };
  151. BABYLON.Color3.prototype.toArray = function (array, index) {
  152. if (index === undefined) {
  153. index = 0;
  154. }
  155. array[index] = this.r;
  156. array[index + 1] = this.g;
  157. array[index + 2] = this.b;
  158. };
  159. BABYLON.Color3.prototype.multiply = function (otherColor) {
  160. return new BABYLON.Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  161. };
  162. BABYLON.Color3.prototype.multiplyToRef = function (otherColor, result) {
  163. result.r = this.r * otherColor.r;
  164. result.g = this.g * otherColor.g;
  165. result.b = this.b * otherColor.b;
  166. };
  167. BABYLON.Color3.prototype.equals = function (otherColor) {
  168. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  169. };
  170. BABYLON.Color3.prototype.scale = function (scale) {
  171. return new BABYLON.Color3(this.r * scale, this.g * scale, this.b * scale);
  172. };
  173. BABYLON.Color3.prototype.scaleToRef = function (scale, result) {
  174. result.r = this.r * scale;
  175. result.g = this.g * scale;
  176. result.b = this.b * scale;
  177. };
  178. BABYLON.Color3.prototype.clone = function () {
  179. return new BABYLON.Color3(this.r, this.g, this.b);
  180. };
  181. BABYLON.Color3.prototype.copyFrom = function (source) {
  182. this.r = source.r;
  183. this.g = source.g;
  184. this.b = source.b;
  185. };
  186. BABYLON.Color3.prototype.copyFromFloats = function (r, g, b) {
  187. this.r = r;
  188. this.g = g;
  189. this.b = b;
  190. };
  191. // Statics
  192. BABYLON.Color3.FromArray = function (array) {
  193. return new BABYLON.Color3(array[0], array[1], array[2]);
  194. };
  195. BABYLON.Color3.FromInts = function (r, g, b) {
  196. return new BABYLON.Color3(r / 255.0, g / 255.0, b / 255.0);
  197. };
  198. ////////////////////////////////// Color4 //////////////////////////////////
  199. BABYLON.Color4 = function (initialR, initialG, initialB, initialA) {
  200. this.r = initialR;
  201. this.g = initialG;
  202. this.b = initialB;
  203. this.a = initialA;
  204. };
  205. // Operators
  206. BABYLON.Color4.prototype.addInPlace = function (right) {
  207. this.r += right.r;
  208. this.g += right.g;
  209. this.b += right.b;
  210. this.a += right.a;
  211. };
  212. BABYLON.Color4.prototype.asArray = function () {
  213. var result = [];
  214. this.toArray(result, 0);
  215. return result;
  216. };
  217. BABYLON.Color4.prototype.toArray = function (array, index) {
  218. if (index === undefined) {
  219. index = 0;
  220. }
  221. array[index] = this.r;
  222. array[index + 1] = this.g;
  223. array[index + 2] = this.b;
  224. array[index + 3] = this.a;
  225. };
  226. BABYLON.Color4.prototype.add = function (right) {
  227. return new BABYLON.Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  228. };
  229. BABYLON.Color4.prototype.subtract = function (right) {
  230. return new BABYLON.Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  231. };
  232. BABYLON.Color4.prototype.subtractToRef = function (right, result) {
  233. result.r = this.r - right.r;
  234. result.g = this.g - right.g;
  235. result.b = this.b - right.b;
  236. result.a = this.a - right.a;
  237. };
  238. BABYLON.Color4.prototype.scale = function (scale) {
  239. return new BABYLON.Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  240. };
  241. BABYLON.Color4.prototype.scaleToRef = function (scale, result) {
  242. result.r = this.r * scale;
  243. result.g = this.g * scale;
  244. result.b = this.b * scale;
  245. result.a = this.a * scale;
  246. };
  247. BABYLON.Color4.prototype.toString = function () {
  248. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  249. };
  250. BABYLON.Color4.prototype.clone = function () {
  251. return new BABYLON.Color4(this.r, this.g, this.b, this.a);
  252. };
  253. // Statics
  254. BABYLON.Color4.Lerp = function (left, right, amount) {
  255. var result = new BABYLON.Color4(0, 0, 0, 0);
  256. BABYLON.Color4.LerpToRef(left, right, amount, result);
  257. return result;
  258. };
  259. BABYLON.Color4.LerpToRef = function (left, right, amount, result) {
  260. result.r = left.r + (right.r - left.r) * amount;
  261. result.g = left.g + (right.g - left.g) * amount;
  262. result.b = left.b + (right.b - left.b) * amount;
  263. result.a = left.a + (right.a - left.a) * amount;
  264. };
  265. BABYLON.Color4.FromArray = function (array, offset) {
  266. if (!offset) {
  267. offset = 0;
  268. }
  269. return new BABYLON.Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  270. };
  271. BABYLON.Color4.FromInts = function (r, g, b, a) {
  272. return new BABYLON.Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  273. };
  274. ////////////////////////////////// Vector2 //////////////////////////////////
  275. BABYLON.Vector2 = function (initialX, initialY) {
  276. this.x = initialX;
  277. this.y = initialY;
  278. };
  279. BABYLON.Vector2.prototype.toString = function () {
  280. return "{X: " + this.x + " Y:" + this.y + "}";
  281. };
  282. // Operators
  283. BABYLON.Vector2.prototype.asArray = function () {
  284. var result = [];
  285. this.toArray(result, 0);
  286. return result;
  287. };
  288. BABYLON.Vector2.prototype.toArray = function (array, index) {
  289. if (index === undefined) {
  290. index = 0;
  291. }
  292. array[index] = this.x;
  293. array[index + 1] = this.y;
  294. };
  295. BABYLON.Vector2.prototype.add = function (otherVector) {
  296. return new BABYLON.Vector2(this.x + otherVector.x, this.y + otherVector.y);
  297. };
  298. BABYLON.Vector2.prototype.subtract = function (otherVector) {
  299. return new BABYLON.Vector2(this.x - otherVector.x, this.y - otherVector.y);
  300. };
  301. BABYLON.Vector2.prototype.negate = function () {
  302. return new BABYLON.Vector2(-this.x, -this.y);
  303. };
  304. BABYLON.Vector2.prototype.scaleInPlace = function (scale) {
  305. this.x *= scale;
  306. this.y *= scale;
  307. };
  308. BABYLON.Vector2.prototype.scale = function (scale) {
  309. return new BABYLON.Vector2(this.x * scale, this.y * scale);
  310. };
  311. BABYLON.Vector2.prototype.equals = function (otherVector) {
  312. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  313. };
  314. // Properties
  315. BABYLON.Vector2.prototype.length = function () {
  316. return Math.sqrt(this.x * this.x + this.y * this.y);
  317. };
  318. BABYLON.Vector2.prototype.lengthSquared = function () {
  319. return (this.x * this.x + this.y * this.y);
  320. };
  321. // Methods
  322. BABYLON.Vector2.prototype.normalize = function () {
  323. var len = this.length();
  324. if (len === 0)
  325. return;
  326. var num = 1.0 / len;
  327. this.x *= num;
  328. this.y *= num;
  329. };
  330. BABYLON.Vector2.prototype.clone = function () {
  331. return new BABYLON.Vector2(this.x, this.y);
  332. };
  333. // Statics
  334. BABYLON.Vector2.Zero = function () {
  335. return new BABYLON.Vector2(0, 0);
  336. };
  337. BABYLON.Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  338. var squared = amount * amount;
  339. var cubed = amount * squared;
  340. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  341. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  342. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  343. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  344. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  345. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  346. return new BABYLON.Vector2(x, y);
  347. };
  348. BABYLON.Vector2.Clamp = function (value, min, max) {
  349. var x = value.x;
  350. x = (x > max.x) ? max.x : x;
  351. x = (x < min.x) ? min.x : x;
  352. var y = value.y;
  353. y = (y > max.y) ? max.y : y;
  354. y = (y < min.y) ? min.y : y;
  355. return new BABYLON.Vector2(x, y);
  356. };
  357. BABYLON.Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  358. var squared = amount * amount;
  359. var cubed = amount * squared;
  360. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  361. var part2 = (-2.0 * cubed) + (3.0 * squared);
  362. var part3 = (cubed - (2.0 * squared)) + amount;
  363. var part4 = cubed - squared;
  364. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  365. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  366. return new BABYLON.Vector2(x, y);
  367. };
  368. BABYLON.Vector2.Lerp = function (start, end, amount) {
  369. var x = start.x + ((end.x - start.x) * amount);
  370. var y = start.y + ((end.y - start.y) * amount);
  371. return new BABYLON.Vector2(x, y);
  372. };
  373. BABYLON.Vector2.Dot = function (left, right) {
  374. return left.x * right.x + left.y * right.y;
  375. };
  376. BABYLON.Vector2.Normalize = function (vector) {
  377. var newVector = vector.clone();
  378. newVector.normalize();
  379. return newVector;
  380. };
  381. BABYLON.Vector2.Minimize = function (left, right) {
  382. var x = (left.x < right.x) ? left.x : right.x;
  383. var y = (left.y < right.y) ? left.y : right.y;
  384. return new BABYLON.Vector2(x, y);
  385. };
  386. BABYLON.Vector2.Maximize = function (left, right) {
  387. var x = (left.x > right.x) ? left.x : right.x;
  388. var y = (left.y > right.y) ? left.y : right.y;
  389. return new BABYLON.Vector2(x, y);
  390. };
  391. BABYLON.Vector2.Transform = function (vector, transformation) {
  392. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  393. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  394. return new BABYLON.Vector2(x, y);
  395. };
  396. BABYLON.Vector2.Distance = function (value1, value2) {
  397. return Math.sqrt(BABYLON.Vector2.DistanceSquared(value1, value2));
  398. };
  399. BABYLON.Vector2.DistanceSquared = function (value1, value2) {
  400. var x = value1.x - value2.x;
  401. var y = value1.y - value2.y;
  402. return (x * x) + (y * y);
  403. };
  404. ////////////////////////////////// Vector3 //////////////////////////////////
  405. BABYLON.Vector3 = function (initialX, initialY, initialZ) {
  406. this.x = initialX;
  407. this.y = initialY;
  408. this.z = initialZ;
  409. };
  410. BABYLON.Vector3.prototype.toString = function () {
  411. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  412. };
  413. // Operators
  414. BABYLON.Vector3.prototype.asArray = function () {
  415. var result = [];
  416. this.toArray(result, 0);
  417. return result;
  418. };
  419. BABYLON.Vector3.prototype.toArray = function (array, index) {
  420. if (index === undefined) {
  421. index = 0;
  422. }
  423. array[index] = this.x;
  424. array[index + 1] = this.y;
  425. array[index + 2] = this.z;
  426. };
  427. BABYLON.Vector3.prototype.addInPlace = function (otherVector) {
  428. this.x += otherVector.x;
  429. this.y += otherVector.y;
  430. this.z += otherVector.z;
  431. };
  432. BABYLON.Vector3.prototype.add = function (otherVector) {
  433. return new BABYLON.Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  434. };
  435. BABYLON.Vector3.prototype.addToRef = function (otherVector, result) {
  436. result.x = this.x + otherVector.x;
  437. result.y = this.y + otherVector.y;
  438. result.z = this.z + otherVector.z;
  439. };
  440. BABYLON.Vector3.prototype.subtractInPlace = function (otherVector) {
  441. this.x -= otherVector.x;
  442. this.y -= otherVector.y;
  443. this.z -= otherVector.z;
  444. };
  445. BABYLON.Vector3.prototype.subtract = function (otherVector) {
  446. return new BABYLON.Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  447. };
  448. BABYLON.Vector3.prototype.subtractToRef = function (otherVector, result) {
  449. result.x = this.x - otherVector.x;
  450. result.y = this.y - otherVector.y;
  451. result.z = this.z - otherVector.z;
  452. };
  453. BABYLON.Vector3.prototype.subtractFromFloats = function (x, y, z) {
  454. return new BABYLON.Vector3(this.x - x, this.y - y, this.z - z);
  455. };
  456. BABYLON.Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  457. result.x = this.x - x;
  458. result.y = this.y - y;
  459. result.z = this.z - z;
  460. };
  461. BABYLON.Vector3.prototype.negate = function () {
  462. return new BABYLON.Vector3(-this.x, -this.y, -this.z);
  463. };
  464. BABYLON.Vector3.prototype.scaleInPlace = function (scale) {
  465. this.x *= scale;
  466. this.y *= scale;
  467. this.z *= scale;
  468. };
  469. BABYLON.Vector3.prototype.scale = function (scale) {
  470. return new BABYLON.Vector3(this.x * scale, this.y * scale, this.z * scale);
  471. };
  472. BABYLON.Vector3.prototype.scaleToRef = function (scale, result) {
  473. result.x = this.x * scale;
  474. result.y = this.y * scale;
  475. result.z = this.z * scale;
  476. };
  477. BABYLON.Vector3.prototype.equals = function (otherVector) {
  478. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  479. };
  480. BABYLON.Vector3.prototype.equalsToFloats = function (x, y, z) {
  481. return this.x === x && this.y === y && this.z === z;
  482. };
  483. BABYLON.Vector3.prototype.multiplyInPlace = function (otherVector) {
  484. this.x *= otherVector.x;
  485. this.y *= otherVector.y;
  486. this.z *= otherVector.z;
  487. };
  488. BABYLON.Vector3.prototype.multiply = function (otherVector) {
  489. return new BABYLON.Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  490. };
  491. BABYLON.Vector3.prototype.multiplyToRef = function (otherVector, result) {
  492. result.x = this.x * otherVector.x;
  493. result.y = this.y * otherVector.y;
  494. result.z = this.z * otherVector.z;
  495. };
  496. BABYLON.Vector3.prototype.multiplyByFloats = function (x, y, z) {
  497. return new BABYLON.Vector3(this.x * x, this.y * y, this.z * z);
  498. };
  499. BABYLON.Vector3.prototype.divide = function (otherVector) {
  500. return new BABYLON.Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  501. };
  502. BABYLON.Vector3.prototype.divideToRef = function (otherVector, result) {
  503. result.x = this.x / otherVector.x;
  504. result.y = this.y / otherVector.y;
  505. result.z = this.z / otherVector.z;
  506. };
  507. // Properties
  508. BABYLON.Vector3.prototype.length = function () {
  509. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  510. };
  511. BABYLON.Vector3.prototype.lengthSquared = function () {
  512. return (this.x * this.x + this.y * this.y + this.z * this.z);
  513. };
  514. // Methods
  515. BABYLON.Vector3.prototype.normalize = function () {
  516. var len = this.length();
  517. if (len === 0)
  518. return;
  519. var num = 1.0 / len;
  520. this.x *= num;
  521. this.y *= num;
  522. this.z *= num;
  523. };
  524. BABYLON.Vector3.prototype.clone = function () {
  525. return new BABYLON.Vector3(this.x, this.y, this.z);
  526. };
  527. BABYLON.Vector3.prototype.copyFrom = function (source) {
  528. this.x = source.x;
  529. this.y = source.y;
  530. this.z = source.z;
  531. };
  532. BABYLON.Vector3.prototype.copyFromFloats = function (x, y, z) {
  533. this.x = x;
  534. this.y = y;
  535. this.z = z;
  536. };
  537. // Statics
  538. BABYLON.Vector3.FromArray = function (array, offset) {
  539. if (!offset) {
  540. offset = 0;
  541. }
  542. return new BABYLON.Vector3(array[offset], array[offset + 1], array[offset + 2]);
  543. };
  544. BABYLON.Vector3.FromArrayToRef = function (array, offset, result) {
  545. if (!offset) {
  546. offset = 0;
  547. }
  548. result.x = array[offset];
  549. result.y = array[offset + 1];
  550. result.z = array[offset + 2];
  551. };
  552. BABYLON.Vector3.FromFloatsToRef = function (x, y, z, result) {
  553. result.x = x;
  554. result.y = y;
  555. result.z = z;
  556. };
  557. BABYLON.Vector3.Zero = function () {
  558. return new BABYLON.Vector3(0, 0, 0);
  559. };
  560. BABYLON.Vector3.Up = function () {
  561. return new BABYLON.Vector3(0, 1.0, 0);
  562. };
  563. BABYLON.Vector3.TransformCoordinates = function (vector, transformation) {
  564. var result = BABYLON.Vector3.Zero();
  565. BABYLON.Vector3.TransformCoordinatesToRef(vector, transformation, result);
  566. return result;
  567. };
  568. BABYLON.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. BABYLON.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. BABYLON.Vector3.TransformNormal = function (vector, transformation) {
  587. var result = BABYLON.Vector3.Zero();
  588. BABYLON.Vector3.TransformNormalToRef(vector, transformation, result);
  589. return result;
  590. };
  591. BABYLON.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. BABYLON.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. BABYLON.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)) +
  605. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  606. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  607. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  608. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  609. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  610. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  611. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  612. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  613. return new BABYLON.Vector3(x, y, z);
  614. };
  615. BABYLON.Vector3.Clamp = function (value, min, max) {
  616. var x = value.x;
  617. x = (x > max.x) ? max.x : x;
  618. x = (x < min.x) ? min.x : x;
  619. var y = value.y;
  620. y = (y > max.y) ? max.y : y;
  621. y = (y < min.y) ? min.y : y;
  622. var z = value.z;
  623. z = (z > max.z) ? max.z : z;
  624. z = (z < min.z) ? min.z : z;
  625. return new BABYLON.Vector3(x, y, z);
  626. };
  627. BABYLON.Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  628. var squared = amount * amount;
  629. var cubed = amount * squared;
  630. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  631. var part2 = (-2.0 * cubed) + (3.0 * squared);
  632. var part3 = (cubed - (2.0 * squared)) + amount;
  633. var part4 = cubed - squared;
  634. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  635. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  636. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  637. return new BABYLON.Vector3(x, y, z);
  638. };
  639. BABYLON.Vector3.Lerp = function (start, end, amount) {
  640. var x = start.x + ((end.x - start.x) * amount);
  641. var y = start.y + ((end.y - start.y) * amount);
  642. var z = start.z + ((end.z - start.z) * amount);
  643. return new BABYLON.Vector3(x, y, z);
  644. };
  645. BABYLON.Vector3.Dot = function (left, right) {
  646. return (left.x * right.x + left.y * right.y + left.z * right.z);
  647. };
  648. BABYLON.Vector3.Cross = function (left, right) {
  649. var result = BABYLON.Vector3.Zero();
  650. BABYLON.Vector3.CrossToRef(left, right, result);
  651. return result;
  652. };
  653. BABYLON.Vector3.CrossToRef = function (left, right, result) {
  654. result.x = left.y * right.z - left.z * right.y;
  655. result.y = left.z * right.x - left.x * right.z;
  656. result.z = left.x * right.y - left.y * right.x;
  657. };
  658. BABYLON.Vector3.Normalize = function (vector) {
  659. var result = BABYLON.Vector3.Zero();
  660. BABYLON.Vector3.NormalizeToRef(vector, result);
  661. return result;
  662. };
  663. BABYLON.Vector3.NormalizeToRef = function (vector, result) {
  664. result.copyFrom(vector);
  665. result.normalize();
  666. };
  667. BABYLON.Vector3.Project = function (vector, world, transform, viewport) {
  668. var cw = viewport.width;
  669. var ch = viewport.height;
  670. var cx = viewport.x;
  671. var cy = viewport.y;
  672. var viewportMatrix = BABYLON.Matrix.FromValues(
  673. cw / 2.0, 0, 0, 0,
  674. 0, -ch / 2.0, 0, 0,
  675. 0, 0, 1, 0,
  676. cx + cw / 2.0, ch / 2.0 + cy, 0, 1);
  677. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  678. return BABYLON.Vector3.TransformCoordinates(vector, finalMatrix);
  679. };
  680. BABYLON.Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  681. var matrix = world.multiply(view).multiply(projection);
  682. matrix.invert();
  683. source.x = source.x / viewportWidth * 2 - 1;
  684. source.y = -(source.y / viewportHeight * 2 - 1);
  685. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  686. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  687. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  688. vector = vector.scale(1.0 / num);
  689. }
  690. return vector;
  691. };
  692. BABYLON.Vector3.Minimize = function (left, right) {
  693. var x = (left.x < right.x) ? left.x : right.x;
  694. var y = (left.y < right.y) ? left.y : right.y;
  695. var z = (left.z < right.z) ? left.z : right.z;
  696. return new BABYLON.Vector3(x, y, z);
  697. };
  698. BABYLON.Vector3.Maximize = function (left, right) {
  699. var x = (left.x > right.x) ? left.x : right.x;
  700. var y = (left.y > right.y) ? left.y : right.y;
  701. var z = (left.z > right.z) ? left.z : right.z;
  702. return new BABYLON.Vector3(x, y, z);
  703. };
  704. BABYLON.Vector3.Distance = function (value1, value2) {
  705. return Math.sqrt(BABYLON.Vector3.DistanceSquared(value1, value2));
  706. };
  707. BABYLON.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. ////////////////////////////////// Quaternion //////////////////////////////////
  714. BABYLON.Quaternion = function (initialX, initialY, initialZ, initialW) {
  715. this.x = initialX;
  716. this.y = initialY;
  717. this.z = initialZ;
  718. this.w = initialW;
  719. };
  720. BABYLON.Quaternion.prototype.toString = function () {
  721. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  722. };
  723. BABYLON.Quaternion.prototype.asArray = function () {
  724. return [this.x, this.y, this.z, this.w];
  725. };
  726. BABYLON.Quaternion.prototype.equals = function (otherQuaternion) {
  727. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  728. };
  729. BABYLON.Quaternion.prototype.clone = function () {
  730. return new BABYLON.Quaternion(this.x, this.y, this.z, this.w);
  731. };
  732. BABYLON.Quaternion.prototype.copyFrom = function (other) {
  733. this.x = other.x;
  734. this.y = other.y;
  735. this.z = other.z;
  736. this.w = other.w;
  737. };
  738. BABYLON.Quaternion.prototype.add = function (other) {
  739. return new BABYLON.Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  740. };
  741. BABYLON.Quaternion.prototype.scale = function (value) {
  742. return new BABYLON.Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  743. };
  744. BABYLON.Quaternion.prototype.multiply = function (q1) {
  745. var result = new BABYLON.Quaternion(0, 0, 0, 1.0);
  746. this.multiplyToRef(q1, result);
  747. return result;
  748. };
  749. BABYLON.Quaternion.prototype.multiplyToRef = function (q1, result) {
  750. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  751. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  752. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  753. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  754. };
  755. BABYLON.Quaternion.prototype.length = function () {
  756. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  757. };
  758. BABYLON.Quaternion.prototype.normalize = function () {
  759. var length = 1.0 / this.length();
  760. this.x *= length;
  761. this.y *= length;
  762. this.z *= length;
  763. this.w *= length;
  764. };
  765. BABYLON.Quaternion.prototype.toEulerAngles = function () {
  766. var qx = this.x;
  767. var qy = this.y;
  768. var qz = this.z;
  769. var qw = this.w;
  770. var sqx = qx * qx;
  771. var sqy = qy * qy;
  772. var sqz = qz * qz;
  773. var yaw = Math.atan2(2.0 * (qy * qw - qx * qz), 1.0 - 2.0 * (sqy + sqz));
  774. var pitch = Math.asin(2.0 * (qx * qy + qz * qw));
  775. var roll = Math.atan2(2.0 * (qx * qw - qy * qz), 1.0 - 2.0 * (sqx + sqz));
  776. var gimbaLockTest = qx * qy + qz * qw;
  777. if (gimbaLockTest > 0.499) {
  778. yaw = 2.0 * Math.atan2(qx, qw);
  779. roll = 0;
  780. } else if (gimbaLockTest < -0.499) {
  781. yaw = -2.0 * Math.atan2(qx, qw);
  782. roll = 0;
  783. }
  784. return new BABYLON.Vector3(pitch, yaw, roll);
  785. };
  786. BABYLON.Quaternion.prototype.toRotationMatrix = function (result) {
  787. var xx = this.x * this.x;
  788. var yy = this.y * this.y;
  789. var zz = this.z * this.z;
  790. var xy = this.x * this.y;
  791. var zw = this.z * this.w;
  792. var zx = this.z * this.x;
  793. var yw = this.y * this.w;
  794. var yz = this.y * this.z;
  795. var xw = this.x * this.w;
  796. result.m[0] = 1.0 - (2.0 * (yy + zz));
  797. result.m[1] = 2.0 * (xy + zw);
  798. result.m[2] = 2.0 * (zx - yw);
  799. result.m[3] = 0;
  800. result.m[4] = 2.0 * (xy - zw);
  801. result.m[5] = 1.0 - (2.0 * (zz + xx));
  802. result.m[6] = 2.0 * (yz + xw);
  803. result.m[7] = 0;
  804. result.m[8] = 2.0 * (zx + yw);
  805. result.m[9] = 2.0 * (yz - xw);
  806. result.m[10] = 1.0 - (2.0 * (yy + xx));
  807. result.m[11] = 0;
  808. result.m[12] = 0;
  809. result.m[13] = 0;
  810. result.m[14] = 0;
  811. result.m[15] = 1.0;
  812. };
  813. // Statics
  814. BABYLON.Quaternion.RotationAxis = function (axis, angle) {
  815. var result = new BABYLON.Quaternion();
  816. var sin = Math.sin(angle / 2);
  817. result.w = Math.cos(angle / 2);
  818. result.x = axis.x * sin;
  819. result.y = axis.y * sin;
  820. result.z = axis.z * sin;
  821. return result;
  822. };
  823. BABYLON.Quaternion.FromArray = function (array, offset) {
  824. if (!offset) {
  825. offset = 0;
  826. }
  827. return new BABYLON.Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  828. };
  829. BABYLON.Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  830. var result = new BABYLON.Quaternion();
  831. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  832. return result;
  833. };
  834. BABYLON.Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  835. var halfRoll = roll * 0.5;
  836. var halfPitch = pitch * 0.5;
  837. var halfYaw = yaw * 0.5;
  838. var sinRoll = Math.sin(halfRoll);
  839. var cosRoll = Math.cos(halfRoll);
  840. var sinPitch = Math.sin(halfPitch);
  841. var cosPitch = Math.cos(halfPitch);
  842. var sinYaw = Math.sin(halfYaw);
  843. var cosYaw = Math.cos(halfYaw);
  844. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  845. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  846. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  847. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  848. };
  849. BABYLON.Quaternion.Slerp = function (left, right, amount) {
  850. var num2;
  851. var num3;
  852. var num = amount;
  853. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  854. var flag = false;
  855. if (num4 < 0) {
  856. flag = true;
  857. num4 = -num4;
  858. }
  859. if (num4 > 0.999999) {
  860. num3 = 1 - num;
  861. num2 = flag ? -num : num;
  862. }
  863. else {
  864. var num5 = Math.acos(num4);
  865. var num6 = (1.0 / Math.sin(num5));
  866. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  867. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  868. }
  869. return new BABYLON.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));
  870. };
  871. ////////////////////////////////// Matrix //////////////////////////////////
  872. if (!BABYLON.MatrixType) {
  873. BABYLON.MatrixType = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  874. }
  875. BABYLON.Matrix = function () {
  876. this.m = new BABYLON.MatrixType(16);
  877. };
  878. // Properties
  879. BABYLON.Matrix.prototype.isIdentity = function () {
  880. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  881. return false;
  882. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  883. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  884. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  885. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  886. return false;
  887. return true;
  888. };
  889. BABYLON.Matrix.prototype.determinant = function () {
  890. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  891. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  892. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  893. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  894. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  895. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  896. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  897. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  898. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  899. };
  900. // Methods
  901. BABYLON.Matrix.prototype.toArray = function () {
  902. return this.m;
  903. };
  904. BABYLON.Matrix.prototype.asArray = function () {
  905. return this.toArray();
  906. };
  907. BABYLON.Matrix.prototype.invert = function () {
  908. this.invertToRef(this);
  909. };
  910. BABYLON.Matrix.prototype.invertToRef = function (other) {
  911. var l1 = this.m[0];
  912. var l2 = this.m[1];
  913. var l3 = this.m[2];
  914. var l4 = this.m[3];
  915. var l5 = this.m[4];
  916. var l6 = this.m[5];
  917. var l7 = this.m[6];
  918. var l8 = this.m[7];
  919. var l9 = this.m[8];
  920. var l10 = this.m[9];
  921. var l11 = this.m[10];
  922. var l12 = this.m[11];
  923. var l13 = this.m[12];
  924. var l14 = this.m[13];
  925. var l15 = this.m[14];
  926. var l16 = this.m[15];
  927. var l17 = (l11 * l16) - (l12 * l15);
  928. var l18 = (l10 * l16) - (l12 * l14);
  929. var l19 = (l10 * l15) - (l11 * l14);
  930. var l20 = (l9 * l16) - (l12 * l13);
  931. var l21 = (l9 * l15) - (l11 * l13);
  932. var l22 = (l9 * l14) - (l10 * l13);
  933. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  934. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  935. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  936. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  937. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  938. var l28 = (l7 * l16) - (l8 * l15);
  939. var l29 = (l6 * l16) - (l8 * l14);
  940. var l30 = (l6 * l15) - (l7 * l14);
  941. var l31 = (l5 * l16) - (l8 * l13);
  942. var l32 = (l5 * l15) - (l7 * l13);
  943. var l33 = (l5 * l14) - (l6 * l13);
  944. var l34 = (l7 * l12) - (l8 * l11);
  945. var l35 = (l6 * l12) - (l8 * l10);
  946. var l36 = (l6 * l11) - (l7 * l10);
  947. var l37 = (l5 * l12) - (l8 * l9);
  948. var l38 = (l5 * l11) - (l7 * l9);
  949. var l39 = (l5 * l10) - (l6 * l9);
  950. other.m[0] = l23 * l27;
  951. other.m[4] = l24 * l27;
  952. other.m[8] = l25 * l27;
  953. other.m[12] = l26 * l27;
  954. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  955. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  956. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  957. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  958. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  959. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  960. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  961. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  962. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  963. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  964. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  965. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  966. };
  967. BABYLON.Matrix.prototype.setTranslation = function (vector3) {
  968. this.m[12] = vector3.x;
  969. this.m[13] = vector3.y;
  970. this.m[14] = vector3.z;
  971. };
  972. BABYLON.Matrix.prototype.multiply = function (other) {
  973. var result = new BABYLON.Matrix();
  974. this.multiplyToRef(other, result);
  975. return result;
  976. };
  977. BABYLON.Matrix.prototype.copyFrom = function (other) {
  978. for (var index = 0; index < 16; index++) {
  979. this.m[index] = other.m[index];
  980. }
  981. };
  982. BABYLON.Matrix.prototype.multiplyToRef = function (other, result) {
  983. this.multiplyToArray(other, result.m, 0);
  984. };
  985. BABYLON.Matrix.prototype.multiplyToArray = function (other, result, offset) {
  986. var tm0 = this.m[0];
  987. var tm1 = this.m[1];
  988. var tm2 = this.m[2];
  989. var tm3 = this.m[3];
  990. var tm4 = this.m[4];
  991. var tm5 = this.m[5];
  992. var tm6 = this.m[6];
  993. var tm7 = this.m[7];
  994. var tm8 = this.m[8];
  995. var tm9 = this.m[9];
  996. var tm10 = this.m[10];
  997. var tm11 = this.m[11];
  998. var tm12 = this.m[12];
  999. var tm13 = this.m[13];
  1000. var tm14 = this.m[14];
  1001. var tm15 = this.m[15];
  1002. var om0 = other.m[0];
  1003. var om1 = other.m[1];
  1004. var om2 = other.m[2];
  1005. var om3 = other.m[3];
  1006. var om4 = other.m[4];
  1007. var om5 = other.m[5];
  1008. var om6 = other.m[6];
  1009. var om7 = other.m[7];
  1010. var om8 = other.m[8];
  1011. var om9 = other.m[9];
  1012. var om10 = other.m[10];
  1013. var om11 = other.m[11];
  1014. var om12 = other.m[12];
  1015. var om13 = other.m[13];
  1016. var om14 = other.m[14];
  1017. var om15 = other.m[15];
  1018. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1019. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1020. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1021. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1022. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1023. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1024. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1025. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1026. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1027. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1028. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1029. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1030. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1031. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1032. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1033. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1034. };
  1035. BABYLON.Matrix.prototype.equals = function (value) {
  1036. return value &&
  1037. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  1038. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  1039. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  1040. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1041. };
  1042. BABYLON.Matrix.prototype.clone = function () {
  1043. return BABYLON.Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  1044. this.m[4], this.m[5], this.m[6], this.m[7],
  1045. this.m[8], this.m[9], this.m[10], this.m[11],
  1046. this.m[12], this.m[13], this.m[14], this.m[15]);
  1047. };
  1048. // Statics
  1049. BABYLON.Matrix.FromArray = function (array, offset) {
  1050. var result = new BABYLON.Matrix();
  1051. BABYLON.Matrix.FromArrayToRef(array, offset, result);
  1052. return result;
  1053. };
  1054. BABYLON.Matrix.FromArrayToRef = function (array, offset, result) {
  1055. if (!offset) {
  1056. offset = 0;
  1057. }
  1058. for (var index = 0; index < 16; index++) {
  1059. result.m[index] = array[index + offset];
  1060. }
  1061. };
  1062. BABYLON.Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14,
  1063. initialM21, initialM22, initialM23, initialM24,
  1064. initialM31, initialM32, initialM33, initialM34,
  1065. initialM41, initialM42, initialM43, initialM44, result) {
  1066. result.m[0] = initialM11;
  1067. result.m[1] = initialM12;
  1068. result.m[2] = initialM13;
  1069. result.m[3] = initialM14;
  1070. result.m[4] = initialM21;
  1071. result.m[5] = initialM22;
  1072. result.m[6] = initialM23;
  1073. result.m[7] = initialM24;
  1074. result.m[8] = initialM31;
  1075. result.m[9] = initialM32;
  1076. result.m[10] = initialM33;
  1077. result.m[11] = initialM34;
  1078. result.m[12] = initialM41;
  1079. result.m[13] = initialM42;
  1080. result.m[14] = initialM43;
  1081. result.m[15] = initialM44;
  1082. };
  1083. BABYLON.Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14,
  1084. initialM21, initialM22, initialM23, initialM24,
  1085. initialM31, initialM32, initialM33, initialM34,
  1086. initialM41, initialM42, initialM43, initialM44) {
  1087. var result = new BABYLON.Matrix();
  1088. result.m[0] = initialM11;
  1089. result.m[1] = initialM12;
  1090. result.m[2] = initialM13;
  1091. result.m[3] = initialM14;
  1092. result.m[4] = initialM21;
  1093. result.m[5] = initialM22;
  1094. result.m[6] = initialM23;
  1095. result.m[7] = initialM24;
  1096. result.m[8] = initialM31;
  1097. result.m[9] = initialM32;
  1098. result.m[10] = initialM33;
  1099. result.m[11] = initialM34;
  1100. result.m[12] = initialM41;
  1101. result.m[13] = initialM42;
  1102. result.m[14] = initialM43;
  1103. result.m[15] = initialM44;
  1104. return result;
  1105. };
  1106. BABYLON.Matrix.Identity = function () {
  1107. return BABYLON.Matrix.FromValues(1.0, 0, 0, 0,
  1108. 0, 1.0, 0, 0,
  1109. 0, 0, 1.0, 0,
  1110. 0, 0, 0, 1.0);
  1111. };
  1112. BABYLON.Matrix.IdentityToRef = function (result) {
  1113. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1114. 0, 1.0, 0, 0,
  1115. 0, 0, 1.0, 0,
  1116. 0, 0, 0, 1.0, result);
  1117. };
  1118. BABYLON.Matrix.Zero = function () {
  1119. return BABYLON.Matrix.FromValues(0, 0, 0, 0,
  1120. 0, 0, 0, 0,
  1121. 0, 0, 0, 0,
  1122. 0, 0, 0, 0);
  1123. };
  1124. BABYLON.Matrix.RotationX = function (angle) {
  1125. var result = new BABYLON.Matrix();
  1126. BABYLON.Matrix.RotationXToRef(angle, result);
  1127. return result;
  1128. };
  1129. BABYLON.Matrix.RotationXToRef = function (angle, result) {
  1130. var s = Math.sin(angle);
  1131. var c = Math.cos(angle);
  1132. result.m[0] = 1.0;
  1133. result.m[15] = 1.0;
  1134. result.m[5] = c;
  1135. result.m[10] = c;
  1136. result.m[9] = -s;
  1137. result.m[6] = s;
  1138. result.m[1] = 0;
  1139. result.m[2] = 0;
  1140. result.m[3] = 0;
  1141. result.m[4] = 0;
  1142. result.m[7] = 0;
  1143. result.m[8] = 0;
  1144. result.m[11] = 0;
  1145. result.m[12] = 0;
  1146. result.m[13] = 0;
  1147. result.m[14] = 0;
  1148. };
  1149. BABYLON.Matrix.RotationY = function (angle) {
  1150. var result = new BABYLON.Matrix();
  1151. BABYLON.Matrix.RotationYToRef(angle, result);
  1152. return result;
  1153. };
  1154. BABYLON.Matrix.RotationYToRef = function (angle, result) {
  1155. var s = Math.sin(angle);
  1156. var c = Math.cos(angle);
  1157. result.m[5] = 1.0;
  1158. result.m[15] = 1.0;
  1159. result.m[0] = c;
  1160. result.m[2] = -s;
  1161. result.m[8] = s;
  1162. result.m[10] = c;
  1163. result.m[1] = 0;
  1164. result.m[3] = 0;
  1165. result.m[4] = 0;
  1166. result.m[6] = 0;
  1167. result.m[7] = 0;
  1168. result.m[9] = 0;
  1169. result.m[11] = 0;
  1170. result.m[12] = 0;
  1171. result.m[13] = 0;
  1172. result.m[14] = 0;
  1173. };
  1174. BABYLON.Matrix.RotationZ = function (angle) {
  1175. var result = new BABYLON.Matrix();
  1176. BABYLON.Matrix.RotationZToRef(angle, result);
  1177. return result;
  1178. };
  1179. BABYLON.Matrix.RotationZToRef = function (angle, result) {
  1180. var s = Math.sin(angle);
  1181. var c = Math.cos(angle);
  1182. result.m[10] = 1.0;
  1183. result.m[15] = 1.0;
  1184. result.m[0] = c;
  1185. result.m[1] = s;
  1186. result.m[4] = -s;
  1187. result.m[5] = c;
  1188. result.m[2] = 0;
  1189. result.m[3] = 0;
  1190. result.m[6] = 0;
  1191. result.m[7] = 0;
  1192. result.m[8] = 0;
  1193. result.m[9] = 0;
  1194. result.m[11] = 0;
  1195. result.m[12] = 0;
  1196. result.m[13] = 0;
  1197. result.m[14] = 0;
  1198. };
  1199. BABYLON.Matrix.RotationAxis = function (axis, angle) {
  1200. var s = Math.sin(-angle);
  1201. var c = Math.cos(-angle);
  1202. var c1 = 1 - c;
  1203. axis.normalize();
  1204. var result = BABYLON.Matrix.Zero();
  1205. result.m[0] = (axis.x * axis.x) * c1 + c;
  1206. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1207. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1208. result.m[3] = 0.0;
  1209. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1210. result.m[5] = (axis.y * axis.y) * c1 + c;
  1211. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1212. result.m[7] = 0.0;
  1213. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1214. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1215. result.m[10] = (axis.z * axis.z) * c1 + c;
  1216. result.m[11] = 0.0;
  1217. result.m[15] = 1.0;
  1218. return result;
  1219. };
  1220. BABYLON.Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1221. var result = new BABYLON.Matrix();
  1222. BABYLON.Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1223. return result;
  1224. };
  1225. var tempQuaternion = new BABYLON.Quaternion(); // For RotationYawPitchRoll
  1226. BABYLON.Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1227. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, tempQuaternion);
  1228. tempQuaternion.toRotationMatrix(result);
  1229. };
  1230. BABYLON.Matrix.Scaling = function (x, y, z) {
  1231. var result = BABYLON.Matrix.Zero();
  1232. BABYLON.Matrix.ScalingToRef(x, y, z, result);
  1233. return result;
  1234. };
  1235. BABYLON.Matrix.ScalingToRef = function (x, y, z, result) {
  1236. result.m[0] = x;
  1237. result.m[1] = 0;
  1238. result.m[2] = 0;
  1239. result.m[3] = 0;
  1240. result.m[4] = 0;
  1241. result.m[5] = y;
  1242. result.m[6] = 0;
  1243. result.m[7] = 0;
  1244. result.m[8] = 0;
  1245. result.m[9] = 0;
  1246. result.m[10] = z;
  1247. result.m[11] = 0;
  1248. result.m[12] = 0;
  1249. result.m[13] = 0;
  1250. result.m[14] = 0;
  1251. result.m[15] = 1.0;
  1252. };
  1253. BABYLON.Matrix.Translation = function (x, y, z) {
  1254. var result = BABYLON.Matrix.Identity();
  1255. BABYLON.Matrix.TranslationToRef(x, y, z, result);
  1256. return result;
  1257. };
  1258. BABYLON.Matrix.TranslationToRef = function (x, y, z, result) {
  1259. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1260. 0, 1.0, 0, 0,
  1261. 0, 0, 1.0, 0,
  1262. x, y, z, 1.0, result);
  1263. };
  1264. BABYLON.Matrix.LookAtLH = function (eye, target, up) {
  1265. var result = BABYLON.Matrix.Zero();
  1266. BABYLON.Matrix.LookAtLHToRef(eye, target, up, result);
  1267. return result;
  1268. };
  1269. var xAxis = BABYLON.Vector3.Zero();
  1270. var yAxis = BABYLON.Vector3.Zero();
  1271. var zAxis = BABYLON.Vector3.Zero();
  1272. BABYLON.Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1273. // Z axis
  1274. target.subtractToRef(eye, zAxis);
  1275. zAxis.normalize();
  1276. // X axis
  1277. BABYLON.Vector3.CrossToRef(up, zAxis, xAxis);
  1278. xAxis.normalize();
  1279. // Y axis
  1280. BABYLON.Vector3.CrossToRef(zAxis, xAxis, yAxis);
  1281. yAxis.normalize();
  1282. // Eye angles
  1283. var ex = -BABYLON.Vector3.Dot(xAxis, eye);
  1284. var ey = -BABYLON.Vector3.Dot(yAxis, eye);
  1285. var ez = -BABYLON.Vector3.Dot(zAxis, eye);
  1286. return BABYLON.Matrix.FromValuesToRef(xAxis.x, yAxis.x, zAxis.x, 0,
  1287. xAxis.y, yAxis.y, zAxis.y, 0,
  1288. xAxis.z, yAxis.z, zAxis.z, 0,
  1289. ex, ey, ez, 1, result);
  1290. };
  1291. BABYLON.Matrix.OrthoLH = function (width, height, znear, zfar) {
  1292. var hw = 2.0 / width;
  1293. var hh = 2.0 / height;
  1294. var id = 1.0 / (zfar - znear);
  1295. var nid = znear / (znear - zfar);
  1296. return BABYLON.Matrix.FromValues(hw, 0, 0, 0,
  1297. 0, hh, 0, 0,
  1298. 0, 0, id, 0,
  1299. 0, 0, nid, 1);
  1300. };
  1301. BABYLON.Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1302. var matrix = BABYLON.Matrix.Zero();
  1303. BABYLON.Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1304. return matrix;
  1305. };
  1306. BABYLON.Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1307. result.m[0] = 2.0 / (right - left);
  1308. result.m[1] = result.m[2] = result.m[3] = 0;
  1309. result.m[5] = 2.0 / (top - bottom);
  1310. result.m[4] = result.m[6] = result.m[7] = 0;
  1311. result.m[10] = -1.0 / (znear - zfar);
  1312. result.m[8] = result.m[9] = result.m[11] = 0;
  1313. result.m[12] = (left + right) / (left - right);
  1314. result.m[13] = (top + bottom) / (bottom - top);
  1315. result.m[14] = znear / (znear - zfar);
  1316. result.m[15] = 1.0;
  1317. };
  1318. BABYLON.Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1319. var matrix = BABYLON.Matrix.Zero();
  1320. matrix.m[0] = (2.0 * znear) / width;
  1321. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1322. matrix.m[5] = (2.0 * znear) / height;
  1323. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1324. matrix.m[10] = -zfar / (znear - zfar);
  1325. matrix.m[8] = matrix.m[9] = 0.0;
  1326. matrix.m[11] = 1.0;
  1327. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1328. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1329. return matrix;
  1330. };
  1331. BABYLON.Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1332. var matrix = BABYLON.Matrix.Zero();
  1333. BABYLON.Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1334. return matrix;
  1335. };
  1336. BABYLON.Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result) {
  1337. var tan = 1.0 / (Math.tan(fov * 0.5));
  1338. result.m[0] = tan / aspect;
  1339. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1340. result.m[5] = tan;
  1341. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1342. result.m[8] = result.m[9] = 0.0;
  1343. result.m[10] = -zfar / (znear - zfar);
  1344. result.m[11] = 1.0;
  1345. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1346. result.m[14] = (znear * zfar) / (znear - zfar);
  1347. };
  1348. BABYLON.Matrix.AffineTransformation = function (scaling, rotationCenter, rotation, translation) {
  1349. return BABYLON.Matrix.Scaling(scaling, scaling, scaling) * BABYLON.Matrix.Translation(-rotationCenter) *
  1350. BABYLON.Matrix.RotationQuaternion(rotation) * BABYLON.Matrix.Translation(rotationCenter) * BABYLON.Matrix.Translation(translation);
  1351. };
  1352. BABYLON.Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  1353. var cw = viewport.width;
  1354. var ch = viewport.height;
  1355. var cx = viewport.x;
  1356. var cy = viewport.y;
  1357. var viewportMatrix = new BABYLON.Matrix(cw / 2.0, 0, 0, 0,
  1358. 0, -ch / 2.0, 0, 0,
  1359. 0, 0, zmax - zmin, 0,
  1360. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  1361. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1362. };
  1363. BABYLON.Matrix.Transpose = function (matrix) {
  1364. var result = new BABYLON.Matrix();
  1365. result.m[0] = matrix.m[0];
  1366. result.m[1] = matrix.m[4];
  1367. result.m[2] = matrix.m[8];
  1368. result.m[3] = matrix.m[12];
  1369. result.m[4] = matrix.m[1];
  1370. result.m[5] = matrix.m[5];
  1371. result.m[6] = matrix.m[9];
  1372. result.m[7] = matrix.m[13];
  1373. result.m[8] = matrix.m[2];
  1374. result.m[9] = matrix.m[6];
  1375. result.m[10] = matrix.m[10];
  1376. result.m[11] = matrix.m[14];
  1377. result.m[12] = matrix.m[3];
  1378. result.m[13] = matrix.m[7];
  1379. result.m[14] = matrix.m[11];
  1380. result.m[15] = matrix.m[15];
  1381. return result;
  1382. };
  1383. BABYLON.Matrix.Reflection = function (plane) {
  1384. var matrix = new BABYLON.Matrix();
  1385. BABYLON.Matrix.ReflectionToRef(plane, matrix);
  1386. return matrix;
  1387. };
  1388. BABYLON.Matrix.ReflectionToRef = function (plane, result) {
  1389. plane.normalize();
  1390. var x = plane.normal.x;
  1391. var y = plane.normal.y;
  1392. var z = plane.normal.z;
  1393. var temp = -2 * x;
  1394. var temp2 = -2 * y;
  1395. var temp3 = -2 * z;
  1396. result.m[0] = (temp * x) + 1;
  1397. result.m[1] = temp2 * x;
  1398. result.m[2] = temp3 * x;
  1399. result.m[3] = 0.0;
  1400. result.m[4] = temp * y;
  1401. result.m[5] = (temp2 * y) + 1;
  1402. result.m[6] = temp3 * y;
  1403. result.m[7] = 0.0;
  1404. result.m[8] = temp * z;
  1405. result.m[9] = temp2 * z;
  1406. result.m[10] = (temp3 * z) + 1;
  1407. result.m[11] = 0.0;
  1408. result.m[12] = temp * plane.d;
  1409. result.m[13] = temp2 * plane.d;
  1410. result.m[14] = temp3 * plane.d;
  1411. result.m[15] = 1.0;
  1412. };
  1413. ////////////////////////////////// Plane //////////////////////////////////
  1414. BABYLON.Plane = function (a, b, c, d) {
  1415. this.normal = new BABYLON.Vector3(a, b, c);
  1416. this.d = d;
  1417. };
  1418. BABYLON.Plane.prototype.asArray = function () {
  1419. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  1420. };
  1421. // Methods
  1422. BABYLON.Plane.prototype.normalize = function () {
  1423. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1424. var magnitude = 0;
  1425. if (norm != 0) {
  1426. magnitude = 1.0 / norm;
  1427. }
  1428. this.normal.x *= magnitude;
  1429. this.normal.y *= magnitude;
  1430. this.normal.z *= magnitude;
  1431. this.d *= magnitude;
  1432. };
  1433. BABYLON.Plane.prototype.transform = function (transformation) {
  1434. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1435. var x = this.normal.x;
  1436. var y = this.normal.y;
  1437. var z = this.normal.z;
  1438. var d = this.d;
  1439. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1440. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1441. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1442. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1443. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1444. };
  1445. BABYLON.Plane.prototype.dotCoordinate = function (point) {
  1446. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1447. };
  1448. BABYLON.Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  1449. var x1 = point2.x - point1.x;
  1450. var y1 = point2.y - point1.y;
  1451. var z1 = point2.z - point1.z;
  1452. var x2 = point3.x - point1.x;
  1453. var y2 = point3.y - point1.y;
  1454. var z2 = point3.z - point1.z;
  1455. var yz = (y1 * z2) - (z1 * y2);
  1456. var xz = (z1 * x2) - (x1 * z2);
  1457. var xy = (x1 * y2) - (y1 * x2);
  1458. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1459. var invPyth;
  1460. if (pyth != 0) {
  1461. invPyth = 1.0 / pyth;
  1462. }
  1463. else {
  1464. invPyth = 0;
  1465. }
  1466. this.normal.x = yz * invPyth;
  1467. this.normal.y = xz * invPyth;
  1468. this.normal.z = xy * invPyth;
  1469. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1470. };
  1471. BABYLON.Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  1472. var dot = BABYLON.Vector3.Dot(this.normal, direction);
  1473. return (dot <= epsilon);
  1474. };
  1475. BABYLON.Plane.prototype.signedDistanceTo = function (point) {
  1476. return BABYLON.Vector3.Dot(point, this.normal) + this.d;
  1477. };
  1478. // Statics
  1479. BABYLON.Plane.FromArray = function (array) {
  1480. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1481. };
  1482. BABYLON.Plane.FromPoints = function (point1, point2, point3) {
  1483. var result = new BABYLON.Plane(0, 0, 0, 0);
  1484. result.copyFromPoints(point1, point2, point3);
  1485. return result;
  1486. };
  1487. BABYLON.Plane.FromPositionAndNormal = function (origin, normal) {
  1488. var result = new BABYLON.Plane(0, 0, 0, 0);
  1489. normal.normalize();
  1490. result.normal = normal;
  1491. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1492. return result;
  1493. };
  1494. BABYLON.Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  1495. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1496. return BABYLON.Vector3.Dot(point, normal) + d;
  1497. };
  1498. ////////////////////////////////// Frustum //////////////////////////////////
  1499. BABYLON.Frustum = {};
  1500. // Statics
  1501. BABYLON.Frustum.GetPlanes = function (transform) {
  1502. var frustumPlanes = [];
  1503. for (var index = 0; index < 6; index++) {
  1504. frustumPlanes.push(new BABYLON.Plane(0, 0, 0, 0));
  1505. }
  1506. BABYLON.Frustum.GetPlanesToRef(transform, frustumPlanes);
  1507. return frustumPlanes;
  1508. };
  1509. BABYLON.Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  1510. // Near
  1511. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1512. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1513. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1514. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1515. frustumPlanes[0].normalize();
  1516. // Far
  1517. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1518. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1519. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1520. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1521. frustumPlanes[1].normalize();
  1522. // Left
  1523. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1524. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1525. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1526. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1527. frustumPlanes[2].normalize();
  1528. // Right
  1529. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1530. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1531. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1532. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1533. frustumPlanes[3].normalize();
  1534. // Top
  1535. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1536. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1537. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1538. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1539. frustumPlanes[4].normalize();
  1540. // Bottom
  1541. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1542. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1543. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1544. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1545. frustumPlanes[5].normalize();
  1546. };
  1547. ////////////////////////////////// Viewport //////////////////////////////////
  1548. BABYLON.Viewport = {};
  1549. BABYLON.Viewport = function (x, y, width, height) {
  1550. this.width = width;
  1551. this.height = height;
  1552. this.x = x;
  1553. this.y = y;
  1554. };
  1555. BABYLON.Viewport.prototype.toGlobal = function (engine) {
  1556. var width = engine.getRenderWidth() * engine.getHardwareScalingLevel();
  1557. var height = engine.getRenderHeight() * engine.getHardwareScalingLevel();
  1558. return new BABYLON.Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1559. };
  1560. })();