babylon.math.js 71 KB

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