babylon.math.js 69 KB

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