babylon.math.js 65 KB

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