babylon.math.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. ////////////////////////////////// Ray //////////////////////////////////
  4. BABYLON.Ray = function (origin, direction) {
  5. this.origin = origin;
  6. this.direction = direction;
  7. };
  8. // Methods
  9. BABYLON.Ray.prototype.intersectsBox = function (box) {
  10. var d = 0.0;
  11. var maxValue = Number.MAX_VALUE;
  12. if (Math.abs(this.direction.x) < 0.0000001)
  13. {
  14. if (this.origin.x < box.minimum.x || this.origin.x > box.maximum.x)
  15. {
  16. return false;
  17. }
  18. }
  19. else
  20. {
  21. var inv = 1.0 / this.direction.x;
  22. var min = (box.minimum.x - this.origin.x) * inv;
  23. var max = (box.maximum.x - this.origin.x) * inv;
  24. if (min > max)
  25. {
  26. var temp = min;
  27. min = max;
  28. max = temp;
  29. }
  30. d = Math.max(min, d);
  31. maxValue = Math.min(max, maxValue);
  32. if (d > maxValue)
  33. {
  34. return false;
  35. }
  36. }
  37. if (Math.abs(this.direction.y) < 0.0000001)
  38. {
  39. if (this.origin.y < box.minimum.y || this.origin.y > box.maximum.y)
  40. {
  41. return false;
  42. }
  43. }
  44. else
  45. {
  46. var inv = 1.0 / this.direction.y;
  47. var min = (box.minimum.y - this.origin.y) * inv;
  48. var max = (box.maximum.y - this.origin.y) * inv;
  49. if (min > max)
  50. {
  51. var temp = min;
  52. min = max;
  53. max = temp;
  54. }
  55. d = Math.max(min, d);
  56. maxValue = Math.min(max, maxValue);
  57. if (d > maxValue)
  58. {
  59. return false;
  60. }
  61. }
  62. if (Math.abs(this.direction.z) < 0.0000001)
  63. {
  64. if (this.origin.z < box.minimum.z || this.origin.z > box.maximum.z)
  65. {
  66. return false;
  67. }
  68. }
  69. else
  70. {
  71. var inv = 1.0 / this.direction.z;
  72. var min = (box.minimum.z - this.origin.z) * inv;
  73. var max = (box.maximum.z - this.origin.z) * inv;
  74. if (min > max)
  75. {
  76. var temp = min;
  77. min = max;
  78. max = temp;
  79. }
  80. d = Math.max(min, d);
  81. maxValue = Math.min(max, maxValue);
  82. if (d > maxValue)
  83. {
  84. return false;
  85. }
  86. }
  87. return true;
  88. };
  89. BABYLON.Ray.prototype.intersectsSphere = function (sphere) {
  90. var x = sphere.center.x - this.origin.x;
  91. var y = sphere.center.y - this.origin.y;
  92. var z = sphere.center.z - this.origin.z;
  93. var pyth = (x * x) + (y * y) + (z * z);
  94. var rr = sphere.radius * sphere.radius;
  95. if (pyth <= rr) {
  96. return true;
  97. }
  98. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  99. if (dot < 0.0) {
  100. return false;
  101. }
  102. var temp = pyth - (dot * dot);
  103. return temp <= rr;
  104. };
  105. BABYLON.Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  106. var edge1 = vertex1.subtract(vertex0);
  107. var edge2 = vertex2.subtract(vertex0);
  108. var pvec = BABYLON.Vector3.Cross(this.direction, edge2);
  109. var det = BABYLON.Vector3.Dot(edge1, pvec);
  110. if (det === 0) {
  111. return {
  112. hit: false,
  113. distance: 0,
  114. bu: 0,
  115. bv: 0
  116. };
  117. }
  118. var invdet = 1 / det;
  119. var tvec = this.origin.subtract(vertex0);
  120. var bu = BABYLON.Vector3.Dot(tvec, pvec) * invdet;
  121. if (bu < 0 || bu > 1.0) {
  122. return {
  123. hit: false,
  124. distance: 0,
  125. bu: bu,
  126. bv: 0
  127. };
  128. }
  129. var qvec = BABYLON.Vector3.Cross(tvec, edge1);
  130. bv = BABYLON.Vector3.Dot(this.direction, qvec) * invdet;
  131. if (bv < 0 || bu + bv > 1.0) {
  132. return {
  133. hit: false,
  134. distance: 0,
  135. bu: bu,
  136. bv: bv
  137. };
  138. }
  139. distance = BABYLON.Vector3.Dot(edge2, qvec) * invdet;
  140. return {
  141. hit: true,
  142. distance: distance,
  143. bu: bu,
  144. bv: bv
  145. };
  146. };
  147. // Statics
  148. BABYLON.Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  149. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  150. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  151. var direction = end.subtract(start);
  152. direction.normalize();
  153. return new BABYLON.Ray(start, direction);
  154. };
  155. ////////////////////////////////// Color3 //////////////////////////////////
  156. BABYLON.Color3 = function (initialR, initialG, initialB) {
  157. this.r = initialR;
  158. this.g = initialG;
  159. this.b = initialB;
  160. };
  161. BABYLON.Color3.prototype.toString = function () {
  162. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  163. };
  164. // Operators
  165. BABYLON.Color3.prototype.multiply = function (otherColor) {
  166. return new BABYLON.Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  167. };
  168. BABYLON.Color3.prototype.equals = function (otherColor) {
  169. return this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  170. };
  171. BABYLON.Color3.prototype.scale = function (scale) {
  172. return new BABYLON.Color3(this.r * scale, this.g * scale, this.b * scale);
  173. };
  174. BABYLON.Color3.prototype.clone = function () {
  175. return new BABYLON.Color3(this.r, this.g, this.b);
  176. };
  177. // Statics
  178. BABYLON.Color3.FromArray = function (array) {
  179. return new BABYLON.Color3(array[0], array[1], array[2]);
  180. };
  181. ////////////////////////////////// Color4 //////////////////////////////////
  182. BABYLON.Color4 = function (initialR, initialG, initialB, initialA) {
  183. this.r = initialR;
  184. this.g = initialG;
  185. this.b = initialB;
  186. this.a = initialA;
  187. };
  188. // Operators
  189. BABYLON.Color4.prototype.add = function (right) {
  190. return new BABYLON.Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  191. };
  192. BABYLON.Color4.prototype.subtract = function (right) {
  193. return new BABYLON.Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  194. };
  195. BABYLON.Color4.prototype.scale = function (scale) {
  196. return new BABYLON.Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  197. };
  198. BABYLON.Color4.prototype.toString = function () {
  199. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  200. };
  201. BABYLON.Color4.prototype.clone = function () {
  202. return new BABYLON.Color4(this.r, this.g, this.b, this.a);
  203. };
  204. // Statics
  205. BABYLON.Color4.Lerp = function(left, right, amount) {
  206. var r = left.r + (right.r - left.r) * amount;
  207. var g = left.g + (right.g - left.g) * amount;
  208. var b = left.b + (right.b - left.b) * amount;
  209. var a = left.a + (right.a - left.a) * amount;
  210. return new BABYLON.Color4(r, g, b, a);
  211. };
  212. BABYLON.Color4.FromArray = function (array, offset) {
  213. if (!offset) {
  214. offset = 0;
  215. }
  216. return new BABYLON.Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  217. };
  218. ////////////////////////////////// Vector2 //////////////////////////////////
  219. BABYLON.Vector2 = function (initialX, initialY) {
  220. this.x = initialX;
  221. this.y = initialY;
  222. };
  223. BABYLON.Vector2.prototype.toString = function () {
  224. return "{X: " + this.x + " Y:" + this.y + "}";
  225. };
  226. // Operators
  227. BABYLON.Vector2.prototype.add = function (otherVector) {
  228. return new BABYLON.Vector2(this.x + otherVector.x, this.y + otherVector.y);
  229. };
  230. BABYLON.Vector2.prototype.subtract = function (otherVector) {
  231. return new BABYLON.Vector2(this.x - otherVector.x, this.y - otherVector.y);
  232. };
  233. BABYLON.Vector2.prototype.negate = function () {
  234. return new BABYLON.Vector2(-this.x, -this.y);
  235. };
  236. BABYLON.Vector2.prototype.scale = function (scale) {
  237. return new BABYLON.Vector2(this.x * scale, this.y * scale);
  238. };
  239. BABYLON.Vector2.prototype.equals = function (otherVector) {
  240. return this.x === otherVector.x && this.y === otherVector.y;
  241. };
  242. // Properties
  243. BABYLON.Vector2.prototype.length = function () {
  244. return Math.sqrt(this.x * this.x + this.y * this.y);
  245. };
  246. BABYLON.Vector2.prototype.lengthSquared = function () {
  247. return (this.x * this.x + this.y * this.y);
  248. };
  249. // Methods
  250. BABYLON.Vector2.prototype.normalize = function () {
  251. var len = this.length();
  252. if (len === 0)
  253. return;
  254. var num = 1.0 / len;
  255. this.x *= num;
  256. this.y *= num;
  257. };
  258. BABYLON.Vector2.prototype.clone = function () {
  259. return new BABYLON.Vector2(this.x, this.y);
  260. };
  261. // Statics
  262. BABYLON.Vector2.Zero = function () {
  263. return new BABYLON.Vector2(0, 0);
  264. };
  265. BABYLON.Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  266. var squared = amount * amount;
  267. var cubed = amount * squared;
  268. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  269. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  270. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  271. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  272. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  273. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  274. return new BABYLON.Vector2(x, y);
  275. };
  276. BABYLON.Vector2.Clamp = function (value, min, max) {
  277. var x = value.x;
  278. x = (x > max.x) ? max.x : x;
  279. x = (x < min.x) ? min.x : x;
  280. var y = value.y;
  281. y = (y > max.y) ? max.y : y;
  282. y = (y < min.y) ? min.y : y;
  283. return new BABYLON.Vector2(x, y);
  284. };
  285. BABYLON.Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  286. var squared = amount * amount;
  287. var cubed = amount * squared;
  288. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  289. var part2 = (-2.0 * cubed) + (3.0 * squared);
  290. var part3 = (cubed - (2.0 * squared)) + amount;
  291. var part4 = cubed - squared;
  292. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  293. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  294. return new BABYLON.Vector2(x, y);
  295. };
  296. BABYLON.Vector2.Lerp = function (start, end, amount) {
  297. var x = start.x + ((end.x - start.x) * amount);
  298. var y = start.y + ((end.y - start.y) * amount);
  299. return new BABYLON.Vector2(x, y);
  300. };
  301. BABYLON.Vector2.Dot = function (left, right) {
  302. return left.x * right.x + left.y * right.y;
  303. };
  304. BABYLON.Vector2.Normalize = function (vector) {
  305. var newVector = vector.clone();
  306. newVector.normalize();
  307. return newVector;
  308. };
  309. BABYLON.Vector2.Minimize = function (left, right) {
  310. var x = (left.x < right.x) ? left.x : right.x;
  311. var y = (left.y < right.y) ? left.y : right.y;
  312. return new BABYLON.Vector2(x, y);
  313. };
  314. BABYLON.Vector2.Maximize = function (left, right) {
  315. var x = (left.x > right.x) ? left.x : right.x;
  316. var y = (left.y > right.y) ? left.y : right.y;
  317. return new BABYLON.Vector2(x, y);
  318. };
  319. BABYLON.Vector2.Transform = function (vector, transformation) {
  320. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  321. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  322. return new BABYLON.Vector2(x, y);
  323. };
  324. BABYLON.Vector2.Distance = function (value1, value2) {
  325. return Math.sqrt(BABYLON.Vector2.DistanceSquared(value1, value2));
  326. };
  327. BABYLON.Vector2.DistanceSquared = function (value1, value2) {
  328. var x = value1.x - value2.x;
  329. var y = value1.y - value2.y;
  330. return (x * x) + (y * y);
  331. };
  332. ////////////////////////////////// Vector3 //////////////////////////////////
  333. BABYLON.Vector3 = function (initialX, initialY, initialZ) {
  334. this.x = initialX;
  335. this.y = initialY;
  336. this.z = initialZ;
  337. };
  338. BABYLON.Vector3.prototype.toString = function () {
  339. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  340. };
  341. // Operators
  342. BABYLON.Vector3.prototype.add = function (otherVector) {
  343. return new BABYLON.Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  344. };
  345. BABYLON.Vector3.prototype.subtract = function (otherVector) {
  346. return new BABYLON.Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  347. };
  348. BABYLON.Vector3.prototype.negate = function () {
  349. return new BABYLON.Vector3(-this.x, -this.y, -this.z);
  350. };
  351. BABYLON.Vector3.prototype.scale = function (scale) {
  352. return new BABYLON.Vector3(this.x * scale, this.y * scale, this.z * scale);
  353. };
  354. BABYLON.Vector3.prototype.equals = function (otherVector) {
  355. return this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  356. };
  357. BABYLON.Vector3.prototype.multiply = function (otherVector) {
  358. return new BABYLON.Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  359. };
  360. BABYLON.Vector3.prototype.divide = function (otherVector) {
  361. return new BABYLON.Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  362. };
  363. // Properties
  364. BABYLON.Vector3.prototype.length = function () {
  365. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  366. };
  367. BABYLON.Vector3.prototype.lengthSquared = function () {
  368. return (this.x * this.x + this.y * this.y + this.z * this.z);
  369. };
  370. // Methods
  371. BABYLON.Vector3.prototype.normalize = function () {
  372. var len = this.length();
  373. if (len === 0)
  374. return;
  375. var num = 1.0 / len;
  376. this.x *= num;
  377. this.y *= num;
  378. this.z *= num;
  379. };
  380. BABYLON.Vector3.prototype.clone = function () {
  381. return new BABYLON.Vector3(this.x, this.y, this.z);
  382. };
  383. // Statics
  384. BABYLON.Vector3.FromArray = function (array, offset) {
  385. if (!offset) {
  386. offset = 0;
  387. }
  388. return new BABYLON.Vector3(array[offset], array[offset + 1], array[offset + 2]);
  389. };
  390. BABYLON.Vector3.Zero = function () {
  391. return new BABYLON.Vector3(0, 0, 0);
  392. };
  393. BABYLON.Vector3.Up = function () {
  394. return new BABYLON.Vector3(0, 1.0, 0);
  395. };
  396. BABYLON.Vector3.TransformCoordinates = function (vector, transformation) {
  397. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  398. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  399. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  400. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  401. return new BABYLON.Vector3(x / w, y / w, z / w);
  402. };
  403. BABYLON.Vector3.TransformNormal = function (vector, transformation) {
  404. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  405. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  406. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  407. return new BABYLON.Vector3(x, y, z);
  408. };
  409. BABYLON.Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  410. var squared = amount * amount;
  411. var cubed = amount * squared;
  412. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  413. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  414. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  415. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  416. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  417. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  418. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  419. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  420. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  421. return new BABYLON.Vector3(x, y, z);
  422. };
  423. BABYLON.Vector3.Clamp = function (value, min, max) {
  424. var x = value.x;
  425. x = (x > max.x) ? max.x : x;
  426. x = (x < min.x) ? min.x : x;
  427. var y = value.y;
  428. y = (y > max.y) ? max.y : y;
  429. y = (y < min.y) ? min.y : y;
  430. var z = value.z;
  431. z = (z > max.z) ? max.z : z;
  432. z = (z < min.z) ? min.z : z;
  433. return new BABYLON.Vector3(x, y, z);
  434. };
  435. BABYLON.Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  436. var squared = amount * amount;
  437. var cubed = amount * squared;
  438. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  439. var part2 = (-2.0 * cubed) + (3.0 * squared);
  440. var part3 = (cubed - (2.0 * squared)) + amount;
  441. var part4 = cubed - squared;
  442. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  443. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  444. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  445. return new BABYLON.Vector3(x, y, z);
  446. };
  447. BABYLON.Vector3.Lerp = function (start, end, amount) {
  448. var x = start.x + ((end.x - start.x) * amount);
  449. var y = start.y + ((end.y - start.y) * amount);
  450. var z = start.z + ((end.z - start.z) * amount);
  451. return new BABYLON.Vector3(x, y, z);
  452. };
  453. BABYLON.Vector3.Dot = function (left, right) {
  454. return (left.x * right.x + left.y * right.y + left.z * right.z);
  455. };
  456. BABYLON.Vector3.Cross = function (left, right) {
  457. var x = left.y * right.z - left.z * right.y;
  458. var y = left.z * right.x - left.x * right.z;
  459. var z = left.x * right.y - left.y * right.x;
  460. return new BABYLON.Vector3(x, y, z);
  461. };
  462. BABYLON.Vector3.Normalize = function (vector) {
  463. var newVector = vector.clone();
  464. newVector.normalize();
  465. return newVector;
  466. };
  467. BABYLON.Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  468. var matrix = world.multiply(view).multiply(projection);
  469. matrix.invert();
  470. source.x = source.x / viewportWidth * 2 - 1;
  471. source.y = -(source.y / viewportHeight * 2 - 1);
  472. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  473. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  474. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  475. vector = vector.scale(1.0 / num);
  476. }
  477. return vector;
  478. };
  479. BABYLON.Vector3.Minimize = function (left, right) {
  480. var x = (left.x < right.x) ? left.x : right.x;
  481. var y = (left.y < right.y) ? left.y : right.y;
  482. var z = (left.z < right.z) ? left.z : right.z;
  483. return new BABYLON.Vector3(x, y, z);
  484. };
  485. BABYLON.Vector3.Maximize = function (left, right) {
  486. var x = (left.x > right.x) ? left.x : right.x;
  487. var y = (left.y > right.y) ? left.y : right.y;
  488. var z = (left.z > right.z) ? left.z : right.z;
  489. return new BABYLON.Vector3(x, y, z);
  490. };
  491. BABYLON.Vector3.Distance = function (value1, value2) {
  492. return Math.sqrt(BABYLON.Vector3.DistanceSquared(value1, value2));
  493. };
  494. BABYLON.Vector3.DistanceSquared = function (value1, value2) {
  495. var x = value1.x - value2.x;
  496. var y = value1.y - value2.y;
  497. var z = value1.z - value2.z;
  498. return (x * x) + (y * y) + (z * z);
  499. };
  500. ////////////////////////////////// Quaternion //////////////////////////////////
  501. BABYLON.Quaternion = function (initialX, initialY, initialZ, initialW) {
  502. this.x = initialX;
  503. this.y = initialY;
  504. this.z = initialZ;
  505. this.w = initialW;
  506. };
  507. BABYLON.Quaternion.prototype.toString = function () {
  508. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  509. };
  510. BABYLON.Quaternion.prototype.clone = function () {
  511. return new BABYLON.Quaternion(this.x, this.y, this.z, this.w);
  512. };
  513. BABYLON.Quaternion.prototype.add = function(other) {
  514. return new BABYLON.Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  515. };
  516. BABYLON.Quaternion.prototype.scale = function (value) {
  517. return new BABYLON.Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  518. };
  519. BABYLON.Quaternion.prototype.toEulerAngles = function () {
  520. var q0 = this.x;
  521. var q1 = this.y;
  522. var q2 = this.y;
  523. var q3 = this.w;
  524. var x = Math.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2));
  525. var y = Math.asin(2 * (q0 * q2 - q3 * q1));
  526. var z = Math.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3));
  527. return new BABYLON.Vector3(x, y, z);
  528. };
  529. // Statics
  530. BABYLON.Quaternion.FromArray = function (array, offset) {
  531. if (!offset) {
  532. offset = 0;
  533. }
  534. return new BABYLON.Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  535. };
  536. BABYLON.Quaternion.Slerp = function(left, right, amount) {
  537. var num2;
  538. var num3;
  539. var num = amount;
  540. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  541. var flag = false;
  542. if (num4 < 0)
  543. {
  544. flag = true;
  545. num4 = -num4;
  546. }
  547. if (num4 > 0.999999)
  548. {
  549. num3 = 1 - num;
  550. num2 = flag ? -num : num;
  551. }
  552. else
  553. {
  554. var num5 = Math.acos(num4);
  555. var num6 = (1.0 / Math.sin(num5));
  556. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  557. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  558. }
  559. 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));
  560. };
  561. ////////////////////////////////// Matrix //////////////////////////////////
  562. BABYLON.Matrix = function () {
  563. this.m = new Array(16);
  564. };
  565. // Properties
  566. BABYLON.Matrix.prototype.isIdentity = function () {
  567. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  568. return false;
  569. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  570. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  571. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  572. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  573. return false;
  574. return true;
  575. };
  576. BABYLON.Matrix.prototype.determinant = function () {
  577. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  578. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  579. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  580. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  581. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  582. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  583. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  584. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  585. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  586. };
  587. // Methods
  588. BABYLON.Matrix.prototype.toArray = function () {
  589. return this.m;
  590. };
  591. BABYLON.Matrix.prototype.invert = function () {
  592. var l1 = this.m[0];
  593. var l2 = this.m[1];
  594. var l3 = this.m[2];
  595. var l4 = this.m[3];
  596. var l5 = this.m[4];
  597. var l6 = this.m[5];
  598. var l7 = this.m[6];
  599. var l8 = this.m[7];
  600. var l9 = this.m[8];
  601. var l10 = this.m[9];
  602. var l11 = this.m[10];
  603. var l12 = this.m[11];
  604. var l13 = this.m[12];
  605. var l14 = this.m[13];
  606. var l15 = this.m[14];
  607. var l16 = this.m[15];
  608. var l17 = (l11 * l16) - (l12 * l15);
  609. var l18 = (l10 * l16) - (l12 * l14);
  610. var l19 = (l10 * l15) - (l11 * l14);
  611. var l20 = (l9 * l16) - (l12 * l13);
  612. var l21 = (l9 * l15) - (l11 * l13);
  613. var l22 = (l9 * l14) - (l10 * l13);
  614. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  615. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  616. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  617. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  618. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  619. var l28 = (l7 * l16) - (l8 * l15);
  620. var l29 = (l6 * l16) - (l8 * l14);
  621. var l30 = (l6 * l15) - (l7 * l14);
  622. var l31 = (l5 * l16) - (l8 * l13);
  623. var l32 = (l5 * l15) - (l7 * l13);
  624. var l33 = (l5 * l14) - (l6 * l13);
  625. var l34 = (l7 * l12) - (l8 * l11);
  626. var l35 = (l6 * l12) - (l8 * l10);
  627. var l36 = (l6 * l11) - (l7 * l10);
  628. var l37 = (l5 * l12) - (l8 * l9);
  629. var l38 = (l5 * l11) - (l7 * l9);
  630. var l39 = (l5 * l10) - (l6 * l9);
  631. this.m[0] = l23 * l27;
  632. this.m[4] = l24 * l27;
  633. this.m[8] = l25 * l27;
  634. this.m[12] = l26 * l27;
  635. this.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  636. this.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  637. this.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  638. this.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  639. this.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  640. this.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  641. this.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  642. this.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  643. this.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  644. this.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  645. this.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  646. this.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  647. };
  648. BABYLON.Matrix.prototype.multiply = function (other) {
  649. var result = new BABYLON.Matrix();
  650. result.m[0] = this.m[0] * other.m[0] + this.m[1] * other.m[4] + this.m[2] * other.m[8] + this.m[3] * other.m[12];
  651. result.m[1] = this.m[0] * other.m[1] + this.m[1] * other.m[5] + this.m[2] * other.m[9] + this.m[3] * other.m[13];
  652. result.m[2] = this.m[0] * other.m[2] + this.m[1] * other.m[6] + this.m[2] * other.m[10] + this.m[3] * other.m[14];
  653. result.m[3] = this.m[0] * other.m[3] + this.m[1] * other.m[7] + this.m[2] * other.m[11] + this.m[3] * other.m[15];
  654. result.m[4] = this.m[4] * other.m[0] + this.m[5] * other.m[4] + this.m[6] * other.m[8] + this.m[7] * other.m[12];
  655. result.m[5] = this.m[4] * other.m[1] + this.m[5] * other.m[5] + this.m[6] * other.m[9] + this.m[7] * other.m[13];
  656. result.m[6] = this.m[4] * other.m[2] + this.m[5] * other.m[6] + this.m[6] * other.m[10] + this.m[7] * other.m[14];
  657. result.m[7] = this.m[4] * other.m[3] + this.m[5] * other.m[7] + this.m[6] * other.m[11] + this.m[7] * other.m[15];
  658. result.m[8] = this.m[8] * other.m[0] + this.m[9] * other.m[4] + this.m[10] * other.m[8] + this.m[11] * other.m[12];
  659. result.m[9] = this.m[8] * other.m[1] + this.m[9] * other.m[5] + this.m[10] * other.m[9] + this.m[11] * other.m[13];
  660. result.m[10] = this.m[8] * other.m[2] + this.m[9] * other.m[6] + this.m[10] * other.m[10] + this.m[11] * other.m[14];
  661. result.m[11] = this.m[8] * other.m[3] + this.m[9] * other.m[7] + this.m[10] * other.m[11] + this.m[11] * other.m[15];
  662. result.m[12] = this.m[12] * other.m[0] + this.m[13] * other.m[4] + this.m[14] * other.m[8] + this.m[15] * other.m[12];
  663. result.m[13] = this.m[12] * other.m[1] + this.m[13] * other.m[5] + this.m[14] * other.m[9] + this.m[15] * other.m[13];
  664. result.m[14] = this.m[12] * other.m[2] + this.m[13] * other.m[6] + this.m[14] * other.m[10] + this.m[15] * other.m[14];
  665. result.m[15] = this.m[12] * other.m[3] + this.m[13] * other.m[7] + this.m[14] * other.m[11] + this.m[15] * other.m[15];
  666. return result;
  667. };
  668. BABYLON.Matrix.prototype.equals = function (value) {
  669. return (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  670. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  671. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  672. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  673. };
  674. BABYLON.Matrix.prototype.clone = function () {
  675. return BABYLON.Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  676. this.m[4], this.m[5], this.m[6], this.m[7],
  677. this.m[8], this.m[9], this.m[10], this.m[11],
  678. this.m[12], this.m[13], this.m[14], this.m[15]);
  679. };
  680. // Statics
  681. BABYLON.Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14,
  682. initialM21, initialM22, initialM23, initialM24,
  683. initialM31, initialM32, initialM33, initialM34,
  684. initialM41, initialM42, initialM43, initialM44) {
  685. var result = new BABYLON.Matrix();
  686. result.m[0] = initialM11;
  687. result.m[1] = initialM12;
  688. result.m[2] = initialM13;
  689. result.m[3] = initialM14;
  690. result.m[4] = initialM21;
  691. result.m[5] = initialM22;
  692. result.m[6] = initialM23;
  693. result.m[7] = initialM24;
  694. result.m[8] = initialM31;
  695. result.m[9] = initialM32;
  696. result.m[10] = initialM33;
  697. result.m[11] = initialM34;
  698. result.m[12] = initialM41;
  699. result.m[13] = initialM42;
  700. result.m[14] = initialM43;
  701. result.m[15] = initialM44;
  702. return result;
  703. };
  704. BABYLON.Matrix.Identity = function () {
  705. return BABYLON.Matrix.FromValues(1.0, 0, 0, 0,
  706. 0, 1.0, 0, 0,
  707. 0, 0, 1.0, 0,
  708. 0, 0, 0, 1.0);
  709. };
  710. BABYLON.Matrix.Zero = function () {
  711. return BABYLON.Matrix.FromValues(0, 0, 0, 0,
  712. 0, 0, 0, 0,
  713. 0, 0, 0, 0,
  714. 0, 0, 0, 0);
  715. };
  716. BABYLON.Matrix.RotationX = function (angle) {
  717. var result = BABYLON.Matrix.Zero();
  718. var s = Math.sin(angle);
  719. var c = Math.cos(angle);
  720. result.m[0] = 1.0;
  721. result.m[15] = 1.0;
  722. result.m[5] = c;
  723. result.m[10] = c;
  724. result.m[9] = -s;
  725. result.m[6] = s;
  726. return result;
  727. };
  728. BABYLON.Matrix.RotationY = function (angle) {
  729. var result = BABYLON.Matrix.Zero();
  730. var s = Math.sin(angle);
  731. var c = Math.cos(angle);
  732. result.m[5] = 1.0;
  733. result.m[15] = 1.0;
  734. result.m[0] = c;
  735. result.m[2] = -s;
  736. result.m[8] = s;
  737. result.m[10] = c;
  738. return result;
  739. };
  740. BABYLON.Matrix.RotationZ = function (angle) {
  741. var result = BABYLON.Matrix.Zero();
  742. var s = Math.sin(angle);
  743. var c = Math.cos(angle);
  744. result.m[10] = 1.0;
  745. result.m[15] = 1.0;
  746. result.m[0] = c;
  747. result.m[1] = s;
  748. result.m[4] = -s;
  749. result.m[5] = c;
  750. return result;
  751. };
  752. BABYLON.Matrix.RotationAxis = function (axis, angle) {
  753. var s = Math.sin(-angle);
  754. var c = Math.cos(-angle);
  755. var c1 = 1 - c;
  756. axis.normalize();
  757. var result = BABYLON.Matrix.Zero();
  758. result.m[0] = (axis.x * axis.x) * c1 + c;
  759. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  760. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  761. result.m[3] = 0.0;
  762. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  763. result.m[5] = (axis.y * axis.y) * c1 + c;
  764. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  765. result.m[7] = 0.0;
  766. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  767. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  768. result.m[10] = (axis.z * axis.z) * c1 + c;
  769. result.m[11] = 0.0;
  770. result.m[15] = 1.0;
  771. return result;
  772. };
  773. BABYLON.Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  774. return BABYLON.Matrix.RotationZ(roll).multiply(BABYLON.Matrix.RotationX(pitch)).multiply(BABYLON.Matrix.RotationY(yaw));
  775. };
  776. BABYLON.Matrix.Scaling = function (x, y, z) {
  777. var result = BABYLON.Matrix.Zero();
  778. result.m[0] = x;
  779. result.m[5] = y;
  780. result.m[10] = z;
  781. result.m[15] = 1.0;
  782. return result;
  783. };
  784. BABYLON.Matrix.Translation = function (x, y, z) {
  785. var result = BABYLON.Matrix.Identity();
  786. result.m[12] = x;
  787. result.m[13] = y;
  788. result.m[14] = z;
  789. return result;
  790. };
  791. BABYLON.Matrix.LookAtLH = function (eye, target, up) {
  792. // Z axis
  793. var zAxis = target.subtract(eye);
  794. zAxis.normalize();
  795. // X axis
  796. var xAxis = BABYLON.Vector3.Cross(up, zAxis);
  797. xAxis.normalize();
  798. // Y axis
  799. var yAxis = BABYLON.Vector3.Cross(zAxis, xAxis);
  800. yAxis.normalize();
  801. // Eye angles
  802. var ex = -BABYLON.Vector3.Dot(xAxis, eye);
  803. var ey = -BABYLON.Vector3.Dot(yAxis, eye);
  804. var ez = -BABYLON.Vector3.Dot(zAxis, eye);
  805. return BABYLON.Matrix.FromValues(xAxis.x, yAxis.x, zAxis.x, 0,
  806. xAxis.y, yAxis.y, zAxis.y, 0,
  807. xAxis.z, yAxis.z, zAxis.z, 0,
  808. ex, ey, ez, 1);
  809. };
  810. BABYLON.Matrix.OrthoLH = function (width, height, znear, zfar) {
  811. var hw = 2.0 / width;
  812. var hh = 2.0 / height;
  813. var id = 1.0 / (zfar - znear);
  814. var nid = znear / (znear - zfar);
  815. return BABYLON.Matrix.FromValues(hw, 0, 0, 0,
  816. 0, hh, 0, 0,
  817. 0, 0, id, 0,
  818. 0, 0, nid, 1);
  819. };
  820. BABYLON.Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  821. var matrix = BABYLON.Matrix.Zero();
  822. matrix.m[0] = 2.0 / (right - left);
  823. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0;
  824. matrix.m[5] = 2.0 / (top - bottom);
  825. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0;
  826. matrix.m[10] = -1.0 / (znear - zfar);
  827. matrix.m[8] = matrix.m[9] = matrix.m[11] = 0;
  828. matrix.m[12] = (left + right) / (left - right);
  829. matrix.m[13] = (top + bottom) / (bottom - top);
  830. matrix.m[14] = znear / (znear - zfar);
  831. matrix.m[15] = 1.0;
  832. return matrix;
  833. };
  834. BABYLON.Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  835. var matrix = BABYLON.Matrix.Zero();
  836. matrix.m[0] = (2.0 * znear) / width;
  837. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  838. matrix.m[5] = (2.0 * znear) / height;
  839. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  840. matrix.m[10] = -zfar / (znear - zfar);
  841. matrix.m[8] = matrix.m[9] = 0.0;
  842. matrix.m[11] = 1.0;
  843. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  844. matrix.m[14] = (znear * zfar) / (znear - zfar);
  845. return matrix;
  846. };
  847. BABYLON.Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  848. var matrix = BABYLON.Matrix.Zero();
  849. var tan = 1.0 / (Math.tan(fov * 0.5));
  850. matrix.m[0] = tan / aspect;
  851. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  852. matrix.m[5] = tan;
  853. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  854. matrix.m[8] = matrix.m[9] = 0.0;
  855. matrix.m[10] = -zfar / (znear - zfar);
  856. matrix.m[11] = 1.0;
  857. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  858. matrix.m[14] = (znear * zfar) / (znear - zfar);
  859. return matrix;
  860. };
  861. BABYLON.Matrix.AffineTransformation = function (scaling, rotationCenter, rotation, translation) {
  862. return BABYLON.Matrix.Scaling(scaling, scaling, scaling) * BABYLON.Matrix.Translation(-rotationCenter) *
  863. BABYLON.Matrix.RotationQuaternion(rotation) * BABYLON.Matrix.Translation(rotationCenter) * BABYLON.Matrix.Translation(translation);
  864. };
  865. BABYLON.Matrix.GetFinalMatrix = function (viewport, world, view, projection) {
  866. var cw = viewport.width;
  867. var ch = viewport.height;
  868. var cx = viewport.x;
  869. var cy = viewport.y;
  870. var zmin = viewport.minZ;
  871. var zmax = viewport.maxZ;
  872. var viewportMatrix = new BABYLON.Matrix(cw / 2.0, 0, 0, 0,
  873. 0, -ch / 2.0, 0, 0,
  874. 0, 0, zmax - zmin, 0,
  875. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  876. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  877. };
  878. BABYLON.Matrix.Transpose = function (matrix) {
  879. var result = new BABYLON.Matrix();
  880. result.m[0] = matrix.m[0];
  881. result.m[1] = matrix.m[4];
  882. result.m[2] = matrix.m[8];
  883. result.m[3] = matrix.m[12];
  884. result.m[4] = matrix.m[1];
  885. result.m[5] = matrix.m[5];
  886. result.m[6] = matrix.m[9];
  887. result.m[7] = matrix.m[13];
  888. result.m[8] = matrix.m[2];
  889. result.m[9] = matrix.m[6];
  890. result.m[10] = matrix.m[10];
  891. result.m[11] = matrix.m[14];
  892. result.m[12] = matrix.m[3];
  893. result.m[13] = matrix.m[7];
  894. result.m[14] = matrix.m[11];
  895. result.m[15] = matrix.m[15];
  896. return result;
  897. };
  898. BABYLON.Matrix.Reflection = function (plane) {
  899. var matrix = new BABYLON.Matrix();
  900. plane.normalize();
  901. var x = plane.normal.x;
  902. var y = plane.normal.y;
  903. var z = plane.normal.z;
  904. var temp = -2 * x;
  905. var temp2 = -2 * y;
  906. var temp3 = -2 * z;
  907. matrix.m[0] = (temp * x) + 1;
  908. matrix.m[1] = temp2 * x;
  909. matrix.m[2] = temp3 * x;
  910. matrix.m[3] = 0.0;
  911. matrix.m[4] = temp * y;
  912. matrix.m[5] = (temp2 * y) + 1;
  913. matrix.m[6] = temp3 * y;
  914. matrix.m[7] = 0.0;
  915. matrix.m[8] = temp * z;
  916. matrix.m[9] = temp2 * z;
  917. matrix.m[10] = (temp3 * z) + 1;
  918. matrix.m[11] = 0.0;
  919. matrix.m[12] = temp * plane.d;
  920. matrix.m[13] = temp2 * plane.d;
  921. matrix.m[14] = temp3 * plane.d;
  922. matrix.m[15] = 1.0;
  923. return matrix;
  924. };
  925. ////////////////////////////////// Plane //////////////////////////////////
  926. BABYLON.Plane = function (a, b, c, d) {
  927. this.normal = new BABYLON.Vector3(a, b, c);
  928. this.d = d;
  929. };
  930. // Methods
  931. BABYLON.Plane.prototype.normalize = function () {
  932. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  933. var magnitude = 0;
  934. if (norm != 0) {
  935. magnitude = 1.0 / norm;
  936. }
  937. this.normal.x *= magnitude;
  938. this.normal.y *= magnitude;
  939. this.normal.z *= magnitude;
  940. this.d *= magnitude;
  941. };
  942. BABYLON.Plane.prototype.transform = function(transformation) {
  943. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  944. var x = this.normal.x;
  945. var y = this.normal.y;
  946. var z = this.normal.z;
  947. var d = this.d;
  948. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  949. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  950. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  951. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  952. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  953. };
  954. BABYLON.Plane.prototype.dotCoordinate = function (point) {
  955. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  956. };
  957. // Statics
  958. BABYLON.Plane.FromArray = function (array) {
  959. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  960. };
  961. BABYLON.Plane.FromPoints = function(point1, point2, point3) {
  962. var x1 = point2.x - point1.x;
  963. var y1 = point2.y - point1.y;
  964. var z1 = point2.z - point1.z;
  965. var x2 = point3.x - point1.x;
  966. var y2 = point3.y - point1.y;
  967. var z2 = point3.z - point1.z;
  968. var yz = (y1 * z2) - (z1 * y2);
  969. var xz = (z1 * x2) - (x1 * z2);
  970. var xy = (x1 * y2) - (y1 * x2);
  971. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  972. var invPyth;
  973. if (pyth != 0)
  974. invPyth = 1.0 / pyth;
  975. else
  976. invPyth = 0;
  977. var normal = new BABYLON.Vector3(yz * invPyth, xz * invPyth, xy * invPyth);
  978. var d = -((normal.x * point1.x) + (normal.y * point1.y) + (normal.z * point1.z));
  979. return new BABYLON.Plane(normal.x, normal.y, normal.z, d);
  980. };
  981. ////////////////////////////////// Frustum //////////////////////////////////
  982. BABYLON.Frustum = {};
  983. // Statics
  984. BABYLON.Frustum.GetPlanes = function (transform) {
  985. var frustumPlanes = [];
  986. frustumPlanes.push(new BABYLON.Plane( // near
  987. transform.m[3] + transform.m[2],
  988. transform.m[7] + transform.m[6],
  989. transform.m[10] + transform.m[10],
  990. transform.m[15] + transform.m[14]));
  991. frustumPlanes[0].normalize();
  992. frustumPlanes.push(new BABYLON.Plane( // far
  993. transform.m[3] - transform.m[2],
  994. transform.m[7] - transform.m[6],
  995. transform.m[11] - transform.m[10],
  996. transform.m[15] - transform.m[14]));
  997. frustumPlanes[1].normalize();
  998. frustumPlanes.push(new BABYLON.Plane( // left
  999. transform.m[3] + transform.m[0],
  1000. transform.m[7] + transform.m[4],
  1001. transform.m[11] + transform.m[8],
  1002. transform.m[15] + transform.m[12]));
  1003. frustumPlanes[2].normalize();
  1004. frustumPlanes.push(new BABYLON.Plane( // right
  1005. transform.m[3] - transform.m[0],
  1006. transform.m[7] - transform.m[4],
  1007. transform.m[11] - transform.m[8],
  1008. transform.m[15] - transform.m[12]));
  1009. frustumPlanes[3].normalize();
  1010. frustumPlanes.push(new BABYLON.Plane( // top
  1011. transform.m[3] - transform.m[1],
  1012. transform.m[7] - transform.m[5],
  1013. transform.m[11] - transform.m[9],
  1014. transform.m[15] - transform.m[13]));
  1015. frustumPlanes[4].normalize();
  1016. frustumPlanes.push(new BABYLON.Plane( // bottom
  1017. transform.m[3] + transform.m[1],
  1018. transform.m[7] + transform.m[5],
  1019. transform.m[11] + transform.m[9],
  1020. transform.m[15] + transform.m[13]));
  1021. frustumPlanes[5].normalize();
  1022. return frustumPlanes;
  1023. };
  1024. })();