babylon.math.js 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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.length = function () {
  680. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  681. };
  682. BABYLON.Quaternion.prototype.normalize = function () {
  683. var length = 1.0 / this.length();
  684. this.x *= length;
  685. this.y *= length;
  686. this.z *= length;
  687. this.w *= length;
  688. };
  689. BABYLON.Quaternion.prototype.toEulerAngles = function () {
  690. var qx = this.x;
  691. var qy = this.y;
  692. var qz = this.z;
  693. var qw = this.w;
  694. var sqx = qx * qx;
  695. var sqy = qy * qy;
  696. var sqz = qz * qz;
  697. var yaw = Math.atan2(2.0 * (qy * qw - qx * qz), 1.0 - 2.0 * (sqy + sqz));
  698. var pitch = Math.asin(2.0 * (qx * qy + qz * qw));
  699. var roll = Math.atan2(2.0 * (qx * qw - qy * qz), 1.0 - 2.0 * (sqx + sqz));
  700. var gimbaLockTest = qx * qy + qz * qw;
  701. if (gimbaLockTest > 0.499) {
  702. yaw = 2.0 * Math.atan2(qx, qw);
  703. roll = 0;
  704. } else if (gimbaLockTest < -0.499) {
  705. yaw = -2.0 * Math.atan2(qx, qw);
  706. roll = 0;
  707. }
  708. return new BABYLON.Vector3(pitch, yaw, roll);
  709. };
  710. BABYLON.Quaternion.prototype.toRotationMatrix = function (result) {
  711. var xx = this.x * this.x;
  712. var yy = this.y * this.y;
  713. var zz = this.z * this.z;
  714. var xy = this.x * this.y;
  715. var zw = this.z * this.w;
  716. var zx = this.z * this.x;
  717. var yw = this.y * this.w;
  718. var yz = this.y * this.z;
  719. var xw = this.x * this.w;
  720. result.m[0] = 1.0 - (2.0 * (yy + zz));
  721. result.m[1] = 2.0 * (xy + zw);
  722. result.m[2] = 2.0 * (zx - yw);
  723. result.m[3] = 0;
  724. result.m[4] = 2.0 * (xy - zw);
  725. result.m[5] = 1.0 - (2.0 * (zz + xx));
  726. result.m[6] = 2.0 * (yz + xw);
  727. result.m[7] = 0;
  728. result.m[8] = 2.0 * (zx + yw);
  729. result.m[9] = 2.0 * (yz - xw);
  730. result.m[10] = 1.0 - (2.0 * (yy + xx));
  731. result.m[11] = 0;
  732. result.m[12] = 0;
  733. result.m[13] = 0;
  734. result.m[14] = 0;
  735. result.m[15] = 1.0;
  736. };
  737. // Statics
  738. BABYLON.Quaternion.FromArray = function (array, offset) {
  739. if (!offset) {
  740. offset = 0;
  741. }
  742. return new BABYLON.Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  743. };
  744. BABYLON.Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  745. var result = new BABYLON.Quaternion();
  746. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  747. return result;
  748. };
  749. BABYLON.Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  750. var halfRoll = roll * 0.5;
  751. var halfPitch = pitch * 0.5;
  752. var halfYaw = yaw * 0.5;
  753. var sinRoll = Math.sin(halfRoll);
  754. var cosRoll = Math.cos(halfRoll);
  755. var sinPitch = Math.sin(halfPitch);
  756. var cosPitch = Math.cos(halfPitch);
  757. var sinYaw = Math.sin(halfYaw);
  758. var cosYaw = Math.cos(halfYaw);
  759. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  760. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  761. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  762. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  763. };
  764. BABYLON.Quaternion.Slerp = function (left, right, amount) {
  765. var num2;
  766. var num3;
  767. var num = amount;
  768. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  769. var flag = false;
  770. if (num4 < 0) {
  771. flag = true;
  772. num4 = -num4;
  773. }
  774. if (num4 > 0.999999) {
  775. num3 = 1 - num;
  776. num2 = flag ? -num : num;
  777. }
  778. else {
  779. var num5 = Math.acos(num4);
  780. var num6 = (1.0 / Math.sin(num5));
  781. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  782. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  783. }
  784. 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));
  785. };
  786. ////////////////////////////////// Matrix //////////////////////////////////
  787. if (!BABYLON.MatrixType) {
  788. BABYLON.MatrixType = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  789. }
  790. BABYLON.Matrix = function () {
  791. this.m = new BABYLON.MatrixType(16);
  792. };
  793. // Properties
  794. BABYLON.Matrix.prototype.isIdentity = function () {
  795. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  796. return false;
  797. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  798. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  799. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  800. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  801. return false;
  802. return true;
  803. };
  804. BABYLON.Matrix.prototype.determinant = function () {
  805. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  806. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  807. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  808. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  809. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  810. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  811. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  812. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  813. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  814. };
  815. // Methods
  816. BABYLON.Matrix.prototype.toArray = function () {
  817. return this.m;
  818. };
  819. BABYLON.Matrix.prototype.invert = function () {
  820. this.invertToRef(this);
  821. };
  822. BABYLON.Matrix.prototype.invertToRef = function (other) {
  823. var l1 = this.m[0];
  824. var l2 = this.m[1];
  825. var l3 = this.m[2];
  826. var l4 = this.m[3];
  827. var l5 = this.m[4];
  828. var l6 = this.m[5];
  829. var l7 = this.m[6];
  830. var l8 = this.m[7];
  831. var l9 = this.m[8];
  832. var l10 = this.m[9];
  833. var l11 = this.m[10];
  834. var l12 = this.m[11];
  835. var l13 = this.m[12];
  836. var l14 = this.m[13];
  837. var l15 = this.m[14];
  838. var l16 = this.m[15];
  839. var l17 = (l11 * l16) - (l12 * l15);
  840. var l18 = (l10 * l16) - (l12 * l14);
  841. var l19 = (l10 * l15) - (l11 * l14);
  842. var l20 = (l9 * l16) - (l12 * l13);
  843. var l21 = (l9 * l15) - (l11 * l13);
  844. var l22 = (l9 * l14) - (l10 * l13);
  845. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  846. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  847. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  848. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  849. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  850. var l28 = (l7 * l16) - (l8 * l15);
  851. var l29 = (l6 * l16) - (l8 * l14);
  852. var l30 = (l6 * l15) - (l7 * l14);
  853. var l31 = (l5 * l16) - (l8 * l13);
  854. var l32 = (l5 * l15) - (l7 * l13);
  855. var l33 = (l5 * l14) - (l6 * l13);
  856. var l34 = (l7 * l12) - (l8 * l11);
  857. var l35 = (l6 * l12) - (l8 * l10);
  858. var l36 = (l6 * l11) - (l7 * l10);
  859. var l37 = (l5 * l12) - (l8 * l9);
  860. var l38 = (l5 * l11) - (l7 * l9);
  861. var l39 = (l5 * l10) - (l6 * l9);
  862. other.m[0] = l23 * l27;
  863. other.m[4] = l24 * l27;
  864. other.m[8] = l25 * l27;
  865. other.m[12] = l26 * l27;
  866. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  867. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  868. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  869. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  870. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  871. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  872. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  873. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  874. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  875. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  876. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  877. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  878. };
  879. BABYLON.Matrix.prototype.multiply = function (other) {
  880. var result = new BABYLON.Matrix();
  881. this.multiplyToRef(other, result);
  882. return result;
  883. };
  884. BABYLON.Matrix.prototype.copyFrom = function (other) {
  885. for (var index = 0; index < 16; index++) {
  886. this.m[index] = other.m[index];
  887. }
  888. };
  889. BABYLON.Matrix.prototype.multiplyToRef = function (other, result) {
  890. this.multiplyToArray(other, result.m, 0);
  891. };
  892. BABYLON.Matrix.prototype.multiplyToArray = function (other, result, offset) {
  893. 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];
  894. 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];
  895. 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];
  896. 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];
  897. 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];
  898. 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];
  899. 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];
  900. 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];
  901. 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];
  902. 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];
  903. 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];
  904. 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];
  905. 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];
  906. 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];
  907. 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];
  908. 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];
  909. };
  910. BABYLON.Matrix.prototype.equals = function (value) {
  911. 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] &&
  912. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  913. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  914. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  915. };
  916. BABYLON.Matrix.prototype.clone = function () {
  917. return BABYLON.Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  918. this.m[4], this.m[5], this.m[6], this.m[7],
  919. this.m[8], this.m[9], this.m[10], this.m[11],
  920. this.m[12], this.m[13], this.m[14], this.m[15]);
  921. };
  922. // Statics
  923. BABYLON.Matrix.FromArray = function (array, offset) {
  924. var result = new BABYLON.Matrix();
  925. BABYLON.Matrix.FromArrayToRef(array, offset, result);
  926. return result;
  927. };
  928. BABYLON.Matrix.FromArrayToRef = function (array, offset, result) {
  929. if (!offset) {
  930. offset = 0;
  931. }
  932. for (var index = 0; index < 16; index++) {
  933. result.m[index] = array[index + offset];
  934. }
  935. };
  936. BABYLON.Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14,
  937. initialM21, initialM22, initialM23, initialM24,
  938. initialM31, initialM32, initialM33, initialM34,
  939. initialM41, initialM42, initialM43, initialM44, result) {
  940. result.m[0] = initialM11;
  941. result.m[1] = initialM12;
  942. result.m[2] = initialM13;
  943. result.m[3] = initialM14;
  944. result.m[4] = initialM21;
  945. result.m[5] = initialM22;
  946. result.m[6] = initialM23;
  947. result.m[7] = initialM24;
  948. result.m[8] = initialM31;
  949. result.m[9] = initialM32;
  950. result.m[10] = initialM33;
  951. result.m[11] = initialM34;
  952. result.m[12] = initialM41;
  953. result.m[13] = initialM42;
  954. result.m[14] = initialM43;
  955. result.m[15] = initialM44;
  956. };
  957. BABYLON.Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14,
  958. initialM21, initialM22, initialM23, initialM24,
  959. initialM31, initialM32, initialM33, initialM34,
  960. initialM41, initialM42, initialM43, initialM44) {
  961. var result = new BABYLON.Matrix();
  962. result.m[0] = initialM11;
  963. result.m[1] = initialM12;
  964. result.m[2] = initialM13;
  965. result.m[3] = initialM14;
  966. result.m[4] = initialM21;
  967. result.m[5] = initialM22;
  968. result.m[6] = initialM23;
  969. result.m[7] = initialM24;
  970. result.m[8] = initialM31;
  971. result.m[9] = initialM32;
  972. result.m[10] = initialM33;
  973. result.m[11] = initialM34;
  974. result.m[12] = initialM41;
  975. result.m[13] = initialM42;
  976. result.m[14] = initialM43;
  977. result.m[15] = initialM44;
  978. return result;
  979. };
  980. BABYLON.Matrix.Identity = function () {
  981. return BABYLON.Matrix.FromValues(1.0, 0, 0, 0,
  982. 0, 1.0, 0, 0,
  983. 0, 0, 1.0, 0,
  984. 0, 0, 0, 1.0);
  985. };
  986. BABYLON.Matrix.IdentityToRef = function (result) {
  987. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  988. 0, 1.0, 0, 0,
  989. 0, 0, 1.0, 0,
  990. 0, 0, 0, 1.0, result);
  991. };
  992. BABYLON.Matrix.Zero = function () {
  993. return BABYLON.Matrix.FromValues(0, 0, 0, 0,
  994. 0, 0, 0, 0,
  995. 0, 0, 0, 0,
  996. 0, 0, 0, 0);
  997. };
  998. BABYLON.Matrix.RotationX = function (angle) {
  999. var result = new BABYLON.Matrix();
  1000. BABYLON.Matrix.RotationXToRef(angle, result);
  1001. return result;
  1002. };
  1003. BABYLON.Matrix.RotationXToRef = function (angle, result) {
  1004. var s = Math.sin(angle);
  1005. var c = Math.cos(angle);
  1006. result.m[0] = 1.0;
  1007. result.m[15] = 1.0;
  1008. result.m[5] = c;
  1009. result.m[10] = c;
  1010. result.m[9] = -s;
  1011. result.m[6] = s;
  1012. result.m[1] = 0;
  1013. result.m[2] = 0;
  1014. result.m[3] = 0;
  1015. result.m[4] = 0;
  1016. result.m[7] = 0;
  1017. result.m[8] = 0;
  1018. result.m[11] = 0;
  1019. result.m[12] = 0;
  1020. result.m[13] = 0;
  1021. result.m[14] = 0;
  1022. };
  1023. BABYLON.Matrix.RotationY = function (angle) {
  1024. var result = new BABYLON.Matrix();
  1025. BABYLON.Matrix.RotationYToRef(angle, result);
  1026. return result;
  1027. };
  1028. BABYLON.Matrix.RotationYToRef = function (angle, result) {
  1029. var s = Math.sin(angle);
  1030. var c = Math.cos(angle);
  1031. result.m[5] = 1.0;
  1032. result.m[15] = 1.0;
  1033. result.m[0] = c;
  1034. result.m[2] = -s;
  1035. result.m[8] = s;
  1036. result.m[10] = c;
  1037. result.m[1] = 0;
  1038. result.m[3] = 0;
  1039. result.m[4] = 0;
  1040. result.m[6] = 0;
  1041. result.m[7] = 0;
  1042. result.m[9] = 0;
  1043. result.m[11] = 0;
  1044. result.m[12] = 0;
  1045. result.m[13] = 0;
  1046. result.m[14] = 0;
  1047. };
  1048. BABYLON.Matrix.RotationZ = function (angle) {
  1049. var result = new BABYLON.Matrix();
  1050. BABYLON.Matrix.RotationZToRef(angle, result);
  1051. return result;
  1052. };
  1053. BABYLON.Matrix.RotationZToRef = function (angle, result) {
  1054. var s = Math.sin(angle);
  1055. var c = Math.cos(angle);
  1056. result.m[10] = 1.0;
  1057. result.m[15] = 1.0;
  1058. result.m[0] = c;
  1059. result.m[1] = s;
  1060. result.m[4] = -s;
  1061. result.m[5] = c;
  1062. result.m[2] = 0;
  1063. result.m[3] = 0;
  1064. result.m[6] = 0;
  1065. result.m[7] = 0;
  1066. result.m[8] = 0;
  1067. result.m[9] = 0;
  1068. result.m[11] = 0;
  1069. result.m[12] = 0;
  1070. result.m[13] = 0;
  1071. result.m[14] = 0;
  1072. };
  1073. BABYLON.Matrix.RotationAxis = function (axis, angle) {
  1074. var s = Math.sin(-angle);
  1075. var c = Math.cos(-angle);
  1076. var c1 = 1 - c;
  1077. axis.normalize();
  1078. var result = BABYLON.Matrix.Zero();
  1079. result.m[0] = (axis.x * axis.x) * c1 + c;
  1080. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1081. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1082. result.m[3] = 0.0;
  1083. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1084. result.m[5] = (axis.y * axis.y) * c1 + c;
  1085. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1086. result.m[7] = 0.0;
  1087. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1088. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1089. result.m[10] = (axis.z * axis.z) * c1 + c;
  1090. result.m[11] = 0.0;
  1091. result.m[15] = 1.0;
  1092. return result;
  1093. };
  1094. BABYLON.Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1095. var result = new BABYLON.Matrix();
  1096. BABYLON.Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1097. return result;
  1098. };
  1099. var tempQuaternion = new BABYLON.Quaternion(); // For RotationYawPitchRoll
  1100. BABYLON.Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1101. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, tempQuaternion);
  1102. tempQuaternion.toRotationMatrix(result);
  1103. };
  1104. BABYLON.Matrix.Scaling = function (x, y, z) {
  1105. var result = BABYLON.Matrix.Zero();
  1106. BABYLON.Matrix.ScalingToRef(x, y, z, result);
  1107. return result;
  1108. };
  1109. BABYLON.Matrix.ScalingToRef = function (x, y, z, result) {
  1110. result.m[0] = x;
  1111. result.m[1] = 0;
  1112. result.m[2] = 0;
  1113. result.m[3] = 0;
  1114. result.m[4] = 0;
  1115. result.m[5] = y;
  1116. result.m[6] = 0;
  1117. result.m[7] = 0;
  1118. result.m[8] = 0;
  1119. result.m[9] = 0;
  1120. result.m[10] = z;
  1121. result.m[11] = 0;
  1122. result.m[12] = 0;
  1123. result.m[13] = 0;
  1124. result.m[14] = 0;
  1125. result.m[15] = 1.0;
  1126. };
  1127. BABYLON.Matrix.Translation = function (x, y, z) {
  1128. var result = BABYLON.Matrix.Identity();
  1129. BABYLON.Matrix.TranslationToRef(x, y, z, result);
  1130. return result;
  1131. };
  1132. BABYLON.Matrix.TranslationToRef = function (x, y, z, result) {
  1133. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1134. 0, 1.0, 0, 0,
  1135. 0, 0, 1.0, 0,
  1136. x, y, z, 1.0, result);
  1137. };
  1138. BABYLON.Matrix.LookAtLH = function (eye, target, up) {
  1139. var result = BABYLON.Matrix.Zero();
  1140. BABYLON.Matrix.LookAtLHToRef(eye, target, up, result);
  1141. return result;
  1142. };
  1143. var xAxis = BABYLON.Vector3.Zero();
  1144. var yAxis = BABYLON.Vector3.Zero();
  1145. var zAxis = BABYLON.Vector3.Zero();
  1146. BABYLON.Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1147. // Z axis
  1148. target.subtractToRef(eye, zAxis);
  1149. zAxis.normalize();
  1150. // X axis
  1151. BABYLON.Vector3.CrossToRef(up, zAxis, xAxis);
  1152. xAxis.normalize();
  1153. // Y axis
  1154. BABYLON.Vector3.CrossToRef(zAxis, xAxis, yAxis);
  1155. yAxis.normalize();
  1156. // Eye angles
  1157. var ex = -BABYLON.Vector3.Dot(xAxis, eye);
  1158. var ey = -BABYLON.Vector3.Dot(yAxis, eye);
  1159. var ez = -BABYLON.Vector3.Dot(zAxis, eye);
  1160. return BABYLON.Matrix.FromValuesToRef(xAxis.x, yAxis.x, zAxis.x, 0,
  1161. xAxis.y, yAxis.y, zAxis.y, 0,
  1162. xAxis.z, yAxis.z, zAxis.z, 0,
  1163. ex, ey, ez, 1, result);
  1164. };
  1165. BABYLON.Matrix.OrthoLH = function (width, height, znear, zfar) {
  1166. var hw = 2.0 / width;
  1167. var hh = 2.0 / height;
  1168. var id = 1.0 / (zfar - znear);
  1169. var nid = znear / (znear - zfar);
  1170. return BABYLON.Matrix.FromValues(hw, 0, 0, 0,
  1171. 0, hh, 0, 0,
  1172. 0, 0, id, 0,
  1173. 0, 0, nid, 1);
  1174. };
  1175. BABYLON.Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1176. var matrix = BABYLON.Matrix.Zero();
  1177. BABYLON.Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1178. return matrix;
  1179. };
  1180. BABYLON.Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1181. result.m[0] = 2.0 / (right - left);
  1182. result.m[1] = result.m[2] = result.m[3] = 0;
  1183. result.m[5] = 2.0 / (top - bottom);
  1184. result.m[4] = result.m[6] = result.m[7] = 0;
  1185. result.m[10] = -1.0 / (znear - zfar);
  1186. result.m[8] = result.m[9] = result.m[11] = 0;
  1187. result.m[12] = (left + right) / (left - right);
  1188. result.m[13] = (top + bottom) / (bottom - top);
  1189. result.m[14] = znear / (znear - zfar);
  1190. result.m[15] = 1.0;
  1191. };
  1192. BABYLON.Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1193. var matrix = BABYLON.Matrix.Zero();
  1194. matrix.m[0] = (2.0 * znear) / width;
  1195. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1196. matrix.m[5] = (2.0 * znear) / height;
  1197. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1198. matrix.m[10] = -zfar / (znear - zfar);
  1199. matrix.m[8] = matrix.m[9] = 0.0;
  1200. matrix.m[11] = 1.0;
  1201. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1202. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1203. return matrix;
  1204. };
  1205. BABYLON.Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1206. var matrix = BABYLON.Matrix.Zero();
  1207. BABYLON.Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1208. return matrix;
  1209. };
  1210. BABYLON.Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result) {
  1211. var tan = 1.0 / (Math.tan(fov * 0.5));
  1212. result.m[0] = tan / aspect;
  1213. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1214. result.m[5] = tan;
  1215. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1216. result.m[8] = result.m[9] = 0.0;
  1217. result.m[10] = -zfar / (znear - zfar);
  1218. result.m[11] = 1.0;
  1219. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1220. result.m[14] = (znear * zfar) / (znear - zfar);
  1221. };
  1222. BABYLON.Matrix.AffineTransformation = function (scaling, rotationCenter, rotation, translation) {
  1223. return BABYLON.Matrix.Scaling(scaling, scaling, scaling) * BABYLON.Matrix.Translation(-rotationCenter) *
  1224. BABYLON.Matrix.RotationQuaternion(rotation) * BABYLON.Matrix.Translation(rotationCenter) * BABYLON.Matrix.Translation(translation);
  1225. };
  1226. BABYLON.Matrix.GetFinalMatrix = function (viewport, world, view, projection) {
  1227. var cw = viewport.width;
  1228. var ch = viewport.height;
  1229. var cx = viewport.x;
  1230. var cy = viewport.y;
  1231. var zmin = viewport.minZ;
  1232. var zmax = viewport.maxZ;
  1233. var viewportMatrix = new BABYLON.Matrix(cw / 2.0, 0, 0, 0,
  1234. 0, -ch / 2.0, 0, 0,
  1235. 0, 0, zmax - zmin, 0,
  1236. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  1237. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1238. };
  1239. BABYLON.Matrix.Transpose = function (matrix) {
  1240. var result = new BABYLON.Matrix();
  1241. result.m[0] = matrix.m[0];
  1242. result.m[1] = matrix.m[4];
  1243. result.m[2] = matrix.m[8];
  1244. result.m[3] = matrix.m[12];
  1245. result.m[4] = matrix.m[1];
  1246. result.m[5] = matrix.m[5];
  1247. result.m[6] = matrix.m[9];
  1248. result.m[7] = matrix.m[13];
  1249. result.m[8] = matrix.m[2];
  1250. result.m[9] = matrix.m[6];
  1251. result.m[10] = matrix.m[10];
  1252. result.m[11] = matrix.m[14];
  1253. result.m[12] = matrix.m[3];
  1254. result.m[13] = matrix.m[7];
  1255. result.m[14] = matrix.m[11];
  1256. result.m[15] = matrix.m[15];
  1257. return result;
  1258. };
  1259. BABYLON.Matrix.Reflection = function (plane) {
  1260. var matrix = new BABYLON.Matrix();
  1261. BABYLON.Matrix.ReflectionToRef(plane, matrix);
  1262. return matrix;
  1263. };
  1264. BABYLON.Matrix.ReflectionToRef = function (plane, result) {
  1265. plane.normalize();
  1266. var x = plane.normal.x;
  1267. var y = plane.normal.y;
  1268. var z = plane.normal.z;
  1269. var temp = -2 * x;
  1270. var temp2 = -2 * y;
  1271. var temp3 = -2 * z;
  1272. result.m[0] = (temp * x) + 1;
  1273. result.m[1] = temp2 * x;
  1274. result.m[2] = temp3 * x;
  1275. result.m[3] = 0.0;
  1276. result.m[4] = temp * y;
  1277. result.m[5] = (temp2 * y) + 1;
  1278. result.m[6] = temp3 * y;
  1279. result.m[7] = 0.0;
  1280. result.m[8] = temp * z;
  1281. result.m[9] = temp2 * z;
  1282. result.m[10] = (temp3 * z) + 1;
  1283. result.m[11] = 0.0;
  1284. result.m[12] = temp * plane.d;
  1285. result.m[13] = temp2 * plane.d;
  1286. result.m[14] = temp3 * plane.d;
  1287. result.m[15] = 1.0;
  1288. };
  1289. ////////////////////////////////// Plane //////////////////////////////////
  1290. BABYLON.Plane = function (a, b, c, d) {
  1291. this.normal = new BABYLON.Vector3(a, b, c);
  1292. this.d = d;
  1293. };
  1294. // Methods
  1295. BABYLON.Plane.prototype.normalize = function () {
  1296. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1297. var magnitude = 0;
  1298. if (norm != 0) {
  1299. magnitude = 1.0 / norm;
  1300. }
  1301. this.normal.x *= magnitude;
  1302. this.normal.y *= magnitude;
  1303. this.normal.z *= magnitude;
  1304. this.d *= magnitude;
  1305. };
  1306. BABYLON.Plane.prototype.transform = function (transformation) {
  1307. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1308. var x = this.normal.x;
  1309. var y = this.normal.y;
  1310. var z = this.normal.z;
  1311. var d = this.d;
  1312. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1313. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1314. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1315. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1316. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1317. };
  1318. BABYLON.Plane.prototype.dotCoordinate = function (point) {
  1319. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1320. };
  1321. BABYLON.Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  1322. var x1 = point2.x - point1.x;
  1323. var y1 = point2.y - point1.y;
  1324. var z1 = point2.z - point1.z;
  1325. var x2 = point3.x - point1.x;
  1326. var y2 = point3.y - point1.y;
  1327. var z2 = point3.z - point1.z;
  1328. var yz = (y1 * z2) - (z1 * y2);
  1329. var xz = (z1 * x2) - (x1 * z2);
  1330. var xy = (x1 * y2) - (y1 * x2);
  1331. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1332. var invPyth;
  1333. if (pyth != 0)
  1334. invPyth = 1.0 / pyth;
  1335. else
  1336. invPyth = 0;
  1337. this.normal.x = yz * invPyth;
  1338. this.normal.y = xz * invPyth;
  1339. this.normal.z = xy * invPyth;
  1340. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1341. };
  1342. BABYLON.Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  1343. var dot = BABYLON.Vector3.Dot(this.normal, direction);
  1344. return (dot <= epsilon);
  1345. };
  1346. BABYLON.Plane.prototype.signedDistanceTo = function (point) {
  1347. return BABYLON.Vector3.Dot(point, this.normal) + this.d;
  1348. };
  1349. // Statics
  1350. BABYLON.Plane.FromArray = function (array) {
  1351. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1352. };
  1353. BABYLON.Plane.FromPoints = function (point1, point2, point3) {
  1354. var result = new BABYLON.Plane(0, 0, 0, 0);
  1355. result.copyFromPoints(point1, point2, point3);
  1356. return result;
  1357. };
  1358. BABYLON.Plane.FromPositionAndNormal = function (origin, normal) {
  1359. var result = new BABYLON.Plane(0, 0, 0, 0);
  1360. normal.normalize();
  1361. result.normal = normal;
  1362. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1363. return result;
  1364. };
  1365. BABYLON.Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  1366. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1367. return BABYLON.Vector3.Dot(point, normal) + d;
  1368. };
  1369. ////////////////////////////////// Frustum //////////////////////////////////
  1370. BABYLON.Frustum = {};
  1371. // Statics
  1372. BABYLON.Frustum.GetPlanes = function (transform) {
  1373. var frustumPlanes = [];
  1374. for (var index = 0; index < 6; index++) {
  1375. frustumPlanes.push(new BABYLON.Plane(0, 0, 0, 0));
  1376. }
  1377. BABYLON.Frustum.GetPlanesToRef(transform, frustumPlanes);
  1378. return frustumPlanes;
  1379. };
  1380. BABYLON.Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  1381. // Near
  1382. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1383. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1384. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1385. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1386. frustumPlanes[0].normalize();
  1387. // Far
  1388. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1389. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1390. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1391. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1392. frustumPlanes[1].normalize();
  1393. // Left
  1394. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1395. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1396. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1397. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1398. frustumPlanes[2].normalize();
  1399. // Right
  1400. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1401. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1402. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1403. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1404. frustumPlanes[3].normalize();
  1405. // Top
  1406. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1407. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1408. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1409. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1410. frustumPlanes[4].normalize();
  1411. // Bottom
  1412. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1413. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1414. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1415. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1416. frustumPlanes[5].normalize();
  1417. };
  1418. })();