babylon.math.js 63 KB

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