babylon.math.js 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226
  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.TransformCoordinates = function (vector, transformation) {
  822. var result = Vector3.Zero();
  823. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  824. return result;
  825. };
  826. Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  827. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  828. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  829. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  830. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  831. result.x = x / w;
  832. result.y = y / w;
  833. result.z = z / w;
  834. };
  835. Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  836. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  837. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  838. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  839. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  840. result.x = rx / rw;
  841. result.y = ry / rw;
  842. result.z = rz / rw;
  843. };
  844. Vector3.TransformNormal = function (vector, transformation) {
  845. var result = Vector3.Zero();
  846. Vector3.TransformNormalToRef(vector, transformation, result);
  847. return result;
  848. };
  849. Vector3.TransformNormalToRef = function (vector, transformation, result) {
  850. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  851. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  852. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  853. };
  854. Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  855. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  856. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  857. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  858. };
  859. Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  860. var squared = amount * amount;
  861. var cubed = amount * squared;
  862. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  863. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  864. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  865. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  866. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  867. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  868. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  869. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  870. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  871. return new Vector3(x, y, z);
  872. };
  873. Vector3.Clamp = function (value, min, max) {
  874. var x = value.x;
  875. x = (x > max.x) ? max.x : x;
  876. x = (x < min.x) ? min.x : x;
  877. var y = value.y;
  878. y = (y > max.y) ? max.y : y;
  879. y = (y < min.y) ? min.y : y;
  880. var z = value.z;
  881. z = (z > max.z) ? max.z : z;
  882. z = (z < min.z) ? min.z : z;
  883. return new Vector3(x, y, z);
  884. };
  885. Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  886. var squared = amount * amount;
  887. var cubed = amount * squared;
  888. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  889. var part2 = (-2.0 * cubed) + (3.0 * squared);
  890. var part3 = (cubed - (2.0 * squared)) + amount;
  891. var part4 = cubed - squared;
  892. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  893. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  894. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  895. return new Vector3(x, y, z);
  896. };
  897. Vector3.Lerp = function (start, end, amount) {
  898. var x = start.x + ((end.x - start.x) * amount);
  899. var y = start.y + ((end.y - start.y) * amount);
  900. var z = start.z + ((end.z - start.z) * amount);
  901. return new Vector3(x, y, z);
  902. };
  903. Vector3.Dot = function (left, right) {
  904. return (left.x * right.x + left.y * right.y + left.z * right.z);
  905. };
  906. Vector3.Cross = function (left, right) {
  907. var result = Vector3.Zero();
  908. Vector3.CrossToRef(left, right, result);
  909. return result;
  910. };
  911. Vector3.CrossToRef = function (left, right, result) {
  912. Tmp.Vector3[0].x = left.y * right.z - left.z * right.y;
  913. Tmp.Vector3[0].y = left.z * right.x - left.x * right.z;
  914. Tmp.Vector3[0].z = left.x * right.y - left.y * right.x;
  915. result.copyFrom(Tmp.Vector3[0]);
  916. };
  917. Vector3.Normalize = function (vector) {
  918. var result = Vector3.Zero();
  919. Vector3.NormalizeToRef(vector, result);
  920. return result;
  921. };
  922. Vector3.NormalizeToRef = function (vector, result) {
  923. result.copyFrom(vector);
  924. result.normalize();
  925. };
  926. Vector3.Project = function (vector, world, transform, viewport) {
  927. var cw = viewport.width;
  928. var ch = viewport.height;
  929. var cx = viewport.x;
  930. var cy = viewport.y;
  931. var viewportMatrix = Vector3._viewportMatrixCache ? Vector3._viewportMatrixCache : (Vector3._viewportMatrixCache = new Matrix());
  932. 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);
  933. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  934. world.multiplyToRef(transform, matrix);
  935. matrix.multiplyToRef(viewportMatrix, matrix);
  936. return Vector3.TransformCoordinates(vector, matrix);
  937. };
  938. Vector3.UnprojectFromTransform = function (source, viewportWidth, viewportHeight, world, transform) {
  939. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  940. world.multiplyToRef(transform, matrix);
  941. matrix.invert();
  942. source.x = source.x / viewportWidth * 2 - 1;
  943. source.y = -(source.y / viewportHeight * 2 - 1);
  944. var vector = Vector3.TransformCoordinates(source, matrix);
  945. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  946. if (MathTools.WithinEpsilon(num, 1.0)) {
  947. vector = vector.scale(1.0 / num);
  948. }
  949. return vector;
  950. };
  951. Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  952. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  953. world.multiplyToRef(view, matrix);
  954. matrix.multiplyToRef(projection, matrix);
  955. matrix.invert();
  956. var screenSource = new Vector3(source.x / viewportWidth * 2 - 1, -(source.y / viewportHeight * 2 - 1), source.z);
  957. var vector = Vector3.TransformCoordinates(screenSource, matrix);
  958. var num = screenSource.x * matrix.m[3] + screenSource.y * matrix.m[7] + screenSource.z * matrix.m[11] + matrix.m[15];
  959. if (MathTools.WithinEpsilon(num, 1.0)) {
  960. vector = vector.scale(1.0 / num);
  961. }
  962. return vector;
  963. };
  964. Vector3.Minimize = function (left, right) {
  965. var min = left.clone();
  966. min.MinimizeInPlace(right);
  967. return min;
  968. };
  969. Vector3.Maximize = function (left, right) {
  970. var max = left.clone();
  971. max.MaximizeInPlace(right);
  972. return max;
  973. };
  974. Vector3.Distance = function (value1, value2) {
  975. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  976. };
  977. Vector3.DistanceSquared = function (value1, value2) {
  978. var x = value1.x - value2.x;
  979. var y = value1.y - value2.y;
  980. var z = value1.z - value2.z;
  981. return (x * x) + (y * y) + (z * z);
  982. };
  983. Vector3.Center = function (value1, value2) {
  984. var center = value1.add(value2);
  985. center.scaleInPlace(0.5);
  986. return center;
  987. };
  988. /**
  989. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  990. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  991. * to something in order to rotate it from its local system to the given target system.
  992. */
  993. Vector3.RotationFromAxis = function (axis1, axis2, axis3) {
  994. var rotation = Vector3.Zero();
  995. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  996. return rotation;
  997. };
  998. /**
  999. * The same than RotationFromAxis but updates the passed ref Vector3 parameter.
  1000. */
  1001. Vector3.RotationFromAxisToRef = function (axis1, axis2, axis3, ref) {
  1002. var u = axis1.normalize();
  1003. var w = axis3.normalize();
  1004. // world axis
  1005. var X = Axis.X;
  1006. var Y = Axis.Y;
  1007. // equation unknowns and vars
  1008. var yaw = 0.0;
  1009. var pitch = 0.0;
  1010. var roll = 0.0;
  1011. var x = 0.0;
  1012. var y = 0.0;
  1013. var z = 0.0;
  1014. var t = 0.0;
  1015. var sign = -1.0;
  1016. var nbRevert = 0;
  1017. var cross = Tmp.Vector3[0];
  1018. var dot = 0.0;
  1019. // step 1 : rotation around w
  1020. // Rv3(u) = u1, and u1 belongs to plane xOz
  1021. // Rv3(w) = w1 = w invariant
  1022. var u1 = Tmp.Vector3[1];
  1023. if (MathTools.WithinEpsilon(w.z, 0, BABYLON.Epsilon)) {
  1024. z = 1.0;
  1025. }
  1026. else if (MathTools.WithinEpsilon(w.x, 0, BABYLON.Epsilon)) {
  1027. x = 1.0;
  1028. }
  1029. else {
  1030. t = w.z / w.x;
  1031. x = -t * Math.sqrt(1 / (1 + t * t));
  1032. z = Math.sqrt(1 / (1 + t * t));
  1033. }
  1034. u1.x = x;
  1035. u1.y = y;
  1036. u1.z = z;
  1037. u1.normalize();
  1038. Vector3.CrossToRef(u, u1, cross); // returns same direction as w (=local z) if positive angle : cross(source, image)
  1039. cross.normalize();
  1040. if (Vector3.Dot(w, cross) < 0) {
  1041. sign = 1.0;
  1042. }
  1043. dot = Vector3.Dot(u, u1);
  1044. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  1045. roll = Math.acos(dot) * sign;
  1046. if (Vector3.Dot(u1, X) < 0) {
  1047. roll = Math.PI + roll;
  1048. u1 = u1.scaleInPlace(-1);
  1049. nbRevert++;
  1050. }
  1051. // step 2 : rotate around u1
  1052. // Ru1(w1) = Ru1(w) = w2, and w2 belongs to plane xOz
  1053. // u1 is yet in xOz and invariant by Ru1, so after this step u1 and w2 will be in xOz
  1054. var w2 = Tmp.Vector3[2];
  1055. var v2 = Tmp.Vector3[3];
  1056. x = 0.0;
  1057. y = 0.0;
  1058. z = 0.0;
  1059. sign = -1.0;
  1060. if (MathTools.WithinEpsilon(w.z, 0, BABYLON.Epsilon)) {
  1061. x = 1.0;
  1062. }
  1063. else {
  1064. t = u1.z / u1.x;
  1065. x = -t * Math.sqrt(1 / (1 + t * t));
  1066. z = Math.sqrt(1 / (1 + t * t));
  1067. }
  1068. w2.x = x;
  1069. w2.y = y;
  1070. w2.z = z;
  1071. w2.normalize();
  1072. Vector3.CrossToRef(w2, u1, v2); // v2 image of v1 through rotation around u1
  1073. v2.normalize();
  1074. Vector3.CrossToRef(w, w2, cross); // returns same direction as u1 (=local x) if positive angle : cross(source, image)
  1075. cross.normalize();
  1076. if (Vector3.Dot(u1, cross) < 0) {
  1077. sign = 1.0;
  1078. }
  1079. dot = Vector3.Dot(w, w2);
  1080. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  1081. pitch = Math.acos(dot) * sign;
  1082. if (Vector3.Dot(v2, Y) < 0) {
  1083. pitch = Math.PI + pitch;
  1084. nbRevert++;
  1085. }
  1086. // step 3 : rotate around v2
  1087. // Rv2(u1) = X, same as Rv2(w2) = Z, with X=(1,0,0) and Z=(0,0,1)
  1088. sign = -1.0;
  1089. Vector3.CrossToRef(X, u1, cross); // returns same direction as Y if positive angle : cross(source, image)
  1090. cross.normalize();
  1091. if (Vector3.Dot(cross, Y) < 0) {
  1092. sign = 1.0;
  1093. }
  1094. dot = Vector3.Dot(u1, X);
  1095. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  1096. yaw = -Math.acos(dot) * sign; // negative : plane zOx oriented clockwise
  1097. if (dot < 0 && nbRevert < 2) {
  1098. yaw = Math.PI + yaw;
  1099. }
  1100. ref.x = pitch;
  1101. ref.y = yaw;
  1102. ref.z = roll;
  1103. };
  1104. return Vector3;
  1105. }());
  1106. BABYLON.Vector3 = Vector3;
  1107. //Vector4 class created for EulerAngle class conversion to Quaternion
  1108. var Vector4 = (function () {
  1109. function Vector4(x, y, z, w) {
  1110. this.x = x;
  1111. this.y = y;
  1112. this.z = z;
  1113. this.w = w;
  1114. }
  1115. Vector4.prototype.toString = function () {
  1116. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1117. };
  1118. Vector4.prototype.getClassName = function () {
  1119. return "Vector4";
  1120. };
  1121. Vector4.prototype.getHashCode = function () {
  1122. var hash = this.x || 0;
  1123. hash = (hash * 397) ^ (this.y || 0);
  1124. hash = (hash * 397) ^ (this.z || 0);
  1125. hash = (hash * 397) ^ (this.w || 0);
  1126. return hash;
  1127. };
  1128. // Operators
  1129. Vector4.prototype.asArray = function () {
  1130. var result = [];
  1131. this.toArray(result, 0);
  1132. return result;
  1133. };
  1134. Vector4.prototype.toArray = function (array, index) {
  1135. if (index === undefined) {
  1136. index = 0;
  1137. }
  1138. array[index] = this.x;
  1139. array[index + 1] = this.y;
  1140. array[index + 2] = this.z;
  1141. array[index + 3] = this.w;
  1142. return this;
  1143. };
  1144. Vector4.prototype.addInPlace = function (otherVector) {
  1145. this.x += otherVector.x;
  1146. this.y += otherVector.y;
  1147. this.z += otherVector.z;
  1148. this.w += otherVector.w;
  1149. return this;
  1150. };
  1151. Vector4.prototype.add = function (otherVector) {
  1152. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  1153. };
  1154. Vector4.prototype.addToRef = function (otherVector, result) {
  1155. result.x = this.x + otherVector.x;
  1156. result.y = this.y + otherVector.y;
  1157. result.z = this.z + otherVector.z;
  1158. result.w = this.w + otherVector.w;
  1159. return this;
  1160. };
  1161. Vector4.prototype.subtractInPlace = function (otherVector) {
  1162. this.x -= otherVector.x;
  1163. this.y -= otherVector.y;
  1164. this.z -= otherVector.z;
  1165. this.w -= otherVector.w;
  1166. return this;
  1167. };
  1168. Vector4.prototype.subtract = function (otherVector) {
  1169. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  1170. };
  1171. Vector4.prototype.subtractToRef = function (otherVector, result) {
  1172. result.x = this.x - otherVector.x;
  1173. result.y = this.y - otherVector.y;
  1174. result.z = this.z - otherVector.z;
  1175. result.w = this.w - otherVector.w;
  1176. return this;
  1177. };
  1178. Vector4.prototype.subtractFromFloats = function (x, y, z, w) {
  1179. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  1180. };
  1181. Vector4.prototype.subtractFromFloatsToRef = function (x, y, z, w, result) {
  1182. result.x = this.x - x;
  1183. result.y = this.y - y;
  1184. result.z = this.z - z;
  1185. result.w = this.w - w;
  1186. return this;
  1187. };
  1188. Vector4.prototype.negate = function () {
  1189. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  1190. };
  1191. Vector4.prototype.scaleInPlace = function (scale) {
  1192. this.x *= scale;
  1193. this.y *= scale;
  1194. this.z *= scale;
  1195. this.w *= scale;
  1196. return this;
  1197. };
  1198. Vector4.prototype.scale = function (scale) {
  1199. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  1200. };
  1201. Vector4.prototype.scaleToRef = function (scale, result) {
  1202. result.x = this.x * scale;
  1203. result.y = this.y * scale;
  1204. result.z = this.z * scale;
  1205. result.w = this.w * scale;
  1206. };
  1207. Vector4.prototype.equals = function (otherVector) {
  1208. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  1209. };
  1210. Vector4.prototype.equalsWithEpsilon = function (otherVector, epsilon) {
  1211. if (epsilon === void 0) { epsilon = BABYLON.Epsilon; }
  1212. return otherVector
  1213. && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon)
  1214. && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon)
  1215. && MathTools.WithinEpsilon(this.z, otherVector.z, epsilon)
  1216. && MathTools.WithinEpsilon(this.w, otherVector.w, epsilon);
  1217. };
  1218. Vector4.prototype.equalsToFloats = function (x, y, z, w) {
  1219. return this.x === x && this.y === y && this.z === z && this.w === w;
  1220. };
  1221. Vector4.prototype.multiplyInPlace = function (otherVector) {
  1222. this.x *= otherVector.x;
  1223. this.y *= otherVector.y;
  1224. this.z *= otherVector.z;
  1225. this.w *= otherVector.w;
  1226. return this;
  1227. };
  1228. Vector4.prototype.multiply = function (otherVector) {
  1229. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  1230. };
  1231. Vector4.prototype.multiplyToRef = function (otherVector, result) {
  1232. result.x = this.x * otherVector.x;
  1233. result.y = this.y * otherVector.y;
  1234. result.z = this.z * otherVector.z;
  1235. result.w = this.w * otherVector.w;
  1236. return this;
  1237. };
  1238. Vector4.prototype.multiplyByFloats = function (x, y, z, w) {
  1239. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  1240. };
  1241. Vector4.prototype.divide = function (otherVector) {
  1242. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  1243. };
  1244. Vector4.prototype.divideToRef = function (otherVector, result) {
  1245. result.x = this.x / otherVector.x;
  1246. result.y = this.y / otherVector.y;
  1247. result.z = this.z / otherVector.z;
  1248. result.w = this.w / otherVector.w;
  1249. return this;
  1250. };
  1251. Vector4.prototype.MinimizeInPlace = function (other) {
  1252. if (other.x < this.x)
  1253. this.x = other.x;
  1254. if (other.y < this.y)
  1255. this.y = other.y;
  1256. if (other.z < this.z)
  1257. this.z = other.z;
  1258. if (other.w < this.w)
  1259. this.w = other.w;
  1260. return this;
  1261. };
  1262. Vector4.prototype.MaximizeInPlace = function (other) {
  1263. if (other.x > this.x)
  1264. this.x = other.x;
  1265. if (other.y > this.y)
  1266. this.y = other.y;
  1267. if (other.z > this.z)
  1268. this.z = other.z;
  1269. if (other.w > this.w)
  1270. this.w = other.w;
  1271. return this;
  1272. };
  1273. // Properties
  1274. Vector4.prototype.length = function () {
  1275. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1276. };
  1277. Vector4.prototype.lengthSquared = function () {
  1278. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1279. };
  1280. // Methods
  1281. Vector4.prototype.normalize = function () {
  1282. var len = this.length();
  1283. if (len === 0)
  1284. return this;
  1285. var num = 1.0 / len;
  1286. this.x *= num;
  1287. this.y *= num;
  1288. this.z *= num;
  1289. this.w *= num;
  1290. return this;
  1291. };
  1292. Vector4.prototype.toVector3 = function () {
  1293. return new Vector3(this.x, this.y, this.z);
  1294. };
  1295. Vector4.prototype.clone = function () {
  1296. return new Vector4(this.x, this.y, this.z, this.w);
  1297. };
  1298. Vector4.prototype.copyFrom = function (source) {
  1299. this.x = source.x;
  1300. this.y = source.y;
  1301. this.z = source.z;
  1302. this.w = source.w;
  1303. return this;
  1304. };
  1305. Vector4.prototype.copyFromFloats = function (x, y, z, w) {
  1306. this.x = x;
  1307. this.y = y;
  1308. this.z = z;
  1309. this.w = w;
  1310. return this;
  1311. };
  1312. // Statics
  1313. Vector4.FromArray = function (array, offset) {
  1314. if (!offset) {
  1315. offset = 0;
  1316. }
  1317. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1318. };
  1319. Vector4.FromArrayToRef = function (array, offset, result) {
  1320. result.x = array[offset];
  1321. result.y = array[offset + 1];
  1322. result.z = array[offset + 2];
  1323. result.w = array[offset + 3];
  1324. };
  1325. Vector4.FromFloatArrayToRef = function (array, offset, result) {
  1326. result.x = array[offset];
  1327. result.y = array[offset + 1];
  1328. result.z = array[offset + 2];
  1329. result.w = array[offset + 3];
  1330. };
  1331. Vector4.FromFloatsToRef = function (x, y, z, w, result) {
  1332. result.x = x;
  1333. result.y = y;
  1334. result.z = z;
  1335. result.w = w;
  1336. };
  1337. Vector4.Zero = function () {
  1338. return new Vector4(0, 0, 0, 0);
  1339. };
  1340. Vector4.Normalize = function (vector) {
  1341. var result = Vector4.Zero();
  1342. Vector4.NormalizeToRef(vector, result);
  1343. return result;
  1344. };
  1345. Vector4.NormalizeToRef = function (vector, result) {
  1346. result.copyFrom(vector);
  1347. result.normalize();
  1348. };
  1349. Vector4.Minimize = function (left, right) {
  1350. var min = left.clone();
  1351. min.MinimizeInPlace(right);
  1352. return min;
  1353. };
  1354. Vector4.Maximize = function (left, right) {
  1355. var max = left.clone();
  1356. max.MaximizeInPlace(right);
  1357. return max;
  1358. };
  1359. Vector4.Distance = function (value1, value2) {
  1360. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1361. };
  1362. Vector4.DistanceSquared = function (value1, value2) {
  1363. var x = value1.x - value2.x;
  1364. var y = value1.y - value2.y;
  1365. var z = value1.z - value2.z;
  1366. var w = value1.w - value2.w;
  1367. return (x * x) + (y * y) + (z * z) + (w * w);
  1368. };
  1369. Vector4.Center = function (value1, value2) {
  1370. var center = value1.add(value2);
  1371. center.scaleInPlace(0.5);
  1372. return center;
  1373. };
  1374. return Vector4;
  1375. }());
  1376. BABYLON.Vector4 = Vector4;
  1377. var Size = (function () {
  1378. function Size(width, height) {
  1379. this.width = width;
  1380. this.height = height;
  1381. }
  1382. Size.prototype.toString = function () {
  1383. return "{W: " + this.width + ", H: " + this.height + "}";
  1384. };
  1385. Size.prototype.getClassName = function () {
  1386. return "Size";
  1387. };
  1388. Size.prototype.getHashCode = function () {
  1389. var hash = this.width || 0;
  1390. hash = (hash * 397) ^ (this.height || 0);
  1391. return hash;
  1392. };
  1393. Size.prototype.copyFrom = function (src) {
  1394. this.width = src.width;
  1395. this.height = src.height;
  1396. };
  1397. Size.prototype.copyFromFloats = function (width, height) {
  1398. this.width = width;
  1399. this.height = height;
  1400. };
  1401. Size.prototype.multiplyByFloats = function (w, h) {
  1402. return new Size(this.width * w, this.height * h);
  1403. };
  1404. Size.prototype.clone = function () {
  1405. return new Size(this.width, this.height);
  1406. };
  1407. Size.prototype.equals = function (other) {
  1408. if (!other) {
  1409. return false;
  1410. }
  1411. return (this.width === other.width) && (this.height === other.height);
  1412. };
  1413. Object.defineProperty(Size.prototype, "surface", {
  1414. get: function () {
  1415. return this.width * this.height;
  1416. },
  1417. enumerable: true,
  1418. configurable: true
  1419. });
  1420. Size.Zero = function () {
  1421. return new Size(0, 0);
  1422. };
  1423. Size.prototype.add = function (otherSize) {
  1424. var r = new Size(this.width + otherSize.width, this.height + otherSize.height);
  1425. return r;
  1426. };
  1427. Size.prototype.substract = function (otherSize) {
  1428. var r = new Size(this.width - otherSize.width, this.height - otherSize.height);
  1429. return r;
  1430. };
  1431. Size.Lerp = function (start, end, amount) {
  1432. var w = start.width + ((end.width - start.width) * amount);
  1433. var h = start.height + ((end.height - start.height) * amount);
  1434. return new Size(w, h);
  1435. };
  1436. return Size;
  1437. }());
  1438. BABYLON.Size = Size;
  1439. var Quaternion = (function () {
  1440. function Quaternion(x, y, z, w) {
  1441. if (x === void 0) { x = 0; }
  1442. if (y === void 0) { y = 0; }
  1443. if (z === void 0) { z = 0; }
  1444. if (w === void 0) { w = 1; }
  1445. this.x = x;
  1446. this.y = y;
  1447. this.z = z;
  1448. this.w = w;
  1449. }
  1450. Quaternion.prototype.toString = function () {
  1451. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1452. };
  1453. Quaternion.prototype.getClassName = function () {
  1454. return "Quaternion";
  1455. };
  1456. Quaternion.prototype.getHashCode = function () {
  1457. var hash = this.x || 0;
  1458. hash = (hash * 397) ^ (this.y || 0);
  1459. hash = (hash * 397) ^ (this.z || 0);
  1460. hash = (hash * 397) ^ (this.w || 0);
  1461. return hash;
  1462. };
  1463. Quaternion.prototype.asArray = function () {
  1464. return [this.x, this.y, this.z, this.w];
  1465. };
  1466. Quaternion.prototype.equals = function (otherQuaternion) {
  1467. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  1468. };
  1469. Quaternion.prototype.clone = function () {
  1470. return new Quaternion(this.x, this.y, this.z, this.w);
  1471. };
  1472. Quaternion.prototype.copyFrom = function (other) {
  1473. this.x = other.x;
  1474. this.y = other.y;
  1475. this.z = other.z;
  1476. this.w = other.w;
  1477. return this;
  1478. };
  1479. Quaternion.prototype.copyFromFloats = function (x, y, z, w) {
  1480. this.x = x;
  1481. this.y = y;
  1482. this.z = z;
  1483. this.w = w;
  1484. return this;
  1485. };
  1486. Quaternion.prototype.add = function (other) {
  1487. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  1488. };
  1489. Quaternion.prototype.subtract = function (other) {
  1490. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  1491. };
  1492. Quaternion.prototype.scale = function (value) {
  1493. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  1494. };
  1495. Quaternion.prototype.multiply = function (q1) {
  1496. var result = new Quaternion(0, 0, 0, 1.0);
  1497. this.multiplyToRef(q1, result);
  1498. return result;
  1499. };
  1500. Quaternion.prototype.multiplyToRef = function (q1, result) {
  1501. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1502. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1503. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1504. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1505. result.copyFromFloats(x, y, z, w);
  1506. return this;
  1507. };
  1508. Quaternion.prototype.multiplyInPlace = function (q1) {
  1509. this.multiplyToRef(q1, this);
  1510. return this;
  1511. };
  1512. Quaternion.prototype.conjugateToRef = function (ref) {
  1513. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  1514. return this;
  1515. };
  1516. Quaternion.prototype.conjugateInPlace = function () {
  1517. this.x *= -1;
  1518. this.y *= -1;
  1519. this.z *= -1;
  1520. return this;
  1521. };
  1522. Quaternion.prototype.conjugate = function () {
  1523. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  1524. return result;
  1525. };
  1526. Quaternion.prototype.length = function () {
  1527. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1528. };
  1529. Quaternion.prototype.normalize = function () {
  1530. var length = 1.0 / this.length();
  1531. this.x *= length;
  1532. this.y *= length;
  1533. this.z *= length;
  1534. this.w *= length;
  1535. return this;
  1536. };
  1537. Quaternion.prototype.toEulerAngles = function (order) {
  1538. if (order === void 0) { order = "YZX"; }
  1539. var result = Vector3.Zero();
  1540. this.toEulerAnglesToRef(result, order);
  1541. return result;
  1542. };
  1543. Quaternion.prototype.toEulerAnglesToRef = function (result, order) {
  1544. if (order === void 0) { order = "YZX"; }
  1545. var qx = this.x;
  1546. var qy = this.y;
  1547. var qz = this.z;
  1548. var qw = this.w;
  1549. var xsqr = qx * qx;
  1550. var t0 = -2.0 * (xsqr + qy * qy) + 1.0;
  1551. var t1 = 2.0 * (qz * qx + qw * qy);
  1552. var t2 = -2.0 * (qz * qy - qw * qx);
  1553. var t3 = 2.0 * (qx * qy + qw * qz);
  1554. var t4 = -2.0 * (qz * qz + xsqr) + 1.0;
  1555. t2 = t2 > 1.0 ? 1.0 : t2;
  1556. t2 = t2 < -1.0 ? -1.0 : t2;
  1557. result.x = Math.asin(t2);
  1558. result.z = Math.atan2(t3, t4);
  1559. result.y = Math.atan2(t1, t0);
  1560. return this;
  1561. };
  1562. ;
  1563. Quaternion.prototype.toRotationMatrix = function (result) {
  1564. var xx = this.x * this.x;
  1565. var yy = this.y * this.y;
  1566. var zz = this.z * this.z;
  1567. var xy = this.x * this.y;
  1568. var zw = this.z * this.w;
  1569. var zx = this.z * this.x;
  1570. var yw = this.y * this.w;
  1571. var yz = this.y * this.z;
  1572. var xw = this.x * this.w;
  1573. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1574. result.m[1] = 2.0 * (xy + zw);
  1575. result.m[2] = 2.0 * (zx - yw);
  1576. result.m[3] = 0;
  1577. result.m[4] = 2.0 * (xy - zw);
  1578. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1579. result.m[6] = 2.0 * (yz + xw);
  1580. result.m[7] = 0;
  1581. result.m[8] = 2.0 * (zx + yw);
  1582. result.m[9] = 2.0 * (yz - xw);
  1583. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1584. result.m[11] = 0;
  1585. result.m[12] = 0;
  1586. result.m[13] = 0;
  1587. result.m[14] = 0;
  1588. result.m[15] = 1.0;
  1589. return this;
  1590. };
  1591. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  1592. Quaternion.FromRotationMatrixToRef(matrix, this);
  1593. return this;
  1594. };
  1595. // Statics
  1596. Quaternion.FromRotationMatrix = function (matrix) {
  1597. var result = new Quaternion();
  1598. Quaternion.FromRotationMatrixToRef(matrix, result);
  1599. return result;
  1600. };
  1601. Quaternion.FromRotationMatrixToRef = function (matrix, result) {
  1602. var data = matrix.m;
  1603. var m11 = data[0], m12 = data[4], m13 = data[8];
  1604. var m21 = data[1], m22 = data[5], m23 = data[9];
  1605. var m31 = data[2], m32 = data[6], m33 = data[10];
  1606. var trace = m11 + m22 + m33;
  1607. var s;
  1608. if (trace > 0) {
  1609. s = 0.5 / Math.sqrt(trace + 1.0);
  1610. result.w = 0.25 / s;
  1611. result.x = (m32 - m23) * s;
  1612. result.y = (m13 - m31) * s;
  1613. result.z = (m21 - m12) * s;
  1614. }
  1615. else if (m11 > m22 && m11 > m33) {
  1616. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1617. result.w = (m32 - m23) / s;
  1618. result.x = 0.25 * s;
  1619. result.y = (m12 + m21) / s;
  1620. result.z = (m13 + m31) / s;
  1621. }
  1622. else if (m22 > m33) {
  1623. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1624. result.w = (m13 - m31) / s;
  1625. result.x = (m12 + m21) / s;
  1626. result.y = 0.25 * s;
  1627. result.z = (m23 + m32) / s;
  1628. }
  1629. else {
  1630. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1631. result.w = (m21 - m12) / s;
  1632. result.x = (m13 + m31) / s;
  1633. result.y = (m23 + m32) / s;
  1634. result.z = 0.25 * s;
  1635. }
  1636. };
  1637. Quaternion.Inverse = function (q) {
  1638. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1639. };
  1640. Quaternion.Identity = function () {
  1641. return new Quaternion(0, 0, 0, 1);
  1642. };
  1643. Quaternion.RotationAxis = function (axis, angle) {
  1644. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  1645. };
  1646. Quaternion.RotationAxisToRef = function (axis, angle, result) {
  1647. var sin = Math.sin(angle / 2);
  1648. axis.normalize();
  1649. result.w = Math.cos(angle / 2);
  1650. result.x = axis.x * sin;
  1651. result.y = axis.y * sin;
  1652. result.z = axis.z * sin;
  1653. return result;
  1654. };
  1655. Quaternion.FromArray = function (array, offset) {
  1656. if (!offset) {
  1657. offset = 0;
  1658. }
  1659. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1660. };
  1661. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1662. var q = new Quaternion();
  1663. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  1664. return q;
  1665. };
  1666. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1667. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  1668. var halfRoll = roll * 0.5;
  1669. var halfPitch = pitch * 0.5;
  1670. var halfYaw = yaw * 0.5;
  1671. var sinRoll = Math.sin(halfRoll);
  1672. var cosRoll = Math.cos(halfRoll);
  1673. var sinPitch = Math.sin(halfPitch);
  1674. var cosPitch = Math.cos(halfPitch);
  1675. var sinYaw = Math.sin(halfYaw);
  1676. var cosYaw = Math.cos(halfYaw);
  1677. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1678. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1679. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1680. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1681. };
  1682. Quaternion.RotationAlphaBetaGamma = function (alpha, beta, gamma) {
  1683. var result = new Quaternion();
  1684. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  1685. return result;
  1686. };
  1687. Quaternion.RotationAlphaBetaGammaToRef = function (alpha, beta, gamma, result) {
  1688. // Produces a quaternion from Euler angles in the z-x-z orientation
  1689. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  1690. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  1691. var halfBeta = beta * 0.5;
  1692. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1693. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1694. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1695. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1696. };
  1697. Quaternion.Slerp = function (left, right, amount) {
  1698. var num2;
  1699. var num3;
  1700. var num = amount;
  1701. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1702. var flag = false;
  1703. if (num4 < 0) {
  1704. flag = true;
  1705. num4 = -num4;
  1706. }
  1707. if (num4 > 0.999999) {
  1708. num3 = 1 - num;
  1709. num2 = flag ? -num : num;
  1710. }
  1711. else {
  1712. var num5 = Math.acos(num4);
  1713. var num6 = (1.0 / Math.sin(num5));
  1714. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1715. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1716. }
  1717. 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));
  1718. };
  1719. return Quaternion;
  1720. }());
  1721. BABYLON.Quaternion = Quaternion;
  1722. var Matrix = (function () {
  1723. function Matrix() {
  1724. this.m = new Float32Array(16);
  1725. }
  1726. // Properties
  1727. Matrix.prototype.isIdentity = function () {
  1728. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1729. return false;
  1730. if (this.m[1] !== 0.0 || this.m[2] !== 0.0 || this.m[3] !== 0.0 ||
  1731. this.m[4] !== 0.0 || this.m[6] !== 0.0 || this.m[7] !== 0.0 ||
  1732. this.m[8] !== 0.0 || this.m[9] !== 0.0 || this.m[11] !== 0.0 ||
  1733. this.m[12] !== 0.0 || this.m[13] !== 0.0 || this.m[14] !== 0.0)
  1734. return false;
  1735. return true;
  1736. };
  1737. Matrix.prototype.determinant = function () {
  1738. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1739. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1740. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1741. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1742. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1743. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1744. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  1745. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  1746. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  1747. };
  1748. // Methods
  1749. Matrix.prototype.toArray = function () {
  1750. return this.m;
  1751. };
  1752. Matrix.prototype.asArray = function () {
  1753. return this.toArray();
  1754. };
  1755. Matrix.prototype.invert = function () {
  1756. this.invertToRef(this);
  1757. return this;
  1758. };
  1759. Matrix.prototype.reset = function () {
  1760. for (var index = 0; index < 16; index++) {
  1761. this.m[index] = 0;
  1762. }
  1763. return this;
  1764. };
  1765. Matrix.prototype.add = function (other) {
  1766. var result = new Matrix();
  1767. this.addToRef(other, result);
  1768. return result;
  1769. };
  1770. Matrix.prototype.addToRef = function (other, result) {
  1771. for (var index = 0; index < 16; index++) {
  1772. result.m[index] = this.m[index] + other.m[index];
  1773. }
  1774. return this;
  1775. };
  1776. Matrix.prototype.addToSelf = function (other) {
  1777. for (var index = 0; index < 16; index++) {
  1778. this.m[index] += other.m[index];
  1779. }
  1780. return this;
  1781. };
  1782. Matrix.prototype.invertToRef = function (other) {
  1783. var l1 = this.m[0];
  1784. var l2 = this.m[1];
  1785. var l3 = this.m[2];
  1786. var l4 = this.m[3];
  1787. var l5 = this.m[4];
  1788. var l6 = this.m[5];
  1789. var l7 = this.m[6];
  1790. var l8 = this.m[7];
  1791. var l9 = this.m[8];
  1792. var l10 = this.m[9];
  1793. var l11 = this.m[10];
  1794. var l12 = this.m[11];
  1795. var l13 = this.m[12];
  1796. var l14 = this.m[13];
  1797. var l15 = this.m[14];
  1798. var l16 = this.m[15];
  1799. var l17 = (l11 * l16) - (l12 * l15);
  1800. var l18 = (l10 * l16) - (l12 * l14);
  1801. var l19 = (l10 * l15) - (l11 * l14);
  1802. var l20 = (l9 * l16) - (l12 * l13);
  1803. var l21 = (l9 * l15) - (l11 * l13);
  1804. var l22 = (l9 * l14) - (l10 * l13);
  1805. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1806. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1807. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1808. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1809. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1810. var l28 = (l7 * l16) - (l8 * l15);
  1811. var l29 = (l6 * l16) - (l8 * l14);
  1812. var l30 = (l6 * l15) - (l7 * l14);
  1813. var l31 = (l5 * l16) - (l8 * l13);
  1814. var l32 = (l5 * l15) - (l7 * l13);
  1815. var l33 = (l5 * l14) - (l6 * l13);
  1816. var l34 = (l7 * l12) - (l8 * l11);
  1817. var l35 = (l6 * l12) - (l8 * l10);
  1818. var l36 = (l6 * l11) - (l7 * l10);
  1819. var l37 = (l5 * l12) - (l8 * l9);
  1820. var l38 = (l5 * l11) - (l7 * l9);
  1821. var l39 = (l5 * l10) - (l6 * l9);
  1822. other.m[0] = l23 * l27;
  1823. other.m[4] = l24 * l27;
  1824. other.m[8] = l25 * l27;
  1825. other.m[12] = l26 * l27;
  1826. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1827. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1828. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1829. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1830. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1831. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1832. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1833. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1834. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1835. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1836. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1837. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1838. return this;
  1839. };
  1840. Matrix.prototype.setTranslation = function (vector3) {
  1841. this.m[12] = vector3.x;
  1842. this.m[13] = vector3.y;
  1843. this.m[14] = vector3.z;
  1844. return this;
  1845. };
  1846. Matrix.prototype.getTranslation = function () {
  1847. return new Vector3(this.m[12], this.m[13], this.m[14]);
  1848. };
  1849. Matrix.prototype.multiply = function (other) {
  1850. var result = new Matrix();
  1851. this.multiplyToRef(other, result);
  1852. return result;
  1853. };
  1854. Matrix.prototype.copyFrom = function (other) {
  1855. for (var index = 0; index < 16; index++) {
  1856. this.m[index] = other.m[index];
  1857. }
  1858. return this;
  1859. };
  1860. Matrix.prototype.copyToArray = function (array, offset) {
  1861. if (offset === void 0) { offset = 0; }
  1862. for (var index = 0; index < 16; index++) {
  1863. array[offset + index] = this.m[index];
  1864. }
  1865. return this;
  1866. };
  1867. Matrix.prototype.multiplyToRef = function (other, result) {
  1868. this.multiplyToArray(other, result.m, 0);
  1869. return this;
  1870. };
  1871. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1872. var tm0 = this.m[0];
  1873. var tm1 = this.m[1];
  1874. var tm2 = this.m[2];
  1875. var tm3 = this.m[3];
  1876. var tm4 = this.m[4];
  1877. var tm5 = this.m[5];
  1878. var tm6 = this.m[6];
  1879. var tm7 = this.m[7];
  1880. var tm8 = this.m[8];
  1881. var tm9 = this.m[9];
  1882. var tm10 = this.m[10];
  1883. var tm11 = this.m[11];
  1884. var tm12 = this.m[12];
  1885. var tm13 = this.m[13];
  1886. var tm14 = this.m[14];
  1887. var tm15 = this.m[15];
  1888. var om0 = other.m[0];
  1889. var om1 = other.m[1];
  1890. var om2 = other.m[2];
  1891. var om3 = other.m[3];
  1892. var om4 = other.m[4];
  1893. var om5 = other.m[5];
  1894. var om6 = other.m[6];
  1895. var om7 = other.m[7];
  1896. var om8 = other.m[8];
  1897. var om9 = other.m[9];
  1898. var om10 = other.m[10];
  1899. var om11 = other.m[11];
  1900. var om12 = other.m[12];
  1901. var om13 = other.m[13];
  1902. var om14 = other.m[14];
  1903. var om15 = other.m[15];
  1904. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1905. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1906. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1907. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1908. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1909. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1910. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1911. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1912. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1913. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1914. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1915. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1916. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1917. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1918. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1919. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1920. return this;
  1921. };
  1922. Matrix.prototype.equals = function (value) {
  1923. return value &&
  1924. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  1925. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  1926. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  1927. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1928. };
  1929. Matrix.prototype.clone = function () {
  1930. 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]);
  1931. };
  1932. Matrix.prototype.getClassName = function () {
  1933. return "Matrix";
  1934. };
  1935. Matrix.prototype.getHashCode = function () {
  1936. var hash = this.m[0] || 0;
  1937. for (var i = 1; i < 16; i++) {
  1938. hash = (hash * 397) ^ (this.m[i] || 0);
  1939. }
  1940. return hash;
  1941. };
  1942. Matrix.prototype.decompose = function (scale, rotation, translation) {
  1943. translation.x = this.m[12];
  1944. translation.y = this.m[13];
  1945. translation.z = this.m[14];
  1946. var xs = MathTools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1947. var ys = MathTools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1948. var zs = MathTools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1949. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1950. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1951. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1952. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  1953. rotation.x = 0;
  1954. rotation.y = 0;
  1955. rotation.z = 0;
  1956. rotation.w = 1;
  1957. return false;
  1958. }
  1959. 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]);
  1960. Quaternion.FromRotationMatrixToRef(Tmp.Matrix[0], rotation);
  1961. return true;
  1962. };
  1963. // Statics
  1964. Matrix.FromArray = function (array, offset) {
  1965. var result = new Matrix();
  1966. if (!offset) {
  1967. offset = 0;
  1968. }
  1969. Matrix.FromArrayToRef(array, offset, result);
  1970. return result;
  1971. };
  1972. Matrix.FromArrayToRef = function (array, offset, result) {
  1973. for (var index = 0; index < 16; index++) {
  1974. result.m[index] = array[index + offset];
  1975. }
  1976. };
  1977. Matrix.FromFloat32ArrayToRefScaled = function (array, offset, scale, result) {
  1978. for (var index = 0; index < 16; index++) {
  1979. result.m[index] = array[index + offset] * scale;
  1980. }
  1981. };
  1982. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1983. result.m[0] = initialM11;
  1984. result.m[1] = initialM12;
  1985. result.m[2] = initialM13;
  1986. result.m[3] = initialM14;
  1987. result.m[4] = initialM21;
  1988. result.m[5] = initialM22;
  1989. result.m[6] = initialM23;
  1990. result.m[7] = initialM24;
  1991. result.m[8] = initialM31;
  1992. result.m[9] = initialM32;
  1993. result.m[10] = initialM33;
  1994. result.m[11] = initialM34;
  1995. result.m[12] = initialM41;
  1996. result.m[13] = initialM42;
  1997. result.m[14] = initialM43;
  1998. result.m[15] = initialM44;
  1999. };
  2000. Matrix.prototype.getRow = function (index) {
  2001. if (index < 0 || index > 3) {
  2002. return null;
  2003. }
  2004. var i = index * 4;
  2005. return new Vector4(this.m[i + 0], this.m[i + 1], this.m[i + 2], this.m[i + 3]);
  2006. };
  2007. Matrix.prototype.setRow = function (index, row) {
  2008. if (index < 0 || index > 3) {
  2009. return this;
  2010. }
  2011. var i = index * 4;
  2012. this.m[i + 0] = row.x;
  2013. this.m[i + 1] = row.y;
  2014. this.m[i + 2] = row.z;
  2015. this.m[i + 3] = row.w;
  2016. return this;
  2017. };
  2018. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  2019. var result = new Matrix();
  2020. result.m[0] = initialM11;
  2021. result.m[1] = initialM12;
  2022. result.m[2] = initialM13;
  2023. result.m[3] = initialM14;
  2024. result.m[4] = initialM21;
  2025. result.m[5] = initialM22;
  2026. result.m[6] = initialM23;
  2027. result.m[7] = initialM24;
  2028. result.m[8] = initialM31;
  2029. result.m[9] = initialM32;
  2030. result.m[10] = initialM33;
  2031. result.m[11] = initialM34;
  2032. result.m[12] = initialM41;
  2033. result.m[13] = initialM42;
  2034. result.m[14] = initialM43;
  2035. result.m[15] = initialM44;
  2036. return result;
  2037. };
  2038. Matrix.Compose = function (scale, rotation, translation) {
  2039. var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
  2040. var rotationMatrix = Matrix.Identity();
  2041. rotation.toRotationMatrix(rotationMatrix);
  2042. result = result.multiply(rotationMatrix);
  2043. result.setTranslation(translation);
  2044. return result;
  2045. };
  2046. Matrix.Identity = function () {
  2047. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  2048. };
  2049. Matrix.IdentityToRef = function (result) {
  2050. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  2051. };
  2052. Matrix.Zero = function () {
  2053. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  2054. };
  2055. Matrix.RotationX = function (angle) {
  2056. var result = new Matrix();
  2057. Matrix.RotationXToRef(angle, result);
  2058. return result;
  2059. };
  2060. Matrix.Invert = function (source) {
  2061. var result = new Matrix();
  2062. source.invertToRef(result);
  2063. return result;
  2064. };
  2065. Matrix.RotationXToRef = function (angle, result) {
  2066. var s = Math.sin(angle);
  2067. var c = Math.cos(angle);
  2068. result.m[0] = 1.0;
  2069. result.m[15] = 1.0;
  2070. result.m[5] = c;
  2071. result.m[10] = c;
  2072. result.m[9] = -s;
  2073. result.m[6] = s;
  2074. result.m[1] = 0;
  2075. result.m[2] = 0;
  2076. result.m[3] = 0;
  2077. result.m[4] = 0;
  2078. result.m[7] = 0;
  2079. result.m[8] = 0;
  2080. result.m[11] = 0;
  2081. result.m[12] = 0;
  2082. result.m[13] = 0;
  2083. result.m[14] = 0;
  2084. };
  2085. Matrix.RotationY = function (angle) {
  2086. var result = new Matrix();
  2087. Matrix.RotationYToRef(angle, result);
  2088. return result;
  2089. };
  2090. Matrix.RotationYToRef = function (angle, result) {
  2091. var s = Math.sin(angle);
  2092. var c = Math.cos(angle);
  2093. result.m[5] = 1.0;
  2094. result.m[15] = 1.0;
  2095. result.m[0] = c;
  2096. result.m[2] = -s;
  2097. result.m[8] = s;
  2098. result.m[10] = c;
  2099. result.m[1] = 0;
  2100. result.m[3] = 0;
  2101. result.m[4] = 0;
  2102. result.m[6] = 0;
  2103. result.m[7] = 0;
  2104. result.m[9] = 0;
  2105. result.m[11] = 0;
  2106. result.m[12] = 0;
  2107. result.m[13] = 0;
  2108. result.m[14] = 0;
  2109. };
  2110. Matrix.RotationZ = function (angle) {
  2111. var result = new Matrix();
  2112. Matrix.RotationZToRef(angle, result);
  2113. return result;
  2114. };
  2115. Matrix.RotationZToRef = function (angle, result) {
  2116. var s = Math.sin(angle);
  2117. var c = Math.cos(angle);
  2118. result.m[10] = 1.0;
  2119. result.m[15] = 1.0;
  2120. result.m[0] = c;
  2121. result.m[1] = s;
  2122. result.m[4] = -s;
  2123. result.m[5] = c;
  2124. result.m[2] = 0;
  2125. result.m[3] = 0;
  2126. result.m[6] = 0;
  2127. result.m[7] = 0;
  2128. result.m[8] = 0;
  2129. result.m[9] = 0;
  2130. result.m[11] = 0;
  2131. result.m[12] = 0;
  2132. result.m[13] = 0;
  2133. result.m[14] = 0;
  2134. };
  2135. Matrix.RotationAxis = function (axis, angle) {
  2136. var result = Matrix.Zero();
  2137. Matrix.RotationAxisToRef(axis, angle, result);
  2138. return result;
  2139. };
  2140. Matrix.RotationAxisToRef = function (axis, angle, result) {
  2141. var s = Math.sin(-angle);
  2142. var c = Math.cos(-angle);
  2143. var c1 = 1 - c;
  2144. axis.normalize();
  2145. result.m[0] = (axis.x * axis.x) * c1 + c;
  2146. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  2147. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  2148. result.m[3] = 0.0;
  2149. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  2150. result.m[5] = (axis.y * axis.y) * c1 + c;
  2151. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  2152. result.m[7] = 0.0;
  2153. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  2154. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  2155. result.m[10] = (axis.z * axis.z) * c1 + c;
  2156. result.m[11] = 0.0;
  2157. result.m[15] = 1.0;
  2158. };
  2159. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  2160. var result = new Matrix();
  2161. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  2162. return result;
  2163. };
  2164. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  2165. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  2166. this._tempQuaternion.toRotationMatrix(result);
  2167. };
  2168. Matrix.Scaling = function (x, y, z) {
  2169. var result = Matrix.Zero();
  2170. Matrix.ScalingToRef(x, y, z, result);
  2171. return result;
  2172. };
  2173. Matrix.ScalingToRef = function (x, y, z, result) {
  2174. result.m[0] = x;
  2175. result.m[1] = 0;
  2176. result.m[2] = 0;
  2177. result.m[3] = 0;
  2178. result.m[4] = 0;
  2179. result.m[5] = y;
  2180. result.m[6] = 0;
  2181. result.m[7] = 0;
  2182. result.m[8] = 0;
  2183. result.m[9] = 0;
  2184. result.m[10] = z;
  2185. result.m[11] = 0;
  2186. result.m[12] = 0;
  2187. result.m[13] = 0;
  2188. result.m[14] = 0;
  2189. result.m[15] = 1.0;
  2190. };
  2191. Matrix.Translation = function (x, y, z) {
  2192. var result = Matrix.Identity();
  2193. Matrix.TranslationToRef(x, y, z, result);
  2194. return result;
  2195. };
  2196. Matrix.TranslationToRef = function (x, y, z, result) {
  2197. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  2198. };
  2199. Matrix.Lerp = function (startValue, endValue, gradient) {
  2200. var result = Matrix.Zero();
  2201. for (var index = 0; index < 16; index++) {
  2202. result.m[index] = startValue.m[index] * (1.0 - gradient) + endValue.m[index] * gradient;
  2203. }
  2204. return result;
  2205. };
  2206. Matrix.DecomposeLerp = function (startValue, endValue, gradient) {
  2207. var startScale = new Vector3(0, 0, 0);
  2208. var startRotation = new Quaternion();
  2209. var startTranslation = new Vector3(0, 0, 0);
  2210. startValue.decompose(startScale, startRotation, startTranslation);
  2211. var endScale = new Vector3(0, 0, 0);
  2212. var endRotation = new Quaternion();
  2213. var endTranslation = new Vector3(0, 0, 0);
  2214. endValue.decompose(endScale, endRotation, endTranslation);
  2215. var resultScale = Vector3.Lerp(startScale, endScale, gradient);
  2216. var resultRotation = Quaternion.Slerp(startRotation, endRotation, gradient);
  2217. var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
  2218. return Matrix.Compose(resultScale, resultRotation, resultTranslation);
  2219. };
  2220. Matrix.LookAtLH = function (eye, target, up) {
  2221. var result = Matrix.Zero();
  2222. Matrix.LookAtLHToRef(eye, target, up, result);
  2223. return result;
  2224. };
  2225. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  2226. // Z axis
  2227. target.subtractToRef(eye, this._zAxis);
  2228. this._zAxis.normalize();
  2229. // X axis
  2230. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  2231. if (this._xAxis.lengthSquared() === 0) {
  2232. this._xAxis.x = 1.0;
  2233. }
  2234. else {
  2235. this._xAxis.normalize();
  2236. }
  2237. // Y axis
  2238. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  2239. this._yAxis.normalize();
  2240. // Eye angles
  2241. var ex = -Vector3.Dot(this._xAxis, eye);
  2242. var ey = -Vector3.Dot(this._yAxis, eye);
  2243. var ez = -Vector3.Dot(this._zAxis, eye);
  2244. 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);
  2245. };
  2246. Matrix.LookAtRH = function (eye, target, up) {
  2247. var result = Matrix.Zero();
  2248. Matrix.LookAtRHToRef(eye, target, up, result);
  2249. return result;
  2250. };
  2251. Matrix.LookAtRHToRef = function (eye, target, up, result) {
  2252. // Z axis
  2253. eye.subtractToRef(target, this._zAxis);
  2254. this._zAxis.normalize();
  2255. // X axis
  2256. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  2257. if (this._xAxis.lengthSquared() === 0) {
  2258. this._xAxis.x = 1.0;
  2259. }
  2260. else {
  2261. this._xAxis.normalize();
  2262. }
  2263. // Y axis
  2264. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  2265. this._yAxis.normalize();
  2266. // Eye angles
  2267. var ex = -Vector3.Dot(this._xAxis, eye);
  2268. var ey = -Vector3.Dot(this._yAxis, eye);
  2269. var ez = -Vector3.Dot(this._zAxis, eye);
  2270. 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);
  2271. };
  2272. Matrix.OrthoLH = function (width, height, znear, zfar) {
  2273. var matrix = Matrix.Zero();
  2274. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  2275. return matrix;
  2276. };
  2277. Matrix.OrthoLHToRef = function (width, height, znear, zfar, result) {
  2278. var hw = 2.0 / width;
  2279. var hh = 2.0 / height;
  2280. var id = 1.0 / (zfar - znear);
  2281. var nid = znear / (znear - zfar);
  2282. Matrix.FromValuesToRef(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1, result);
  2283. };
  2284. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  2285. var matrix = Matrix.Zero();
  2286. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  2287. return matrix;
  2288. };
  2289. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  2290. result.m[0] = 2.0 / (right - left);
  2291. result.m[1] = result.m[2] = result.m[3] = 0;
  2292. result.m[5] = 2.0 / (top - bottom);
  2293. result.m[4] = result.m[6] = result.m[7] = 0;
  2294. result.m[10] = 1.0 / (zfar - znear);
  2295. result.m[8] = result.m[9] = result.m[11] = 0;
  2296. result.m[12] = (left + right) / (left - right);
  2297. result.m[13] = (top + bottom) / (bottom - top);
  2298. result.m[14] = -znear / (zfar - znear);
  2299. result.m[15] = 1.0;
  2300. };
  2301. Matrix.OrthoOffCenterRH = function (left, right, bottom, top, znear, zfar) {
  2302. var matrix = Matrix.Zero();
  2303. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  2304. return matrix;
  2305. };
  2306. Matrix.OrthoOffCenterRHToRef = function (left, right, bottom, top, znear, zfar, result) {
  2307. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  2308. result.m[10] *= -1.0;
  2309. };
  2310. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  2311. var matrix = Matrix.Zero();
  2312. matrix.m[0] = (2.0 * znear) / width;
  2313. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  2314. matrix.m[5] = (2.0 * znear) / height;
  2315. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  2316. matrix.m[10] = -zfar / (znear - zfar);
  2317. matrix.m[8] = matrix.m[9] = 0.0;
  2318. matrix.m[11] = 1.0;
  2319. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  2320. matrix.m[14] = (znear * zfar) / (znear - zfar);
  2321. return matrix;
  2322. };
  2323. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  2324. var matrix = Matrix.Zero();
  2325. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  2326. return matrix;
  2327. };
  2328. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result, isVerticalFovFixed) {
  2329. if (isVerticalFovFixed === void 0) { isVerticalFovFixed = true; }
  2330. var tan = 1.0 / (Math.tan(fov * 0.5));
  2331. if (isVerticalFovFixed) {
  2332. result.m[0] = tan / aspect;
  2333. }
  2334. else {
  2335. result.m[0] = tan;
  2336. }
  2337. result.m[1] = result.m[2] = result.m[3] = 0.0;
  2338. if (isVerticalFovFixed) {
  2339. result.m[5] = tan;
  2340. }
  2341. else {
  2342. result.m[5] = tan * aspect;
  2343. }
  2344. result.m[4] = result.m[6] = result.m[7] = 0.0;
  2345. result.m[8] = result.m[9] = 0.0;
  2346. result.m[10] = zfar / (zfar - znear);
  2347. result.m[11] = 1.0;
  2348. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2349. result.m[14] = -(znear * zfar) / (zfar - znear);
  2350. };
  2351. Matrix.PerspectiveFovRH = function (fov, aspect, znear, zfar) {
  2352. var matrix = Matrix.Zero();
  2353. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  2354. return matrix;
  2355. };
  2356. Matrix.PerspectiveFovRHToRef = function (fov, aspect, znear, zfar, result, isVerticalFovFixed) {
  2357. if (isVerticalFovFixed === void 0) { isVerticalFovFixed = true; }
  2358. var tan = 1.0 / (Math.tan(fov * 0.5));
  2359. if (isVerticalFovFixed) {
  2360. result.m[0] = tan / aspect;
  2361. }
  2362. else {
  2363. result.m[0] = tan;
  2364. }
  2365. result.m[1] = result.m[2] = result.m[3] = 0.0;
  2366. if (isVerticalFovFixed) {
  2367. result.m[5] = tan;
  2368. }
  2369. else {
  2370. result.m[5] = tan * aspect;
  2371. }
  2372. result.m[4] = result.m[6] = result.m[7] = 0.0;
  2373. result.m[8] = result.m[9] = 0.0;
  2374. result.m[10] = zfar / (znear - zfar);
  2375. result.m[11] = -1.0;
  2376. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2377. result.m[14] = (znear * zfar) / (znear - zfar);
  2378. };
  2379. Matrix.PerspectiveFovWebVRToRef = function (fov, znear, zfar, result, isVerticalFovFixed) {
  2380. if (isVerticalFovFixed === void 0) { isVerticalFovFixed = true; }
  2381. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  2382. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  2383. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  2384. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  2385. var xScale = 2.0 / (leftTan + rightTan);
  2386. var yScale = 2.0 / (upTan + downTan);
  2387. result.m[0] = xScale;
  2388. result.m[1] = result.m[2] = result.m[3] = result.m[4] = 0.0;
  2389. result.m[5] = yScale;
  2390. result.m[6] = result.m[7] = 0.0;
  2391. result.m[8] = ((leftTan - rightTan) * xScale * 0.5);
  2392. result.m[9] = -((upTan - downTan) * yScale * 0.5);
  2393. //result.m[10] = -(znear + zfar) / (zfar - znear);
  2394. result.m[10] = -zfar / (znear - zfar);
  2395. result.m[11] = 1.0;
  2396. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2397. //result.m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  2398. result.m[14] = (znear * zfar) / (znear - zfar);
  2399. };
  2400. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  2401. var cw = viewport.width;
  2402. var ch = viewport.height;
  2403. var cx = viewport.x;
  2404. var cy = viewport.y;
  2405. 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);
  2406. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  2407. };
  2408. Matrix.GetAsMatrix2x2 = function (matrix) {
  2409. return new Float32Array([
  2410. matrix.m[0], matrix.m[1],
  2411. matrix.m[4], matrix.m[5]
  2412. ]);
  2413. };
  2414. Matrix.GetAsMatrix3x3 = function (matrix) {
  2415. return new Float32Array([
  2416. matrix.m[0], matrix.m[1], matrix.m[2],
  2417. matrix.m[4], matrix.m[5], matrix.m[6],
  2418. matrix.m[8], matrix.m[9], matrix.m[10]
  2419. ]);
  2420. };
  2421. Matrix.Transpose = function (matrix) {
  2422. var result = new Matrix();
  2423. result.m[0] = matrix.m[0];
  2424. result.m[1] = matrix.m[4];
  2425. result.m[2] = matrix.m[8];
  2426. result.m[3] = matrix.m[12];
  2427. result.m[4] = matrix.m[1];
  2428. result.m[5] = matrix.m[5];
  2429. result.m[6] = matrix.m[9];
  2430. result.m[7] = matrix.m[13];
  2431. result.m[8] = matrix.m[2];
  2432. result.m[9] = matrix.m[6];
  2433. result.m[10] = matrix.m[10];
  2434. result.m[11] = matrix.m[14];
  2435. result.m[12] = matrix.m[3];
  2436. result.m[13] = matrix.m[7];
  2437. result.m[14] = matrix.m[11];
  2438. result.m[15] = matrix.m[15];
  2439. return result;
  2440. };
  2441. Matrix.Reflection = function (plane) {
  2442. var matrix = new Matrix();
  2443. Matrix.ReflectionToRef(plane, matrix);
  2444. return matrix;
  2445. };
  2446. Matrix.ReflectionToRef = function (plane, result) {
  2447. plane.normalize();
  2448. var x = plane.normal.x;
  2449. var y = plane.normal.y;
  2450. var z = plane.normal.z;
  2451. var temp = -2 * x;
  2452. var temp2 = -2 * y;
  2453. var temp3 = -2 * z;
  2454. result.m[0] = (temp * x) + 1;
  2455. result.m[1] = temp2 * x;
  2456. result.m[2] = temp3 * x;
  2457. result.m[3] = 0.0;
  2458. result.m[4] = temp * y;
  2459. result.m[5] = (temp2 * y) + 1;
  2460. result.m[6] = temp3 * y;
  2461. result.m[7] = 0.0;
  2462. result.m[8] = temp * z;
  2463. result.m[9] = temp2 * z;
  2464. result.m[10] = (temp3 * z) + 1;
  2465. result.m[11] = 0.0;
  2466. result.m[12] = temp * plane.d;
  2467. result.m[13] = temp2 * plane.d;
  2468. result.m[14] = temp3 * plane.d;
  2469. result.m[15] = 1.0;
  2470. };
  2471. Matrix._tempQuaternion = new Quaternion();
  2472. Matrix._xAxis = Vector3.Zero();
  2473. Matrix._yAxis = Vector3.Zero();
  2474. Matrix._zAxis = Vector3.Zero();
  2475. return Matrix;
  2476. }());
  2477. BABYLON.Matrix = Matrix;
  2478. var Plane = (function () {
  2479. function Plane(a, b, c, d) {
  2480. this.normal = new Vector3(a, b, c);
  2481. this.d = d;
  2482. }
  2483. Plane.prototype.asArray = function () {
  2484. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  2485. };
  2486. // Methods
  2487. Plane.prototype.clone = function () {
  2488. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  2489. };
  2490. Plane.prototype.getClassName = function () {
  2491. return "Plane";
  2492. };
  2493. Plane.prototype.getHashCode = function () {
  2494. var hash = this.normal.getHashCode();
  2495. hash = (hash * 397) ^ (this.d || 0);
  2496. return hash;
  2497. };
  2498. Plane.prototype.normalize = function () {
  2499. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  2500. var magnitude = 0;
  2501. if (norm !== 0) {
  2502. magnitude = 1.0 / norm;
  2503. }
  2504. this.normal.x *= magnitude;
  2505. this.normal.y *= magnitude;
  2506. this.normal.z *= magnitude;
  2507. this.d *= magnitude;
  2508. return this;
  2509. };
  2510. Plane.prototype.transform = function (transformation) {
  2511. var transposedMatrix = Matrix.Transpose(transformation);
  2512. var x = this.normal.x;
  2513. var y = this.normal.y;
  2514. var z = this.normal.z;
  2515. var d = this.d;
  2516. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  2517. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  2518. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  2519. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  2520. return new Plane(normalX, normalY, normalZ, finalD);
  2521. };
  2522. Plane.prototype.dotCoordinate = function (point) {
  2523. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  2524. };
  2525. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  2526. var x1 = point2.x - point1.x;
  2527. var y1 = point2.y - point1.y;
  2528. var z1 = point2.z - point1.z;
  2529. var x2 = point3.x - point1.x;
  2530. var y2 = point3.y - point1.y;
  2531. var z2 = point3.z - point1.z;
  2532. var yz = (y1 * z2) - (z1 * y2);
  2533. var xz = (z1 * x2) - (x1 * z2);
  2534. var xy = (x1 * y2) - (y1 * x2);
  2535. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  2536. var invPyth;
  2537. if (pyth !== 0) {
  2538. invPyth = 1.0 / pyth;
  2539. }
  2540. else {
  2541. invPyth = 0;
  2542. }
  2543. this.normal.x = yz * invPyth;
  2544. this.normal.y = xz * invPyth;
  2545. this.normal.z = xy * invPyth;
  2546. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  2547. return this;
  2548. };
  2549. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  2550. var dot = Vector3.Dot(this.normal, direction);
  2551. return (dot <= epsilon);
  2552. };
  2553. Plane.prototype.signedDistanceTo = function (point) {
  2554. return Vector3.Dot(point, this.normal) + this.d;
  2555. };
  2556. // Statics
  2557. Plane.FromArray = function (array) {
  2558. return new Plane(array[0], array[1], array[2], array[3]);
  2559. };
  2560. Plane.FromPoints = function (point1, point2, point3) {
  2561. var result = new Plane(0, 0, 0, 0);
  2562. result.copyFromPoints(point1, point2, point3);
  2563. return result;
  2564. };
  2565. Plane.FromPositionAndNormal = function (origin, normal) {
  2566. var result = new Plane(0, 0, 0, 0);
  2567. normal.normalize();
  2568. result.normal = normal;
  2569. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2570. return result;
  2571. };
  2572. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  2573. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2574. return Vector3.Dot(point, normal) + d;
  2575. };
  2576. return Plane;
  2577. }());
  2578. BABYLON.Plane = Plane;
  2579. var Viewport = (function () {
  2580. function Viewport(x, y, width, height) {
  2581. this.x = x;
  2582. this.y = y;
  2583. this.width = width;
  2584. this.height = height;
  2585. }
  2586. Viewport.prototype.toGlobal = function (renderWidth, renderHeight) {
  2587. return new Viewport(this.x * renderWidth, this.y * renderHeight, this.width * renderWidth, this.height * renderHeight);
  2588. };
  2589. return Viewport;
  2590. }());
  2591. BABYLON.Viewport = Viewport;
  2592. var Frustum = (function () {
  2593. function Frustum() {
  2594. }
  2595. Frustum.GetPlanes = function (transform) {
  2596. var frustumPlanes = [];
  2597. for (var index = 0; index < 6; index++) {
  2598. frustumPlanes.push(new Plane(0, 0, 0, 0));
  2599. }
  2600. Frustum.GetPlanesToRef(transform, frustumPlanes);
  2601. return frustumPlanes;
  2602. };
  2603. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  2604. // Near
  2605. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  2606. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  2607. frustumPlanes[0].normal.z = transform.m[11] + transform.m[10];
  2608. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  2609. frustumPlanes[0].normalize();
  2610. // Far
  2611. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  2612. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  2613. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  2614. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  2615. frustumPlanes[1].normalize();
  2616. // Left
  2617. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  2618. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  2619. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  2620. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  2621. frustumPlanes[2].normalize();
  2622. // Right
  2623. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  2624. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  2625. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  2626. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  2627. frustumPlanes[3].normalize();
  2628. // Top
  2629. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  2630. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  2631. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  2632. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  2633. frustumPlanes[4].normalize();
  2634. // Bottom
  2635. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  2636. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  2637. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  2638. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  2639. frustumPlanes[5].normalize();
  2640. };
  2641. return Frustum;
  2642. }());
  2643. BABYLON.Frustum = Frustum;
  2644. (function (Space) {
  2645. Space[Space["LOCAL"] = 0] = "LOCAL";
  2646. Space[Space["WORLD"] = 1] = "WORLD";
  2647. })(BABYLON.Space || (BABYLON.Space = {}));
  2648. var Space = BABYLON.Space;
  2649. var Axis = (function () {
  2650. function Axis() {
  2651. }
  2652. Axis.X = new Vector3(1, 0, 0);
  2653. Axis.Y = new Vector3(0, 1, 0);
  2654. Axis.Z = new Vector3(0, 0, 1);
  2655. return Axis;
  2656. }());
  2657. BABYLON.Axis = Axis;
  2658. ;
  2659. var BezierCurve = (function () {
  2660. function BezierCurve() {
  2661. }
  2662. BezierCurve.interpolate = function (t, x1, y1, x2, y2) {
  2663. // Extract X (which is equal to time here)
  2664. var f0 = 1 - 3 * x2 + 3 * x1;
  2665. var f1 = 3 * x2 - 6 * x1;
  2666. var f2 = 3 * x1;
  2667. var refinedT = t;
  2668. for (var i = 0; i < 5; i++) {
  2669. var refinedT2 = refinedT * refinedT;
  2670. var refinedT3 = refinedT2 * refinedT;
  2671. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2672. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2673. refinedT -= (x - t) * slope;
  2674. refinedT = Math.min(1, Math.max(0, refinedT));
  2675. }
  2676. // Resolve cubic bezier for the given x
  2677. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
  2678. 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
  2679. Math.pow(refinedT, 3);
  2680. };
  2681. return BezierCurve;
  2682. }());
  2683. BABYLON.BezierCurve = BezierCurve;
  2684. (function (Orientation) {
  2685. Orientation[Orientation["CW"] = 0] = "CW";
  2686. Orientation[Orientation["CCW"] = 1] = "CCW";
  2687. })(BABYLON.Orientation || (BABYLON.Orientation = {}));
  2688. var Orientation = BABYLON.Orientation;
  2689. var Angle = (function () {
  2690. function Angle(radians) {
  2691. var _this = this;
  2692. this.degrees = function () { return _this._radians * 180 / Math.PI; };
  2693. this.radians = function () { return _this._radians; };
  2694. this._radians = radians;
  2695. if (this._radians < 0)
  2696. this._radians += (2 * Math.PI);
  2697. }
  2698. Angle.BetweenTwoPoints = function (a, b) {
  2699. var delta = b.subtract(a);
  2700. var theta = Math.atan2(delta.y, delta.x);
  2701. return new Angle(theta);
  2702. };
  2703. Angle.FromRadians = function (radians) {
  2704. return new Angle(radians);
  2705. };
  2706. Angle.FromDegrees = function (degrees) {
  2707. return new Angle(degrees * Math.PI / 180);
  2708. };
  2709. return Angle;
  2710. }());
  2711. BABYLON.Angle = Angle;
  2712. var Arc2 = (function () {
  2713. function Arc2(startPoint, midPoint, endPoint) {
  2714. this.startPoint = startPoint;
  2715. this.midPoint = midPoint;
  2716. this.endPoint = endPoint;
  2717. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2718. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2719. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2720. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2721. 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);
  2722. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2723. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2724. var a1 = this.startAngle.degrees();
  2725. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2726. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2727. // angles correction
  2728. if (a2 - a1 > +180.0)
  2729. a2 -= 360.0;
  2730. if (a2 - a1 < -180.0)
  2731. a2 += 360.0;
  2732. if (a3 - a2 > +180.0)
  2733. a3 -= 360.0;
  2734. if (a3 - a2 < -180.0)
  2735. a3 += 360.0;
  2736. this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
  2737. this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
  2738. }
  2739. return Arc2;
  2740. }());
  2741. BABYLON.Arc2 = Arc2;
  2742. var Path2 = (function () {
  2743. function Path2(x, y) {
  2744. this._points = new Array();
  2745. this._length = 0;
  2746. this.closed = false;
  2747. this._points.push(new Vector2(x, y));
  2748. }
  2749. Path2.prototype.addLineTo = function (x, y) {
  2750. if (closed) {
  2751. //Tools.Error("cannot add lines to closed paths");
  2752. return this;
  2753. }
  2754. var newPoint = new Vector2(x, y);
  2755. var previousPoint = this._points[this._points.length - 1];
  2756. this._points.push(newPoint);
  2757. this._length += newPoint.subtract(previousPoint).length();
  2758. return this;
  2759. };
  2760. Path2.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
  2761. if (numberOfSegments === void 0) { numberOfSegments = 36; }
  2762. if (closed) {
  2763. //Tools.Error("cannot add arcs to closed paths");
  2764. return this;
  2765. }
  2766. var startPoint = this._points[this._points.length - 1];
  2767. var midPoint = new Vector2(midX, midY);
  2768. var endPoint = new Vector2(endX, endY);
  2769. var arc = new Arc2(startPoint, midPoint, endPoint);
  2770. var increment = arc.angle.radians() / numberOfSegments;
  2771. if (arc.orientation === Orientation.CW)
  2772. increment *= -1;
  2773. var currentAngle = arc.startAngle.radians() + increment;
  2774. for (var i = 0; i < numberOfSegments; i++) {
  2775. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2776. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2777. this.addLineTo(x, y);
  2778. currentAngle += increment;
  2779. }
  2780. return this;
  2781. };
  2782. Path2.prototype.close = function () {
  2783. this.closed = true;
  2784. return this;
  2785. };
  2786. Path2.prototype.length = function () {
  2787. var result = this._length;
  2788. if (!this.closed) {
  2789. var lastPoint = this._points[this._points.length - 1];
  2790. var firstPoint = this._points[0];
  2791. result += (firstPoint.subtract(lastPoint).length());
  2792. }
  2793. return result;
  2794. };
  2795. Path2.prototype.getPoints = function () {
  2796. return this._points;
  2797. };
  2798. Path2.prototype.getPointAtLengthPosition = function (normalizedLengthPosition) {
  2799. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2800. //Tools.Error("normalized length position should be between 0 and 1.");
  2801. return Vector2.Zero();
  2802. }
  2803. var lengthPosition = normalizedLengthPosition * this.length();
  2804. var previousOffset = 0;
  2805. for (var i = 0; i < this._points.length; i++) {
  2806. var j = (i + 1) % this._points.length;
  2807. var a = this._points[i];
  2808. var b = this._points[j];
  2809. var bToA = b.subtract(a);
  2810. var nextOffset = (bToA.length() + previousOffset);
  2811. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2812. var dir = bToA.normalize();
  2813. var localOffset = lengthPosition - previousOffset;
  2814. return new Vector2(a.x + (dir.x * localOffset), a.y + (dir.y * localOffset));
  2815. }
  2816. previousOffset = nextOffset;
  2817. }
  2818. //Tools.Error("internal error");
  2819. return Vector2.Zero();
  2820. };
  2821. Path2.StartingAt = function (x, y) {
  2822. return new Path2(x, y);
  2823. };
  2824. return Path2;
  2825. }());
  2826. BABYLON.Path2 = Path2;
  2827. var Path3D = (function () {
  2828. /**
  2829. * new Path3D(path, normal, raw)
  2830. * Creates a Path3D. A Path3D is a logical math object, so not a mesh.
  2831. * please read the description in the tutorial : http://doc.babylonjs.com/tutorials/How_to_use_Path3D
  2832. * path : an array of Vector3, the curve axis of the Path3D
  2833. * normal (optional) : Vector3, the first wanted normal to the curve. Ex (0, 1, 0) for a vertical normal.
  2834. * raw (optional, default false) : boolean, if true the returned Path3D isn't normalized. Useful to depict path acceleration or speed.
  2835. */
  2836. function Path3D(path, firstNormal, raw) {
  2837. this.path = path;
  2838. this._curve = new Array();
  2839. this._distances = new Array();
  2840. this._tangents = new Array();
  2841. this._normals = new Array();
  2842. this._binormals = new Array();
  2843. for (var p = 0; p < path.length; p++) {
  2844. this._curve[p] = path[p].clone(); // hard copy
  2845. }
  2846. this._raw = raw || false;
  2847. this._compute(firstNormal);
  2848. }
  2849. /**
  2850. * Returns the Path3D array of successive Vector3 designing its curve.
  2851. */
  2852. Path3D.prototype.getCurve = function () {
  2853. return this._curve;
  2854. };
  2855. /**
  2856. * Returns an array populated with tangent vectors on each Path3D curve point.
  2857. */
  2858. Path3D.prototype.getTangents = function () {
  2859. return this._tangents;
  2860. };
  2861. /**
  2862. * Returns an array populated with normal vectors on each Path3D curve point.
  2863. */
  2864. Path3D.prototype.getNormals = function () {
  2865. return this._normals;
  2866. };
  2867. /**
  2868. * Returns an array populated with binormal vectors on each Path3D curve point.
  2869. */
  2870. Path3D.prototype.getBinormals = function () {
  2871. return this._binormals;
  2872. };
  2873. /**
  2874. * Returns an array populated with distances (float) of the i-th point from the first curve point.
  2875. */
  2876. Path3D.prototype.getDistances = function () {
  2877. return this._distances;
  2878. };
  2879. /**
  2880. * Forces the Path3D tangent, normal, binormal and distance recomputation.
  2881. * Returns the same object updated.
  2882. */
  2883. Path3D.prototype.update = function (path, firstNormal) {
  2884. for (var p = 0; p < path.length; p++) {
  2885. this._curve[p].x = path[p].x;
  2886. this._curve[p].y = path[p].y;
  2887. this._curve[p].z = path[p].z;
  2888. }
  2889. this._compute(firstNormal);
  2890. return this;
  2891. };
  2892. // private function compute() : computes tangents, normals and binormals
  2893. Path3D.prototype._compute = function (firstNormal) {
  2894. var l = this._curve.length;
  2895. // first and last tangents
  2896. this._tangents[0] = this._getFirstNonNullVector(0);
  2897. if (!this._raw) {
  2898. this._tangents[0].normalize();
  2899. }
  2900. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  2901. if (!this._raw) {
  2902. this._tangents[l - 1].normalize();
  2903. }
  2904. // normals and binormals at first point : arbitrary vector with _normalVector()
  2905. var tg0 = this._tangents[0];
  2906. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  2907. this._normals[0] = pp0;
  2908. if (!this._raw) {
  2909. this._normals[0].normalize();
  2910. }
  2911. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  2912. if (!this._raw) {
  2913. this._binormals[0].normalize();
  2914. }
  2915. this._distances[0] = 0.0;
  2916. // normals and binormals : next points
  2917. var prev; // previous vector (segment)
  2918. var cur; // current vector (segment)
  2919. var curTang; // current tangent
  2920. // previous normal
  2921. var prevBinor; // previous binormal
  2922. for (var i = 1; i < l; i++) {
  2923. // tangents
  2924. prev = this._getLastNonNullVector(i);
  2925. if (i < l - 1) {
  2926. cur = this._getFirstNonNullVector(i);
  2927. this._tangents[i] = prev.add(cur);
  2928. this._tangents[i].normalize();
  2929. }
  2930. this._distances[i] = this._distances[i - 1] + prev.length();
  2931. // normals and binormals
  2932. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  2933. curTang = this._tangents[i];
  2934. prevBinor = this._binormals[i - 1];
  2935. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  2936. if (!this._raw) {
  2937. this._normals[i].normalize();
  2938. }
  2939. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  2940. if (!this._raw) {
  2941. this._binormals[i].normalize();
  2942. }
  2943. }
  2944. };
  2945. // private function getFirstNonNullVector(index)
  2946. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  2947. Path3D.prototype._getFirstNonNullVector = function (index) {
  2948. var i = 1;
  2949. var nNVector = this._curve[index + i].subtract(this._curve[index]);
  2950. while (nNVector.length() === 0 && index + i + 1 < this._curve.length) {
  2951. i++;
  2952. nNVector = this._curve[index + i].subtract(this._curve[index]);
  2953. }
  2954. return nNVector;
  2955. };
  2956. // private function getLastNonNullVector(index)
  2957. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  2958. Path3D.prototype._getLastNonNullVector = function (index) {
  2959. var i = 1;
  2960. var nLVector = this._curve[index].subtract(this._curve[index - i]);
  2961. while (nLVector.length() === 0 && index > i + 1) {
  2962. i++;
  2963. nLVector = this._curve[index].subtract(this._curve[index - i]);
  2964. }
  2965. return nLVector;
  2966. };
  2967. // private function normalVector(v0, vt, va) :
  2968. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  2969. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  2970. Path3D.prototype._normalVector = function (v0, vt, va) {
  2971. var normal0;
  2972. var tgl = vt.length();
  2973. if (tgl === 0.0) {
  2974. tgl = 1.0;
  2975. }
  2976. if (va === undefined || va === null) {
  2977. var point;
  2978. if (!MathTools.WithinEpsilon(Math.abs(vt.y) / tgl, 1.0, BABYLON.Epsilon)) {
  2979. point = new Vector3(0.0, -1.0, 0.0);
  2980. }
  2981. else if (!MathTools.WithinEpsilon(Math.abs(vt.x) / tgl, 1.0, BABYLON.Epsilon)) {
  2982. point = new Vector3(1.0, 0.0, 0.0);
  2983. }
  2984. else if (!MathTools.WithinEpsilon(Math.abs(vt.z) / tgl, 1.0, BABYLON.Epsilon)) {
  2985. point = new Vector3(0.0, 0.0, 1.0);
  2986. }
  2987. normal0 = Vector3.Cross(vt, point);
  2988. }
  2989. else {
  2990. normal0 = Vector3.Cross(vt, va);
  2991. Vector3.CrossToRef(normal0, vt, normal0);
  2992. }
  2993. normal0.normalize();
  2994. return normal0;
  2995. };
  2996. return Path3D;
  2997. }());
  2998. BABYLON.Path3D = Path3D;
  2999. var Curve3 = (function () {
  3000. /**
  3001. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  3002. * A Curve3 is designed from a series of successive Vector3.
  3003. * Tuto : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#curve3-object
  3004. */
  3005. function Curve3(points) {
  3006. this._length = 0.0;
  3007. this._points = points;
  3008. this._length = this._computeLength(points);
  3009. }
  3010. /**
  3011. * Returns a Curve3 object along a Quadratic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#quadratic-bezier-curve
  3012. * @param v0 (Vector3) the origin point of the Quadratic Bezier
  3013. * @param v1 (Vector3) the control point
  3014. * @param v2 (Vector3) the end point of the Quadratic Bezier
  3015. * @param nbPoints (integer) the wanted number of points in the curve
  3016. */
  3017. Curve3.CreateQuadraticBezier = function (v0, v1, v2, nbPoints) {
  3018. nbPoints = nbPoints > 2 ? nbPoints : 3;
  3019. var bez = new Array();
  3020. var equation = function (t, val0, val1, val2) {
  3021. var res = (1.0 - t) * (1.0 - t) * val0 + 2.0 * t * (1.0 - t) * val1 + t * t * val2;
  3022. return res;
  3023. };
  3024. for (var i = 0; i <= nbPoints; i++) {
  3025. 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)));
  3026. }
  3027. return new Curve3(bez);
  3028. };
  3029. /**
  3030. * Returns a Curve3 object along a Cubic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#cubic-bezier-curve
  3031. * @param v0 (Vector3) the origin point of the Cubic Bezier
  3032. * @param v1 (Vector3) the first control point
  3033. * @param v2 (Vector3) the second control point
  3034. * @param v3 (Vector3) the end point of the Cubic Bezier
  3035. * @param nbPoints (integer) the wanted number of points in the curve
  3036. */
  3037. Curve3.CreateCubicBezier = function (v0, v1, v2, v3, nbPoints) {
  3038. nbPoints = nbPoints > 3 ? nbPoints : 4;
  3039. var bez = new Array();
  3040. var equation = function (t, val0, val1, val2, val3) {
  3041. 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;
  3042. return res;
  3043. };
  3044. for (var i = 0; i <= nbPoints; i++) {
  3045. 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)));
  3046. }
  3047. return new Curve3(bez);
  3048. };
  3049. /**
  3050. * Returns a Curve3 object along a Hermite Spline curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#hermite-spline
  3051. * @param p1 (Vector3) the origin point of the Hermite Spline
  3052. * @param t1 (Vector3) the tangent vector at the origin point
  3053. * @param p2 (Vector3) the end point of the Hermite Spline
  3054. * @param t2 (Vector3) the tangent vector at the end point
  3055. * @param nbPoints (integer) the wanted number of points in the curve
  3056. */
  3057. Curve3.CreateHermiteSpline = function (p1, t1, p2, t2, nbPoints) {
  3058. var hermite = new Array();
  3059. var step = 1.0 / nbPoints;
  3060. for (var i = 0; i <= nbPoints; i++) {
  3061. hermite.push(Vector3.Hermite(p1, t1, p2, t2, i * step));
  3062. }
  3063. return new Curve3(hermite);
  3064. };
  3065. /**
  3066. * Returns the Curve3 stored array of successive Vector3
  3067. */
  3068. Curve3.prototype.getPoints = function () {
  3069. return this._points;
  3070. };
  3071. /**
  3072. * Returns the computed length (float) of the curve.
  3073. */
  3074. Curve3.prototype.length = function () {
  3075. return this._length;
  3076. };
  3077. /**
  3078. * Returns a new instance of Curve3 object : var curve = curveA.continue(curveB);
  3079. * This new Curve3 is built by translating and sticking the curveB at the end of the curveA.
  3080. * curveA and curveB keep unchanged.
  3081. */
  3082. Curve3.prototype.continue = function (curve) {
  3083. var lastPoint = this._points[this._points.length - 1];
  3084. var continuedPoints = this._points.slice();
  3085. var curvePoints = curve.getPoints();
  3086. for (var i = 1; i < curvePoints.length; i++) {
  3087. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  3088. }
  3089. var continuedCurve = new Curve3(continuedPoints);
  3090. return continuedCurve;
  3091. };
  3092. Curve3.prototype._computeLength = function (path) {
  3093. var l = 0;
  3094. for (var i = 1; i < path.length; i++) {
  3095. l += (path[i].subtract(path[i - 1])).length();
  3096. }
  3097. return l;
  3098. };
  3099. return Curve3;
  3100. }());
  3101. BABYLON.Curve3 = Curve3;
  3102. // SphericalHarmonics
  3103. var SphericalHarmonics = (function () {
  3104. function SphericalHarmonics() {
  3105. this.L00 = Vector3.Zero();
  3106. this.L1_1 = Vector3.Zero();
  3107. this.L10 = Vector3.Zero();
  3108. this.L11 = Vector3.Zero();
  3109. this.L2_2 = Vector3.Zero();
  3110. this.L2_1 = Vector3.Zero();
  3111. this.L20 = Vector3.Zero();
  3112. this.L21 = Vector3.Zero();
  3113. this.L22 = Vector3.Zero();
  3114. }
  3115. SphericalHarmonics.prototype.addLight = function (direction, color, deltaSolidAngle) {
  3116. var colorVector = new Vector3(color.r, color.g, color.b);
  3117. var c = colorVector.scale(deltaSolidAngle);
  3118. this.L00 = this.L00.add(c.scale(0.282095));
  3119. this.L1_1 = this.L1_1.add(c.scale(0.488603 * direction.y));
  3120. this.L10 = this.L10.add(c.scale(0.488603 * direction.z));
  3121. this.L11 = this.L11.add(c.scale(0.488603 * direction.x));
  3122. this.L2_2 = this.L2_2.add(c.scale(1.092548 * direction.x * direction.y));
  3123. this.L2_1 = this.L2_1.add(c.scale(1.092548 * direction.y * direction.z));
  3124. this.L21 = this.L21.add(c.scale(1.092548 * direction.x * direction.z));
  3125. this.L20 = this.L20.add(c.scale(0.315392 * (3.0 * direction.z * direction.z - 1.0)));
  3126. this.L22 = this.L22.add(c.scale(0.546274 * (direction.x * direction.x - direction.y * direction.y)));
  3127. };
  3128. SphericalHarmonics.prototype.scale = function (scale) {
  3129. this.L00 = this.L00.scale(scale);
  3130. this.L1_1 = this.L1_1.scale(scale);
  3131. this.L10 = this.L10.scale(scale);
  3132. this.L11 = this.L11.scale(scale);
  3133. this.L2_2 = this.L2_2.scale(scale);
  3134. this.L2_1 = this.L2_1.scale(scale);
  3135. this.L20 = this.L20.scale(scale);
  3136. this.L21 = this.L21.scale(scale);
  3137. this.L22 = this.L22.scale(scale);
  3138. };
  3139. return SphericalHarmonics;
  3140. }());
  3141. BABYLON.SphericalHarmonics = SphericalHarmonics;
  3142. // SphericalPolynomial
  3143. var SphericalPolynomial = (function () {
  3144. function SphericalPolynomial() {
  3145. this.x = Vector3.Zero();
  3146. this.y = Vector3.Zero();
  3147. this.z = Vector3.Zero();
  3148. this.xx = Vector3.Zero();
  3149. this.yy = Vector3.Zero();
  3150. this.zz = Vector3.Zero();
  3151. this.xy = Vector3.Zero();
  3152. this.yz = Vector3.Zero();
  3153. this.zx = Vector3.Zero();
  3154. }
  3155. SphericalPolynomial.prototype.addAmbient = function (color) {
  3156. var colorVector = new Vector3(color.r, color.g, color.b);
  3157. this.xx = this.xx.add(colorVector);
  3158. this.yy = this.yy.add(colorVector);
  3159. this.zz = this.zz.add(colorVector);
  3160. };
  3161. SphericalPolynomial.getSphericalPolynomialFromHarmonics = function (harmonics) {
  3162. var result = new SphericalPolynomial();
  3163. result.x = harmonics.L11.scale(1.02333);
  3164. result.y = harmonics.L1_1.scale(1.02333);
  3165. result.z = harmonics.L10.scale(1.02333);
  3166. result.xx = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).add(harmonics.L22.scale(0.429043));
  3167. result.yy = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).subtract(harmonics.L22.scale(0.429043));
  3168. result.zz = harmonics.L00.scale(0.886277).add(harmonics.L20.scale(0.495417));
  3169. result.yz = harmonics.L2_1.scale(0.858086);
  3170. result.zx = harmonics.L21.scale(0.858086);
  3171. result.xy = harmonics.L2_2.scale(0.858086);
  3172. return result;
  3173. };
  3174. return SphericalPolynomial;
  3175. }());
  3176. BABYLON.SphericalPolynomial = SphericalPolynomial;
  3177. // Vertex formats
  3178. var PositionNormalVertex = (function () {
  3179. function PositionNormalVertex(position, normal) {
  3180. if (position === void 0) { position = Vector3.Zero(); }
  3181. if (normal === void 0) { normal = Vector3.Up(); }
  3182. this.position = position;
  3183. this.normal = normal;
  3184. }
  3185. PositionNormalVertex.prototype.clone = function () {
  3186. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  3187. };
  3188. return PositionNormalVertex;
  3189. }());
  3190. BABYLON.PositionNormalVertex = PositionNormalVertex;
  3191. var PositionNormalTextureVertex = (function () {
  3192. function PositionNormalTextureVertex(position, normal, uv) {
  3193. if (position === void 0) { position = Vector3.Zero(); }
  3194. if (normal === void 0) { normal = Vector3.Up(); }
  3195. if (uv === void 0) { uv = Vector2.Zero(); }
  3196. this.position = position;
  3197. this.normal = normal;
  3198. this.uv = uv;
  3199. }
  3200. PositionNormalTextureVertex.prototype.clone = function () {
  3201. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  3202. };
  3203. return PositionNormalTextureVertex;
  3204. }());
  3205. BABYLON.PositionNormalTextureVertex = PositionNormalTextureVertex;
  3206. // Temporary pre-allocated objects for engine internal use
  3207. // usage in any internal function :
  3208. // var tmp = Tmp.Vector3[0]; <= gets access to the first pre-created Vector3
  3209. // There's a Tmp array per object type : int, float, Vector2, Vector3, Vector4, Quaternion, Matrix
  3210. var Tmp = (function () {
  3211. function Tmp() {
  3212. }
  3213. Tmp.Color3 = [Color3.Black(), Color3.Black(), Color3.Black()];
  3214. Tmp.Vector2 = [Vector2.Zero(), Vector2.Zero(), Vector2.Zero()]; // 3 temp Vector2 at once should be enough
  3215. Tmp.Vector3 = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(),
  3216. Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()]; // 9 temp Vector3 at once should be enough
  3217. Tmp.Vector4 = [Vector4.Zero(), Vector4.Zero(), Vector4.Zero()]; // 3 temp Vector4 at once should be enough
  3218. Tmp.Quaternion = [new Quaternion(0, 0, 0, 0)]; // 1 temp Quaternion at once should be enough
  3219. Tmp.Matrix = [Matrix.Zero(), Matrix.Zero(),
  3220. Matrix.Zero(), Matrix.Zero(),
  3221. Matrix.Zero(), Matrix.Zero(),
  3222. Matrix.Zero(), Matrix.Zero()]; // 6 temp Matrices at once should be enough
  3223. return Tmp;
  3224. }());
  3225. BABYLON.Tmp = Tmp;
  3226. })(BABYLON || (BABYLON = {}));