babylon.math.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572
  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.toColor4 = function (alpha) {
  25. if (typeof alpha === "undefined") { alpha = 1; }
  26. return new Color4(this.r, this.g, this.b, alpha);
  27. };
  28. Color3.prototype.asArray = function () {
  29. var result = [];
  30. this.toArray(result, 0);
  31. return result;
  32. };
  33. Color3.prototype.toLuminance = function () {
  34. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  35. };
  36. Color3.prototype.multiply = function (otherColor) {
  37. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  38. };
  39. Color3.prototype.multiplyToRef = function (otherColor, result) {
  40. result.r = this.r * otherColor.r;
  41. result.g = this.g * otherColor.g;
  42. result.b = this.b * otherColor.b;
  43. };
  44. Color3.prototype.equals = function (otherColor) {
  45. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  46. };
  47. Color3.prototype.scale = function (scale) {
  48. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  49. };
  50. Color3.prototype.scaleToRef = function (scale, result) {
  51. result.r = this.r * scale;
  52. result.g = this.g * scale;
  53. result.b = this.b * scale;
  54. };
  55. Color3.prototype.add = function (otherColor) {
  56. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  57. };
  58. Color3.prototype.addToRef = function (otherColor, result) {
  59. result.r = this.r + otherColor.r;
  60. result.g = this.g + otherColor.g;
  61. result.b = this.b + otherColor.b;
  62. };
  63. Color3.prototype.subtract = function (otherColor) {
  64. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  65. };
  66. Color3.prototype.subtractToRef = function (otherColor, result) {
  67. result.r = this.r - otherColor.r;
  68. result.g = this.g - otherColor.g;
  69. result.b = this.b - otherColor.b;
  70. };
  71. Color3.prototype.clone = function () {
  72. return new Color3(this.r, this.g, this.b);
  73. };
  74. Color3.prototype.copyFrom = function (source) {
  75. this.r = source.r;
  76. this.g = source.g;
  77. this.b = source.b;
  78. };
  79. Color3.prototype.copyFromFloats = function (r, g, b) {
  80. this.r = r;
  81. this.g = g;
  82. this.b = b;
  83. };
  84. // Statics
  85. Color3.FromArray = function (array) {
  86. return new Color3(array[0], array[1], array[2]);
  87. };
  88. Color3.FromInts = function (r, g, b) {
  89. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  90. };
  91. Color3.Lerp = function (start, end, amount) {
  92. var r = start.r + ((end.r - start.r) * amount);
  93. var g = start.g + ((end.g - start.g) * amount);
  94. var b = start.b + ((end.b - start.b) * amount);
  95. return new Color3(r, g, b);
  96. };
  97. Color3.Red = function () {
  98. return new Color3(1, 0, 0);
  99. };
  100. Color3.Green = function () {
  101. return new Color3(0, 1, 0);
  102. };
  103. Color3.Blue = function () {
  104. return new Color3(0, 0, 1);
  105. };
  106. Color3.Black = function () {
  107. return new Color3(0, 0, 0);
  108. };
  109. Color3.White = function () {
  110. return new Color3(1, 1, 1);
  111. };
  112. Color3.Purple = function () {
  113. return new Color3(0.5, 0, 0.5);
  114. };
  115. Color3.Magenta = function () {
  116. return new Color3(1, 0, 1);
  117. };
  118. Color3.Yellow = function () {
  119. return new Color3(1, 1, 0);
  120. };
  121. Color3.Gray = function () {
  122. return new Color3(0.5, 0.5, 0.5);
  123. };
  124. return Color3;
  125. })();
  126. BABYLON.Color3 = Color3;
  127. var Color4 = (function () {
  128. function Color4(r, g, b, a) {
  129. this.r = r;
  130. this.g = g;
  131. this.b = b;
  132. this.a = a;
  133. }
  134. // Operators
  135. Color4.prototype.addInPlace = function (right) {
  136. this.r += right.r;
  137. this.g += right.g;
  138. this.b += right.b;
  139. this.a += right.a;
  140. };
  141. Color4.prototype.asArray = function () {
  142. var result = [];
  143. this.toArray(result, 0);
  144. return result;
  145. };
  146. Color4.prototype.toArray = function (array, index) {
  147. if (index === undefined) {
  148. index = 0;
  149. }
  150. array[index] = this.r;
  151. array[index + 1] = this.g;
  152. array[index + 2] = this.b;
  153. array[index + 3] = this.a;
  154. };
  155. Color4.prototype.add = function (right) {
  156. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  157. };
  158. Color4.prototype.subtract = function (right) {
  159. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  160. };
  161. Color4.prototype.subtractToRef = function (right, result) {
  162. result.r = this.r - right.r;
  163. result.g = this.g - right.g;
  164. result.b = this.b - right.b;
  165. result.a = this.a - right.a;
  166. };
  167. Color4.prototype.scale = function (scale) {
  168. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  169. };
  170. Color4.prototype.scaleToRef = function (scale, result) {
  171. result.r = this.r * scale;
  172. result.g = this.g * scale;
  173. result.b = this.b * scale;
  174. result.a = this.a * scale;
  175. };
  176. Color4.prototype.toString = function () {
  177. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  178. };
  179. Color4.prototype.clone = function () {
  180. return new Color4(this.r, this.g, this.b, this.a);
  181. };
  182. // Statics
  183. Color4.Lerp = function (left, right, amount) {
  184. var result = new Color4(0, 0, 0, 0);
  185. BABYLON.Color4.LerpToRef(left, right, amount, result);
  186. return result;
  187. };
  188. Color4.LerpToRef = function (left, right, amount, result) {
  189. result.r = left.r + (right.r - left.r) * amount;
  190. result.g = left.g + (right.g - left.g) * amount;
  191. result.b = left.b + (right.b - left.b) * amount;
  192. result.a = left.a + (right.a - left.a) * amount;
  193. };
  194. Color4.FromArray = function (array, offset) {
  195. if (typeof offset === "undefined") { offset = 0; }
  196. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  197. };
  198. Color4.FromInts = function (r, g, b, a) {
  199. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  200. };
  201. return Color4;
  202. })();
  203. BABYLON.Color4 = Color4;
  204. var Vector2 = (function () {
  205. function Vector2(x, y) {
  206. this.x = x;
  207. this.y = y;
  208. }
  209. Vector2.prototype.toString = function () {
  210. return "{X: " + this.x + " Y:" + this.y + "}";
  211. };
  212. // Operators
  213. Vector2.prototype.toArray = function (array, index) {
  214. if (index === undefined) {
  215. index = 0;
  216. }
  217. array[index] = this.x;
  218. array[index + 1] = this.y;
  219. };
  220. Vector2.prototype.asArray = function () {
  221. var result = [];
  222. this.toArray(result, 0);
  223. return result;
  224. };
  225. Vector2.prototype.copyFrom = function (source) {
  226. this.x = source.x;
  227. this.y = source.y;
  228. };
  229. Vector2.prototype.copyFromFloats = function (x, y) {
  230. this.x = x;
  231. this.y = y;
  232. };
  233. Vector2.prototype.add = function (otherVector) {
  234. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  235. };
  236. Vector2.prototype.addVector3 = function (otherVector) {
  237. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  238. };
  239. Vector2.prototype.subtract = function (otherVector) {
  240. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  241. };
  242. Vector2.prototype.subtractInPlace = function (otherVector) {
  243. this.x -= otherVector.x;
  244. this.y -= otherVector.y;
  245. };
  246. Vector2.prototype.multiplyInPlace = function (otherVector) {
  247. this.x *= otherVector.x;
  248. this.y *= otherVector.y;
  249. };
  250. Vector2.prototype.multiply = function (otherVector) {
  251. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  252. };
  253. Vector2.prototype.multiplyToRef = function (otherVector, result) {
  254. result.x = this.x * otherVector.x;
  255. result.y = this.y * otherVector.y;
  256. };
  257. Vector2.prototype.multiplyByFloats = function (x, y) {
  258. return new Vector2(this.x * x, this.y * y);
  259. };
  260. Vector2.prototype.divide = function (otherVector) {
  261. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  262. };
  263. Vector2.prototype.divideToRef = function (otherVector, result) {
  264. result.x = this.x / otherVector.x;
  265. result.y = this.y / otherVector.y;
  266. };
  267. Vector2.prototype.negate = function () {
  268. return new Vector2(-this.x, -this.y);
  269. };
  270. Vector2.prototype.scaleInPlace = function (scale) {
  271. this.x *= scale;
  272. this.y *= scale;
  273. return this;
  274. };
  275. Vector2.prototype.scale = function (scale) {
  276. return new Vector2(this.x * scale, this.y * scale);
  277. };
  278. Vector2.prototype.equals = function (otherVector) {
  279. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  280. };
  281. // Properties
  282. Vector2.prototype.length = function () {
  283. return Math.sqrt(this.x * this.x + this.y * this.y);
  284. };
  285. Vector2.prototype.lengthSquared = function () {
  286. return (this.x * this.x + this.y * this.y);
  287. };
  288. // Methods
  289. Vector2.prototype.normalize = function () {
  290. var len = this.length();
  291. if (len === 0)
  292. return this;
  293. var num = 1.0 / len;
  294. this.x *= num;
  295. this.y *= num;
  296. return this;
  297. };
  298. Vector2.prototype.clone = function () {
  299. return new Vector2(this.x, this.y);
  300. };
  301. // Statics
  302. Vector2.Zero = function () {
  303. return new Vector2(0, 0);
  304. };
  305. Vector2.FromArray = function (array, offset) {
  306. if (!offset) {
  307. offset = 0;
  308. }
  309. return new Vector2(array[offset], array[offset + 1]);
  310. };
  311. Vector2.FromArrayToRef = function (array, offset, result) {
  312. result.x = array[offset];
  313. result.y = array[offset + 1];
  314. };
  315. Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  316. var squared = amount * amount;
  317. var cubed = amount * squared;
  318. 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));
  319. 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));
  320. return new Vector2(x, y);
  321. };
  322. Vector2.Clamp = function (value, min, max) {
  323. var x = value.x;
  324. x = (x > max.x) ? max.x : x;
  325. x = (x < min.x) ? min.x : x;
  326. var y = value.y;
  327. y = (y > max.y) ? max.y : y;
  328. y = (y < min.y) ? min.y : y;
  329. return new Vector2(x, y);
  330. };
  331. Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  332. var squared = amount * amount;
  333. var cubed = amount * squared;
  334. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  335. var part2 = (-2.0 * cubed) + (3.0 * squared);
  336. var part3 = (cubed - (2.0 * squared)) + amount;
  337. var part4 = cubed - squared;
  338. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  339. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  340. return new Vector2(x, y);
  341. };
  342. Vector2.Lerp = function (start, end, amount) {
  343. var x = start.x + ((end.x - start.x) * amount);
  344. var y = start.y + ((end.y - start.y) * amount);
  345. return new Vector2(x, y);
  346. };
  347. Vector2.Dot = function (left, right) {
  348. return left.x * right.x + left.y * right.y;
  349. };
  350. Vector2.Normalize = function (vector) {
  351. var newVector = vector.clone();
  352. newVector.normalize();
  353. return newVector;
  354. };
  355. Vector2.Minimize = function (left, right) {
  356. var x = (left.x < right.x) ? left.x : right.x;
  357. var y = (left.y < right.y) ? left.y : right.y;
  358. return new Vector2(x, y);
  359. };
  360. Vector2.Maximize = function (left, right) {
  361. var x = (left.x > right.x) ? left.x : right.x;
  362. var y = (left.y > right.y) ? left.y : right.y;
  363. return new Vector2(x, y);
  364. };
  365. Vector2.Transform = function (vector, transformation) {
  366. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  367. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  368. return new Vector2(x, y);
  369. };
  370. Vector2.Distance = function (value1, value2) {
  371. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  372. };
  373. Vector2.DistanceSquared = function (value1, value2) {
  374. var x = value1.x - value2.x;
  375. var y = value1.y - value2.y;
  376. return (x * x) + (y * y);
  377. };
  378. return Vector2;
  379. })();
  380. BABYLON.Vector2 = Vector2;
  381. var Vector3 = (function () {
  382. function Vector3(x, y, z) {
  383. this.x = x;
  384. this.y = y;
  385. this.z = z;
  386. }
  387. Vector3.prototype.toString = function () {
  388. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  389. };
  390. // Operators
  391. Vector3.prototype.asArray = function () {
  392. var result = [];
  393. this.toArray(result, 0);
  394. return result;
  395. };
  396. Vector3.prototype.toArray = function (array, index) {
  397. if (index === undefined) {
  398. index = 0;
  399. }
  400. array[index] = this.x;
  401. array[index + 1] = this.y;
  402. array[index + 2] = this.z;
  403. };
  404. Vector3.prototype.addInPlace = function (otherVector) {
  405. this.x += otherVector.x;
  406. this.y += otherVector.y;
  407. this.z += otherVector.z;
  408. };
  409. Vector3.prototype.add = function (otherVector) {
  410. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  411. };
  412. Vector3.prototype.addToRef = function (otherVector, result) {
  413. result.x = this.x + otherVector.x;
  414. result.y = this.y + otherVector.y;
  415. result.z = this.z + otherVector.z;
  416. };
  417. Vector3.prototype.subtractInPlace = function (otherVector) {
  418. this.x -= otherVector.x;
  419. this.y -= otherVector.y;
  420. this.z -= otherVector.z;
  421. };
  422. Vector3.prototype.subtract = function (otherVector) {
  423. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  424. };
  425. Vector3.prototype.subtractToRef = function (otherVector, result) {
  426. result.x = this.x - otherVector.x;
  427. result.y = this.y - otherVector.y;
  428. result.z = this.z - otherVector.z;
  429. };
  430. Vector3.prototype.subtractFromFloats = function (x, y, z) {
  431. return new Vector3(this.x - x, this.y - y, this.z - z);
  432. };
  433. Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  434. result.x = this.x - x;
  435. result.y = this.y - y;
  436. result.z = this.z - z;
  437. };
  438. Vector3.prototype.negate = function () {
  439. return new Vector3(-this.x, -this.y, -this.z);
  440. };
  441. Vector3.prototype.scaleInPlace = function (scale) {
  442. this.x *= scale;
  443. this.y *= scale;
  444. this.z *= scale;
  445. return this;
  446. };
  447. Vector3.prototype.scale = function (scale) {
  448. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  449. };
  450. Vector3.prototype.scaleToRef = function (scale, result) {
  451. result.x = this.x * scale;
  452. result.y = this.y * scale;
  453. result.z = this.z * scale;
  454. };
  455. Vector3.prototype.equals = function (otherVector) {
  456. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  457. };
  458. Vector3.prototype.equalsWithEpsilon = function (otherVector) {
  459. return Math.abs(this.x - otherVector.x) < BABYLON.Engine.Epsilon && Math.abs(this.y - otherVector.y) < BABYLON.Engine.Epsilon && Math.abs(this.z - otherVector.z) < BABYLON.Engine.Epsilon;
  460. };
  461. Vector3.prototype.equalsToFloats = function (x, y, z) {
  462. return this.x === x && this.y === y && this.z === z;
  463. };
  464. Vector3.prototype.multiplyInPlace = function (otherVector) {
  465. this.x *= otherVector.x;
  466. this.y *= otherVector.y;
  467. this.z *= otherVector.z;
  468. };
  469. Vector3.prototype.multiply = function (otherVector) {
  470. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  471. };
  472. Vector3.prototype.multiplyToRef = function (otherVector, result) {
  473. result.x = this.x * otherVector.x;
  474. result.y = this.y * otherVector.y;
  475. result.z = this.z * otherVector.z;
  476. };
  477. Vector3.prototype.multiplyByFloats = function (x, y, z) {
  478. return new Vector3(this.x * x, this.y * y, this.z * z);
  479. };
  480. Vector3.prototype.divide = function (otherVector) {
  481. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  482. };
  483. Vector3.prototype.divideToRef = function (otherVector, result) {
  484. result.x = this.x / otherVector.x;
  485. result.y = this.y / otherVector.y;
  486. result.z = this.z / otherVector.z;
  487. };
  488. Vector3.prototype.MinimizeInPlace = function (other) {
  489. if (other.x < this.x)
  490. this.x = other.x;
  491. if (other.y < this.y)
  492. this.y = other.y;
  493. if (other.z < this.z)
  494. this.z = other.z;
  495. };
  496. Vector3.prototype.MaximizeInPlace = function (other) {
  497. if (other.x > this.x)
  498. this.x = other.x;
  499. if (other.y > this.y)
  500. this.y = other.y;
  501. if (other.z > this.z)
  502. this.z = other.z;
  503. };
  504. // Properties
  505. Vector3.prototype.length = function () {
  506. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  507. };
  508. Vector3.prototype.lengthSquared = function () {
  509. return (this.x * this.x + this.y * this.y + this.z * this.z);
  510. };
  511. // Methods
  512. Vector3.prototype.normalize = function () {
  513. var len = this.length();
  514. if (len === 0)
  515. return this;
  516. var num = 1.0 / len;
  517. this.x *= num;
  518. this.y *= num;
  519. this.z *= num;
  520. return this;
  521. };
  522. Vector3.prototype.clone = function () {
  523. return new Vector3(this.x, this.y, this.z);
  524. };
  525. Vector3.prototype.copyFrom = function (source) {
  526. this.x = source.x;
  527. this.y = source.y;
  528. this.z = source.z;
  529. };
  530. Vector3.prototype.copyFromFloats = function (x, y, z) {
  531. this.x = x;
  532. this.y = y;
  533. this.z = z;
  534. };
  535. // Statics
  536. Vector3.FromArray = function (array, offset) {
  537. if (!offset) {
  538. offset = 0;
  539. }
  540. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  541. };
  542. Vector3.FromArrayToRef = function (array, offset, result) {
  543. result.x = array[offset];
  544. result.y = array[offset + 1];
  545. result.z = array[offset + 2];
  546. };
  547. Vector3.FromFloatArrayToRef = function (array, offset, result) {
  548. result.x = array[offset];
  549. result.y = array[offset + 1];
  550. result.z = array[offset + 2];
  551. };
  552. Vector3.FromFloatsToRef = function (x, y, z, result) {
  553. result.x = x;
  554. result.y = y;
  555. result.z = z;
  556. };
  557. Vector3.Zero = function () {
  558. return new Vector3(0, 0, 0);
  559. };
  560. Vector3.Up = function () {
  561. return new Vector3(0, 1.0, 0);
  562. };
  563. Vector3.TransformCoordinates = function (vector, transformation) {
  564. var result = Vector3.Zero();
  565. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  566. return result;
  567. };
  568. Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  569. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  570. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  571. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  572. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  573. result.x = x / w;
  574. result.y = y / w;
  575. result.z = z / w;
  576. };
  577. Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  578. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  579. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  580. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  581. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  582. result.x = rx / rw;
  583. result.y = ry / rw;
  584. result.z = rz / rw;
  585. };
  586. Vector3.TransformNormal = function (vector, transformation) {
  587. var result = Vector3.Zero();
  588. Vector3.TransformNormalToRef(vector, transformation, result);
  589. return result;
  590. };
  591. Vector3.TransformNormalToRef = function (vector, transformation, result) {
  592. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  593. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  594. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  595. };
  596. Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  597. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  598. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  599. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  600. };
  601. Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  602. var squared = amount * amount;
  603. var cubed = amount * squared;
  604. 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));
  605. 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));
  606. 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));
  607. return new Vector3(x, y, z);
  608. };
  609. Vector3.Clamp = function (value, min, max) {
  610. var x = value.x;
  611. x = (x > max.x) ? max.x : x;
  612. x = (x < min.x) ? min.x : x;
  613. var y = value.y;
  614. y = (y > max.y) ? max.y : y;
  615. y = (y < min.y) ? min.y : y;
  616. var z = value.z;
  617. z = (z > max.z) ? max.z : z;
  618. z = (z < min.z) ? min.z : z;
  619. return new Vector3(x, y, z);
  620. };
  621. Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  622. var squared = amount * amount;
  623. var cubed = amount * squared;
  624. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  625. var part2 = (-2.0 * cubed) + (3.0 * squared);
  626. var part3 = (cubed - (2.0 * squared)) + amount;
  627. var part4 = cubed - squared;
  628. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  629. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  630. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  631. return new Vector3(x, y, z);
  632. };
  633. Vector3.Lerp = function (start, end, amount) {
  634. var x = start.x + ((end.x - start.x) * amount);
  635. var y = start.y + ((end.y - start.y) * amount);
  636. var z = start.z + ((end.z - start.z) * amount);
  637. return new Vector3(x, y, z);
  638. };
  639. Vector3.Dot = function (left, right) {
  640. return (left.x * right.x + left.y * right.y + left.z * right.z);
  641. };
  642. Vector3.Cross = function (left, right) {
  643. var result = Vector3.Zero();
  644. Vector3.CrossToRef(left, right, result);
  645. return result;
  646. };
  647. Vector3.CrossToRef = function (left, right, result) {
  648. result.x = left.y * right.z - left.z * right.y;
  649. result.y = left.z * right.x - left.x * right.z;
  650. result.z = left.x * right.y - left.y * right.x;
  651. };
  652. Vector3.Normalize = function (vector) {
  653. var result = Vector3.Zero();
  654. Vector3.NormalizeToRef(vector, result);
  655. return result;
  656. };
  657. Vector3.NormalizeToRef = function (vector, result) {
  658. result.copyFrom(vector);
  659. result.normalize();
  660. };
  661. Vector3.Project = function (vector, world, transform, viewport) {
  662. var cw = viewport.width;
  663. var ch = viewport.height;
  664. var cx = viewport.x;
  665. var cy = viewport.y;
  666. 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);
  667. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  668. return Vector3.TransformCoordinates(vector, finalMatrix);
  669. };
  670. Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  671. var matrix = world.multiply(view).multiply(projection);
  672. matrix.invert();
  673. source.x = source.x / viewportWidth * 2 - 1;
  674. source.y = -(source.y / viewportHeight * 2 - 1);
  675. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  676. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  677. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  678. vector = vector.scale(1.0 / num);
  679. }
  680. return vector;
  681. };
  682. Vector3.Minimize = function (left, right) {
  683. var min = left.clone();
  684. min.MinimizeInPlace(right);
  685. return min;
  686. };
  687. Vector3.Maximize = function (left, right) {
  688. var max = left.clone();
  689. max.MaximizeInPlace(right);
  690. return max;
  691. };
  692. Vector3.Distance = function (value1, value2) {
  693. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  694. };
  695. Vector3.DistanceSquared = function (value1, value2) {
  696. var x = value1.x - value2.x;
  697. var y = value1.y - value2.y;
  698. var z = value1.z - value2.z;
  699. return (x * x) + (y * y) + (z * z);
  700. };
  701. Vector3.Center = function (value1, value2) {
  702. var center = value1.add(value2);
  703. center.scaleInPlace(0.5);
  704. return center;
  705. };
  706. return Vector3;
  707. })();
  708. BABYLON.Vector3 = Vector3;
  709. var Vector4 = (function () {
  710. function Vector4(x, y, z, w) {
  711. this.x = x;
  712. this.y = y;
  713. this.z = z;
  714. this.w = w;
  715. }
  716. Vector4.prototype.toString = function ()
  717. { return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "W:" + this.w + "}"; };
  718. Vector4.prototype.asArray = function () {
  719. var result = [];
  720. this.toArray(result, 0);
  721. return result;
  722. };
  723. Vector4.prototype.toArray = function (array, index) {
  724. if (index === undefined) {
  725. index = 0;
  726. }
  727. array[index] = this.x;
  728. array[index + 1] = this.y;
  729. array[index + 2] = this.z;
  730. array[index + 3] = this.w;
  731. };
  732. Vector4.prototype.addInPlace = function (otherVector) {
  733. this.x += otherVector.x;
  734. this.y += otherVector.y;
  735. this.z += otherVector.z;
  736. this.w += otherVector.w;
  737. };
  738. Vector4.prototype.add = function (otherVector) {
  739. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  740. };
  741. Vector4.prototype.addToRef = function (otherVector, result) {
  742. result.x = this.x + otherVector.x;
  743. result.y = this.y + otherVector.y;
  744. result.z = this.z + otherVector.z;
  745. result.w = this.w + otherVector.w;
  746. };
  747. Vector4.prototype.subtractInPlace = function (otherVector) {
  748. this.x -= otherVector.x;
  749. this.y -= otherVector.y;
  750. this.z -= otherVector.z;
  751. this.w -= otherVector.w;
  752. };
  753. Vector4.prototype.subtract = function (otherVector)
  754. { return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w); };
  755. Vector4.prototype.subtractToRef = function (otherVector, result) {
  756. result.x = this.x - otherVector.x;
  757. result.y = this.y - otherVector.y;
  758. result.z = this.z - otherVector.z;
  759. result.w = this.w - otherVector.w;
  760. };
  761. Vector4.prototype.subtractFromFloats = function (x, y, z, w)
  762. { return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w); };
  763. Vector4.prototype.subtractFromFloatsToRef = function (x, y, z, w, result) {
  764. result.x = this.x - x;
  765. result.y = this.y - y;
  766. result.z = this.z - z;
  767. result.w = this.w - w;
  768. };
  769. Vector4.prototype.negate = function ()
  770. { return new Vector4(-this.x, -this.y, -this.z, -this.w); };
  771. Vector4.prototype.scaleInPlace = function (scale) {
  772. this.x *= scale;
  773. this.y *= scale;
  774. this.z *= scale;
  775. this.w *= scale;
  776. return this;
  777. };
  778. Vector4.prototype.scale = function (scale)
  779. { return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale); };
  780. Vector4.prototype.scaleToRef = function (scale, result) {
  781. result.x = this.x * scale;
  782. result.y = this.y * scale;
  783. result.z = this.z * scale;
  784. result.w = this.w * scale;
  785. };
  786. Vector4.prototype.equals = function (otherVector)
  787. { return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w; };
  788. Vector4.prototype.equalsWithEpsilon = function (otherVector) {
  789. return Math.abs(this.x - otherVector.x) < Engine.Epsilon &&
  790. Math.abs(this.y - otherVector.y) < Engine.Epsilon &&
  791. Math.abs(this.z - otherVector.z) < Engine.Epsilon &&
  792. Math.abs(this.w - otherVector.w) < Engine.Epsilon;
  793. };
  794. Vector4.prototype.equalsToFloats = function (x, y, z, w)
  795. { return this.x === x && this.y === y && this.z === z && this.w === w; };
  796. Vector4.prototype.multiplyInPlace = function (otherVector) {
  797. this.x *= otherVector.x;
  798. this.y *= otherVector.y;
  799. this.z *= otherVector.z;
  800. this.w *= otherVector.w;
  801. };
  802. Vector4.prototype.multiply = function (otherVector)
  803. { return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w); };
  804. Vector4.prototype.multiplyToRef = function (otherVector, result) {
  805. result.x = this.x * otherVector.x;
  806. result.y = this.y * otherVector.y;
  807. result.z = this.z * otherVector.z;
  808. result.w = this.w * otherVector.w;
  809. };
  810. Vector4.prototype.multiplyByFloats = function (x, y, z, w)
  811. { return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w); };
  812. Vector4.prototype.divide = function (otherVector)
  813. { return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w); };
  814. Vector4.prototype.divideToRef = function (otherVector, result) {
  815. result.x = this.x / otherVector.x;
  816. result.y = this.y / otherVector.y;
  817. result.z = this.z / otherVector.z;
  818. result.w = this.w / otherVector.w;
  819. };
  820. Vector4.prototype.minimizeInPlace = function (other) {
  821. if (other.x < this.x) this.x = other.x;
  822. if (other.y < this.y) this.y = other.y;
  823. if (other.z < this.z) this.z = other.z;
  824. if (other.w < this.w) this.w = other.w;
  825. };
  826. Vector4.prototype.MaximizeInPlace = function (other) {
  827. if (other.x > this.x) this.x = other.x;
  828. if (other.y > this.y) this.y = other.y;
  829. if (other.z > this.z) this.z = other.z;
  830. if (other.w > this.w) this.w = other.w;
  831. };
  832. Vector4.prototype.length = function ()
  833. { return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w); };
  834. Vector4.prototype.lengthSquared = function ()
  835. { return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w); };
  836. Vector4.prototype.normalize = function () {
  837. var len = this.length();
  838. if (len === 0)
  839. return this;
  840. var num = 1.0 / len;
  841. this.x *= num;
  842. this.y *= num;
  843. this.z *= num;
  844. this.w *= num;
  845. return this;
  846. };
  847. Vector4.prototype.clone = function ()
  848. { return new Vector4(this.x, this.y, this.z, this.w); };
  849. Vector4.prototype.copyFrom = function (source) {
  850. this.x = source.x;
  851. this.y = source.y;
  852. this.z = source.z;
  853. this.w = source.w;
  854. };
  855. Vector4.prototype.copyFromFloats = function (x, y, z, w) {
  856. this.x = x;
  857. this.y = y;
  858. this.z = z;
  859. this.w = w;
  860. };
  861. Vector4.FromArray = function (array, offset) {
  862. if (!offset) { offset = 0; }
  863. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  864. };
  865. Vector4.FromArrayToRef = function (array, offset, result) {
  866. result.x = array[offset];
  867. result.y = array[offset + 1];
  868. result.z = array[offset + 2];
  869. result.w = array[offset + 3];
  870. };
  871. Vector4.FromFloatArrayToRef = function (array, offset, result) {
  872. result.x = array[offset];
  873. result.y = array[offset + 1];
  874. result.z = array[offset + 2];
  875. result.w = array[offset + 3];
  876. };
  877. Vector4.FromFloatsToRef = function (x, y, z, w, result) {
  878. result.x = x;
  879. result.y = y;
  880. result.z = z;
  881. result.w = w;
  882. };
  883. Vector4.Zero = function () { return new Vector4(0, 0, 0, 0); };
  884. Vector4.Normalize = function (vector) {
  885. var result = Vector4.Zero();
  886. Vector4.NormalizeToRef(vector, result);
  887. return result;
  888. };
  889. Vector4.NormalizeToRef = function (vector, result) {
  890. result.copyFrom(vector);
  891. result.normalize();
  892. };
  893. Vector4.Minimize = function (left, right) {
  894. var min = left.clone();
  895. min.MinimizeInPlace(right);
  896. return min;
  897. };
  898. Vector4.Maximize = function (left, right) {
  899. var max = left.clone();
  900. max.MaximizeInPlace(right);
  901. return max;
  902. };
  903. Vector4.Distance = function (value1, value2) { return Math.sqrt(Vector4.DistanceSquared(value1, value2)); };
  904. Vector4.DistanceSquared = function (value1, value2) {
  905. var x = value1.x - value2.x;
  906. var y = value1.y - value2.y;
  907. var z = value1.z - value2.z;
  908. var w = value1.w - value2.w;
  909. return (x * x) + (y * y) + (z * z) + (w * w);
  910. };
  911. Vector4.Center = function (value1, value2) {
  912. var center = value1.add(value2);
  913. center.scaleInPlace(0.5);
  914. return center;
  915. };
  916. return Vector4;
  917. })();
  918. BABYLON.Vector4 = Vector4;
  919. var Quaternion = (function () {
  920. function Quaternion(x, y, z, w) {
  921. if (typeof x === "undefined") { x = 0; }
  922. if (typeof y === "undefined") { y = 0; }
  923. if (typeof z === "undefined") { z = 0; }
  924. if (typeof w === "undefined") { w = 1; }
  925. this.x = x;
  926. this.y = y;
  927. this.z = z;
  928. this.w = w;
  929. }
  930. Quaternion.prototype.toString = function () {
  931. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  932. };
  933. Quaternion.prototype.asArray = function () {
  934. return [this.x, this.y, this.z, this.w];
  935. };
  936. Quaternion.prototype.equals = function (otherQuaternion) {
  937. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  938. };
  939. Quaternion.prototype.clone = function () {
  940. return new Quaternion(this.x, this.y, this.z, this.w);
  941. };
  942. Quaternion.prototype.copyFrom = function (other) {
  943. this.x = other.x;
  944. this.y = other.y;
  945. this.z = other.z;
  946. this.w = other.w;
  947. };
  948. Quaternion.prototype.copyFromFloats = function (x, y, z, w) {
  949. this.x = x;
  950. this.y = y;
  951. this.z = z;
  952. this.w = w;
  953. };
  954. Quaternion.prototype.add = function (other) {
  955. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  956. };
  957. Quaternion.prototype.subtract = function (other) {
  958. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  959. };
  960. Quaternion.prototype.scale = function (value) {
  961. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  962. };
  963. Quaternion.prototype.multiply = function (q1) {
  964. var result = new Quaternion(0, 0, 0, 1.0);
  965. this.multiplyToRef(q1, result);
  966. return result;
  967. };
  968. Quaternion.prototype.multiplyToRef = function (q1, result) {
  969. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  970. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  971. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  972. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  973. };
  974. Quaternion.prototype.length = function () {
  975. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  976. };
  977. Quaternion.prototype.normalize = function () {
  978. var length = 1.0 / this.length();
  979. this.x *= length;
  980. this.y *= length;
  981. this.z *= length;
  982. this.w *= length;
  983. };
  984. Quaternion.prototype.toEulerAngles = function () {
  985. var result = Vector3.Zero();
  986. this.toEulerAnglesToRef(result);
  987. return result;
  988. };
  989. Quaternion.prototype.toEulerAnglesToRef = function (result) {
  990. //result is an EulerAngles in the in the z-x-z convention
  991. var qx = this.x;
  992. var qy = this.y;
  993. var qz = this.z;
  994. var qw = this.w;
  995. var qxy = qx * qy;
  996. var qxz = qx * qz;
  997. var qwy = qw * qy;
  998. var qwz = qw * qz;
  999. var qwx = qw * qx;
  1000. var qyz = qy * qz;
  1001. var sqx = qx * qx;
  1002. var sqy = qy * qy;
  1003. var determinant = sqx + sqy;
  1004. if (determinant != 0.000 && determinant != 1.000) {
  1005. result.x = Math.atan2(qxz + qwy, qwx - qyz);
  1006. result.y = Math.acos(1 - 2 * determinant);
  1007. result.z = Math.atan2(qxz - qwy, qwx + qyz);
  1008. }
  1009. else
  1010. if (determinant == 0.000) {
  1011. result.x = 0.0;
  1012. result.y = 0.0;
  1013. result.z = Math.atan2(qxy - qwz, 0.5 - sqy - qz * qz); //actually, degeneracy gives us choice with x+z=Math.atan2(qxy-qwz,0.5-sqy-qz*qz)
  1014. }
  1015. else //determinant == 1.000
  1016. {
  1017. result.x = Math.atan2(qxy - qwz, 0.5 - sqy - qz * qz); //actually, degeneracy gives us choice with x-z=Math.atan2(qxy-qwz,0.5-sqy-qz*qz)
  1018. result.y = Math.PI;
  1019. result.z = 0.0;
  1020. }
  1021. };
  1022. Quaternion.prototype.toRotationMatrix = function (result) {
  1023. var xx = this.x * this.x;
  1024. var yy = this.y * this.y;
  1025. var zz = this.z * this.z;
  1026. var xy = this.x * this.y;
  1027. var zw = this.z * this.w;
  1028. var zx = this.z * this.x;
  1029. var yw = this.y * this.w;
  1030. var yz = this.y * this.z;
  1031. var xw = this.x * this.w;
  1032. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1033. result.m[1] = 2.0 * (xy + zw);
  1034. result.m[2] = 2.0 * (zx - yw);
  1035. result.m[3] = 0;
  1036. result.m[4] = 2.0 * (xy - zw);
  1037. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1038. result.m[6] = 2.0 * (yz + xw);
  1039. result.m[7] = 0;
  1040. result.m[8] = 2.0 * (zx + yw);
  1041. result.m[9] = 2.0 * (yz - xw);
  1042. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1043. result.m[11] = 0;
  1044. result.m[12] = 0;
  1045. result.m[13] = 0;
  1046. result.m[14] = 0;
  1047. result.m[15] = 1.0;
  1048. };
  1049. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  1050. var data = matrix.m;
  1051. var m11 = data[0], m12 = data[4], m13 = data[8];
  1052. var m21 = data[1], m22 = data[5], m23 = data[9];
  1053. var m31 = data[2], m32 = data[6], m33 = data[10];
  1054. var trace = m11 + m22 + m33;
  1055. var s;
  1056. if (trace > 0) {
  1057. s = 0.5 / Math.sqrt(trace + 1.0);
  1058. this.w = 0.25 / s;
  1059. this.x = (m32 - m23) * s;
  1060. this.y = (m13 - m31) * s;
  1061. this.z = (m21 - m12) * s;
  1062. return;
  1063. }
  1064. if (m11 > m22 && m11 > m33) {
  1065. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1066. this.w = (m32 - m23) / s;
  1067. this.x = 0.25 * s;
  1068. this.y = (m12 + m21) / s;
  1069. this.z = (m13 + m31) / s;
  1070. return;
  1071. }
  1072. if (m22 > m33) {
  1073. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1074. this.w = (m13 - m31) / s;
  1075. this.x = (m12 + m21) / s;
  1076. this.y = 0.25 * s;
  1077. this.z = (m23 + m32) / s;
  1078. return;
  1079. }
  1080. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1081. this.w = (m21 - m12) / s;
  1082. this.x = (m13 + m31) / s;
  1083. this.y = (m23 + m32) / s;
  1084. this.z = 0.25 * s;
  1085. };
  1086. // Statics
  1087. Quaternion.Inverse = function (q) {
  1088. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1089. };
  1090. Quaternion.RotationAxis = function (axis, angle) {
  1091. var result = new Quaternion();
  1092. var sin = Math.sin(angle / 2);
  1093. result.w = Math.cos(angle / 2);
  1094. result.x = axis.x * sin;
  1095. result.y = axis.y * sin;
  1096. result.z = axis.z * sin;
  1097. return result;
  1098. };
  1099. Quaternion.FromArray = function (array, offset) {
  1100. if (!offset) {
  1101. offset = 0;
  1102. }
  1103. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1104. };
  1105. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1106. var result = new Quaternion();
  1107. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1108. return result;
  1109. };
  1110. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1111. var halfRoll = roll * 0.5;
  1112. var halfPitch = pitch * 0.5;
  1113. var halfYaw = yaw * 0.5;
  1114. var sinRoll = Math.sin(halfRoll);
  1115. var cosRoll = Math.cos(halfRoll);
  1116. var sinPitch = Math.sin(halfPitch);
  1117. var cosPitch = Math.cos(halfPitch);
  1118. var sinYaw = Math.sin(halfYaw);
  1119. var cosYaw = Math.cos(halfYaw);
  1120. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1121. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1122. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1123. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1124. };
  1125. Quaternion.Slerp = function (left, right, amount) {
  1126. var num2;
  1127. var num3;
  1128. var num = amount;
  1129. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1130. var flag = false;
  1131. if (num4 < 0) {
  1132. flag = true;
  1133. num4 = -num4;
  1134. }
  1135. if (num4 > 0.999999) {
  1136. num3 = 1 - num;
  1137. num2 = flag ? -num : num;
  1138. } else {
  1139. var num5 = Math.acos(num4);
  1140. var num6 = (1.0 / Math.sin(num5));
  1141. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1142. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1143. }
  1144. 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));
  1145. };
  1146. return Quaternion;
  1147. })();
  1148. BABYLON.Quaternion = Quaternion;
  1149. var EulerAngles = (function () {
  1150. function EulerAngles(x, y, z) {
  1151. if (typeof x === "undefined") { x = 0; }
  1152. if (typeof y === "undefined") { y = 0; }
  1153. if (typeof z === "undefined") { z = 0; }
  1154. this.x = x;
  1155. this.y = y;
  1156. this.z = z;
  1157. }
  1158. EulerAngles.prototype.toString = function () {
  1159. return "{x: " + this.x + " y:" + this.y + " z:" + this.z + "}";
  1160. };
  1161. EulerAngles.prototype.asArray = function () {
  1162. return [this.x, this.y, this.z];
  1163. };
  1164. EulerAngles.prototype.equals = function (otherEulerAngles) {
  1165. return otherEulerAngles && this.x === otherEulerAngles.x && this.y === otherEulerAngles.y && this.z === otherEulerAngles.z;
  1166. };
  1167. EulerAngles.prototype.clone = function () {
  1168. return new EulerAngles(this.x, this.y, this.z);
  1169. };
  1170. EulerAngles.prototype.copyFrom = function (other) {
  1171. this.x = other.x;
  1172. this.y = other.y;
  1173. this.z = other.z;
  1174. };
  1175. EulerAngles.prototype.copyFromFloats = function (x, y, z) {
  1176. this.x = x;
  1177. this.y = y;
  1178. this.z = z;
  1179. };
  1180. EulerAngles.prototype.add = function (other) {
  1181. return new EulerAngles(this.x + other.x, this.y + other.y, this.z + other.z);
  1182. };
  1183. EulerAngles.prototype.subtract = function (other) {
  1184. return new EulerAngles(this.x - other.x, this.y - other.y, this.z - other.z);
  1185. };
  1186. EulerAngles.prototype.scale = function (value) {
  1187. return new EulerAngles(this.x * value, this.y * value, this.z * value);
  1188. };
  1189. EulerAngles.prototype.length = function () {
  1190. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z));
  1191. };
  1192. EulerAngles.prototype.normalize = function () {
  1193. var length = 1.0 / this.length();
  1194. this.x *= length;
  1195. this.y *= length;
  1196. this.z *= length;
  1197. };
  1198. EulerAngles.prototype.toQuaternion = function () {
  1199. var result;
  1200. //result is a Quaternion in the z-x-z rotation convention
  1201. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  1202. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  1203. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  1204. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  1205. var cosy = Math.cos(this.y * 0.5);
  1206. var siny = Math.sin(this.y * 0.5);
  1207. result.x = coszMinusx * siny;
  1208. result.y = -sinzMinusx * siny;
  1209. result.z = sinxPlusz * cosy;
  1210. result.w = cosxPlusz * cosy;
  1211. return result;
  1212. };
  1213. EulerAngles.prototype.toRotationMatrix = function (result) {
  1214. //returns matrix with result.m[0]=m11,result.m[1]=m21,result.m[2]=m31,result.m[4]=12, etc
  1215. //done in the z-x-z rotation convention
  1216. var cosx = Math.cos(this.x);
  1217. var sinx = Math.sin(this.x);
  1218. var cosy = Math.cos(this.y);
  1219. var siny = Math.sin(this.y);
  1220. var cosz = Math.cos(this.z);
  1221. var sinz = Math.sin(this.z);
  1222. result.m[0] = cosx * cosz - cosy * sinx * sinz;
  1223. result.m[1] = cosy * sinx * cosz + cosx * sinz;
  1224. result.m[2] = siny * sinx;
  1225. result.m[4] = -sinx * cosz - cosy * cosx * sinz;
  1226. result.m[5] = cosy * cosx * cosz - sinx * sinz;
  1227. result.m[6] = siny * cosx;
  1228. result.m[8] = siny * sinz;
  1229. result.m[9] = -siny * cosz;
  1230. result.m[10] = cosy;
  1231. };
  1232. EulerAngles.prototype.fromRotationMatrix = function (matrix) {
  1233. var data = matrix.m;
  1234. var m11 = data[0], m12 = data[4], m13 = data[8];
  1235. var m21 = data[1], m22 = data[5], m23 = data[9];
  1236. var m31 = data[2], m32 = data[6], m33 = data[10];
  1237. if (m33 == -1) {
  1238. this.x = 0; //any angle works here
  1239. this.y = Math.PI;
  1240. this.z = Math.atan2(m21, m11); //generally, atan2(m21,m11)-x
  1241. }
  1242. else
  1243. if (m33 == 1) {
  1244. this.x = 0; //any angle works here
  1245. this.y = 0;
  1246. this.z = Math.atan2(m21, m11); //generally, atan2(m21,m11)-x
  1247. }
  1248. else {
  1249. this.x = Math.atan2(m31, m32);
  1250. this.y = Math.acos(m33); //principal value (between 0 and PI)
  1251. this.z = Math.atan2(m13, -m23);
  1252. }
  1253. };
  1254. return EulerAngles;
  1255. })();
  1256. BABYLON.EulerAngles = EulerAngles;
  1257. var Matrix = (function () {
  1258. function Matrix() {
  1259. this.m = new Float32Array(16);
  1260. }
  1261. // Properties
  1262. Matrix.prototype.isIdentity = function () {
  1263. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  1264. return false;
  1265. 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)
  1266. return false;
  1267. return true;
  1268. };
  1269. Matrix.prototype.determinant = function () {
  1270. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1271. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1272. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1273. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1274. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1275. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1276. 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))));
  1277. };
  1278. // Methods
  1279. Matrix.prototype.toArray = function () {
  1280. return this.m;
  1281. };
  1282. Matrix.prototype.asArray = function () {
  1283. return this.toArray();
  1284. };
  1285. Matrix.prototype.invert = function () {
  1286. this.invertToRef(this);
  1287. };
  1288. Matrix.prototype.invertToRef = function (other) {
  1289. var l1 = this.m[0];
  1290. var l2 = this.m[1];
  1291. var l3 = this.m[2];
  1292. var l4 = this.m[3];
  1293. var l5 = this.m[4];
  1294. var l6 = this.m[5];
  1295. var l7 = this.m[6];
  1296. var l8 = this.m[7];
  1297. var l9 = this.m[8];
  1298. var l10 = this.m[9];
  1299. var l11 = this.m[10];
  1300. var l12 = this.m[11];
  1301. var l13 = this.m[12];
  1302. var l14 = this.m[13];
  1303. var l15 = this.m[14];
  1304. var l16 = this.m[15];
  1305. var l17 = (l11 * l16) - (l12 * l15);
  1306. var l18 = (l10 * l16) - (l12 * l14);
  1307. var l19 = (l10 * l15) - (l11 * l14);
  1308. var l20 = (l9 * l16) - (l12 * l13);
  1309. var l21 = (l9 * l15) - (l11 * l13);
  1310. var l22 = (l9 * l14) - (l10 * l13);
  1311. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1312. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1313. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1314. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1315. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1316. var l28 = (l7 * l16) - (l8 * l15);
  1317. var l29 = (l6 * l16) - (l8 * l14);
  1318. var l30 = (l6 * l15) - (l7 * l14);
  1319. var l31 = (l5 * l16) - (l8 * l13);
  1320. var l32 = (l5 * l15) - (l7 * l13);
  1321. var l33 = (l5 * l14) - (l6 * l13);
  1322. var l34 = (l7 * l12) - (l8 * l11);
  1323. var l35 = (l6 * l12) - (l8 * l10);
  1324. var l36 = (l6 * l11) - (l7 * l10);
  1325. var l37 = (l5 * l12) - (l8 * l9);
  1326. var l38 = (l5 * l11) - (l7 * l9);
  1327. var l39 = (l5 * l10) - (l6 * l9);
  1328. other.m[0] = l23 * l27;
  1329. other.m[4] = l24 * l27;
  1330. other.m[8] = l25 * l27;
  1331. other.m[12] = l26 * l27;
  1332. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1333. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1334. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1335. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1336. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1337. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1338. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1339. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1340. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1341. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1342. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1343. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1344. };
  1345. Matrix.prototype.setTranslation = function (vector3) {
  1346. this.m[12] = vector3.x;
  1347. this.m[13] = vector3.y;
  1348. this.m[14] = vector3.z;
  1349. };
  1350. Matrix.prototype.multiply = function (other) {
  1351. var result = new Matrix();
  1352. this.multiplyToRef(other, result);
  1353. return result;
  1354. };
  1355. Matrix.prototype.copyFrom = function (other) {
  1356. for (var index = 0; index < 16; index++) {
  1357. this.m[index] = other.m[index];
  1358. }
  1359. };
  1360. Matrix.prototype.copyToArray = function (array, offset) {
  1361. if (typeof offset === "undefined") { offset = 0; }
  1362. for (var index = 0; index < 16; index++) {
  1363. array[offset + index] = this.m[index];
  1364. }
  1365. };
  1366. Matrix.prototype.multiplyToRef = function (other, result) {
  1367. this.multiplyToArray(other, result.m, 0);
  1368. };
  1369. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1370. var tm0 = this.m[0];
  1371. var tm1 = this.m[1];
  1372. var tm2 = this.m[2];
  1373. var tm3 = this.m[3];
  1374. var tm4 = this.m[4];
  1375. var tm5 = this.m[5];
  1376. var tm6 = this.m[6];
  1377. var tm7 = this.m[7];
  1378. var tm8 = this.m[8];
  1379. var tm9 = this.m[9];
  1380. var tm10 = this.m[10];
  1381. var tm11 = this.m[11];
  1382. var tm12 = this.m[12];
  1383. var tm13 = this.m[13];
  1384. var tm14 = this.m[14];
  1385. var tm15 = this.m[15];
  1386. var om0 = other.m[0];
  1387. var om1 = other.m[1];
  1388. var om2 = other.m[2];
  1389. var om3 = other.m[3];
  1390. var om4 = other.m[4];
  1391. var om5 = other.m[5];
  1392. var om6 = other.m[6];
  1393. var om7 = other.m[7];
  1394. var om8 = other.m[8];
  1395. var om9 = other.m[9];
  1396. var om10 = other.m[10];
  1397. var om11 = other.m[11];
  1398. var om12 = other.m[12];
  1399. var om13 = other.m[13];
  1400. var om14 = other.m[14];
  1401. var om15 = other.m[15];
  1402. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1403. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1404. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1405. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1406. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1407. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1408. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1409. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1410. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1411. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1412. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1413. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1414. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1415. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1416. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1417. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1418. };
  1419. Matrix.prototype.equals = function (value) {
  1420. 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]);
  1421. };
  1422. Matrix.prototype.clone = function () {
  1423. 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]);
  1424. };
  1425. // Statics
  1426. Matrix.FromArray = function (array, offset) {
  1427. var result = new Matrix();
  1428. if (!offset) {
  1429. offset = 0;
  1430. }
  1431. Matrix.FromArrayToRef(array, offset, result);
  1432. return result;
  1433. };
  1434. Matrix.FromArrayToRef = function (array, offset, result) {
  1435. for (var index = 0; index < 16; index++) {
  1436. result.m[index] = array[index + offset];
  1437. }
  1438. };
  1439. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1440. result.m[0] = initialM11;
  1441. result.m[1] = initialM12;
  1442. result.m[2] = initialM13;
  1443. result.m[3] = initialM14;
  1444. result.m[4] = initialM21;
  1445. result.m[5] = initialM22;
  1446. result.m[6] = initialM23;
  1447. result.m[7] = initialM24;
  1448. result.m[8] = initialM31;
  1449. result.m[9] = initialM32;
  1450. result.m[10] = initialM33;
  1451. result.m[11] = initialM34;
  1452. result.m[12] = initialM41;
  1453. result.m[13] = initialM42;
  1454. result.m[14] = initialM43;
  1455. result.m[15] = initialM44;
  1456. };
  1457. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  1458. var result = new Matrix();
  1459. result.m[0] = initialM11;
  1460. result.m[1] = initialM12;
  1461. result.m[2] = initialM13;
  1462. result.m[3] = initialM14;
  1463. result.m[4] = initialM21;
  1464. result.m[5] = initialM22;
  1465. result.m[6] = initialM23;
  1466. result.m[7] = initialM24;
  1467. result.m[8] = initialM31;
  1468. result.m[9] = initialM32;
  1469. result.m[10] = initialM33;
  1470. result.m[11] = initialM34;
  1471. result.m[12] = initialM41;
  1472. result.m[13] = initialM42;
  1473. result.m[14] = initialM43;
  1474. result.m[15] = initialM44;
  1475. return result;
  1476. };
  1477. Matrix.Identity = function () {
  1478. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  1479. };
  1480. Matrix.IdentityToRef = function (result) {
  1481. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  1482. };
  1483. Matrix.Zero = function () {
  1484. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1485. };
  1486. Matrix.RotationX = function (angle) {
  1487. var result = new Matrix();
  1488. Matrix.RotationXToRef(angle, result);
  1489. return result;
  1490. };
  1491. Matrix.RotationXToRef = function (angle, result) {
  1492. var s = Math.sin(angle);
  1493. var c = Math.cos(angle);
  1494. result.m[0] = 1.0;
  1495. result.m[15] = 1.0;
  1496. result.m[5] = c;
  1497. result.m[10] = c;
  1498. result.m[9] = -s;
  1499. result.m[6] = s;
  1500. result.m[1] = 0;
  1501. result.m[2] = 0;
  1502. result.m[3] = 0;
  1503. result.m[4] = 0;
  1504. result.m[7] = 0;
  1505. result.m[8] = 0;
  1506. result.m[11] = 0;
  1507. result.m[12] = 0;
  1508. result.m[13] = 0;
  1509. result.m[14] = 0;
  1510. };
  1511. Matrix.RotationY = function (angle) {
  1512. var result = new Matrix();
  1513. Matrix.RotationYToRef(angle, result);
  1514. return result;
  1515. };
  1516. Matrix.RotationYToRef = function (angle, result) {
  1517. var s = Math.sin(angle);
  1518. var c = Math.cos(angle);
  1519. result.m[5] = 1.0;
  1520. result.m[15] = 1.0;
  1521. result.m[0] = c;
  1522. result.m[2] = -s;
  1523. result.m[8] = s;
  1524. result.m[10] = c;
  1525. result.m[1] = 0;
  1526. result.m[3] = 0;
  1527. result.m[4] = 0;
  1528. result.m[6] = 0;
  1529. result.m[7] = 0;
  1530. result.m[9] = 0;
  1531. result.m[11] = 0;
  1532. result.m[12] = 0;
  1533. result.m[13] = 0;
  1534. result.m[14] = 0;
  1535. };
  1536. Matrix.RotationZ = function (angle) {
  1537. var result = new Matrix();
  1538. Matrix.RotationZToRef(angle, result);
  1539. return result;
  1540. };
  1541. Matrix.RotationZToRef = function (angle, result) {
  1542. var s = Math.sin(angle);
  1543. var c = Math.cos(angle);
  1544. result.m[10] = 1.0;
  1545. result.m[15] = 1.0;
  1546. result.m[0] = c;
  1547. result.m[1] = s;
  1548. result.m[4] = -s;
  1549. result.m[5] = c;
  1550. result.m[2] = 0;
  1551. result.m[3] = 0;
  1552. result.m[6] = 0;
  1553. result.m[7] = 0;
  1554. result.m[8] = 0;
  1555. result.m[9] = 0;
  1556. result.m[11] = 0;
  1557. result.m[12] = 0;
  1558. result.m[13] = 0;
  1559. result.m[14] = 0;
  1560. };
  1561. Matrix.RotationAxis = function (axis, angle) {
  1562. var s = Math.sin(-angle);
  1563. var c = Math.cos(-angle);
  1564. var c1 = 1 - c;
  1565. axis.normalize();
  1566. var result = Matrix.Zero();
  1567. result.m[0] = (axis.x * axis.x) * c1 + c;
  1568. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1569. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1570. result.m[3] = 0.0;
  1571. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1572. result.m[5] = (axis.y * axis.y) * c1 + c;
  1573. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1574. result.m[7] = 0.0;
  1575. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1576. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1577. result.m[10] = (axis.z * axis.z) * c1 + c;
  1578. result.m[11] = 0.0;
  1579. result.m[15] = 1.0;
  1580. return result;
  1581. };
  1582. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1583. var result = new Matrix();
  1584. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1585. return result;
  1586. };
  1587. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1588. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1589. this._tempQuaternion.toRotationMatrix(result);
  1590. };
  1591. Matrix.Scaling = function (x, y, z) {
  1592. var result = Matrix.Zero();
  1593. Matrix.ScalingToRef(x, y, z, result);
  1594. return result;
  1595. };
  1596. Matrix.ScalingToRef = function (x, y, z, result) {
  1597. result.m[0] = x;
  1598. result.m[1] = 0;
  1599. result.m[2] = 0;
  1600. result.m[3] = 0;
  1601. result.m[4] = 0;
  1602. result.m[5] = y;
  1603. result.m[6] = 0;
  1604. result.m[7] = 0;
  1605. result.m[8] = 0;
  1606. result.m[9] = 0;
  1607. result.m[10] = z;
  1608. result.m[11] = 0;
  1609. result.m[12] = 0;
  1610. result.m[13] = 0;
  1611. result.m[14] = 0;
  1612. result.m[15] = 1.0;
  1613. };
  1614. Matrix.Translation = function (x, y, z) {
  1615. var result = Matrix.Identity();
  1616. Matrix.TranslationToRef(x, y, z, result);
  1617. return result;
  1618. };
  1619. Matrix.TranslationToRef = function (x, y, z, result) {
  1620. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  1621. };
  1622. Matrix.LookAtLH = function (eye, target, up) {
  1623. var result = Matrix.Zero();
  1624. Matrix.LookAtLHToRef(eye, target, up, result);
  1625. return result;
  1626. };
  1627. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1628. // Z axis
  1629. target.subtractToRef(eye, this._zAxis);
  1630. this._zAxis.normalize();
  1631. // X axis
  1632. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1633. this._xAxis.normalize();
  1634. // Y axis
  1635. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1636. this._yAxis.normalize();
  1637. // Eye angles
  1638. var ex = -Vector3.Dot(this._xAxis, eye);
  1639. var ey = -Vector3.Dot(this._yAxis, eye);
  1640. var ez = -Vector3.Dot(this._zAxis, eye);
  1641. 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);
  1642. };
  1643. Matrix.OrthoLH = function (width, height, znear, zfar) {
  1644. var hw = 2.0 / width;
  1645. var hh = 2.0 / height;
  1646. var id = 1.0 / (zfar - znear);
  1647. var nid = znear / (znear - zfar);
  1648. return Matrix.FromValues(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1);
  1649. };
  1650. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1651. var matrix = Matrix.Zero();
  1652. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1653. return matrix;
  1654. };
  1655. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1656. result.m[0] = 2.0 / (right - left);
  1657. result.m[1] = result.m[2] = result.m[3] = 0;
  1658. result.m[5] = 2.0 / (top - bottom);
  1659. result.m[4] = result.m[6] = result.m[7] = 0;
  1660. result.m[10] = -1.0 / (znear - zfar);
  1661. result.m[8] = result.m[9] = result.m[11] = 0;
  1662. result.m[12] = (left + right) / (left - right);
  1663. result.m[13] = (top + bottom) / (bottom - top);
  1664. result.m[14] = znear / (znear - zfar);
  1665. result.m[15] = 1.0;
  1666. };
  1667. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1668. var matrix = Matrix.Zero();
  1669. matrix.m[0] = (2.0 * znear) / width;
  1670. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1671. matrix.m[5] = (2.0 * znear) / height;
  1672. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1673. matrix.m[10] = -zfar / (znear - zfar);
  1674. matrix.m[8] = matrix.m[9] = 0.0;
  1675. matrix.m[11] = 1.0;
  1676. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1677. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1678. return matrix;
  1679. };
  1680. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1681. var matrix = Matrix.Zero();
  1682. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1683. return matrix;
  1684. };
  1685. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result) {
  1686. var tan = 1.0 / (Math.tan(fov * 0.5));
  1687. result.m[0] = tan / aspect;
  1688. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1689. result.m[5] = tan;
  1690. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1691. result.m[8] = result.m[9] = 0.0;
  1692. result.m[10] = -zfar / (znear - zfar);
  1693. result.m[11] = 1.0;
  1694. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1695. result.m[14] = (znear * zfar) / (znear - zfar);
  1696. };
  1697. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  1698. var cw = viewport.width;
  1699. var ch = viewport.height;
  1700. var cx = viewport.x;
  1701. var cy = viewport.y;
  1702. 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);
  1703. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1704. };
  1705. Matrix.Transpose = function (matrix) {
  1706. var result = new Matrix();
  1707. result.m[0] = matrix.m[0];
  1708. result.m[1] = matrix.m[4];
  1709. result.m[2] = matrix.m[8];
  1710. result.m[3] = matrix.m[12];
  1711. result.m[4] = matrix.m[1];
  1712. result.m[5] = matrix.m[5];
  1713. result.m[6] = matrix.m[9];
  1714. result.m[7] = matrix.m[13];
  1715. result.m[8] = matrix.m[2];
  1716. result.m[9] = matrix.m[6];
  1717. result.m[10] = matrix.m[10];
  1718. result.m[11] = matrix.m[14];
  1719. result.m[12] = matrix.m[3];
  1720. result.m[13] = matrix.m[7];
  1721. result.m[14] = matrix.m[11];
  1722. result.m[15] = matrix.m[15];
  1723. return result;
  1724. };
  1725. Matrix.Reflection = function (plane) {
  1726. var matrix = new Matrix();
  1727. Matrix.ReflectionToRef(plane, matrix);
  1728. return matrix;
  1729. };
  1730. Matrix.ReflectionToRef = function (plane, result) {
  1731. plane.normalize();
  1732. var x = plane.normal.x;
  1733. var y = plane.normal.y;
  1734. var z = plane.normal.z;
  1735. var temp = -2 * x;
  1736. var temp2 = -2 * y;
  1737. var temp3 = -2 * z;
  1738. result.m[0] = (temp * x) + 1;
  1739. result.m[1] = temp2 * x;
  1740. result.m[2] = temp3 * x;
  1741. result.m[3] = 0.0;
  1742. result.m[4] = temp * y;
  1743. result.m[5] = (temp2 * y) + 1;
  1744. result.m[6] = temp3 * y;
  1745. result.m[7] = 0.0;
  1746. result.m[8] = temp * z;
  1747. result.m[9] = temp2 * z;
  1748. result.m[10] = (temp3 * z) + 1;
  1749. result.m[11] = 0.0;
  1750. result.m[12] = temp * plane.d;
  1751. result.m[13] = temp2 * plane.d;
  1752. result.m[14] = temp3 * plane.d;
  1753. result.m[15] = 1.0;
  1754. };
  1755. Matrix._tempQuaternion = new Quaternion();
  1756. Matrix._xAxis = Vector3.Zero();
  1757. Matrix._yAxis = Vector3.Zero();
  1758. Matrix._zAxis = Vector3.Zero();
  1759. return Matrix;
  1760. })();
  1761. BABYLON.Matrix = Matrix;
  1762. var Plane = (function () {
  1763. function Plane(a, b, c, d) {
  1764. this.normal = new Vector3(a, b, c);
  1765. this.d = d;
  1766. }
  1767. Plane.prototype.asArray = function () {
  1768. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  1769. };
  1770. // Methods
  1771. Plane.prototype.clone = function () {
  1772. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  1773. };
  1774. Plane.prototype.normalize = function () {
  1775. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1776. var magnitude = 0;
  1777. if (norm != 0) {
  1778. magnitude = 1.0 / norm;
  1779. }
  1780. this.normal.x *= magnitude;
  1781. this.normal.y *= magnitude;
  1782. this.normal.z *= magnitude;
  1783. this.d *= magnitude;
  1784. };
  1785. Plane.prototype.transform = function (transformation) {
  1786. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1787. var x = this.normal.x;
  1788. var y = this.normal.y;
  1789. var z = this.normal.z;
  1790. var d = this.d;
  1791. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1792. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1793. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1794. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1795. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1796. };
  1797. Plane.prototype.dotCoordinate = function (point) {
  1798. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1799. };
  1800. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  1801. var x1 = point2.x - point1.x;
  1802. var y1 = point2.y - point1.y;
  1803. var z1 = point2.z - point1.z;
  1804. var x2 = point3.x - point1.x;
  1805. var y2 = point3.y - point1.y;
  1806. var z2 = point3.z - point1.z;
  1807. var yz = (y1 * z2) - (z1 * y2);
  1808. var xz = (z1 * x2) - (x1 * z2);
  1809. var xy = (x1 * y2) - (y1 * x2);
  1810. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1811. var invPyth;
  1812. if (pyth != 0) {
  1813. invPyth = 1.0 / pyth;
  1814. } else {
  1815. invPyth = 0;
  1816. }
  1817. this.normal.x = yz * invPyth;
  1818. this.normal.y = xz * invPyth;
  1819. this.normal.z = xy * invPyth;
  1820. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1821. };
  1822. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  1823. var dot = Vector3.Dot(this.normal, direction);
  1824. return (dot <= epsilon);
  1825. };
  1826. Plane.prototype.signedDistanceTo = function (point) {
  1827. return Vector3.Dot(point, this.normal) + this.d;
  1828. };
  1829. // Statics
  1830. Plane.FromArray = function (array) {
  1831. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1832. };
  1833. Plane.FromPoints = function (point1, point2, point3) {
  1834. var result = new BABYLON.Plane(0, 0, 0, 0);
  1835. result.copyFromPoints(point1, point2, point3);
  1836. return result;
  1837. };
  1838. Plane.FromPositionAndNormal = function (origin, normal) {
  1839. var result = new BABYLON.Plane(0, 0, 0, 0);
  1840. normal.normalize();
  1841. result.normal = normal;
  1842. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1843. return result;
  1844. };
  1845. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  1846. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1847. return Vector3.Dot(point, normal) + d;
  1848. };
  1849. return Plane;
  1850. })();
  1851. BABYLON.Plane = Plane;
  1852. var Viewport = (function () {
  1853. function Viewport(x, y, width, height) {
  1854. this.x = x;
  1855. this.y = y;
  1856. this.width = width;
  1857. this.height = height;
  1858. }
  1859. Viewport.prototype.toGlobal = function (engine) {
  1860. var width = engine.getRenderWidth();
  1861. var height = engine.getRenderHeight();
  1862. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1863. };
  1864. return Viewport;
  1865. })();
  1866. BABYLON.Viewport = Viewport;
  1867. var Frustum = (function () {
  1868. function Frustum() {
  1869. }
  1870. Frustum.GetPlanes = function (transform) {
  1871. var frustumPlanes = [];
  1872. for (var index = 0; index < 6; index++) {
  1873. frustumPlanes.push(new Plane(0, 0, 0, 0));
  1874. }
  1875. Frustum.GetPlanesToRef(transform, frustumPlanes);
  1876. return frustumPlanes;
  1877. };
  1878. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  1879. // Near
  1880. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1881. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1882. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1883. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1884. frustumPlanes[0].normalize();
  1885. // Far
  1886. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1887. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1888. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1889. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1890. frustumPlanes[1].normalize();
  1891. // Left
  1892. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1893. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1894. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1895. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1896. frustumPlanes[2].normalize();
  1897. // Right
  1898. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1899. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1900. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1901. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1902. frustumPlanes[3].normalize();
  1903. // Top
  1904. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1905. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1906. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1907. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1908. frustumPlanes[4].normalize();
  1909. // Bottom
  1910. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1911. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1912. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1913. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1914. frustumPlanes[5].normalize();
  1915. };
  1916. return Frustum;
  1917. })();
  1918. BABYLON.Frustum = Frustum;
  1919. var Ray = (function () {
  1920. function Ray(origin, direction) {
  1921. this.origin = origin;
  1922. this.direction = direction;
  1923. }
  1924. // Methods
  1925. Ray.prototype.intersectsBoxMinMax = function (minimum, maximum) {
  1926. var d = 0.0;
  1927. var maxValue = Number.MAX_VALUE;
  1928. if (Math.abs(this.direction.x) < 0.0000001) {
  1929. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  1930. return false;
  1931. }
  1932. } else {
  1933. var inv = 1.0 / this.direction.x;
  1934. var min = (minimum.x - this.origin.x) * inv;
  1935. var max = (maximum.x - this.origin.x) * inv;
  1936. if (min > max) {
  1937. var temp = min;
  1938. min = max;
  1939. max = temp;
  1940. }
  1941. d = Math.max(min, d);
  1942. maxValue = Math.min(max, maxValue);
  1943. if (d > maxValue) {
  1944. return false;
  1945. }
  1946. }
  1947. if (Math.abs(this.direction.y) < 0.0000001) {
  1948. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  1949. return false;
  1950. }
  1951. } else {
  1952. inv = 1.0 / this.direction.y;
  1953. min = (minimum.y - this.origin.y) * inv;
  1954. max = (maximum.y - this.origin.y) * inv;
  1955. if (min > max) {
  1956. temp = min;
  1957. min = max;
  1958. max = temp;
  1959. }
  1960. d = Math.max(min, d);
  1961. maxValue = Math.min(max, maxValue);
  1962. if (d > maxValue) {
  1963. return false;
  1964. }
  1965. }
  1966. if (Math.abs(this.direction.z) < 0.0000001) {
  1967. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  1968. return false;
  1969. }
  1970. } else {
  1971. inv = 1.0 / this.direction.z;
  1972. min = (minimum.z - this.origin.z) * inv;
  1973. max = (maximum.z - this.origin.z) * inv;
  1974. if (min > max) {
  1975. temp = min;
  1976. min = max;
  1977. max = temp;
  1978. }
  1979. d = Math.max(min, d);
  1980. maxValue = Math.min(max, maxValue);
  1981. if (d > maxValue) {
  1982. return false;
  1983. }
  1984. }
  1985. return true;
  1986. };
  1987. Ray.prototype.intersectsBox = function (box) {
  1988. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  1989. };
  1990. Ray.prototype.intersectsSphere = function (sphere) {
  1991. var x = sphere.center.x - this.origin.x;
  1992. var y = sphere.center.y - this.origin.y;
  1993. var z = sphere.center.z - this.origin.z;
  1994. var pyth = (x * x) + (y * y) + (z * z);
  1995. var rr = sphere.radius * sphere.radius;
  1996. if (pyth <= rr) {
  1997. return true;
  1998. }
  1999. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  2000. if (dot < 0.0) {
  2001. return false;
  2002. }
  2003. var temp = pyth - (dot * dot);
  2004. return temp <= rr;
  2005. };
  2006. Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  2007. if (!this._edge1) {
  2008. this._edge1 = BABYLON.Vector3.Zero();
  2009. this._edge2 = BABYLON.Vector3.Zero();
  2010. this._pvec = BABYLON.Vector3.Zero();
  2011. this._tvec = BABYLON.Vector3.Zero();
  2012. this._qvec = BABYLON.Vector3.Zero();
  2013. }
  2014. vertex1.subtractToRef(vertex0, this._edge1);
  2015. vertex2.subtractToRef(vertex0, this._edge2);
  2016. BABYLON.Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  2017. var det = Vector3.Dot(this._edge1, this._pvec);
  2018. if (det === 0) {
  2019. return null;
  2020. }
  2021. var invdet = 1 / det;
  2022. this.origin.subtractToRef(vertex0, this._tvec);
  2023. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  2024. if (bu < 0 || bu > 1.0) {
  2025. return null;
  2026. }
  2027. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  2028. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  2029. if (bv < 0 || bu + bv > 1.0) {
  2030. return null;
  2031. }
  2032. return new BABYLON.IntersectionInfo(bu, bv, Vector3.Dot(this._edge2, this._qvec) * invdet);
  2033. };
  2034. // Statics
  2035. Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  2036. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  2037. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  2038. var direction = end.subtract(start);
  2039. direction.normalize();
  2040. return new Ray(start, direction);
  2041. };
  2042. Ray.Transform = function (ray, matrix) {
  2043. var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
  2044. var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
  2045. return new Ray(newOrigin, newDirection);
  2046. };
  2047. return Ray;
  2048. })();
  2049. BABYLON.Ray = Ray;
  2050. (function (Space) {
  2051. Space[Space["LOCAL"] = 0] = "LOCAL";
  2052. Space[Space["WORLD"] = 1] = "WORLD";
  2053. })(BABYLON.Space || (BABYLON.Space = {}));
  2054. var Space = BABYLON.Space;
  2055. var Axis = (function () {
  2056. function Axis() {
  2057. }
  2058. Axis.X = new BABYLON.Vector3(1, 0, 0);
  2059. Axis.Y = new BABYLON.Vector3(0, 1, 0);
  2060. Axis.Z = new BABYLON.Vector3(0, 0, 1);
  2061. return Axis;
  2062. })();
  2063. BABYLON.Axis = Axis;
  2064. ;
  2065. })(BABYLON || (BABYLON = {}));
  2066. //# sourceMappingURL=babylon.math.js.map