babylon.math.js 101 KB

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