babylon.math.js 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256
  1. var BABYLON;
  2. (function (BABYLON) {
  3. BABYLON.ToGammaSpace = 1 / 2.2;
  4. BABYLON.ToLinearSpace = 2.2;
  5. BABYLON.Epsilon = 0.001;
  6. var MathTools = (function () {
  7. function MathTools() {
  8. }
  9. MathTools.WithinEpsilon = function (a, b, epsilon) {
  10. if (epsilon === void 0) { epsilon = 1.401298E-45; }
  11. var num = a - b;
  12. return -epsilon <= num && num <= epsilon;
  13. };
  14. MathTools.ToHex = function (i) {
  15. var str = i.toString(16);
  16. if (i <= 15) {
  17. return ("0" + str).toUpperCase();
  18. }
  19. return str.toUpperCase();
  20. };
  21. // Returns -1 when value is a negative number and
  22. // +1 when value is a positive number.
  23. MathTools.Sign = function (value) {
  24. value = +value; // convert to a number
  25. if (value === 0 || isNaN(value))
  26. return value;
  27. return value > 0 ? 1 : -1;
  28. };
  29. MathTools.Clamp = function (value, min, max) {
  30. if (min === void 0) { min = 0; }
  31. if (max === void 0) { max = 1; }
  32. return Math.min(max, Math.max(min, value));
  33. };
  34. return MathTools;
  35. }());
  36. BABYLON.MathTools = MathTools;
  37. var Color3 = (function () {
  38. function Color3(r, g, b) {
  39. if (r === void 0) { r = 0; }
  40. if (g === void 0) { g = 0; }
  41. if (b === void 0) { b = 0; }
  42. this.r = r;
  43. this.g = g;
  44. this.b = b;
  45. }
  46. Color3.prototype.toString = function () {
  47. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  48. };
  49. Color3.prototype.getClassName = function () {
  50. return "Color3";
  51. };
  52. Color3.prototype.getHashCode = function () {
  53. var hash = this.r || 0;
  54. hash = (hash * 397) ^ (this.g || 0);
  55. hash = (hash * 397) ^ (this.b || 0);
  56. return hash;
  57. };
  58. // Operators
  59. Color3.prototype.toArray = function (array, index) {
  60. if (index === undefined) {
  61. index = 0;
  62. }
  63. array[index] = this.r;
  64. array[index + 1] = this.g;
  65. array[index + 2] = this.b;
  66. return this;
  67. };
  68. Color3.prototype.toColor4 = function (alpha) {
  69. if (alpha === void 0) { alpha = 1; }
  70. return new Color4(this.r, this.g, this.b, alpha);
  71. };
  72. Color3.prototype.asArray = function () {
  73. var result = [];
  74. this.toArray(result, 0);
  75. return result;
  76. };
  77. Color3.prototype.toLuminance = function () {
  78. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  79. };
  80. Color3.prototype.multiply = function (otherColor) {
  81. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  82. };
  83. Color3.prototype.multiplyToRef = function (otherColor, result) {
  84. result.r = this.r * otherColor.r;
  85. result.g = this.g * otherColor.g;
  86. result.b = this.b * otherColor.b;
  87. return this;
  88. };
  89. Color3.prototype.equals = function (otherColor) {
  90. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  91. };
  92. Color3.prototype.equalsFloats = function (r, g, b) {
  93. return this.r === r && this.g === g && this.b === b;
  94. };
  95. Color3.prototype.scale = function (scale) {
  96. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  97. };
  98. Color3.prototype.scaleToRef = function (scale, result) {
  99. result.r = this.r * scale;
  100. result.g = this.g * scale;
  101. result.b = this.b * scale;
  102. return this;
  103. };
  104. Color3.prototype.add = function (otherColor) {
  105. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  106. };
  107. Color3.prototype.addToRef = function (otherColor, result) {
  108. result.r = this.r + otherColor.r;
  109. result.g = this.g + otherColor.g;
  110. result.b = this.b + otherColor.b;
  111. return this;
  112. };
  113. Color3.prototype.subtract = function (otherColor) {
  114. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  115. };
  116. Color3.prototype.subtractToRef = function (otherColor, result) {
  117. result.r = this.r - otherColor.r;
  118. result.g = this.g - otherColor.g;
  119. result.b = this.b - otherColor.b;
  120. return this;
  121. };
  122. Color3.prototype.clone = function () {
  123. return new Color3(this.r, this.g, this.b);
  124. };
  125. Color3.prototype.copyFrom = function (source) {
  126. this.r = source.r;
  127. this.g = source.g;
  128. this.b = source.b;
  129. return this;
  130. };
  131. Color3.prototype.copyFromFloats = function (r, g, b) {
  132. this.r = r;
  133. this.g = g;
  134. this.b = b;
  135. return this;
  136. };
  137. Color3.prototype.toHexString = function () {
  138. var intR = (this.r * 255) | 0;
  139. var intG = (this.g * 255) | 0;
  140. var intB = (this.b * 255) | 0;
  141. return "#" + MathTools.ToHex(intR) + MathTools.ToHex(intG) + MathTools.ToHex(intB);
  142. };
  143. Color3.prototype.toLinearSpace = function () {
  144. var convertedColor = new Color3();
  145. this.toLinearSpaceToRef(convertedColor);
  146. return convertedColor;
  147. };
  148. Color3.prototype.toLinearSpaceToRef = function (convertedColor) {
  149. convertedColor.r = Math.pow(this.r, BABYLON.ToLinearSpace);
  150. convertedColor.g = Math.pow(this.g, BABYLON.ToLinearSpace);
  151. convertedColor.b = Math.pow(this.b, BABYLON.ToLinearSpace);
  152. return this;
  153. };
  154. Color3.prototype.toGammaSpace = function () {
  155. var convertedColor = new Color3();
  156. this.toGammaSpaceToRef(convertedColor);
  157. return convertedColor;
  158. };
  159. Color3.prototype.toGammaSpaceToRef = function (convertedColor) {
  160. convertedColor.r = Math.pow(this.r, BABYLON.ToGammaSpace);
  161. convertedColor.g = Math.pow(this.g, BABYLON.ToGammaSpace);
  162. convertedColor.b = Math.pow(this.b, BABYLON.ToGammaSpace);
  163. return this;
  164. };
  165. // Statics
  166. Color3.FromHexString = function (hex) {
  167. if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
  168. //Tools.Warn("Color3.FromHexString must be called with a string like #FFFFFF");
  169. return new Color3(0, 0, 0);
  170. }
  171. var r = parseInt(hex.substring(1, 3), 16);
  172. var g = parseInt(hex.substring(3, 5), 16);
  173. var b = parseInt(hex.substring(5, 7), 16);
  174. return Color3.FromInts(r, g, b);
  175. };
  176. Color3.FromArray = function (array, offset) {
  177. if (offset === void 0) { offset = 0; }
  178. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  179. };
  180. Color3.FromInts = function (r, g, b) {
  181. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  182. };
  183. Color3.Lerp = function (start, end, amount) {
  184. var r = start.r + ((end.r - start.r) * amount);
  185. var g = start.g + ((end.g - start.g) * amount);
  186. var b = start.b + ((end.b - start.b) * amount);
  187. return new Color3(r, g, b);
  188. };
  189. Color3.Red = function () { return new Color3(1, 0, 0); };
  190. Color3.Green = function () { return new Color3(0, 1, 0); };
  191. Color3.Blue = function () { return new Color3(0, 0, 1); };
  192. Color3.Black = function () { return new Color3(0, 0, 0); };
  193. Color3.White = function () { return new Color3(1, 1, 1); };
  194. Color3.Purple = function () { return new Color3(0.5, 0, 0.5); };
  195. Color3.Magenta = function () { return new Color3(1, 0, 1); };
  196. Color3.Yellow = function () { return new Color3(1, 1, 0); };
  197. Color3.Gray = function () { return new Color3(0.5, 0.5, 0.5); };
  198. return Color3;
  199. }());
  200. BABYLON.Color3 = Color3;
  201. var Color4 = (function () {
  202. function Color4(r, g, b, a) {
  203. this.r = r;
  204. this.g = g;
  205. this.b = b;
  206. this.a = a;
  207. }
  208. // Operators
  209. Color4.prototype.addInPlace = function (right) {
  210. this.r += right.r;
  211. this.g += right.g;
  212. this.b += right.b;
  213. this.a += right.a;
  214. return this;
  215. };
  216. Color4.prototype.asArray = function () {
  217. var result = [];
  218. this.toArray(result, 0);
  219. return result;
  220. };
  221. Color4.prototype.toArray = function (array, index) {
  222. if (index === undefined) {
  223. index = 0;
  224. }
  225. array[index] = this.r;
  226. array[index + 1] = this.g;
  227. array[index + 2] = this.b;
  228. array[index + 3] = this.a;
  229. return this;
  230. };
  231. Color4.prototype.add = function (right) {
  232. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  233. };
  234. Color4.prototype.subtract = function (right) {
  235. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  236. };
  237. Color4.prototype.subtractToRef = function (right, result) {
  238. result.r = this.r - right.r;
  239. result.g = this.g - right.g;
  240. result.b = this.b - right.b;
  241. result.a = this.a - right.a;
  242. return this;
  243. };
  244. Color4.prototype.scale = function (scale) {
  245. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  246. };
  247. Color4.prototype.scaleToRef = function (scale, result) {
  248. result.r = this.r * scale;
  249. result.g = this.g * scale;
  250. result.b = this.b * scale;
  251. result.a = this.a * scale;
  252. return this;
  253. };
  254. /**
  255. * Multipy an RGBA Color4 value by another and return a new Color4 object
  256. * @param color The Color4 (RGBA) value to multiply by
  257. * @returns A new Color4.
  258. */
  259. Color4.prototype.multiply = function (color) {
  260. return new Color4(this.r * color.r, this.g * color.g, this.b * color.b, this.a * color.a);
  261. };
  262. /**
  263. * Multipy an RGBA Color4 value by another and push the result in a reference value
  264. * @param color The Color4 (RGBA) value to multiply by
  265. * @param result The Color4 (RGBA) to fill the result in
  266. * @returns the result Color4.
  267. */
  268. Color4.prototype.multiplyToRef = function (color, result) {
  269. result.r = this.r * color.r;
  270. result.g = this.g * color.g;
  271. result.b = this.b * color.b;
  272. result.a = this.a * color.a;
  273. return result;
  274. };
  275. Color4.prototype.toString = function () {
  276. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  277. };
  278. Color4.prototype.getClassName = function () {
  279. return "Color4";
  280. };
  281. Color4.prototype.getHashCode = function () {
  282. var hash = this.r || 0;
  283. hash = (hash * 397) ^ (this.g || 0);
  284. hash = (hash * 397) ^ (this.b || 0);
  285. hash = (hash * 397) ^ (this.a || 0);
  286. return hash;
  287. };
  288. Color4.prototype.clone = function () {
  289. return new Color4(this.r, this.g, this.b, this.a);
  290. };
  291. Color4.prototype.copyFrom = function (source) {
  292. this.r = source.r;
  293. this.g = source.g;
  294. this.b = source.b;
  295. this.a = source.a;
  296. return this;
  297. };
  298. Color4.prototype.toHexString = function () {
  299. var intR = (this.r * 255) | 0;
  300. var intG = (this.g * 255) | 0;
  301. var intB = (this.b * 255) | 0;
  302. var intA = (this.a * 255) | 0;
  303. return "#" + MathTools.ToHex(intR) + MathTools.ToHex(intG) + MathTools.ToHex(intB) + MathTools.ToHex(intA);
  304. };
  305. // Statics
  306. Color4.FromHexString = function (hex) {
  307. if (hex.substring(0, 1) !== "#" || hex.length !== 9) {
  308. //Tools.Warn("Color4.FromHexString must be called with a string like #FFFFFFFF");
  309. return new Color4(0, 0, 0, 0);
  310. }
  311. var r = parseInt(hex.substring(1, 3), 16);
  312. var g = parseInt(hex.substring(3, 5), 16);
  313. var b = parseInt(hex.substring(5, 7), 16);
  314. var a = parseInt(hex.substring(7, 9), 16);
  315. return Color4.FromInts(r, g, b, a);
  316. };
  317. Color4.Lerp = function (left, right, amount) {
  318. var result = new Color4(0, 0, 0, 0);
  319. Color4.LerpToRef(left, right, amount, result);
  320. return result;
  321. };
  322. Color4.LerpToRef = function (left, right, amount, result) {
  323. result.r = left.r + (right.r - left.r) * amount;
  324. result.g = left.g + (right.g - left.g) * amount;
  325. result.b = left.b + (right.b - left.b) * amount;
  326. result.a = left.a + (right.a - left.a) * amount;
  327. };
  328. Color4.FromArray = function (array, offset) {
  329. if (offset === void 0) { offset = 0; }
  330. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  331. };
  332. Color4.FromInts = function (r, g, b, a) {
  333. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  334. };
  335. Color4.CheckColors4 = function (colors, count) {
  336. // Check if color3 was used
  337. if (colors.length === count * 3) {
  338. var colors4 = [];
  339. for (var index = 0; index < colors.length; index += 3) {
  340. var newIndex = (index / 3) * 4;
  341. colors4[newIndex] = colors[index];
  342. colors4[newIndex + 1] = colors[index + 1];
  343. colors4[newIndex + 2] = colors[index + 2];
  344. colors4[newIndex + 3] = 1.0;
  345. }
  346. return colors4;
  347. }
  348. return colors;
  349. };
  350. return Color4;
  351. }());
  352. BABYLON.Color4 = Color4;
  353. var Vector2 = (function () {
  354. function Vector2(x, y) {
  355. this.x = x;
  356. this.y = y;
  357. }
  358. Vector2.prototype.toString = function () {
  359. return "{X: " + this.x + " Y:" + this.y + "}";
  360. };
  361. Vector2.prototype.getClassName = function () {
  362. return "Vector2";
  363. };
  364. Vector2.prototype.getHashCode = function () {
  365. var hash = this.x || 0;
  366. hash = (hash * 397) ^ (this.y || 0);
  367. return hash;
  368. };
  369. // Operators
  370. Vector2.prototype.toArray = function (array, index) {
  371. if (index === void 0) { index = 0; }
  372. array[index] = this.x;
  373. array[index + 1] = this.y;
  374. return this;
  375. };
  376. Vector2.prototype.asArray = function () {
  377. var result = [];
  378. this.toArray(result, 0);
  379. return result;
  380. };
  381. Vector2.prototype.copyFrom = function (source) {
  382. this.x = source.x;
  383. this.y = source.y;
  384. return this;
  385. };
  386. Vector2.prototype.copyFromFloats = function (x, y) {
  387. this.x = x;
  388. this.y = y;
  389. return this;
  390. };
  391. Vector2.prototype.add = function (otherVector) {
  392. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  393. };
  394. Vector2.prototype.addToRef = function (otherVector, result) {
  395. result.x = this.x + otherVector.x;
  396. result.y = this.y + otherVector.y;
  397. return this;
  398. };
  399. Vector2.prototype.addInPlace = function (otherVector) {
  400. this.x += otherVector.x;
  401. this.y += otherVector.y;
  402. return this;
  403. };
  404. Vector2.prototype.addVector3 = function (otherVector) {
  405. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  406. };
  407. Vector2.prototype.subtract = function (otherVector) {
  408. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  409. };
  410. Vector2.prototype.subtractToRef = function (otherVector, result) {
  411. result.x = this.x - otherVector.x;
  412. result.y = this.y - otherVector.y;
  413. return this;
  414. };
  415. Vector2.prototype.subtractInPlace = function (otherVector) {
  416. this.x -= otherVector.x;
  417. this.y -= otherVector.y;
  418. return this;
  419. };
  420. Vector2.prototype.multiplyInPlace = function (otherVector) {
  421. this.x *= otherVector.x;
  422. this.y *= otherVector.y;
  423. return this;
  424. };
  425. Vector2.prototype.multiply = function (otherVector) {
  426. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  427. };
  428. Vector2.prototype.multiplyToRef = function (otherVector, result) {
  429. result.x = this.x * otherVector.x;
  430. result.y = this.y * otherVector.y;
  431. return this;
  432. };
  433. Vector2.prototype.multiplyByFloats = function (x, y) {
  434. return new Vector2(this.x * x, this.y * y);
  435. };
  436. Vector2.prototype.divide = function (otherVector) {
  437. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  438. };
  439. Vector2.prototype.divideToRef = function (otherVector, result) {
  440. result.x = this.x / otherVector.x;
  441. result.y = this.y / otherVector.y;
  442. return this;
  443. };
  444. Vector2.prototype.negate = function () {
  445. return new Vector2(-this.x, -this.y);
  446. };
  447. Vector2.prototype.scaleInPlace = function (scale) {
  448. this.x *= scale;
  449. this.y *= scale;
  450. return this;
  451. };
  452. Vector2.prototype.scale = function (scale) {
  453. return new Vector2(this.x * scale, this.y * scale);
  454. };
  455. Vector2.prototype.equals = function (otherVector) {
  456. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  457. };
  458. Vector2.prototype.equalsWithEpsilon = function (otherVector, epsilon) {
  459. if (epsilon === void 0) { epsilon = BABYLON.Epsilon; }
  460. return otherVector && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon) && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon);
  461. };
  462. // Properties
  463. Vector2.prototype.length = function () {
  464. return Math.sqrt(this.x * this.x + this.y * this.y);
  465. };
  466. Vector2.prototype.lengthSquared = function () {
  467. return (this.x * this.x + this.y * this.y);
  468. };
  469. // Methods
  470. Vector2.prototype.normalize = function () {
  471. var len = this.length();
  472. if (len === 0)
  473. return this;
  474. var num = 1.0 / len;
  475. this.x *= num;
  476. this.y *= num;
  477. return this;
  478. };
  479. Vector2.prototype.clone = function () {
  480. return new Vector2(this.x, this.y);
  481. };
  482. // Statics
  483. Vector2.Zero = function () {
  484. return new Vector2(0, 0);
  485. };
  486. Vector2.FromArray = function (array, offset) {
  487. if (offset === void 0) { offset = 0; }
  488. return new Vector2(array[offset], array[offset + 1]);
  489. };
  490. Vector2.FromArrayToRef = function (array, offset, result) {
  491. result.x = array[offset];
  492. result.y = array[offset + 1];
  493. };
  494. Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  495. var squared = amount * amount;
  496. var cubed = amount * squared;
  497. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  498. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  499. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  500. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  501. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  502. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  503. return new Vector2(x, y);
  504. };
  505. Vector2.Clamp = function (value, min, max) {
  506. var x = value.x;
  507. x = (x > max.x) ? max.x : x;
  508. x = (x < min.x) ? min.x : x;
  509. var y = value.y;
  510. y = (y > max.y) ? max.y : y;
  511. y = (y < min.y) ? min.y : y;
  512. return new Vector2(x, y);
  513. };
  514. Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  515. var squared = amount * amount;
  516. var cubed = amount * squared;
  517. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  518. var part2 = (-2.0 * cubed) + (3.0 * squared);
  519. var part3 = (cubed - (2.0 * squared)) + amount;
  520. var part4 = cubed - squared;
  521. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  522. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  523. return new Vector2(x, y);
  524. };
  525. Vector2.Lerp = function (start, end, amount) {
  526. var x = start.x + ((end.x - start.x) * amount);
  527. var y = start.y + ((end.y - start.y) * amount);
  528. return new Vector2(x, y);
  529. };
  530. Vector2.Dot = function (left, right) {
  531. return left.x * right.x + left.y * right.y;
  532. };
  533. Vector2.Normalize = function (vector) {
  534. var newVector = vector.clone();
  535. newVector.normalize();
  536. return newVector;
  537. };
  538. Vector2.Minimize = function (left, right) {
  539. var x = (left.x < right.x) ? left.x : right.x;
  540. var y = (left.y < right.y) ? left.y : right.y;
  541. return new Vector2(x, y);
  542. };
  543. Vector2.Maximize = function (left, right) {
  544. var x = (left.x > right.x) ? left.x : right.x;
  545. var y = (left.y > right.y) ? left.y : right.y;
  546. return new Vector2(x, y);
  547. };
  548. Vector2.Transform = function (vector, transformation) {
  549. var r = Vector2.Zero();
  550. Vector2.TransformToRef(vector, transformation, r);
  551. return r;
  552. };
  553. Vector2.TransformToRef = function (vector, transformation, result) {
  554. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + transformation.m[12];
  555. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + transformation.m[13];
  556. result.x = x;
  557. result.y = y;
  558. };
  559. Vector2.PointInTriangle = function (p, p0, p1, p2) {
  560. var a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  561. var sign = a < 0 ? -1 : 1;
  562. var s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  563. var t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
  564. return s > 0 && t > 0 && (s + t) < 2 * a * sign;
  565. };
  566. Vector2.Distance = function (value1, value2) {
  567. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  568. };
  569. Vector2.DistanceSquared = function (value1, value2) {
  570. var x = value1.x - value2.x;
  571. var y = value1.y - value2.y;
  572. return (x * x) + (y * y);
  573. };
  574. Vector2.Center = function (value1, value2) {
  575. var center = value1.add(value2);
  576. center.scaleInPlace(0.5);
  577. return center;
  578. };
  579. Vector2.DistanceOfPointFromSegment = function (p, segA, segB) {
  580. var l2 = Vector2.DistanceSquared(segA, segB);
  581. if (l2 === 0.0) {
  582. return Vector2.Distance(p, segA);
  583. }
  584. var v = segB.subtract(segA);
  585. var t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
  586. var proj = segA.add(v.multiplyByFloats(t, t));
  587. return Vector2.Distance(p, proj);
  588. };
  589. return Vector2;
  590. }());
  591. BABYLON.Vector2 = Vector2;
  592. var Vector3 = (function () {
  593. function Vector3(x, y, z) {
  594. this.x = x;
  595. this.y = y;
  596. this.z = z;
  597. }
  598. Vector3.prototype.toString = function () {
  599. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  600. };
  601. Vector3.prototype.getClassName = function () {
  602. return "Vector3";
  603. };
  604. Vector3.prototype.getHashCode = function () {
  605. var hash = this.x || 0;
  606. hash = (hash * 397) ^ (this.y || 0);
  607. hash = (hash * 397) ^ (this.z || 0);
  608. return hash;
  609. };
  610. // Operators
  611. Vector3.prototype.asArray = function () {
  612. var result = [];
  613. this.toArray(result, 0);
  614. return result;
  615. };
  616. Vector3.prototype.toArray = function (array, index) {
  617. if (index === void 0) { index = 0; }
  618. array[index] = this.x;
  619. array[index + 1] = this.y;
  620. array[index + 2] = this.z;
  621. return this;
  622. };
  623. Vector3.prototype.toQuaternion = function () {
  624. var result = new Quaternion(0, 0, 0, 1);
  625. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  626. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  627. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  628. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  629. var cosy = Math.cos(this.y * 0.5);
  630. var siny = Math.sin(this.y * 0.5);
  631. result.x = coszMinusx * siny;
  632. result.y = -sinzMinusx * siny;
  633. result.z = sinxPlusz * cosy;
  634. result.w = cosxPlusz * cosy;
  635. return result;
  636. };
  637. Vector3.prototype.addInPlace = function (otherVector) {
  638. this.x += otherVector.x;
  639. this.y += otherVector.y;
  640. this.z += otherVector.z;
  641. return this;
  642. };
  643. Vector3.prototype.add = function (otherVector) {
  644. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  645. };
  646. Vector3.prototype.addToRef = function (otherVector, result) {
  647. result.x = this.x + otherVector.x;
  648. result.y = this.y + otherVector.y;
  649. result.z = this.z + otherVector.z;
  650. return this;
  651. };
  652. Vector3.prototype.subtractInPlace = function (otherVector) {
  653. this.x -= otherVector.x;
  654. this.y -= otherVector.y;
  655. this.z -= otherVector.z;
  656. return this;
  657. };
  658. Vector3.prototype.subtract = function (otherVector) {
  659. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  660. };
  661. Vector3.prototype.subtractToRef = function (otherVector, result) {
  662. result.x = this.x - otherVector.x;
  663. result.y = this.y - otherVector.y;
  664. result.z = this.z - otherVector.z;
  665. return this;
  666. };
  667. Vector3.prototype.subtractFromFloats = function (x, y, z) {
  668. return new Vector3(this.x - x, this.y - y, this.z - z);
  669. };
  670. Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  671. result.x = this.x - x;
  672. result.y = this.y - y;
  673. result.z = this.z - z;
  674. return this;
  675. };
  676. Vector3.prototype.negate = function () {
  677. return new Vector3(-this.x, -this.y, -this.z);
  678. };
  679. Vector3.prototype.scaleInPlace = function (scale) {
  680. this.x *= scale;
  681. this.y *= scale;
  682. this.z *= scale;
  683. return this;
  684. };
  685. Vector3.prototype.scale = function (scale) {
  686. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  687. };
  688. Vector3.prototype.scaleToRef = function (scale, result) {
  689. result.x = this.x * scale;
  690. result.y = this.y * scale;
  691. result.z = this.z * scale;
  692. };
  693. Vector3.prototype.equals = function (otherVector) {
  694. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  695. };
  696. Vector3.prototype.equalsWithEpsilon = function (otherVector, epsilon) {
  697. if (epsilon === void 0) { epsilon = BABYLON.Epsilon; }
  698. return otherVector && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon) && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon) && MathTools.WithinEpsilon(this.z, otherVector.z, epsilon);
  699. };
  700. Vector3.prototype.equalsToFloats = function (x, y, z) {
  701. return this.x === x && this.y === y && this.z === z;
  702. };
  703. Vector3.prototype.multiplyInPlace = function (otherVector) {
  704. this.x *= otherVector.x;
  705. this.y *= otherVector.y;
  706. this.z *= otherVector.z;
  707. return this;
  708. };
  709. Vector3.prototype.multiply = function (otherVector) {
  710. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  711. };
  712. Vector3.prototype.multiplyToRef = function (otherVector, result) {
  713. result.x = this.x * otherVector.x;
  714. result.y = this.y * otherVector.y;
  715. result.z = this.z * otherVector.z;
  716. return this;
  717. };
  718. Vector3.prototype.multiplyByFloats = function (x, y, z) {
  719. return new Vector3(this.x * x, this.y * y, this.z * z);
  720. };
  721. Vector3.prototype.divide = function (otherVector) {
  722. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  723. };
  724. Vector3.prototype.divideToRef = function (otherVector, result) {
  725. result.x = this.x / otherVector.x;
  726. result.y = this.y / otherVector.y;
  727. result.z = this.z / otherVector.z;
  728. return this;
  729. };
  730. Vector3.prototype.MinimizeInPlace = function (other) {
  731. if (other.x < this.x)
  732. this.x = other.x;
  733. if (other.y < this.y)
  734. this.y = other.y;
  735. if (other.z < this.z)
  736. this.z = other.z;
  737. return this;
  738. };
  739. Vector3.prototype.MaximizeInPlace = function (other) {
  740. if (other.x > this.x)
  741. this.x = other.x;
  742. if (other.y > this.y)
  743. this.y = other.y;
  744. if (other.z > this.z)
  745. this.z = other.z;
  746. return this;
  747. };
  748. // Properties
  749. Vector3.prototype.length = function () {
  750. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  751. };
  752. Vector3.prototype.lengthSquared = function () {
  753. return (this.x * this.x + this.y * this.y + this.z * this.z);
  754. };
  755. // Methods
  756. Vector3.prototype.normalize = function () {
  757. var len = this.length();
  758. if (len === 0 || len === 1.0)
  759. return this;
  760. var num = 1.0 / len;
  761. this.x *= num;
  762. this.y *= num;
  763. this.z *= num;
  764. return this;
  765. };
  766. Vector3.prototype.clone = function () {
  767. return new Vector3(this.x, this.y, this.z);
  768. };
  769. Vector3.prototype.copyFrom = function (source) {
  770. this.x = source.x;
  771. this.y = source.y;
  772. this.z = source.z;
  773. return this;
  774. };
  775. Vector3.prototype.copyFromFloats = function (x, y, z) {
  776. this.x = x;
  777. this.y = y;
  778. this.z = z;
  779. return this;
  780. };
  781. // Statics
  782. Vector3.GetClipFactor = function (vector0, vector1, axis, size) {
  783. var d0 = Vector3.Dot(vector0, axis) - size;
  784. var d1 = Vector3.Dot(vector1, axis) - size;
  785. var s = d0 / (d0 - d1);
  786. return s;
  787. };
  788. Vector3.FromArray = function (array, offset) {
  789. if (!offset) {
  790. offset = 0;
  791. }
  792. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  793. };
  794. Vector3.FromFloatArray = function (array, offset) {
  795. if (!offset) {
  796. offset = 0;
  797. }
  798. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  799. };
  800. Vector3.FromArrayToRef = function (array, offset, result) {
  801. result.x = array[offset];
  802. result.y = array[offset + 1];
  803. result.z = array[offset + 2];
  804. };
  805. Vector3.FromFloatArrayToRef = function (array, offset, result) {
  806. result.x = array[offset];
  807. result.y = array[offset + 1];
  808. result.z = array[offset + 2];
  809. };
  810. Vector3.FromFloatsToRef = function (x, y, z, result) {
  811. result.x = x;
  812. result.y = y;
  813. result.z = z;
  814. };
  815. Vector3.Zero = function () {
  816. return new Vector3(0, 0, 0);
  817. };
  818. Vector3.Up = function () {
  819. return new Vector3(0, 1.0, 0);
  820. };
  821. Vector3.Forward = function () {
  822. return new Vector3(0, 0, 1.0);
  823. };
  824. Vector3.Right = function () {
  825. return new Vector3(1.0, 0, 0);
  826. };
  827. Vector3.Left = function () {
  828. return new Vector3(-1.0, 0, 0);
  829. };
  830. Vector3.TransformCoordinates = function (vector, transformation) {
  831. var result = Vector3.Zero();
  832. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  833. return result;
  834. };
  835. Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  836. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  837. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  838. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  839. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  840. result.x = x / w;
  841. result.y = y / w;
  842. result.z = z / w;
  843. };
  844. Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  845. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  846. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  847. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  848. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  849. result.x = rx / rw;
  850. result.y = ry / rw;
  851. result.z = rz / rw;
  852. };
  853. Vector3.TransformNormal = function (vector, transformation) {
  854. var result = Vector3.Zero();
  855. Vector3.TransformNormalToRef(vector, transformation, result);
  856. return result;
  857. };
  858. Vector3.TransformNormalToRef = function (vector, transformation, result) {
  859. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  860. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  861. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  862. result.x = x;
  863. result.y = y;
  864. result.z = z;
  865. };
  866. Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  867. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  868. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  869. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  870. };
  871. Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  872. var squared = amount * amount;
  873. var cubed = amount * squared;
  874. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  875. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  876. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  877. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  878. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  879. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  880. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  881. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  882. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  883. return new Vector3(x, y, z);
  884. };
  885. Vector3.Clamp = function (value, min, max) {
  886. var x = value.x;
  887. x = (x > max.x) ? max.x : x;
  888. x = (x < min.x) ? min.x : x;
  889. var y = value.y;
  890. y = (y > max.y) ? max.y : y;
  891. y = (y < min.y) ? min.y : y;
  892. var z = value.z;
  893. z = (z > max.z) ? max.z : z;
  894. z = (z < min.z) ? min.z : z;
  895. return new Vector3(x, y, z);
  896. };
  897. Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  898. var squared = amount * amount;
  899. var cubed = amount * squared;
  900. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  901. var part2 = (-2.0 * cubed) + (3.0 * squared);
  902. var part3 = (cubed - (2.0 * squared)) + amount;
  903. var part4 = cubed - squared;
  904. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  905. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  906. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  907. return new Vector3(x, y, z);
  908. };
  909. Vector3.Lerp = function (start, end, amount) {
  910. var x = start.x + ((end.x - start.x) * amount);
  911. var y = start.y + ((end.y - start.y) * amount);
  912. var z = start.z + ((end.z - start.z) * amount);
  913. return new Vector3(x, y, z);
  914. };
  915. Vector3.Dot = function (left, right) {
  916. return (left.x * right.x + left.y * right.y + left.z * right.z);
  917. };
  918. Vector3.Cross = function (left, right) {
  919. var result = Vector3.Zero();
  920. Vector3.CrossToRef(left, right, result);
  921. return result;
  922. };
  923. Vector3.CrossToRef = function (left, right, result) {
  924. Tmp.Vector3[0].x = left.y * right.z - left.z * right.y;
  925. Tmp.Vector3[0].y = left.z * right.x - left.x * right.z;
  926. Tmp.Vector3[0].z = left.x * right.y - left.y * right.x;
  927. result.copyFrom(Tmp.Vector3[0]);
  928. };
  929. Vector3.Normalize = function (vector) {
  930. var result = Vector3.Zero();
  931. Vector3.NormalizeToRef(vector, result);
  932. return result;
  933. };
  934. Vector3.NormalizeToRef = function (vector, result) {
  935. result.copyFrom(vector);
  936. result.normalize();
  937. };
  938. Vector3.Project = function (vector, world, transform, viewport) {
  939. var cw = viewport.width;
  940. var ch = viewport.height;
  941. var cx = viewport.x;
  942. var cy = viewport.y;
  943. var viewportMatrix = Vector3._viewportMatrixCache ? Vector3._viewportMatrixCache : (Vector3._viewportMatrixCache = new Matrix());
  944. Matrix.FromValuesToRef(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, viewportMatrix);
  945. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  946. world.multiplyToRef(transform, matrix);
  947. matrix.multiplyToRef(viewportMatrix, matrix);
  948. return Vector3.TransformCoordinates(vector, matrix);
  949. };
  950. Vector3.UnprojectFromTransform = function (source, viewportWidth, viewportHeight, world, transform) {
  951. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  952. world.multiplyToRef(transform, matrix);
  953. matrix.invert();
  954. source.x = source.x / viewportWidth * 2 - 1;
  955. source.y = -(source.y / viewportHeight * 2 - 1);
  956. var vector = Vector3.TransformCoordinates(source, matrix);
  957. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  958. if (MathTools.WithinEpsilon(num, 1.0)) {
  959. vector = vector.scale(1.0 / num);
  960. }
  961. return vector;
  962. };
  963. Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  964. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  965. world.multiplyToRef(view, matrix);
  966. matrix.multiplyToRef(projection, matrix);
  967. matrix.invert();
  968. var screenSource = new Vector3(source.x / viewportWidth * 2 - 1, -(source.y / viewportHeight * 2 - 1), source.z);
  969. var vector = Vector3.TransformCoordinates(screenSource, matrix);
  970. var num = screenSource.x * matrix.m[3] + screenSource.y * matrix.m[7] + screenSource.z * matrix.m[11] + matrix.m[15];
  971. if (MathTools.WithinEpsilon(num, 1.0)) {
  972. vector = vector.scale(1.0 / num);
  973. }
  974. return vector;
  975. };
  976. Vector3.Minimize = function (left, right) {
  977. var min = left.clone();
  978. min.MinimizeInPlace(right);
  979. return min;
  980. };
  981. Vector3.Maximize = function (left, right) {
  982. var max = left.clone();
  983. max.MaximizeInPlace(right);
  984. return max;
  985. };
  986. Vector3.Distance = function (value1, value2) {
  987. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  988. };
  989. Vector3.DistanceSquared = function (value1, value2) {
  990. var x = value1.x - value2.x;
  991. var y = value1.y - value2.y;
  992. var z = value1.z - value2.z;
  993. return (x * x) + (y * y) + (z * z);
  994. };
  995. Vector3.Center = function (value1, value2) {
  996. var center = value1.add(value2);
  997. center.scaleInPlace(0.5);
  998. return center;
  999. };
  1000. /**
  1001. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  1002. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  1003. * to something in order to rotate it from its local system to the given target system.
  1004. */
  1005. Vector3.RotationFromAxis = function (axis1, axis2, axis3) {
  1006. var rotation = Vector3.Zero();
  1007. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  1008. return rotation;
  1009. };
  1010. /**
  1011. * The same than RotationFromAxis but updates the passed ref Vector3 parameter.
  1012. */
  1013. Vector3.RotationFromAxisToRef = function (axis1, axis2, axis3, ref) {
  1014. var u = axis1.normalize();
  1015. var w = axis3.normalize();
  1016. // world axis
  1017. var X = Axis.X;
  1018. var Y = Axis.Y;
  1019. // equation unknowns and vars
  1020. var yaw = 0.0;
  1021. var pitch = 0.0;
  1022. var roll = 0.0;
  1023. var x = 0.0;
  1024. var y = 0.0;
  1025. var z = 0.0;
  1026. var t = 0.0;
  1027. var sign = -1.0;
  1028. var nbRevert = 0;
  1029. var cross = Tmp.Vector3[0];
  1030. var dot = 0.0;
  1031. // step 1 : rotation around w
  1032. // Rv3(u) = u1, and u1 belongs to plane xOz
  1033. // Rv3(w) = w1 = w invariant
  1034. var u1 = Tmp.Vector3[1];
  1035. if (MathTools.WithinEpsilon(w.z, 0, BABYLON.Epsilon)) {
  1036. z = 1.0;
  1037. }
  1038. else if (MathTools.WithinEpsilon(w.x, 0, BABYLON.Epsilon)) {
  1039. x = 1.0;
  1040. }
  1041. else {
  1042. t = w.z / w.x;
  1043. x = -t * Math.sqrt(1 / (1 + t * t));
  1044. z = Math.sqrt(1 / (1 + t * t));
  1045. }
  1046. u1.x = x;
  1047. u1.y = y;
  1048. u1.z = z;
  1049. u1.normalize();
  1050. Vector3.CrossToRef(u, u1, cross); // returns same direction as w (=local z) if positive angle : cross(source, image)
  1051. cross.normalize();
  1052. if (Vector3.Dot(w, cross) < 0) {
  1053. sign = 1.0;
  1054. }
  1055. dot = Vector3.Dot(u, u1);
  1056. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  1057. roll = Math.acos(dot) * sign;
  1058. if (Vector3.Dot(u1, X) < 0) {
  1059. roll = Math.PI + roll;
  1060. u1 = u1.scaleInPlace(-1);
  1061. nbRevert++;
  1062. }
  1063. // step 2 : rotate around u1
  1064. // Ru1(w1) = Ru1(w) = w2, and w2 belongs to plane xOz
  1065. // u1 is yet in xOz and invariant by Ru1, so after this step u1 and w2 will be in xOz
  1066. var w2 = Tmp.Vector3[2];
  1067. var v2 = Tmp.Vector3[3];
  1068. x = 0.0;
  1069. y = 0.0;
  1070. z = 0.0;
  1071. sign = -1.0;
  1072. if (MathTools.WithinEpsilon(w.z, 0, BABYLON.Epsilon)) {
  1073. x = 1.0;
  1074. }
  1075. else {
  1076. t = u1.z / u1.x;
  1077. x = -t * Math.sqrt(1 / (1 + t * t));
  1078. z = Math.sqrt(1 / (1 + t * t));
  1079. }
  1080. w2.x = x;
  1081. w2.y = y;
  1082. w2.z = z;
  1083. w2.normalize();
  1084. Vector3.CrossToRef(w2, u1, v2); // v2 image of v1 through rotation around u1
  1085. v2.normalize();
  1086. Vector3.CrossToRef(w, w2, cross); // returns same direction as u1 (=local x) if positive angle : cross(source, image)
  1087. cross.normalize();
  1088. if (Vector3.Dot(u1, cross) < 0) {
  1089. sign = 1.0;
  1090. }
  1091. dot = Vector3.Dot(w, w2);
  1092. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  1093. pitch = Math.acos(dot) * sign;
  1094. if (Vector3.Dot(v2, Y) < 0) {
  1095. pitch = Math.PI + pitch;
  1096. nbRevert++;
  1097. }
  1098. // step 3 : rotate around v2
  1099. // Rv2(u1) = X, same as Rv2(w2) = Z, with X=(1,0,0) and Z=(0,0,1)
  1100. sign = -1.0;
  1101. Vector3.CrossToRef(X, u1, cross); // returns same direction as Y if positive angle : cross(source, image)
  1102. cross.normalize();
  1103. if (Vector3.Dot(cross, Y) < 0) {
  1104. sign = 1.0;
  1105. }
  1106. dot = Vector3.Dot(u1, X);
  1107. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  1108. yaw = -Math.acos(dot) * sign; // negative : plane zOx oriented clockwise
  1109. if (dot < 0 && nbRevert < 2) {
  1110. yaw = Math.PI + yaw;
  1111. }
  1112. ref.x = pitch;
  1113. ref.y = yaw;
  1114. ref.z = roll;
  1115. };
  1116. return Vector3;
  1117. }());
  1118. BABYLON.Vector3 = Vector3;
  1119. //Vector4 class created for EulerAngle class conversion to Quaternion
  1120. var Vector4 = (function () {
  1121. function Vector4(x, y, z, w) {
  1122. this.x = x;
  1123. this.y = y;
  1124. this.z = z;
  1125. this.w = w;
  1126. }
  1127. Vector4.prototype.toString = function () {
  1128. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1129. };
  1130. Vector4.prototype.getClassName = function () {
  1131. return "Vector4";
  1132. };
  1133. Vector4.prototype.getHashCode = function () {
  1134. var hash = this.x || 0;
  1135. hash = (hash * 397) ^ (this.y || 0);
  1136. hash = (hash * 397) ^ (this.z || 0);
  1137. hash = (hash * 397) ^ (this.w || 0);
  1138. return hash;
  1139. };
  1140. // Operators
  1141. Vector4.prototype.asArray = function () {
  1142. var result = [];
  1143. this.toArray(result, 0);
  1144. return result;
  1145. };
  1146. Vector4.prototype.toArray = function (array, index) {
  1147. if (index === undefined) {
  1148. index = 0;
  1149. }
  1150. array[index] = this.x;
  1151. array[index + 1] = this.y;
  1152. array[index + 2] = this.z;
  1153. array[index + 3] = this.w;
  1154. return this;
  1155. };
  1156. Vector4.prototype.addInPlace = function (otherVector) {
  1157. this.x += otherVector.x;
  1158. this.y += otherVector.y;
  1159. this.z += otherVector.z;
  1160. this.w += otherVector.w;
  1161. return this;
  1162. };
  1163. Vector4.prototype.add = function (otherVector) {
  1164. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  1165. };
  1166. Vector4.prototype.addToRef = function (otherVector, result) {
  1167. result.x = this.x + otherVector.x;
  1168. result.y = this.y + otherVector.y;
  1169. result.z = this.z + otherVector.z;
  1170. result.w = this.w + otherVector.w;
  1171. return this;
  1172. };
  1173. Vector4.prototype.subtractInPlace = function (otherVector) {
  1174. this.x -= otherVector.x;
  1175. this.y -= otherVector.y;
  1176. this.z -= otherVector.z;
  1177. this.w -= otherVector.w;
  1178. return this;
  1179. };
  1180. Vector4.prototype.subtract = function (otherVector) {
  1181. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  1182. };
  1183. Vector4.prototype.subtractToRef = function (otherVector, result) {
  1184. result.x = this.x - otherVector.x;
  1185. result.y = this.y - otherVector.y;
  1186. result.z = this.z - otherVector.z;
  1187. result.w = this.w - otherVector.w;
  1188. return this;
  1189. };
  1190. Vector4.prototype.subtractFromFloats = function (x, y, z, w) {
  1191. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  1192. };
  1193. Vector4.prototype.subtractFromFloatsToRef = function (x, y, z, w, result) {
  1194. result.x = this.x - x;
  1195. result.y = this.y - y;
  1196. result.z = this.z - z;
  1197. result.w = this.w - w;
  1198. return this;
  1199. };
  1200. Vector4.prototype.negate = function () {
  1201. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  1202. };
  1203. Vector4.prototype.scaleInPlace = function (scale) {
  1204. this.x *= scale;
  1205. this.y *= scale;
  1206. this.z *= scale;
  1207. this.w *= scale;
  1208. return this;
  1209. };
  1210. Vector4.prototype.scale = function (scale) {
  1211. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  1212. };
  1213. Vector4.prototype.scaleToRef = function (scale, result) {
  1214. result.x = this.x * scale;
  1215. result.y = this.y * scale;
  1216. result.z = this.z * scale;
  1217. result.w = this.w * scale;
  1218. };
  1219. Vector4.prototype.equals = function (otherVector) {
  1220. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  1221. };
  1222. Vector4.prototype.equalsWithEpsilon = function (otherVector, epsilon) {
  1223. if (epsilon === void 0) { epsilon = BABYLON.Epsilon; }
  1224. return otherVector
  1225. && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon)
  1226. && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon)
  1227. && MathTools.WithinEpsilon(this.z, otherVector.z, epsilon)
  1228. && MathTools.WithinEpsilon(this.w, otherVector.w, epsilon);
  1229. };
  1230. Vector4.prototype.equalsToFloats = function (x, y, z, w) {
  1231. return this.x === x && this.y === y && this.z === z && this.w === w;
  1232. };
  1233. Vector4.prototype.multiplyInPlace = function (otherVector) {
  1234. this.x *= otherVector.x;
  1235. this.y *= otherVector.y;
  1236. this.z *= otherVector.z;
  1237. this.w *= otherVector.w;
  1238. return this;
  1239. };
  1240. Vector4.prototype.multiply = function (otherVector) {
  1241. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  1242. };
  1243. Vector4.prototype.multiplyToRef = function (otherVector, result) {
  1244. result.x = this.x * otherVector.x;
  1245. result.y = this.y * otherVector.y;
  1246. result.z = this.z * otherVector.z;
  1247. result.w = this.w * otherVector.w;
  1248. return this;
  1249. };
  1250. Vector4.prototype.multiplyByFloats = function (x, y, z, w) {
  1251. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  1252. };
  1253. Vector4.prototype.divide = function (otherVector) {
  1254. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  1255. };
  1256. Vector4.prototype.divideToRef = function (otherVector, result) {
  1257. result.x = this.x / otherVector.x;
  1258. result.y = this.y / otherVector.y;
  1259. result.z = this.z / otherVector.z;
  1260. result.w = this.w / otherVector.w;
  1261. return this;
  1262. };
  1263. Vector4.prototype.MinimizeInPlace = function (other) {
  1264. if (other.x < this.x)
  1265. this.x = other.x;
  1266. if (other.y < this.y)
  1267. this.y = other.y;
  1268. if (other.z < this.z)
  1269. this.z = other.z;
  1270. if (other.w < this.w)
  1271. this.w = other.w;
  1272. return this;
  1273. };
  1274. Vector4.prototype.MaximizeInPlace = function (other) {
  1275. if (other.x > this.x)
  1276. this.x = other.x;
  1277. if (other.y > this.y)
  1278. this.y = other.y;
  1279. if (other.z > this.z)
  1280. this.z = other.z;
  1281. if (other.w > this.w)
  1282. this.w = other.w;
  1283. return this;
  1284. };
  1285. // Properties
  1286. Vector4.prototype.length = function () {
  1287. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1288. };
  1289. Vector4.prototype.lengthSquared = function () {
  1290. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1291. };
  1292. // Methods
  1293. Vector4.prototype.normalize = function () {
  1294. var len = this.length();
  1295. if (len === 0)
  1296. return this;
  1297. var num = 1.0 / len;
  1298. this.x *= num;
  1299. this.y *= num;
  1300. this.z *= num;
  1301. this.w *= num;
  1302. return this;
  1303. };
  1304. Vector4.prototype.toVector3 = function () {
  1305. return new Vector3(this.x, this.y, this.z);
  1306. };
  1307. Vector4.prototype.clone = function () {
  1308. return new Vector4(this.x, this.y, this.z, this.w);
  1309. };
  1310. Vector4.prototype.copyFrom = function (source) {
  1311. this.x = source.x;
  1312. this.y = source.y;
  1313. this.z = source.z;
  1314. this.w = source.w;
  1315. return this;
  1316. };
  1317. Vector4.prototype.copyFromFloats = function (x, y, z, w) {
  1318. this.x = x;
  1319. this.y = y;
  1320. this.z = z;
  1321. this.w = w;
  1322. return this;
  1323. };
  1324. // Statics
  1325. Vector4.FromArray = function (array, offset) {
  1326. if (!offset) {
  1327. offset = 0;
  1328. }
  1329. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1330. };
  1331. Vector4.FromArrayToRef = function (array, offset, result) {
  1332. result.x = array[offset];
  1333. result.y = array[offset + 1];
  1334. result.z = array[offset + 2];
  1335. result.w = array[offset + 3];
  1336. };
  1337. Vector4.FromFloatArrayToRef = function (array, offset, result) {
  1338. result.x = array[offset];
  1339. result.y = array[offset + 1];
  1340. result.z = array[offset + 2];
  1341. result.w = array[offset + 3];
  1342. };
  1343. Vector4.FromFloatsToRef = function (x, y, z, w, result) {
  1344. result.x = x;
  1345. result.y = y;
  1346. result.z = z;
  1347. result.w = w;
  1348. };
  1349. Vector4.Zero = function () {
  1350. return new Vector4(0, 0, 0, 0);
  1351. };
  1352. Vector4.Normalize = function (vector) {
  1353. var result = Vector4.Zero();
  1354. Vector4.NormalizeToRef(vector, result);
  1355. return result;
  1356. };
  1357. Vector4.NormalizeToRef = function (vector, result) {
  1358. result.copyFrom(vector);
  1359. result.normalize();
  1360. };
  1361. Vector4.Minimize = function (left, right) {
  1362. var min = left.clone();
  1363. min.MinimizeInPlace(right);
  1364. return min;
  1365. };
  1366. Vector4.Maximize = function (left, right) {
  1367. var max = left.clone();
  1368. max.MaximizeInPlace(right);
  1369. return max;
  1370. };
  1371. Vector4.Distance = function (value1, value2) {
  1372. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1373. };
  1374. Vector4.DistanceSquared = function (value1, value2) {
  1375. var x = value1.x - value2.x;
  1376. var y = value1.y - value2.y;
  1377. var z = value1.z - value2.z;
  1378. var w = value1.w - value2.w;
  1379. return (x * x) + (y * y) + (z * z) + (w * w);
  1380. };
  1381. Vector4.Center = function (value1, value2) {
  1382. var center = value1.add(value2);
  1383. center.scaleInPlace(0.5);
  1384. return center;
  1385. };
  1386. return Vector4;
  1387. }());
  1388. BABYLON.Vector4 = Vector4;
  1389. var Size = (function () {
  1390. function Size(width, height) {
  1391. this.width = width;
  1392. this.height = height;
  1393. }
  1394. Size.prototype.toString = function () {
  1395. return "{W: " + this.width + ", H: " + this.height + "}";
  1396. };
  1397. Size.prototype.getClassName = function () {
  1398. return "Size";
  1399. };
  1400. Size.prototype.getHashCode = function () {
  1401. var hash = this.width || 0;
  1402. hash = (hash * 397) ^ (this.height || 0);
  1403. return hash;
  1404. };
  1405. Size.prototype.copyFrom = function (src) {
  1406. this.width = src.width;
  1407. this.height = src.height;
  1408. };
  1409. Size.prototype.copyFromFloats = function (width, height) {
  1410. this.width = width;
  1411. this.height = height;
  1412. };
  1413. Size.prototype.multiplyByFloats = function (w, h) {
  1414. return new Size(this.width * w, this.height * h);
  1415. };
  1416. Size.prototype.clone = function () {
  1417. return new Size(this.width, this.height);
  1418. };
  1419. Size.prototype.equals = function (other) {
  1420. if (!other) {
  1421. return false;
  1422. }
  1423. return (this.width === other.width) && (this.height === other.height);
  1424. };
  1425. Object.defineProperty(Size.prototype, "surface", {
  1426. get: function () {
  1427. return this.width * this.height;
  1428. },
  1429. enumerable: true,
  1430. configurable: true
  1431. });
  1432. Size.Zero = function () {
  1433. return new Size(0, 0);
  1434. };
  1435. Size.prototype.add = function (otherSize) {
  1436. var r = new Size(this.width + otherSize.width, this.height + otherSize.height);
  1437. return r;
  1438. };
  1439. Size.prototype.substract = function (otherSize) {
  1440. var r = new Size(this.width - otherSize.width, this.height - otherSize.height);
  1441. return r;
  1442. };
  1443. Size.Lerp = function (start, end, amount) {
  1444. var w = start.width + ((end.width - start.width) * amount);
  1445. var h = start.height + ((end.height - start.height) * amount);
  1446. return new Size(w, h);
  1447. };
  1448. return Size;
  1449. }());
  1450. BABYLON.Size = Size;
  1451. var Quaternion = (function () {
  1452. function Quaternion(x, y, z, w) {
  1453. if (x === void 0) { x = 0; }
  1454. if (y === void 0) { y = 0; }
  1455. if (z === void 0) { z = 0; }
  1456. if (w === void 0) { w = 1; }
  1457. this.x = x;
  1458. this.y = y;
  1459. this.z = z;
  1460. this.w = w;
  1461. }
  1462. Quaternion.prototype.toString = function () {
  1463. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1464. };
  1465. Quaternion.prototype.getClassName = function () {
  1466. return "Quaternion";
  1467. };
  1468. Quaternion.prototype.getHashCode = function () {
  1469. var hash = this.x || 0;
  1470. hash = (hash * 397) ^ (this.y || 0);
  1471. hash = (hash * 397) ^ (this.z || 0);
  1472. hash = (hash * 397) ^ (this.w || 0);
  1473. return hash;
  1474. };
  1475. Quaternion.prototype.asArray = function () {
  1476. return [this.x, this.y, this.z, this.w];
  1477. };
  1478. Quaternion.prototype.equals = function (otherQuaternion) {
  1479. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  1480. };
  1481. Quaternion.prototype.clone = function () {
  1482. return new Quaternion(this.x, this.y, this.z, this.w);
  1483. };
  1484. Quaternion.prototype.copyFrom = function (other) {
  1485. this.x = other.x;
  1486. this.y = other.y;
  1487. this.z = other.z;
  1488. this.w = other.w;
  1489. return this;
  1490. };
  1491. Quaternion.prototype.copyFromFloats = function (x, y, z, w) {
  1492. this.x = x;
  1493. this.y = y;
  1494. this.z = z;
  1495. this.w = w;
  1496. return this;
  1497. };
  1498. Quaternion.prototype.add = function (other) {
  1499. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  1500. };
  1501. Quaternion.prototype.subtract = function (other) {
  1502. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  1503. };
  1504. Quaternion.prototype.scale = function (value) {
  1505. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  1506. };
  1507. Quaternion.prototype.multiply = function (q1) {
  1508. var result = new Quaternion(0, 0, 0, 1.0);
  1509. this.multiplyToRef(q1, result);
  1510. return result;
  1511. };
  1512. Quaternion.prototype.multiplyToRef = function (q1, result) {
  1513. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1514. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1515. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1516. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1517. result.copyFromFloats(x, y, z, w);
  1518. return this;
  1519. };
  1520. Quaternion.prototype.multiplyInPlace = function (q1) {
  1521. this.multiplyToRef(q1, this);
  1522. return this;
  1523. };
  1524. Quaternion.prototype.conjugateToRef = function (ref) {
  1525. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  1526. return this;
  1527. };
  1528. Quaternion.prototype.conjugateInPlace = function () {
  1529. this.x *= -1;
  1530. this.y *= -1;
  1531. this.z *= -1;
  1532. return this;
  1533. };
  1534. Quaternion.prototype.conjugate = function () {
  1535. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  1536. return result;
  1537. };
  1538. Quaternion.prototype.length = function () {
  1539. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1540. };
  1541. Quaternion.prototype.normalize = function () {
  1542. var length = 1.0 / this.length();
  1543. this.x *= length;
  1544. this.y *= length;
  1545. this.z *= length;
  1546. this.w *= length;
  1547. return this;
  1548. };
  1549. Quaternion.prototype.toEulerAngles = function (order) {
  1550. if (order === void 0) { order = "YZX"; }
  1551. var result = Vector3.Zero();
  1552. this.toEulerAnglesToRef(result, order);
  1553. return result;
  1554. };
  1555. Quaternion.prototype.toEulerAnglesToRef = function (result, order) {
  1556. if (order === void 0) { order = "YZX"; }
  1557. var qx = this.x;
  1558. var qy = this.y;
  1559. var qz = this.z;
  1560. var qw = this.w;
  1561. var xsqr = qx * qx;
  1562. var t0 = -2.0 * (xsqr + qy * qy) + 1.0;
  1563. var t1 = 2.0 * (qz * qx + qw * qy);
  1564. var t2 = -2.0 * (qz * qy - qw * qx);
  1565. var t3 = 2.0 * (qx * qy + qw * qz);
  1566. var t4 = -2.0 * (qz * qz + xsqr) + 1.0;
  1567. t2 = t2 > 1.0 ? 1.0 : t2;
  1568. t2 = t2 < -1.0 ? -1.0 : t2;
  1569. result.x = Math.asin(t2);
  1570. result.z = Math.atan2(t3, t4);
  1571. result.y = Math.atan2(t1, t0);
  1572. return this;
  1573. };
  1574. ;
  1575. Quaternion.prototype.toRotationMatrix = function (result) {
  1576. var xx = this.x * this.x;
  1577. var yy = this.y * this.y;
  1578. var zz = this.z * this.z;
  1579. var xy = this.x * this.y;
  1580. var zw = this.z * this.w;
  1581. var zx = this.z * this.x;
  1582. var yw = this.y * this.w;
  1583. var yz = this.y * this.z;
  1584. var xw = this.x * this.w;
  1585. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1586. result.m[1] = 2.0 * (xy + zw);
  1587. result.m[2] = 2.0 * (zx - yw);
  1588. result.m[3] = 0;
  1589. result.m[4] = 2.0 * (xy - zw);
  1590. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1591. result.m[6] = 2.0 * (yz + xw);
  1592. result.m[7] = 0;
  1593. result.m[8] = 2.0 * (zx + yw);
  1594. result.m[9] = 2.0 * (yz - xw);
  1595. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1596. result.m[11] = 0;
  1597. result.m[12] = 0;
  1598. result.m[13] = 0;
  1599. result.m[14] = 0;
  1600. result.m[15] = 1.0;
  1601. return this;
  1602. };
  1603. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  1604. Quaternion.FromRotationMatrixToRef(matrix, this);
  1605. return this;
  1606. };
  1607. // Statics
  1608. Quaternion.FromRotationMatrix = function (matrix) {
  1609. var result = new Quaternion();
  1610. Quaternion.FromRotationMatrixToRef(matrix, result);
  1611. return result;
  1612. };
  1613. Quaternion.FromRotationMatrixToRef = function (matrix, result) {
  1614. var data = matrix.m;
  1615. var m11 = data[0], m12 = data[4], m13 = data[8];
  1616. var m21 = data[1], m22 = data[5], m23 = data[9];
  1617. var m31 = data[2], m32 = data[6], m33 = data[10];
  1618. var trace = m11 + m22 + m33;
  1619. var s;
  1620. if (trace > 0) {
  1621. s = 0.5 / Math.sqrt(trace + 1.0);
  1622. result.w = 0.25 / s;
  1623. result.x = (m32 - m23) * s;
  1624. result.y = (m13 - m31) * s;
  1625. result.z = (m21 - m12) * s;
  1626. }
  1627. else if (m11 > m22 && m11 > m33) {
  1628. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1629. result.w = (m32 - m23) / s;
  1630. result.x = 0.25 * s;
  1631. result.y = (m12 + m21) / s;
  1632. result.z = (m13 + m31) / s;
  1633. }
  1634. else if (m22 > m33) {
  1635. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1636. result.w = (m13 - m31) / s;
  1637. result.x = (m12 + m21) / s;
  1638. result.y = 0.25 * s;
  1639. result.z = (m23 + m32) / s;
  1640. }
  1641. else {
  1642. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1643. result.w = (m21 - m12) / s;
  1644. result.x = (m13 + m31) / s;
  1645. result.y = (m23 + m32) / s;
  1646. result.z = 0.25 * s;
  1647. }
  1648. };
  1649. Quaternion.Inverse = function (q) {
  1650. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1651. };
  1652. Quaternion.Identity = function () {
  1653. return new Quaternion(0, 0, 0, 1);
  1654. };
  1655. Quaternion.RotationAxis = function (axis, angle) {
  1656. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  1657. };
  1658. Quaternion.RotationAxisToRef = function (axis, angle, result) {
  1659. var sin = Math.sin(angle / 2);
  1660. axis.normalize();
  1661. result.w = Math.cos(angle / 2);
  1662. result.x = axis.x * sin;
  1663. result.y = axis.y * sin;
  1664. result.z = axis.z * sin;
  1665. return result;
  1666. };
  1667. Quaternion.FromArray = function (array, offset) {
  1668. if (!offset) {
  1669. offset = 0;
  1670. }
  1671. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1672. };
  1673. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1674. var q = new Quaternion();
  1675. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  1676. return q;
  1677. };
  1678. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1679. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  1680. var halfRoll = roll * 0.5;
  1681. var halfPitch = pitch * 0.5;
  1682. var halfYaw = yaw * 0.5;
  1683. var sinRoll = Math.sin(halfRoll);
  1684. var cosRoll = Math.cos(halfRoll);
  1685. var sinPitch = Math.sin(halfPitch);
  1686. var cosPitch = Math.cos(halfPitch);
  1687. var sinYaw = Math.sin(halfYaw);
  1688. var cosYaw = Math.cos(halfYaw);
  1689. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1690. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1691. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1692. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1693. };
  1694. Quaternion.RotationAlphaBetaGamma = function (alpha, beta, gamma) {
  1695. var result = new Quaternion();
  1696. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  1697. return result;
  1698. };
  1699. Quaternion.RotationAlphaBetaGammaToRef = function (alpha, beta, gamma, result) {
  1700. // Produces a quaternion from Euler angles in the z-x-z orientation
  1701. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  1702. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  1703. var halfBeta = beta * 0.5;
  1704. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1705. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1706. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1707. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1708. };
  1709. Quaternion.Slerp = function (left, right, amount) {
  1710. var num2;
  1711. var num3;
  1712. var num = amount;
  1713. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1714. var flag = false;
  1715. if (num4 < 0) {
  1716. flag = true;
  1717. num4 = -num4;
  1718. }
  1719. if (num4 > 0.999999) {
  1720. num3 = 1 - num;
  1721. num2 = flag ? -num : num;
  1722. }
  1723. else {
  1724. var num5 = Math.acos(num4);
  1725. var num6 = (1.0 / Math.sin(num5));
  1726. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1727. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1728. }
  1729. 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));
  1730. };
  1731. return Quaternion;
  1732. }());
  1733. BABYLON.Quaternion = Quaternion;
  1734. var Matrix = (function () {
  1735. function Matrix() {
  1736. this.m = new Float32Array(16);
  1737. }
  1738. // Properties
  1739. Matrix.prototype.isIdentity = function () {
  1740. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1741. return false;
  1742. if (this.m[1] !== 0.0 || this.m[2] !== 0.0 || this.m[3] !== 0.0 ||
  1743. this.m[4] !== 0.0 || this.m[6] !== 0.0 || this.m[7] !== 0.0 ||
  1744. this.m[8] !== 0.0 || this.m[9] !== 0.0 || this.m[11] !== 0.0 ||
  1745. this.m[12] !== 0.0 || this.m[13] !== 0.0 || this.m[14] !== 0.0)
  1746. return false;
  1747. return true;
  1748. };
  1749. Matrix.prototype.determinant = function () {
  1750. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1751. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1752. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1753. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1754. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1755. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1756. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  1757. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  1758. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  1759. };
  1760. // Methods
  1761. Matrix.prototype.toArray = function () {
  1762. return this.m;
  1763. };
  1764. Matrix.prototype.asArray = function () {
  1765. return this.toArray();
  1766. };
  1767. Matrix.prototype.invert = function () {
  1768. this.invertToRef(this);
  1769. return this;
  1770. };
  1771. Matrix.prototype.reset = function () {
  1772. for (var index = 0; index < 16; index++) {
  1773. this.m[index] = 0;
  1774. }
  1775. return this;
  1776. };
  1777. Matrix.prototype.add = function (other) {
  1778. var result = new Matrix();
  1779. this.addToRef(other, result);
  1780. return result;
  1781. };
  1782. Matrix.prototype.addToRef = function (other, result) {
  1783. for (var index = 0; index < 16; index++) {
  1784. result.m[index] = this.m[index] + other.m[index];
  1785. }
  1786. return this;
  1787. };
  1788. Matrix.prototype.addToSelf = function (other) {
  1789. for (var index = 0; index < 16; index++) {
  1790. this.m[index] += other.m[index];
  1791. }
  1792. return this;
  1793. };
  1794. Matrix.prototype.invertToRef = function (other) {
  1795. var l1 = this.m[0];
  1796. var l2 = this.m[1];
  1797. var l3 = this.m[2];
  1798. var l4 = this.m[3];
  1799. var l5 = this.m[4];
  1800. var l6 = this.m[5];
  1801. var l7 = this.m[6];
  1802. var l8 = this.m[7];
  1803. var l9 = this.m[8];
  1804. var l10 = this.m[9];
  1805. var l11 = this.m[10];
  1806. var l12 = this.m[11];
  1807. var l13 = this.m[12];
  1808. var l14 = this.m[13];
  1809. var l15 = this.m[14];
  1810. var l16 = this.m[15];
  1811. var l17 = (l11 * l16) - (l12 * l15);
  1812. var l18 = (l10 * l16) - (l12 * l14);
  1813. var l19 = (l10 * l15) - (l11 * l14);
  1814. var l20 = (l9 * l16) - (l12 * l13);
  1815. var l21 = (l9 * l15) - (l11 * l13);
  1816. var l22 = (l9 * l14) - (l10 * l13);
  1817. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1818. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1819. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1820. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1821. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1822. var l28 = (l7 * l16) - (l8 * l15);
  1823. var l29 = (l6 * l16) - (l8 * l14);
  1824. var l30 = (l6 * l15) - (l7 * l14);
  1825. var l31 = (l5 * l16) - (l8 * l13);
  1826. var l32 = (l5 * l15) - (l7 * l13);
  1827. var l33 = (l5 * l14) - (l6 * l13);
  1828. var l34 = (l7 * l12) - (l8 * l11);
  1829. var l35 = (l6 * l12) - (l8 * l10);
  1830. var l36 = (l6 * l11) - (l7 * l10);
  1831. var l37 = (l5 * l12) - (l8 * l9);
  1832. var l38 = (l5 * l11) - (l7 * l9);
  1833. var l39 = (l5 * l10) - (l6 * l9);
  1834. other.m[0] = l23 * l27;
  1835. other.m[4] = l24 * l27;
  1836. other.m[8] = l25 * l27;
  1837. other.m[12] = l26 * l27;
  1838. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1839. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1840. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1841. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1842. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1843. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1844. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1845. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1846. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1847. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1848. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1849. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1850. return this;
  1851. };
  1852. Matrix.prototype.setTranslation = function (vector3) {
  1853. this.m[12] = vector3.x;
  1854. this.m[13] = vector3.y;
  1855. this.m[14] = vector3.z;
  1856. return this;
  1857. };
  1858. Matrix.prototype.getTranslation = function () {
  1859. return new Vector3(this.m[12], this.m[13], this.m[14]);
  1860. };
  1861. Matrix.prototype.multiply = function (other) {
  1862. var result = new Matrix();
  1863. this.multiplyToRef(other, result);
  1864. return result;
  1865. };
  1866. Matrix.prototype.copyFrom = function (other) {
  1867. for (var index = 0; index < 16; index++) {
  1868. this.m[index] = other.m[index];
  1869. }
  1870. return this;
  1871. };
  1872. Matrix.prototype.copyToArray = function (array, offset) {
  1873. if (offset === void 0) { offset = 0; }
  1874. for (var index = 0; index < 16; index++) {
  1875. array[offset + index] = this.m[index];
  1876. }
  1877. return this;
  1878. };
  1879. Matrix.prototype.multiplyToRef = function (other, result) {
  1880. this.multiplyToArray(other, result.m, 0);
  1881. return this;
  1882. };
  1883. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1884. var tm0 = this.m[0];
  1885. var tm1 = this.m[1];
  1886. var tm2 = this.m[2];
  1887. var tm3 = this.m[3];
  1888. var tm4 = this.m[4];
  1889. var tm5 = this.m[5];
  1890. var tm6 = this.m[6];
  1891. var tm7 = this.m[7];
  1892. var tm8 = this.m[8];
  1893. var tm9 = this.m[9];
  1894. var tm10 = this.m[10];
  1895. var tm11 = this.m[11];
  1896. var tm12 = this.m[12];
  1897. var tm13 = this.m[13];
  1898. var tm14 = this.m[14];
  1899. var tm15 = this.m[15];
  1900. var om0 = other.m[0];
  1901. var om1 = other.m[1];
  1902. var om2 = other.m[2];
  1903. var om3 = other.m[3];
  1904. var om4 = other.m[4];
  1905. var om5 = other.m[5];
  1906. var om6 = other.m[6];
  1907. var om7 = other.m[7];
  1908. var om8 = other.m[8];
  1909. var om9 = other.m[9];
  1910. var om10 = other.m[10];
  1911. var om11 = other.m[11];
  1912. var om12 = other.m[12];
  1913. var om13 = other.m[13];
  1914. var om14 = other.m[14];
  1915. var om15 = other.m[15];
  1916. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1917. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1918. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1919. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1920. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1921. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1922. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1923. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1924. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1925. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1926. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1927. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1928. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1929. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1930. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1931. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1932. return this;
  1933. };
  1934. Matrix.prototype.equals = function (value) {
  1935. return value &&
  1936. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  1937. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  1938. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  1939. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1940. };
  1941. Matrix.prototype.clone = function () {
  1942. 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]);
  1943. };
  1944. Matrix.prototype.getClassName = function () {
  1945. return "Matrix";
  1946. };
  1947. Matrix.prototype.getHashCode = function () {
  1948. var hash = this.m[0] || 0;
  1949. for (var i = 1; i < 16; i++) {
  1950. hash = (hash * 397) ^ (this.m[i] || 0);
  1951. }
  1952. return hash;
  1953. };
  1954. Matrix.prototype.decompose = function (scale, rotation, translation) {
  1955. translation.x = this.m[12];
  1956. translation.y = this.m[13];
  1957. translation.z = this.m[14];
  1958. var xs = MathTools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1959. var ys = MathTools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1960. var zs = MathTools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1961. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1962. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1963. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1964. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  1965. rotation.x = 0;
  1966. rotation.y = 0;
  1967. rotation.z = 0;
  1968. rotation.w = 1;
  1969. return false;
  1970. }
  1971. Matrix.FromValuesToRef(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, Tmp.Matrix[0]);
  1972. Quaternion.FromRotationMatrixToRef(Tmp.Matrix[0], rotation);
  1973. return true;
  1974. };
  1975. // Statics
  1976. Matrix.FromArray = function (array, offset) {
  1977. var result = new Matrix();
  1978. if (!offset) {
  1979. offset = 0;
  1980. }
  1981. Matrix.FromArrayToRef(array, offset, result);
  1982. return result;
  1983. };
  1984. Matrix.FromArrayToRef = function (array, offset, result) {
  1985. for (var index = 0; index < 16; index++) {
  1986. result.m[index] = array[index + offset];
  1987. }
  1988. };
  1989. Matrix.FromFloat32ArrayToRefScaled = function (array, offset, scale, result) {
  1990. for (var index = 0; index < 16; index++) {
  1991. result.m[index] = array[index + offset] * scale;
  1992. }
  1993. };
  1994. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1995. result.m[0] = initialM11;
  1996. result.m[1] = initialM12;
  1997. result.m[2] = initialM13;
  1998. result.m[3] = initialM14;
  1999. result.m[4] = initialM21;
  2000. result.m[5] = initialM22;
  2001. result.m[6] = initialM23;
  2002. result.m[7] = initialM24;
  2003. result.m[8] = initialM31;
  2004. result.m[9] = initialM32;
  2005. result.m[10] = initialM33;
  2006. result.m[11] = initialM34;
  2007. result.m[12] = initialM41;
  2008. result.m[13] = initialM42;
  2009. result.m[14] = initialM43;
  2010. result.m[15] = initialM44;
  2011. };
  2012. Matrix.prototype.getRow = function (index) {
  2013. if (index < 0 || index > 3) {
  2014. return null;
  2015. }
  2016. var i = index * 4;
  2017. return new Vector4(this.m[i + 0], this.m[i + 1], this.m[i + 2], this.m[i + 3]);
  2018. };
  2019. Matrix.prototype.setRow = function (index, row) {
  2020. if (index < 0 || index > 3) {
  2021. return this;
  2022. }
  2023. var i = index * 4;
  2024. this.m[i + 0] = row.x;
  2025. this.m[i + 1] = row.y;
  2026. this.m[i + 2] = row.z;
  2027. this.m[i + 3] = row.w;
  2028. return this;
  2029. };
  2030. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  2031. var result = new Matrix();
  2032. result.m[0] = initialM11;
  2033. result.m[1] = initialM12;
  2034. result.m[2] = initialM13;
  2035. result.m[3] = initialM14;
  2036. result.m[4] = initialM21;
  2037. result.m[5] = initialM22;
  2038. result.m[6] = initialM23;
  2039. result.m[7] = initialM24;
  2040. result.m[8] = initialM31;
  2041. result.m[9] = initialM32;
  2042. result.m[10] = initialM33;
  2043. result.m[11] = initialM34;
  2044. result.m[12] = initialM41;
  2045. result.m[13] = initialM42;
  2046. result.m[14] = initialM43;
  2047. result.m[15] = initialM44;
  2048. return result;
  2049. };
  2050. Matrix.Compose = function (scale, rotation, translation) {
  2051. var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
  2052. var rotationMatrix = Matrix.Identity();
  2053. rotation.toRotationMatrix(rotationMatrix);
  2054. result = result.multiply(rotationMatrix);
  2055. result.setTranslation(translation);
  2056. return result;
  2057. };
  2058. Matrix.Identity = function () {
  2059. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  2060. };
  2061. Matrix.IdentityToRef = function (result) {
  2062. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  2063. };
  2064. Matrix.Zero = function () {
  2065. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  2066. };
  2067. Matrix.RotationX = function (angle) {
  2068. var result = new Matrix();
  2069. Matrix.RotationXToRef(angle, result);
  2070. return result;
  2071. };
  2072. Matrix.Invert = function (source) {
  2073. var result = new Matrix();
  2074. source.invertToRef(result);
  2075. return result;
  2076. };
  2077. Matrix.RotationXToRef = function (angle, result) {
  2078. var s = Math.sin(angle);
  2079. var c = Math.cos(angle);
  2080. result.m[0] = 1.0;
  2081. result.m[15] = 1.0;
  2082. result.m[5] = c;
  2083. result.m[10] = c;
  2084. result.m[9] = -s;
  2085. result.m[6] = s;
  2086. result.m[1] = 0;
  2087. result.m[2] = 0;
  2088. result.m[3] = 0;
  2089. result.m[4] = 0;
  2090. result.m[7] = 0;
  2091. result.m[8] = 0;
  2092. result.m[11] = 0;
  2093. result.m[12] = 0;
  2094. result.m[13] = 0;
  2095. result.m[14] = 0;
  2096. };
  2097. Matrix.RotationY = function (angle) {
  2098. var result = new Matrix();
  2099. Matrix.RotationYToRef(angle, result);
  2100. return result;
  2101. };
  2102. Matrix.RotationYToRef = function (angle, result) {
  2103. var s = Math.sin(angle);
  2104. var c = Math.cos(angle);
  2105. result.m[5] = 1.0;
  2106. result.m[15] = 1.0;
  2107. result.m[0] = c;
  2108. result.m[2] = -s;
  2109. result.m[8] = s;
  2110. result.m[10] = c;
  2111. result.m[1] = 0;
  2112. result.m[3] = 0;
  2113. result.m[4] = 0;
  2114. result.m[6] = 0;
  2115. result.m[7] = 0;
  2116. result.m[9] = 0;
  2117. result.m[11] = 0;
  2118. result.m[12] = 0;
  2119. result.m[13] = 0;
  2120. result.m[14] = 0;
  2121. };
  2122. Matrix.RotationZ = function (angle) {
  2123. var result = new Matrix();
  2124. Matrix.RotationZToRef(angle, result);
  2125. return result;
  2126. };
  2127. Matrix.RotationZToRef = function (angle, result) {
  2128. var s = Math.sin(angle);
  2129. var c = Math.cos(angle);
  2130. result.m[10] = 1.0;
  2131. result.m[15] = 1.0;
  2132. result.m[0] = c;
  2133. result.m[1] = s;
  2134. result.m[4] = -s;
  2135. result.m[5] = c;
  2136. result.m[2] = 0;
  2137. result.m[3] = 0;
  2138. result.m[6] = 0;
  2139. result.m[7] = 0;
  2140. result.m[8] = 0;
  2141. result.m[9] = 0;
  2142. result.m[11] = 0;
  2143. result.m[12] = 0;
  2144. result.m[13] = 0;
  2145. result.m[14] = 0;
  2146. };
  2147. Matrix.RotationAxis = function (axis, angle) {
  2148. var result = Matrix.Zero();
  2149. Matrix.RotationAxisToRef(axis, angle, result);
  2150. return result;
  2151. };
  2152. Matrix.RotationAxisToRef = function (axis, angle, result) {
  2153. var s = Math.sin(-angle);
  2154. var c = Math.cos(-angle);
  2155. var c1 = 1 - c;
  2156. axis.normalize();
  2157. result.m[0] = (axis.x * axis.x) * c1 + c;
  2158. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  2159. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  2160. result.m[3] = 0.0;
  2161. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  2162. result.m[5] = (axis.y * axis.y) * c1 + c;
  2163. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  2164. result.m[7] = 0.0;
  2165. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  2166. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  2167. result.m[10] = (axis.z * axis.z) * c1 + c;
  2168. result.m[11] = 0.0;
  2169. result.m[15] = 1.0;
  2170. };
  2171. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  2172. var result = new Matrix();
  2173. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  2174. return result;
  2175. };
  2176. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  2177. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  2178. this._tempQuaternion.toRotationMatrix(result);
  2179. };
  2180. Matrix.Scaling = function (x, y, z) {
  2181. var result = Matrix.Zero();
  2182. Matrix.ScalingToRef(x, y, z, result);
  2183. return result;
  2184. };
  2185. Matrix.ScalingToRef = function (x, y, z, result) {
  2186. result.m[0] = x;
  2187. result.m[1] = 0;
  2188. result.m[2] = 0;
  2189. result.m[3] = 0;
  2190. result.m[4] = 0;
  2191. result.m[5] = y;
  2192. result.m[6] = 0;
  2193. result.m[7] = 0;
  2194. result.m[8] = 0;
  2195. result.m[9] = 0;
  2196. result.m[10] = z;
  2197. result.m[11] = 0;
  2198. result.m[12] = 0;
  2199. result.m[13] = 0;
  2200. result.m[14] = 0;
  2201. result.m[15] = 1.0;
  2202. };
  2203. Matrix.Translation = function (x, y, z) {
  2204. var result = Matrix.Identity();
  2205. Matrix.TranslationToRef(x, y, z, result);
  2206. return result;
  2207. };
  2208. Matrix.TranslationToRef = function (x, y, z, result) {
  2209. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  2210. };
  2211. Matrix.Lerp = function (startValue, endValue, gradient) {
  2212. var result = Matrix.Zero();
  2213. for (var index = 0; index < 16; index++) {
  2214. result.m[index] = startValue.m[index] * (1.0 - gradient) + endValue.m[index] * gradient;
  2215. }
  2216. return result;
  2217. };
  2218. Matrix.DecomposeLerp = function (startValue, endValue, gradient) {
  2219. var startScale = new Vector3(0, 0, 0);
  2220. var startRotation = new Quaternion();
  2221. var startTranslation = new Vector3(0, 0, 0);
  2222. startValue.decompose(startScale, startRotation, startTranslation);
  2223. var endScale = new Vector3(0, 0, 0);
  2224. var endRotation = new Quaternion();
  2225. var endTranslation = new Vector3(0, 0, 0);
  2226. endValue.decompose(endScale, endRotation, endTranslation);
  2227. var resultScale = Vector3.Lerp(startScale, endScale, gradient);
  2228. var resultRotation = Quaternion.Slerp(startRotation, endRotation, gradient);
  2229. var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
  2230. return Matrix.Compose(resultScale, resultRotation, resultTranslation);
  2231. };
  2232. Matrix.LookAtLH = function (eye, target, up) {
  2233. var result = Matrix.Zero();
  2234. Matrix.LookAtLHToRef(eye, target, up, result);
  2235. return result;
  2236. };
  2237. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  2238. // Z axis
  2239. target.subtractToRef(eye, this._zAxis);
  2240. this._zAxis.normalize();
  2241. // X axis
  2242. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  2243. if (this._xAxis.lengthSquared() === 0) {
  2244. this._xAxis.x = 1.0;
  2245. }
  2246. else {
  2247. this._xAxis.normalize();
  2248. }
  2249. // Y axis
  2250. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  2251. this._yAxis.normalize();
  2252. // Eye angles
  2253. var ex = -Vector3.Dot(this._xAxis, eye);
  2254. var ey = -Vector3.Dot(this._yAxis, eye);
  2255. var ez = -Vector3.Dot(this._zAxis, eye);
  2256. 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);
  2257. };
  2258. Matrix.LookAtRH = function (eye, target, up) {
  2259. var result = Matrix.Zero();
  2260. Matrix.LookAtRHToRef(eye, target, up, result);
  2261. return result;
  2262. };
  2263. Matrix.LookAtRHToRef = function (eye, target, up, result) {
  2264. // Z axis
  2265. eye.subtractToRef(target, this._zAxis);
  2266. this._zAxis.normalize();
  2267. // X axis
  2268. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  2269. if (this._xAxis.lengthSquared() === 0) {
  2270. this._xAxis.x = 1.0;
  2271. }
  2272. else {
  2273. this._xAxis.normalize();
  2274. }
  2275. // Y axis
  2276. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  2277. this._yAxis.normalize();
  2278. // Eye angles
  2279. var ex = -Vector3.Dot(this._xAxis, eye);
  2280. var ey = -Vector3.Dot(this._yAxis, eye);
  2281. var ez = -Vector3.Dot(this._zAxis, eye);
  2282. 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);
  2283. };
  2284. Matrix.OrthoLH = function (width, height, znear, zfar) {
  2285. var matrix = Matrix.Zero();
  2286. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  2287. return matrix;
  2288. };
  2289. Matrix.OrthoLHToRef = function (width, height, znear, zfar, result) {
  2290. var hw = 2.0 / width;
  2291. var hh = 2.0 / height;
  2292. var id = 1.0 / (zfar - znear);
  2293. var nid = znear / (znear - zfar);
  2294. Matrix.FromValuesToRef(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1, result);
  2295. };
  2296. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  2297. var matrix = Matrix.Zero();
  2298. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  2299. return matrix;
  2300. };
  2301. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  2302. result.m[0] = 2.0 / (right - left);
  2303. result.m[1] = result.m[2] = result.m[3] = 0;
  2304. result.m[5] = 2.0 / (top - bottom);
  2305. result.m[4] = result.m[6] = result.m[7] = 0;
  2306. result.m[10] = 1.0 / (zfar - znear);
  2307. result.m[8] = result.m[9] = result.m[11] = 0;
  2308. result.m[12] = (left + right) / (left - right);
  2309. result.m[13] = (top + bottom) / (bottom - top);
  2310. result.m[14] = -znear / (zfar - znear);
  2311. result.m[15] = 1.0;
  2312. };
  2313. Matrix.OrthoOffCenterRH = function (left, right, bottom, top, znear, zfar) {
  2314. var matrix = Matrix.Zero();
  2315. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  2316. return matrix;
  2317. };
  2318. Matrix.OrthoOffCenterRHToRef = function (left, right, bottom, top, znear, zfar, result) {
  2319. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  2320. result.m[10] *= -1.0;
  2321. };
  2322. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  2323. var matrix = Matrix.Zero();
  2324. matrix.m[0] = (2.0 * znear) / width;
  2325. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  2326. matrix.m[5] = (2.0 * znear) / height;
  2327. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  2328. matrix.m[10] = -zfar / (znear - zfar);
  2329. matrix.m[8] = matrix.m[9] = 0.0;
  2330. matrix.m[11] = 1.0;
  2331. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  2332. matrix.m[14] = (znear * zfar) / (znear - zfar);
  2333. return matrix;
  2334. };
  2335. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  2336. var matrix = Matrix.Zero();
  2337. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  2338. return matrix;
  2339. };
  2340. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result, isVerticalFovFixed) {
  2341. if (isVerticalFovFixed === void 0) { isVerticalFovFixed = true; }
  2342. var tan = 1.0 / (Math.tan(fov * 0.5));
  2343. if (isVerticalFovFixed) {
  2344. result.m[0] = tan / aspect;
  2345. }
  2346. else {
  2347. result.m[0] = tan;
  2348. }
  2349. result.m[1] = result.m[2] = result.m[3] = 0.0;
  2350. if (isVerticalFovFixed) {
  2351. result.m[5] = tan;
  2352. }
  2353. else {
  2354. result.m[5] = tan * aspect;
  2355. }
  2356. result.m[4] = result.m[6] = result.m[7] = 0.0;
  2357. result.m[8] = result.m[9] = 0.0;
  2358. result.m[10] = zfar / (zfar - znear);
  2359. result.m[11] = 1.0;
  2360. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2361. result.m[14] = -(znear * zfar) / (zfar - znear);
  2362. };
  2363. Matrix.PerspectiveFovRH = function (fov, aspect, znear, zfar) {
  2364. var matrix = Matrix.Zero();
  2365. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  2366. return matrix;
  2367. };
  2368. Matrix.PerspectiveFovRHToRef = function (fov, aspect, znear, zfar, result, isVerticalFovFixed) {
  2369. if (isVerticalFovFixed === void 0) { isVerticalFovFixed = true; }
  2370. var tan = 1.0 / (Math.tan(fov * 0.5));
  2371. if (isVerticalFovFixed) {
  2372. result.m[0] = tan / aspect;
  2373. }
  2374. else {
  2375. result.m[0] = tan;
  2376. }
  2377. result.m[1] = result.m[2] = result.m[3] = 0.0;
  2378. if (isVerticalFovFixed) {
  2379. result.m[5] = tan;
  2380. }
  2381. else {
  2382. result.m[5] = tan * aspect;
  2383. }
  2384. result.m[4] = result.m[6] = result.m[7] = 0.0;
  2385. result.m[8] = result.m[9] = 0.0;
  2386. result.m[10] = zfar / (znear - zfar);
  2387. result.m[11] = -1.0;
  2388. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2389. result.m[14] = (znear * zfar) / (znear - zfar);
  2390. };
  2391. Matrix.PerspectiveFovWebVRToRef = function (fov, znear, zfar, result, isVerticalFovFixed) {
  2392. if (isVerticalFovFixed === void 0) { isVerticalFovFixed = true; }
  2393. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  2394. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  2395. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  2396. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  2397. var xScale = 2.0 / (leftTan + rightTan);
  2398. var yScale = 2.0 / (upTan + downTan);
  2399. result.m[0] = xScale;
  2400. result.m[1] = result.m[2] = result.m[3] = result.m[4] = 0.0;
  2401. result.m[5] = yScale;
  2402. result.m[6] = result.m[7] = 0.0;
  2403. result.m[8] = ((leftTan - rightTan) * xScale * 0.5);
  2404. result.m[9] = -((upTan - downTan) * yScale * 0.5);
  2405. //result.m[10] = -(znear + zfar) / (zfar - znear);
  2406. result.m[10] = -zfar / (znear - zfar);
  2407. result.m[11] = 1.0;
  2408. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2409. //result.m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  2410. result.m[14] = (znear * zfar) / (znear - zfar);
  2411. };
  2412. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  2413. var cw = viewport.width;
  2414. var ch = viewport.height;
  2415. var cx = viewport.x;
  2416. var cy = viewport.y;
  2417. 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);
  2418. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  2419. };
  2420. Matrix.GetAsMatrix2x2 = function (matrix) {
  2421. return new Float32Array([
  2422. matrix.m[0], matrix.m[1],
  2423. matrix.m[4], matrix.m[5]
  2424. ]);
  2425. };
  2426. Matrix.GetAsMatrix3x3 = function (matrix) {
  2427. return new Float32Array([
  2428. matrix.m[0], matrix.m[1], matrix.m[2],
  2429. matrix.m[4], matrix.m[5], matrix.m[6],
  2430. matrix.m[8], matrix.m[9], matrix.m[10]
  2431. ]);
  2432. };
  2433. Matrix.Transpose = function (matrix) {
  2434. var result = new Matrix();
  2435. result.m[0] = matrix.m[0];
  2436. result.m[1] = matrix.m[4];
  2437. result.m[2] = matrix.m[8];
  2438. result.m[3] = matrix.m[12];
  2439. result.m[4] = matrix.m[1];
  2440. result.m[5] = matrix.m[5];
  2441. result.m[6] = matrix.m[9];
  2442. result.m[7] = matrix.m[13];
  2443. result.m[8] = matrix.m[2];
  2444. result.m[9] = matrix.m[6];
  2445. result.m[10] = matrix.m[10];
  2446. result.m[11] = matrix.m[14];
  2447. result.m[12] = matrix.m[3];
  2448. result.m[13] = matrix.m[7];
  2449. result.m[14] = matrix.m[11];
  2450. result.m[15] = matrix.m[15];
  2451. return result;
  2452. };
  2453. Matrix.Reflection = function (plane) {
  2454. var matrix = new Matrix();
  2455. Matrix.ReflectionToRef(plane, matrix);
  2456. return matrix;
  2457. };
  2458. Matrix.ReflectionToRef = function (plane, result) {
  2459. plane.normalize();
  2460. var x = plane.normal.x;
  2461. var y = plane.normal.y;
  2462. var z = plane.normal.z;
  2463. var temp = -2 * x;
  2464. var temp2 = -2 * y;
  2465. var temp3 = -2 * z;
  2466. result.m[0] = (temp * x) + 1;
  2467. result.m[1] = temp2 * x;
  2468. result.m[2] = temp3 * x;
  2469. result.m[3] = 0.0;
  2470. result.m[4] = temp * y;
  2471. result.m[5] = (temp2 * y) + 1;
  2472. result.m[6] = temp3 * y;
  2473. result.m[7] = 0.0;
  2474. result.m[8] = temp * z;
  2475. result.m[9] = temp2 * z;
  2476. result.m[10] = (temp3 * z) + 1;
  2477. result.m[11] = 0.0;
  2478. result.m[12] = temp * plane.d;
  2479. result.m[13] = temp2 * plane.d;
  2480. result.m[14] = temp3 * plane.d;
  2481. result.m[15] = 1.0;
  2482. };
  2483. Matrix.FromXYZAxesToRef = function (xaxis, yaxis, zaxis, mat) {
  2484. mat.m[0] = xaxis.x;
  2485. mat.m[1] = xaxis.y;
  2486. mat.m[2] = xaxis.z;
  2487. mat.m[3] = 0;
  2488. mat.m[4] = yaxis.x;
  2489. mat.m[5] = yaxis.y;
  2490. mat.m[6] = yaxis.z;
  2491. mat.m[7] = 0;
  2492. mat.m[8] = zaxis.x;
  2493. mat.m[9] = zaxis.y;
  2494. mat.m[10] = zaxis.z;
  2495. mat.m[11] = 0;
  2496. mat.m[12] = 0;
  2497. mat.m[13] = 0;
  2498. mat.m[14] = 0;
  2499. mat.m[15] = 1;
  2500. };
  2501. Matrix._tempQuaternion = new Quaternion();
  2502. Matrix._xAxis = Vector3.Zero();
  2503. Matrix._yAxis = Vector3.Zero();
  2504. Matrix._zAxis = Vector3.Zero();
  2505. return Matrix;
  2506. }());
  2507. BABYLON.Matrix = Matrix;
  2508. var Plane = (function () {
  2509. function Plane(a, b, c, d) {
  2510. this.normal = new Vector3(a, b, c);
  2511. this.d = d;
  2512. }
  2513. Plane.prototype.asArray = function () {
  2514. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  2515. };
  2516. // Methods
  2517. Plane.prototype.clone = function () {
  2518. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  2519. };
  2520. Plane.prototype.getClassName = function () {
  2521. return "Plane";
  2522. };
  2523. Plane.prototype.getHashCode = function () {
  2524. var hash = this.normal.getHashCode();
  2525. hash = (hash * 397) ^ (this.d || 0);
  2526. return hash;
  2527. };
  2528. Plane.prototype.normalize = function () {
  2529. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  2530. var magnitude = 0;
  2531. if (norm !== 0) {
  2532. magnitude = 1.0 / norm;
  2533. }
  2534. this.normal.x *= magnitude;
  2535. this.normal.y *= magnitude;
  2536. this.normal.z *= magnitude;
  2537. this.d *= magnitude;
  2538. return this;
  2539. };
  2540. Plane.prototype.transform = function (transformation) {
  2541. var transposedMatrix = Matrix.Transpose(transformation);
  2542. var x = this.normal.x;
  2543. var y = this.normal.y;
  2544. var z = this.normal.z;
  2545. var d = this.d;
  2546. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  2547. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  2548. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  2549. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  2550. return new Plane(normalX, normalY, normalZ, finalD);
  2551. };
  2552. Plane.prototype.dotCoordinate = function (point) {
  2553. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  2554. };
  2555. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  2556. var x1 = point2.x - point1.x;
  2557. var y1 = point2.y - point1.y;
  2558. var z1 = point2.z - point1.z;
  2559. var x2 = point3.x - point1.x;
  2560. var y2 = point3.y - point1.y;
  2561. var z2 = point3.z - point1.z;
  2562. var yz = (y1 * z2) - (z1 * y2);
  2563. var xz = (z1 * x2) - (x1 * z2);
  2564. var xy = (x1 * y2) - (y1 * x2);
  2565. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  2566. var invPyth;
  2567. if (pyth !== 0) {
  2568. invPyth = 1.0 / pyth;
  2569. }
  2570. else {
  2571. invPyth = 0;
  2572. }
  2573. this.normal.x = yz * invPyth;
  2574. this.normal.y = xz * invPyth;
  2575. this.normal.z = xy * invPyth;
  2576. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  2577. return this;
  2578. };
  2579. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  2580. var dot = Vector3.Dot(this.normal, direction);
  2581. return (dot <= epsilon);
  2582. };
  2583. Plane.prototype.signedDistanceTo = function (point) {
  2584. return Vector3.Dot(point, this.normal) + this.d;
  2585. };
  2586. // Statics
  2587. Plane.FromArray = function (array) {
  2588. return new Plane(array[0], array[1], array[2], array[3]);
  2589. };
  2590. Plane.FromPoints = function (point1, point2, point3) {
  2591. var result = new Plane(0, 0, 0, 0);
  2592. result.copyFromPoints(point1, point2, point3);
  2593. return result;
  2594. };
  2595. Plane.FromPositionAndNormal = function (origin, normal) {
  2596. var result = new Plane(0, 0, 0, 0);
  2597. normal.normalize();
  2598. result.normal = normal;
  2599. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2600. return result;
  2601. };
  2602. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  2603. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2604. return Vector3.Dot(point, normal) + d;
  2605. };
  2606. return Plane;
  2607. }());
  2608. BABYLON.Plane = Plane;
  2609. var Viewport = (function () {
  2610. function Viewport(x, y, width, height) {
  2611. this.x = x;
  2612. this.y = y;
  2613. this.width = width;
  2614. this.height = height;
  2615. }
  2616. Viewport.prototype.toGlobal = function (renderWidth, renderHeight) {
  2617. return new Viewport(this.x * renderWidth, this.y * renderHeight, this.width * renderWidth, this.height * renderHeight);
  2618. };
  2619. return Viewport;
  2620. }());
  2621. BABYLON.Viewport = Viewport;
  2622. var Frustum = (function () {
  2623. function Frustum() {
  2624. }
  2625. Frustum.GetPlanes = function (transform) {
  2626. var frustumPlanes = [];
  2627. for (var index = 0; index < 6; index++) {
  2628. frustumPlanes.push(new Plane(0, 0, 0, 0));
  2629. }
  2630. Frustum.GetPlanesToRef(transform, frustumPlanes);
  2631. return frustumPlanes;
  2632. };
  2633. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  2634. // Near
  2635. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  2636. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  2637. frustumPlanes[0].normal.z = transform.m[11] + transform.m[10];
  2638. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  2639. frustumPlanes[0].normalize();
  2640. // Far
  2641. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  2642. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  2643. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  2644. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  2645. frustumPlanes[1].normalize();
  2646. // Left
  2647. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  2648. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  2649. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  2650. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  2651. frustumPlanes[2].normalize();
  2652. // Right
  2653. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  2654. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  2655. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  2656. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  2657. frustumPlanes[3].normalize();
  2658. // Top
  2659. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  2660. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  2661. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  2662. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  2663. frustumPlanes[4].normalize();
  2664. // Bottom
  2665. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  2666. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  2667. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  2668. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  2669. frustumPlanes[5].normalize();
  2670. };
  2671. return Frustum;
  2672. }());
  2673. BABYLON.Frustum = Frustum;
  2674. (function (Space) {
  2675. Space[Space["LOCAL"] = 0] = "LOCAL";
  2676. Space[Space["WORLD"] = 1] = "WORLD";
  2677. })(BABYLON.Space || (BABYLON.Space = {}));
  2678. var Space = BABYLON.Space;
  2679. var Axis = (function () {
  2680. function Axis() {
  2681. }
  2682. Axis.X = new Vector3(1, 0, 0);
  2683. Axis.Y = new Vector3(0, 1, 0);
  2684. Axis.Z = new Vector3(0, 0, 1);
  2685. return Axis;
  2686. }());
  2687. BABYLON.Axis = Axis;
  2688. ;
  2689. var BezierCurve = (function () {
  2690. function BezierCurve() {
  2691. }
  2692. BezierCurve.interpolate = function (t, x1, y1, x2, y2) {
  2693. // Extract X (which is equal to time here)
  2694. var f0 = 1 - 3 * x2 + 3 * x1;
  2695. var f1 = 3 * x2 - 6 * x1;
  2696. var f2 = 3 * x1;
  2697. var refinedT = t;
  2698. for (var i = 0; i < 5; i++) {
  2699. var refinedT2 = refinedT * refinedT;
  2700. var refinedT3 = refinedT2 * refinedT;
  2701. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2702. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2703. refinedT -= (x - t) * slope;
  2704. refinedT = Math.min(1, Math.max(0, refinedT));
  2705. }
  2706. // Resolve cubic bezier for the given x
  2707. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
  2708. 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
  2709. Math.pow(refinedT, 3);
  2710. };
  2711. return BezierCurve;
  2712. }());
  2713. BABYLON.BezierCurve = BezierCurve;
  2714. (function (Orientation) {
  2715. Orientation[Orientation["CW"] = 0] = "CW";
  2716. Orientation[Orientation["CCW"] = 1] = "CCW";
  2717. })(BABYLON.Orientation || (BABYLON.Orientation = {}));
  2718. var Orientation = BABYLON.Orientation;
  2719. var Angle = (function () {
  2720. function Angle(radians) {
  2721. var _this = this;
  2722. this.degrees = function () { return _this._radians * 180 / Math.PI; };
  2723. this.radians = function () { return _this._radians; };
  2724. this._radians = radians;
  2725. if (this._radians < 0)
  2726. this._radians += (2 * Math.PI);
  2727. }
  2728. Angle.BetweenTwoPoints = function (a, b) {
  2729. var delta = b.subtract(a);
  2730. var theta = Math.atan2(delta.y, delta.x);
  2731. return new Angle(theta);
  2732. };
  2733. Angle.FromRadians = function (radians) {
  2734. return new Angle(radians);
  2735. };
  2736. Angle.FromDegrees = function (degrees) {
  2737. return new Angle(degrees * Math.PI / 180);
  2738. };
  2739. return Angle;
  2740. }());
  2741. BABYLON.Angle = Angle;
  2742. var Arc2 = (function () {
  2743. function Arc2(startPoint, midPoint, endPoint) {
  2744. this.startPoint = startPoint;
  2745. this.midPoint = midPoint;
  2746. this.endPoint = endPoint;
  2747. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2748. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2749. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2750. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2751. 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);
  2752. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2753. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2754. var a1 = this.startAngle.degrees();
  2755. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2756. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2757. // angles correction
  2758. if (a2 - a1 > +180.0)
  2759. a2 -= 360.0;
  2760. if (a2 - a1 < -180.0)
  2761. a2 += 360.0;
  2762. if (a3 - a2 > +180.0)
  2763. a3 -= 360.0;
  2764. if (a3 - a2 < -180.0)
  2765. a3 += 360.0;
  2766. this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
  2767. this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
  2768. }
  2769. return Arc2;
  2770. }());
  2771. BABYLON.Arc2 = Arc2;
  2772. var Path2 = (function () {
  2773. function Path2(x, y) {
  2774. this._points = new Array();
  2775. this._length = 0;
  2776. this.closed = false;
  2777. this._points.push(new Vector2(x, y));
  2778. }
  2779. Path2.prototype.addLineTo = function (x, y) {
  2780. if (closed) {
  2781. //Tools.Error("cannot add lines to closed paths");
  2782. return this;
  2783. }
  2784. var newPoint = new Vector2(x, y);
  2785. var previousPoint = this._points[this._points.length - 1];
  2786. this._points.push(newPoint);
  2787. this._length += newPoint.subtract(previousPoint).length();
  2788. return this;
  2789. };
  2790. Path2.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
  2791. if (numberOfSegments === void 0) { numberOfSegments = 36; }
  2792. if (closed) {
  2793. //Tools.Error("cannot add arcs to closed paths");
  2794. return this;
  2795. }
  2796. var startPoint = this._points[this._points.length - 1];
  2797. var midPoint = new Vector2(midX, midY);
  2798. var endPoint = new Vector2(endX, endY);
  2799. var arc = new Arc2(startPoint, midPoint, endPoint);
  2800. var increment = arc.angle.radians() / numberOfSegments;
  2801. if (arc.orientation === Orientation.CW)
  2802. increment *= -1;
  2803. var currentAngle = arc.startAngle.radians() + increment;
  2804. for (var i = 0; i < numberOfSegments; i++) {
  2805. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2806. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2807. this.addLineTo(x, y);
  2808. currentAngle += increment;
  2809. }
  2810. return this;
  2811. };
  2812. Path2.prototype.close = function () {
  2813. this.closed = true;
  2814. return this;
  2815. };
  2816. Path2.prototype.length = function () {
  2817. var result = this._length;
  2818. if (!this.closed) {
  2819. var lastPoint = this._points[this._points.length - 1];
  2820. var firstPoint = this._points[0];
  2821. result += (firstPoint.subtract(lastPoint).length());
  2822. }
  2823. return result;
  2824. };
  2825. Path2.prototype.getPoints = function () {
  2826. return this._points;
  2827. };
  2828. Path2.prototype.getPointAtLengthPosition = function (normalizedLengthPosition) {
  2829. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2830. //Tools.Error("normalized length position should be between 0 and 1.");
  2831. return Vector2.Zero();
  2832. }
  2833. var lengthPosition = normalizedLengthPosition * this.length();
  2834. var previousOffset = 0;
  2835. for (var i = 0; i < this._points.length; i++) {
  2836. var j = (i + 1) % this._points.length;
  2837. var a = this._points[i];
  2838. var b = this._points[j];
  2839. var bToA = b.subtract(a);
  2840. var nextOffset = (bToA.length() + previousOffset);
  2841. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2842. var dir = bToA.normalize();
  2843. var localOffset = lengthPosition - previousOffset;
  2844. return new Vector2(a.x + (dir.x * localOffset), a.y + (dir.y * localOffset));
  2845. }
  2846. previousOffset = nextOffset;
  2847. }
  2848. //Tools.Error("internal error");
  2849. return Vector2.Zero();
  2850. };
  2851. Path2.StartingAt = function (x, y) {
  2852. return new Path2(x, y);
  2853. };
  2854. return Path2;
  2855. }());
  2856. BABYLON.Path2 = Path2;
  2857. var Path3D = (function () {
  2858. /**
  2859. * new Path3D(path, normal, raw)
  2860. * Creates a Path3D. A Path3D is a logical math object, so not a mesh.
  2861. * please read the description in the tutorial : http://doc.babylonjs.com/tutorials/How_to_use_Path3D
  2862. * path : an array of Vector3, the curve axis of the Path3D
  2863. * normal (optional) : Vector3, the first wanted normal to the curve. Ex (0, 1, 0) for a vertical normal.
  2864. * raw (optional, default false) : boolean, if true the returned Path3D isn't normalized. Useful to depict path acceleration or speed.
  2865. */
  2866. function Path3D(path, firstNormal, raw) {
  2867. this.path = path;
  2868. this._curve = new Array();
  2869. this._distances = new Array();
  2870. this._tangents = new Array();
  2871. this._normals = new Array();
  2872. this._binormals = new Array();
  2873. for (var p = 0; p < path.length; p++) {
  2874. this._curve[p] = path[p].clone(); // hard copy
  2875. }
  2876. this._raw = raw || false;
  2877. this._compute(firstNormal);
  2878. }
  2879. /**
  2880. * Returns the Path3D array of successive Vector3 designing its curve.
  2881. */
  2882. Path3D.prototype.getCurve = function () {
  2883. return this._curve;
  2884. };
  2885. /**
  2886. * Returns an array populated with tangent vectors on each Path3D curve point.
  2887. */
  2888. Path3D.prototype.getTangents = function () {
  2889. return this._tangents;
  2890. };
  2891. /**
  2892. * Returns an array populated with normal vectors on each Path3D curve point.
  2893. */
  2894. Path3D.prototype.getNormals = function () {
  2895. return this._normals;
  2896. };
  2897. /**
  2898. * Returns an array populated with binormal vectors on each Path3D curve point.
  2899. */
  2900. Path3D.prototype.getBinormals = function () {
  2901. return this._binormals;
  2902. };
  2903. /**
  2904. * Returns an array populated with distances (float) of the i-th point from the first curve point.
  2905. */
  2906. Path3D.prototype.getDistances = function () {
  2907. return this._distances;
  2908. };
  2909. /**
  2910. * Forces the Path3D tangent, normal, binormal and distance recomputation.
  2911. * Returns the same object updated.
  2912. */
  2913. Path3D.prototype.update = function (path, firstNormal) {
  2914. for (var p = 0; p < path.length; p++) {
  2915. this._curve[p].x = path[p].x;
  2916. this._curve[p].y = path[p].y;
  2917. this._curve[p].z = path[p].z;
  2918. }
  2919. this._compute(firstNormal);
  2920. return this;
  2921. };
  2922. // private function compute() : computes tangents, normals and binormals
  2923. Path3D.prototype._compute = function (firstNormal) {
  2924. var l = this._curve.length;
  2925. // first and last tangents
  2926. this._tangents[0] = this._getFirstNonNullVector(0);
  2927. if (!this._raw) {
  2928. this._tangents[0].normalize();
  2929. }
  2930. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  2931. if (!this._raw) {
  2932. this._tangents[l - 1].normalize();
  2933. }
  2934. // normals and binormals at first point : arbitrary vector with _normalVector()
  2935. var tg0 = this._tangents[0];
  2936. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  2937. this._normals[0] = pp0;
  2938. if (!this._raw) {
  2939. this._normals[0].normalize();
  2940. }
  2941. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  2942. if (!this._raw) {
  2943. this._binormals[0].normalize();
  2944. }
  2945. this._distances[0] = 0.0;
  2946. // normals and binormals : next points
  2947. var prev; // previous vector (segment)
  2948. var cur; // current vector (segment)
  2949. var curTang; // current tangent
  2950. // previous normal
  2951. var prevBinor; // previous binormal
  2952. for (var i = 1; i < l; i++) {
  2953. // tangents
  2954. prev = this._getLastNonNullVector(i);
  2955. if (i < l - 1) {
  2956. cur = this._getFirstNonNullVector(i);
  2957. this._tangents[i] = prev.add(cur);
  2958. this._tangents[i].normalize();
  2959. }
  2960. this._distances[i] = this._distances[i - 1] + prev.length();
  2961. // normals and binormals
  2962. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  2963. curTang = this._tangents[i];
  2964. prevBinor = this._binormals[i - 1];
  2965. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  2966. if (!this._raw) {
  2967. this._normals[i].normalize();
  2968. }
  2969. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  2970. if (!this._raw) {
  2971. this._binormals[i].normalize();
  2972. }
  2973. }
  2974. };
  2975. // private function getFirstNonNullVector(index)
  2976. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  2977. Path3D.prototype._getFirstNonNullVector = function (index) {
  2978. var i = 1;
  2979. var nNVector = this._curve[index + i].subtract(this._curve[index]);
  2980. while (nNVector.length() === 0 && index + i + 1 < this._curve.length) {
  2981. i++;
  2982. nNVector = this._curve[index + i].subtract(this._curve[index]);
  2983. }
  2984. return nNVector;
  2985. };
  2986. // private function getLastNonNullVector(index)
  2987. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  2988. Path3D.prototype._getLastNonNullVector = function (index) {
  2989. var i = 1;
  2990. var nLVector = this._curve[index].subtract(this._curve[index - i]);
  2991. while (nLVector.length() === 0 && index > i + 1) {
  2992. i++;
  2993. nLVector = this._curve[index].subtract(this._curve[index - i]);
  2994. }
  2995. return nLVector;
  2996. };
  2997. // private function normalVector(v0, vt, va) :
  2998. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  2999. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  3000. Path3D.prototype._normalVector = function (v0, vt, va) {
  3001. var normal0;
  3002. var tgl = vt.length();
  3003. if (tgl === 0.0) {
  3004. tgl = 1.0;
  3005. }
  3006. if (va === undefined || va === null) {
  3007. var point;
  3008. if (!MathTools.WithinEpsilon(Math.abs(vt.y) / tgl, 1.0, BABYLON.Epsilon)) {
  3009. point = new Vector3(0.0, -1.0, 0.0);
  3010. }
  3011. else if (!MathTools.WithinEpsilon(Math.abs(vt.x) / tgl, 1.0, BABYLON.Epsilon)) {
  3012. point = new Vector3(1.0, 0.0, 0.0);
  3013. }
  3014. else if (!MathTools.WithinEpsilon(Math.abs(vt.z) / tgl, 1.0, BABYLON.Epsilon)) {
  3015. point = new Vector3(0.0, 0.0, 1.0);
  3016. }
  3017. normal0 = Vector3.Cross(vt, point);
  3018. }
  3019. else {
  3020. normal0 = Vector3.Cross(vt, va);
  3021. Vector3.CrossToRef(normal0, vt, normal0);
  3022. }
  3023. normal0.normalize();
  3024. return normal0;
  3025. };
  3026. return Path3D;
  3027. }());
  3028. BABYLON.Path3D = Path3D;
  3029. var Curve3 = (function () {
  3030. /**
  3031. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  3032. * A Curve3 is designed from a series of successive Vector3.
  3033. * Tuto : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#curve3-object
  3034. */
  3035. function Curve3(points) {
  3036. this._length = 0.0;
  3037. this._points = points;
  3038. this._length = this._computeLength(points);
  3039. }
  3040. /**
  3041. * Returns a Curve3 object along a Quadratic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#quadratic-bezier-curve
  3042. * @param v0 (Vector3) the origin point of the Quadratic Bezier
  3043. * @param v1 (Vector3) the control point
  3044. * @param v2 (Vector3) the end point of the Quadratic Bezier
  3045. * @param nbPoints (integer) the wanted number of points in the curve
  3046. */
  3047. Curve3.CreateQuadraticBezier = function (v0, v1, v2, nbPoints) {
  3048. nbPoints = nbPoints > 2 ? nbPoints : 3;
  3049. var bez = new Array();
  3050. var equation = function (t, val0, val1, val2) {
  3051. var res = (1.0 - t) * (1.0 - t) * val0 + 2.0 * t * (1.0 - t) * val1 + t * t * val2;
  3052. return res;
  3053. };
  3054. for (var i = 0; i <= nbPoints; i++) {
  3055. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x), equation(i / nbPoints, v0.y, v1.y, v2.y), equation(i / nbPoints, v0.z, v1.z, v2.z)));
  3056. }
  3057. return new Curve3(bez);
  3058. };
  3059. /**
  3060. * Returns a Curve3 object along a Cubic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#cubic-bezier-curve
  3061. * @param v0 (Vector3) the origin point of the Cubic Bezier
  3062. * @param v1 (Vector3) the first control point
  3063. * @param v2 (Vector3) the second control point
  3064. * @param v3 (Vector3) the end point of the Cubic Bezier
  3065. * @param nbPoints (integer) the wanted number of points in the curve
  3066. */
  3067. Curve3.CreateCubicBezier = function (v0, v1, v2, v3, nbPoints) {
  3068. nbPoints = nbPoints > 3 ? nbPoints : 4;
  3069. var bez = new Array();
  3070. var equation = function (t, val0, val1, val2, val3) {
  3071. var res = (1.0 - t) * (1.0 - t) * (1.0 - t) * val0 + 3.0 * t * (1.0 - t) * (1.0 - t) * val1 + 3.0 * t * t * (1.0 - t) * val2 + t * t * t * val3;
  3072. return res;
  3073. };
  3074. for (var i = 0; i <= nbPoints; i++) {
  3075. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x, v3.x), equation(i / nbPoints, v0.y, v1.y, v2.y, v3.y), equation(i / nbPoints, v0.z, v1.z, v2.z, v3.z)));
  3076. }
  3077. return new Curve3(bez);
  3078. };
  3079. /**
  3080. * Returns a Curve3 object along a Hermite Spline curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#hermite-spline
  3081. * @param p1 (Vector3) the origin point of the Hermite Spline
  3082. * @param t1 (Vector3) the tangent vector at the origin point
  3083. * @param p2 (Vector3) the end point of the Hermite Spline
  3084. * @param t2 (Vector3) the tangent vector at the end point
  3085. * @param nbPoints (integer) the wanted number of points in the curve
  3086. */
  3087. Curve3.CreateHermiteSpline = function (p1, t1, p2, t2, nbPoints) {
  3088. var hermite = new Array();
  3089. var step = 1.0 / nbPoints;
  3090. for (var i = 0; i <= nbPoints; i++) {
  3091. hermite.push(Vector3.Hermite(p1, t1, p2, t2, i * step));
  3092. }
  3093. return new Curve3(hermite);
  3094. };
  3095. /**
  3096. * Returns the Curve3 stored array of successive Vector3
  3097. */
  3098. Curve3.prototype.getPoints = function () {
  3099. return this._points;
  3100. };
  3101. /**
  3102. * Returns the computed length (float) of the curve.
  3103. */
  3104. Curve3.prototype.length = function () {
  3105. return this._length;
  3106. };
  3107. /**
  3108. * Returns a new instance of Curve3 object : var curve = curveA.continue(curveB);
  3109. * This new Curve3 is built by translating and sticking the curveB at the end of the curveA.
  3110. * curveA and curveB keep unchanged.
  3111. */
  3112. Curve3.prototype.continue = function (curve) {
  3113. var lastPoint = this._points[this._points.length - 1];
  3114. var continuedPoints = this._points.slice();
  3115. var curvePoints = curve.getPoints();
  3116. for (var i = 1; i < curvePoints.length; i++) {
  3117. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  3118. }
  3119. var continuedCurve = new Curve3(continuedPoints);
  3120. return continuedCurve;
  3121. };
  3122. Curve3.prototype._computeLength = function (path) {
  3123. var l = 0;
  3124. for (var i = 1; i < path.length; i++) {
  3125. l += (path[i].subtract(path[i - 1])).length();
  3126. }
  3127. return l;
  3128. };
  3129. return Curve3;
  3130. }());
  3131. BABYLON.Curve3 = Curve3;
  3132. // SphericalHarmonics
  3133. var SphericalHarmonics = (function () {
  3134. function SphericalHarmonics() {
  3135. this.L00 = Vector3.Zero();
  3136. this.L1_1 = Vector3.Zero();
  3137. this.L10 = Vector3.Zero();
  3138. this.L11 = Vector3.Zero();
  3139. this.L2_2 = Vector3.Zero();
  3140. this.L2_1 = Vector3.Zero();
  3141. this.L20 = Vector3.Zero();
  3142. this.L21 = Vector3.Zero();
  3143. this.L22 = Vector3.Zero();
  3144. }
  3145. SphericalHarmonics.prototype.addLight = function (direction, color, deltaSolidAngle) {
  3146. var colorVector = new Vector3(color.r, color.g, color.b);
  3147. var c = colorVector.scale(deltaSolidAngle);
  3148. this.L00 = this.L00.add(c.scale(0.282095));
  3149. this.L1_1 = this.L1_1.add(c.scale(0.488603 * direction.y));
  3150. this.L10 = this.L10.add(c.scale(0.488603 * direction.z));
  3151. this.L11 = this.L11.add(c.scale(0.488603 * direction.x));
  3152. this.L2_2 = this.L2_2.add(c.scale(1.092548 * direction.x * direction.y));
  3153. this.L2_1 = this.L2_1.add(c.scale(1.092548 * direction.y * direction.z));
  3154. this.L21 = this.L21.add(c.scale(1.092548 * direction.x * direction.z));
  3155. this.L20 = this.L20.add(c.scale(0.315392 * (3.0 * direction.z * direction.z - 1.0)));
  3156. this.L22 = this.L22.add(c.scale(0.546274 * (direction.x * direction.x - direction.y * direction.y)));
  3157. };
  3158. SphericalHarmonics.prototype.scale = function (scale) {
  3159. this.L00 = this.L00.scale(scale);
  3160. this.L1_1 = this.L1_1.scale(scale);
  3161. this.L10 = this.L10.scale(scale);
  3162. this.L11 = this.L11.scale(scale);
  3163. this.L2_2 = this.L2_2.scale(scale);
  3164. this.L2_1 = this.L2_1.scale(scale);
  3165. this.L20 = this.L20.scale(scale);
  3166. this.L21 = this.L21.scale(scale);
  3167. this.L22 = this.L22.scale(scale);
  3168. };
  3169. return SphericalHarmonics;
  3170. }());
  3171. BABYLON.SphericalHarmonics = SphericalHarmonics;
  3172. // SphericalPolynomial
  3173. var SphericalPolynomial = (function () {
  3174. function SphericalPolynomial() {
  3175. this.x = Vector3.Zero();
  3176. this.y = Vector3.Zero();
  3177. this.z = Vector3.Zero();
  3178. this.xx = Vector3.Zero();
  3179. this.yy = Vector3.Zero();
  3180. this.zz = Vector3.Zero();
  3181. this.xy = Vector3.Zero();
  3182. this.yz = Vector3.Zero();
  3183. this.zx = Vector3.Zero();
  3184. }
  3185. SphericalPolynomial.prototype.addAmbient = function (color) {
  3186. var colorVector = new Vector3(color.r, color.g, color.b);
  3187. this.xx = this.xx.add(colorVector);
  3188. this.yy = this.yy.add(colorVector);
  3189. this.zz = this.zz.add(colorVector);
  3190. };
  3191. SphericalPolynomial.getSphericalPolynomialFromHarmonics = function (harmonics) {
  3192. var result = new SphericalPolynomial();
  3193. result.x = harmonics.L11.scale(1.02333);
  3194. result.y = harmonics.L1_1.scale(1.02333);
  3195. result.z = harmonics.L10.scale(1.02333);
  3196. result.xx = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).add(harmonics.L22.scale(0.429043));
  3197. result.yy = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).subtract(harmonics.L22.scale(0.429043));
  3198. result.zz = harmonics.L00.scale(0.886277).add(harmonics.L20.scale(0.495417));
  3199. result.yz = harmonics.L2_1.scale(0.858086);
  3200. result.zx = harmonics.L21.scale(0.858086);
  3201. result.xy = harmonics.L2_2.scale(0.858086);
  3202. return result;
  3203. };
  3204. return SphericalPolynomial;
  3205. }());
  3206. BABYLON.SphericalPolynomial = SphericalPolynomial;
  3207. // Vertex formats
  3208. var PositionNormalVertex = (function () {
  3209. function PositionNormalVertex(position, normal) {
  3210. if (position === void 0) { position = Vector3.Zero(); }
  3211. if (normal === void 0) { normal = Vector3.Up(); }
  3212. this.position = position;
  3213. this.normal = normal;
  3214. }
  3215. PositionNormalVertex.prototype.clone = function () {
  3216. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  3217. };
  3218. return PositionNormalVertex;
  3219. }());
  3220. BABYLON.PositionNormalVertex = PositionNormalVertex;
  3221. var PositionNormalTextureVertex = (function () {
  3222. function PositionNormalTextureVertex(position, normal, uv) {
  3223. if (position === void 0) { position = Vector3.Zero(); }
  3224. if (normal === void 0) { normal = Vector3.Up(); }
  3225. if (uv === void 0) { uv = Vector2.Zero(); }
  3226. this.position = position;
  3227. this.normal = normal;
  3228. this.uv = uv;
  3229. }
  3230. PositionNormalTextureVertex.prototype.clone = function () {
  3231. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  3232. };
  3233. return PositionNormalTextureVertex;
  3234. }());
  3235. BABYLON.PositionNormalTextureVertex = PositionNormalTextureVertex;
  3236. // Temporary pre-allocated objects for engine internal use
  3237. // usage in any internal function :
  3238. // var tmp = Tmp.Vector3[0]; <= gets access to the first pre-created Vector3
  3239. // There's a Tmp array per object type : int, float, Vector2, Vector3, Vector4, Quaternion, Matrix
  3240. var Tmp = (function () {
  3241. function Tmp() {
  3242. }
  3243. Tmp.Color3 = [Color3.Black(), Color3.Black(), Color3.Black()];
  3244. Tmp.Vector2 = [Vector2.Zero(), Vector2.Zero(), Vector2.Zero()]; // 3 temp Vector2 at once should be enough
  3245. Tmp.Vector3 = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(),
  3246. Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()]; // 9 temp Vector3 at once should be enough
  3247. Tmp.Vector4 = [Vector4.Zero(), Vector4.Zero(), Vector4.Zero()]; // 3 temp Vector4 at once should be enough
  3248. Tmp.Quaternion = [new Quaternion(0, 0, 0, 0)]; // 1 temp Quaternion at once should be enough
  3249. Tmp.Matrix = [Matrix.Zero(), Matrix.Zero(),
  3250. Matrix.Zero(), Matrix.Zero(),
  3251. Matrix.Zero(), Matrix.Zero(),
  3252. Matrix.Zero(), Matrix.Zero()]; // 6 temp Matrices at once should be enough
  3253. return Tmp;
  3254. }());
  3255. BABYLON.Tmp = Tmp;
  3256. })(BABYLON || (BABYLON = {}));