babylon.math.ts 202 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088
  1. module BABYLON {
  2. declare var SIMD;
  3. export const ToGammaSpace = 1 / 2.2;
  4. export const ToLinearSpace = 2.2;
  5. export const Epsilon = 0.001;
  6. export class MathTools {
  7. /**
  8. * Boolean : true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
  9. */
  10. public static WithinEpsilon(a: number, b: number, epsilon: number = 1.401298E-45): boolean {
  11. var num = a - b;
  12. return -epsilon <= num && num <= epsilon;
  13. }
  14. /**
  15. * Returns a string : the upper case translation of the number i to hexadecimal.
  16. */
  17. public static ToHex(i: number): string {
  18. var str = i.toString(16);
  19. if (i <= 15) {
  20. return ("0" + str).toUpperCase();
  21. }
  22. return str.toUpperCase();
  23. }
  24. /**
  25. * Returns -1 if value is negative and +1 is value is positive.
  26. * Returns the value itself if it's equal to zero.
  27. */
  28. public static Sign(value: number): number {
  29. value = +value; // convert to a number
  30. if (value === 0 || isNaN(value))
  31. return value;
  32. return value > 0 ? 1 : -1;
  33. }
  34. /**
  35. * Returns the value itself if it's between min and max.
  36. * Returns min if the value is lower than min.
  37. * Returns max if the value is greater than max.
  38. */
  39. public static Clamp(value: number, min = 0, max = 1): number {
  40. return Math.min(max, Math.max(min, value));
  41. }
  42. }
  43. export class Color3 {
  44. /**
  45. * Creates a new Color3 object from red, green, blue values, all between 0 and 1.
  46. */
  47. constructor(public r: number = 0, public g: number = 0, public b: number = 0) {
  48. }
  49. /**
  50. * Returns a string with the Color3 current values.
  51. */
  52. public toString(): string {
  53. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  54. }
  55. /**
  56. * Returns the string "Color3".
  57. */
  58. public getClassName(): string {
  59. return "Color3";
  60. }
  61. /**
  62. * Returns the Color3 hash code.
  63. */
  64. public getHashCode(): number {
  65. let hash = this.r || 0;
  66. hash = (hash * 397) ^ (this.g || 0);
  67. hash = (hash * 397) ^ (this.b || 0);
  68. return hash;
  69. }
  70. // Operators
  71. /**
  72. * Stores in the passed array from the passed starting index the red, green, blue values as successive elements.
  73. * Returns the Color3.
  74. */
  75. public toArray(array: number[] | Float32Array, index?: number): Color3 {
  76. if (index === undefined) {
  77. index = 0;
  78. }
  79. array[index] = this.r;
  80. array[index + 1] = this.g;
  81. array[index + 2] = this.b;
  82. return this;
  83. }
  84. /**
  85. * Returns a new Color4 object from the current Color3 and the passed alpha.
  86. */
  87. public toColor4(alpha = 1): Color4 {
  88. return new Color4(this.r, this.g, this.b, alpha);
  89. }
  90. /**
  91. * Returns a new array populated with 3 numeric elements : red, green and blue values.
  92. */
  93. public asArray(): number[] {
  94. var result = [];
  95. this.toArray(result, 0);
  96. return result;
  97. }
  98. /**
  99. * Returns the luminance value (float).
  100. */
  101. public toLuminance(): number {
  102. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  103. }
  104. /**
  105. * Multiply each Color3 rgb values by the passed Color3 rgb values in a new Color3 object.
  106. * Returns this new object.
  107. */
  108. public multiply(otherColor: Color3): Color3 {
  109. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  110. }
  111. /**
  112. * Multiply the rgb values of the Color3 and the passed Color3 and stores the result in the object "result".
  113. * Returns the current Color3.
  114. */
  115. public multiplyToRef(otherColor: Color3, result: Color3): Color3 {
  116. result.r = this.r * otherColor.r;
  117. result.g = this.g * otherColor.g;
  118. result.b = this.b * otherColor.b;
  119. return this;
  120. }
  121. /**
  122. * Boolean : True if the rgb values are equal to the passed ones.
  123. */
  124. public equals(otherColor: Color3): boolean {
  125. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  126. }
  127. /**
  128. * Boolean : True if the rgb values are equal to the passed ones.
  129. */
  130. public equalsFloats(r: number, g: number, b: number): boolean {
  131. return this.r === r && this.g === g && this.b === b;
  132. }
  133. /**
  134. * Multiplies in place each rgb value by scale.
  135. * Returns the updated Color3.
  136. */
  137. public scale(scale: number): Color3 {
  138. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  139. }
  140. /**
  141. * Multiplies the rgb values by scale and stores the result into "result".
  142. * Returns the unmodified current Color3.
  143. */
  144. public scaleToRef(scale: number, result: Color3): Color3 {
  145. result.r = this.r * scale;
  146. result.g = this.g * scale;
  147. result.b = this.b * scale;
  148. return this;
  149. }
  150. /**
  151. * Returns a new Color3 set with the added values of the current Color3 and of the passed one.
  152. */
  153. public add(otherColor: Color3): Color3 {
  154. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  155. }
  156. /**
  157. * Stores the result of the addition of the current Color3 and passed one rgb values into "result".
  158. * Returns the unmodified current Color3.
  159. */
  160. public addToRef(otherColor: Color3, result: Color3): Color3 {
  161. result.r = this.r + otherColor.r;
  162. result.g = this.g + otherColor.g;
  163. result.b = this.b + otherColor.b;
  164. return this;
  165. }
  166. /**
  167. * Returns a new Color3 set with the subtracted values of the passed one from the current Color3 .
  168. */
  169. public subtract(otherColor: Color3): Color3 {
  170. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  171. }
  172. /**
  173. * Stores the result of the subtraction of passed one from the current Color3 rgb values into "result".
  174. * Returns the unmodified current Color3.
  175. */
  176. public subtractToRef(otherColor: Color3, result: Color3): Color3 {
  177. result.r = this.r - otherColor.r;
  178. result.g = this.g - otherColor.g;
  179. result.b = this.b - otherColor.b;
  180. return this;
  181. }
  182. /**
  183. * Returns a new Color3 copied the current one.
  184. */
  185. public clone(): Color3 {
  186. return new Color3(this.r, this.g, this.b);
  187. }
  188. /**
  189. * Copies the rgb values from the source in the current Color3.
  190. * Returns the updated Color3.
  191. */
  192. public copyFrom(source: Color3): Color3 {
  193. this.r = source.r;
  194. this.g = source.g;
  195. this.b = source.b;
  196. return this;
  197. }
  198. /**
  199. * Updates the Color3 rgb values from the passed floats.
  200. * Returns the Color3.
  201. */
  202. public copyFromFloats(r: number, g: number, b: number): Color3 {
  203. this.r = r;
  204. this.g = g;
  205. this.b = b;
  206. return this;
  207. }
  208. /**
  209. * Updates the Color3 rgb values from the passed floats.
  210. * Returns the Color3.
  211. */
  212. public set(r: number, g: number, b: number): Color3 {
  213. return this.copyFromFloats(r, g, b);
  214. }
  215. /**
  216. * Returns the Color3 hexadecimal code as a string.
  217. */
  218. public toHexString(): string {
  219. var intR = (this.r * 255) | 0;
  220. var intG = (this.g * 255) | 0;
  221. var intB = (this.b * 255) | 0;
  222. return "#" + MathTools.ToHex(intR) + MathTools.ToHex(intG) + MathTools.ToHex(intB);
  223. }
  224. /**
  225. * Returns a new Color3 converted to linear space.
  226. */
  227. public toLinearSpace(): Color3 {
  228. var convertedColor = new Color3();
  229. this.toLinearSpaceToRef(convertedColor);
  230. return convertedColor;
  231. }
  232. /**
  233. * Converts the Color3 values to linear space and stores the result in "convertedColor".
  234. * Returns the unmodified Color3.
  235. */
  236. public toLinearSpaceToRef(convertedColor: Color3): Color3 {
  237. convertedColor.r = Math.pow(this.r, ToLinearSpace);
  238. convertedColor.g = Math.pow(this.g, ToLinearSpace);
  239. convertedColor.b = Math.pow(this.b, ToLinearSpace);
  240. return this;
  241. }
  242. /**
  243. * Returns a new Color3 converted to gamma space.
  244. */
  245. public toGammaSpace(): Color3 {
  246. var convertedColor = new Color3();
  247. this.toGammaSpaceToRef(convertedColor);
  248. return convertedColor;
  249. }
  250. /**
  251. * Converts the Color3 values to gamma space and stores the result in "convertedColor".
  252. * Returns the unmodified Color3.
  253. */
  254. public toGammaSpaceToRef(convertedColor: Color3): Color3 {
  255. convertedColor.r = Math.pow(this.r, ToGammaSpace);
  256. convertedColor.g = Math.pow(this.g, ToGammaSpace);
  257. convertedColor.b = Math.pow(this.b, ToGammaSpace);
  258. return this;
  259. }
  260. // Statics
  261. /**
  262. * Creates a new Color3 from the string containing valid hexadecimal values.
  263. */
  264. public static FromHexString(hex: string): Color3 {
  265. if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
  266. //Tools.Warn("Color3.FromHexString must be called with a string like #FFFFFF");
  267. return new Color3(0, 0, 0);
  268. }
  269. var r = parseInt(hex.substring(1, 3), 16);
  270. var g = parseInt(hex.substring(3, 5), 16);
  271. var b = parseInt(hex.substring(5, 7), 16);
  272. return Color3.FromInts(r, g, b);
  273. }
  274. /**
  275. * Creates a new Vector3 from the startind index of the passed array.
  276. */
  277. public static FromArray(array: number[], offset: number = 0): Color3 {
  278. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  279. }
  280. /**
  281. * Creates a new Color3 from integer values ( < 256).
  282. */
  283. public static FromInts(r: number, g: number, b: number): Color3 {
  284. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  285. }
  286. /**
  287. * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3.
  288. */
  289. public static Lerp(start: Color3, end: Color3, amount: number): Color3 {
  290. var r = start.r + ((end.r - start.r) * amount);
  291. var g = start.g + ((end.g - start.g) * amount);
  292. var b = start.b + ((end.b - start.b) * amount);
  293. return new Color3(r, g, b);
  294. }
  295. public static Red(): Color3 { return new Color3(1, 0, 0); }
  296. public static Green(): Color3 { return new Color3(0, 1, 0); }
  297. public static Blue(): Color3 { return new Color3(0, 0, 1); }
  298. public static Black(): Color3 { return new Color3(0, 0, 0); }
  299. public static White(): Color3 { return new Color3(1, 1, 1); }
  300. public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
  301. public static Magenta(): Color3 { return new Color3(1, 0, 1); }
  302. public static Yellow(): Color3 { return new Color3(1, 1, 0); }
  303. public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
  304. public static Random(): Color3 { return new Color3(Math.random(), Math.random(), Math.random()); }
  305. }
  306. export class Color4 {
  307. /**
  308. * Creates a new Color4 object from the passed float values ( < 1) : red, green, blue, alpha.
  309. */
  310. constructor(public r: number, public g: number, public b: number, public a: number) {
  311. }
  312. // Operators
  313. /**
  314. * Adds in place the passed Color4 values to the current Color4.
  315. * Returns the updated Color4.
  316. */
  317. public addInPlace(right): Color4 {
  318. this.r += right.r;
  319. this.g += right.g;
  320. this.b += right.b;
  321. this.a += right.a;
  322. return this;
  323. }
  324. /**
  325. * Returns a new array populated with 4 numeric elements : red, green, blue, alpha values.
  326. */
  327. public asArray(): number[] {
  328. var result = [];
  329. this.toArray(result, 0);
  330. return result;
  331. }
  332. /**
  333. * Stores from the starting index in the passed array the Color4 successive values.
  334. * Returns the Color4.
  335. */
  336. public toArray(array: number[], index?: number): Color4 {
  337. if (index === undefined) {
  338. index = 0;
  339. }
  340. array[index] = this.r;
  341. array[index + 1] = this.g;
  342. array[index + 2] = this.b;
  343. array[index + 3] = this.a;
  344. return this;
  345. }
  346. /**
  347. * Returns a new Color4 set with the added values of the current Color4 and of the passed one.
  348. */
  349. public add(right: Color4): Color4 {
  350. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  351. }
  352. /**
  353. * Returns a new Color4 set with the subtracted values of the passed one from the current Color4.
  354. */
  355. public subtract(right: Color4): Color4 {
  356. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  357. }
  358. /**
  359. * Subtracts the passed ones from the current Color4 values and stores the results in "result".
  360. * Returns the Color4.
  361. */
  362. public subtractToRef(right: Color4, result: Color4): Color4 {
  363. result.r = this.r - right.r;
  364. result.g = this.g - right.g;
  365. result.b = this.b - right.b;
  366. result.a = this.a - right.a;
  367. return this;
  368. }
  369. /**
  370. * Creates a new Color4 with the current Color4 values multiplied by scale.
  371. */
  372. public scale(scale: number): Color4 {
  373. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  374. }
  375. /**
  376. * Multiplies the current Color4 values by scale and stores the result in "result".
  377. * Returns the Color4.
  378. */
  379. public scaleToRef(scale: number, result: Color4): Color4 {
  380. result.r = this.r * scale;
  381. result.g = this.g * scale;
  382. result.b = this.b * scale;
  383. result.a = this.a * scale;
  384. return this;
  385. }
  386. /**
  387. * Multipy an RGBA Color4 value by another and return a new Color4 object
  388. * @param color The Color4 (RGBA) value to multiply by
  389. * @returns A new Color4.
  390. */
  391. public multiply(color: Color4): Color4 {
  392. return new Color4(this.r * color.r, this.g * color.g, this.b * color.b, this.a * color.a);
  393. }
  394. /**
  395. * Multipy an RGBA Color4 value by another and push the result in a reference value
  396. * @param color The Color4 (RGBA) value to multiply by
  397. * @param result The Color4 (RGBA) to fill the result in
  398. * @returns the result Color4.
  399. */
  400. public multiplyToRef(color: Color4, result: Color4): Color4 {
  401. result.r = this.r * color.r;
  402. result.g = this.g * color.g;
  403. result.b = this.b * color.b;
  404. result.a = this.a * color.a;
  405. return result;
  406. }
  407. /**
  408. * Returns a string with the Color4 values.
  409. */
  410. public toString(): string {
  411. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  412. }
  413. /**
  414. * Returns the string "Color4"
  415. */
  416. public getClassName(): string {
  417. return "Color4";
  418. }
  419. /**
  420. * Return the Color4 hash code as a number.
  421. */
  422. public getHashCode(): number {
  423. let hash = this.r || 0;
  424. hash = (hash * 397) ^ (this.g || 0);
  425. hash = (hash * 397) ^ (this.b || 0);
  426. hash = (hash * 397) ^ (this.a || 0);
  427. return hash;
  428. }
  429. /**
  430. * Creates a new Color4 copied from the current one.
  431. */
  432. public clone(): Color4 {
  433. return new Color4(this.r, this.g, this.b, this.a);
  434. }
  435. /**
  436. * Copies the passed Color4 values into the current one.
  437. * Returns the updated Color4.
  438. */
  439. public copyFrom(source: Color4): Color4 {
  440. this.r = source.r;
  441. this.g = source.g;
  442. this.b = source.b;
  443. this.a = source.a;
  444. return this;
  445. }
  446. /**
  447. * Copies the passed float values into the current one.
  448. * Returns the updated Color4.
  449. */
  450. public copyFromFloats(r: number, g: number, b: number, a: number): Color4 {
  451. this.r = r;
  452. this.g = g;
  453. this.b = b;
  454. this.a = a;
  455. return this;
  456. }
  457. /**
  458. * Copies the passed float values into the current one.
  459. * Returns the updated Color4.
  460. */
  461. public set(r: number, g: number, b: number, a: number): Color4 {
  462. return this.copyFromFloats(r, g, b,a);
  463. }
  464. /**
  465. * Returns a string containing the hexadecimal Color4 code.
  466. */
  467. public toHexString(): string {
  468. var intR = (this.r * 255) | 0;
  469. var intG = (this.g * 255) | 0;
  470. var intB = (this.b * 255) | 0;
  471. var intA = (this.a * 255) | 0;
  472. return "#" + MathTools.ToHex(intR) + MathTools.ToHex(intG) + MathTools.ToHex(intB) + MathTools.ToHex(intA);
  473. }
  474. // Statics
  475. /**
  476. * Creates a new Color4 from the valid hexadecimal value contained in the passed string.
  477. */
  478. public static FromHexString(hex: string): Color4 {
  479. if (hex.substring(0, 1) !== "#" || hex.length !== 9) {
  480. //Tools.Warn("Color4.FromHexString must be called with a string like #FFFFFFFF");
  481. return new Color4(0.0, 0.0, 0.0, 0.0);
  482. }
  483. var r = parseInt(hex.substring(1, 3), 16);
  484. var g = parseInt(hex.substring(3, 5), 16);
  485. var b = parseInt(hex.substring(5, 7), 16);
  486. var a = parseInt(hex.substring(7, 9), 16);
  487. return Color4.FromInts(r, g, b, a);
  488. }
  489. /**
  490. * Creates a new Color4 object set with the linearly interpolated values of "amount" between the left Color4 and the right Color4.
  491. */
  492. public static Lerp(left: Color4, right: Color4, amount: number): Color4 {
  493. var result = new Color4(0.0, 0.0, 0.0, 0.0);
  494. Color4.LerpToRef(left, right, amount, result);
  495. return result;
  496. }
  497. /**
  498. * Set the passed "result" with the linearly interpolated values of "amount" between the left Color4 and the right Color4.
  499. */
  500. public static LerpToRef(left: Color4, right: Color4, amount: number, result: Color4): void {
  501. result.r = left.r + (right.r - left.r) * amount;
  502. result.g = left.g + (right.g - left.g) * amount;
  503. result.b = left.b + (right.b - left.b) * amount;
  504. result.a = left.a + (right.a - left.a) * amount;
  505. }
  506. /**
  507. * Creates a new Color4 from the starting index element of the passed array.
  508. */
  509. public static FromArray(array: number[], offset: number = 0): Color4 {
  510. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  511. }
  512. /**
  513. * Creates a new Color4 from the passed integers ( < 256 ).
  514. */
  515. public static FromInts(r: number, g: number, b: number, a: number): Color4 {
  516. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  517. }
  518. public static CheckColors4(colors: number[], count: number): number[] {
  519. // Check if color3 was used
  520. if (colors.length === count * 3) {
  521. var colors4 = [];
  522. for (var index = 0; index < colors.length; index += 3) {
  523. var newIndex = (index / 3) * 4;
  524. colors4[newIndex] = colors[index];
  525. colors4[newIndex + 1] = colors[index + 1];
  526. colors4[newIndex + 2] = colors[index + 2];
  527. colors4[newIndex + 3] = 1.0;
  528. }
  529. return colors4;
  530. }
  531. return colors;
  532. }
  533. }
  534. export class Vector2 {
  535. /**
  536. * Creates a new Vector2 from the passed x and y coordinates.
  537. */
  538. constructor(public x: number, public y: number) {
  539. }
  540. /**
  541. * Returns a string with the Vector2 coordinates.
  542. */
  543. public toString(): string {
  544. return "{X: " + this.x + " Y:" + this.y + "}";
  545. }
  546. /**
  547. * Returns the string "Vector2"
  548. */
  549. public getClassName(): string {
  550. return "Vector2";
  551. }
  552. /**
  553. * Returns the Vector2 hash code as a number.
  554. */
  555. public getHashCode(): number {
  556. let hash = this.x || 0;
  557. hash = (hash * 397) ^ (this.y || 0);
  558. return hash;
  559. }
  560. // Operators
  561. /**
  562. * Sets the Vector2 coordinates in the passed array or Float32Array from the passed index.
  563. * Returns the Vector2.
  564. */
  565. public toArray(array: number[] | Float32Array, index: number = 0): Vector2 {
  566. array[index] = this.x;
  567. array[index + 1] = this.y;
  568. return this;
  569. }
  570. /**
  571. * Returns a new array with 2 elements : the Vector2 coordinates.
  572. */
  573. public asArray(): number[] {
  574. var result = [];
  575. this.toArray(result, 0);
  576. return result;
  577. }
  578. /**
  579. * Sets the Vector2 coordinates with the passed Vector2 coordinates.
  580. * Returns the updated Vector2.
  581. */
  582. public copyFrom(source: Vector2): Vector2 {
  583. this.x = source.x;
  584. this.y = source.y;
  585. return this;
  586. }
  587. /**
  588. * Sets the Vector2 coordinates with the passed floats.
  589. * Returns the updated Vector2.
  590. */
  591. public copyFromFloats(x: number, y: number): Vector2 {
  592. this.x = x;
  593. this.y = y;
  594. return this;
  595. }
  596. /**
  597. * Sets the Vector2 coordinates with the passed floats.
  598. * Returns the updated Vector2.
  599. */
  600. public set(x: number, y: number): Vector2 {
  601. return this.copyFromFloats(x, y);
  602. }
  603. /**
  604. * Returns a new Vector2 set with the addition of the current Vector2 and the passed one coordinates.
  605. */
  606. public add(otherVector: Vector2): Vector2 {
  607. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  608. }
  609. /**
  610. * Sets the "result" coordinates with the addition of the current Vector2 and the passed one coordinates.
  611. * Returns the Vector2.
  612. */
  613. public addToRef(otherVector: Vector2, result: Vector2): Vector2 {
  614. result.x = this.x + otherVector.x;
  615. result.y = this.y + otherVector.y;
  616. return this;
  617. }
  618. /**
  619. * Set the Vector2 coordinates by adding the passed Vector2 coordinates.
  620. * Returns the updated Vector2.
  621. */
  622. public addInPlace(otherVector: Vector2): Vector2 {
  623. this.x += otherVector.x;
  624. this.y += otherVector.y;
  625. return this;
  626. }
  627. /**
  628. * Returns a new Vector2 by adding the current Vector2 coordinates to the passed Vector3 x, y coordinates.
  629. */
  630. public addVector3(otherVector: Vector3): Vector2 {
  631. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  632. }
  633. /**
  634. * Returns a new Vector2 set with the subtracted coordinates of the passed one from the current Vector2.
  635. */
  636. public subtract(otherVector: Vector2): Vector2 {
  637. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  638. }
  639. /**
  640. * Sets the "result" coordinates with the subtraction of the passed one from the current Vector2 coordinates.
  641. * Returns the Vector2.
  642. */
  643. public subtractToRef(otherVector: Vector2, result: Vector2): Vector2 {
  644. result.x = this.x - otherVector.x;
  645. result.y = this.y - otherVector.y;
  646. return this;
  647. }
  648. /**
  649. * Sets the current Vector2 coordinates by subtracting from it the passed one coordinates.
  650. * Returns the updated Vector2.
  651. */
  652. public subtractInPlace(otherVector: Vector2): Vector2 {
  653. this.x -= otherVector.x;
  654. this.y -= otherVector.y;
  655. return this;
  656. }
  657. /**
  658. * Multiplies in place the current Vector2 coordinates by the passed ones.
  659. * Returns the updated Vector2.
  660. */
  661. public multiplyInPlace(otherVector: Vector2): Vector2 {
  662. this.x *= otherVector.x;
  663. this.y *= otherVector.y;
  664. return this;
  665. }
  666. /**
  667. * Returns a new Vector2 set with the multiplication of the current Vector2 and the passed one coordinates.
  668. */
  669. public multiply(otherVector: Vector2): Vector2 {
  670. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  671. }
  672. /**
  673. * Sets "result" coordinates with the multiplication of the current Vector2 and the passed one coordinates.
  674. * Returns the Vector2.
  675. */
  676. public multiplyToRef(otherVector: Vector2, result: Vector2): Vector2 {
  677. result.x = this.x * otherVector.x;
  678. result.y = this.y * otherVector.y;
  679. return this;
  680. }
  681. /**
  682. * Returns a new Vector2 set with the Vector2 coordinates multiplied by the passed floats.
  683. */
  684. public multiplyByFloats(x: number, y: number): Vector2 {
  685. return new Vector2(this.x * x, this.y * y);
  686. }
  687. /**
  688. * Returns a new Vector2 set with the Vector2 coordinates divided by the passed one coordinates.
  689. */
  690. public divide(otherVector: Vector2): Vector2 {
  691. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  692. }
  693. /**
  694. * Sets the "result" coordinates with the Vector2 divided by the passed one coordinates.
  695. * Returns the Vector2.
  696. */
  697. public divideToRef(otherVector: Vector2, result: Vector2): Vector2 {
  698. result.x = this.x / otherVector.x;
  699. result.y = this.y / otherVector.y;
  700. return this;
  701. }
  702. /**
  703. * Returns a new Vector2 with current Vector2 negated coordinates.
  704. */
  705. public negate(): Vector2 {
  706. return new Vector2(-this.x, -this.y);
  707. }
  708. /**
  709. * Multiply the Vector2 coordinates by scale.
  710. * Returns the updated Vector2.
  711. */
  712. public scaleInPlace(scale: number): Vector2 {
  713. this.x *= scale;
  714. this.y *= scale;
  715. return this;
  716. }
  717. /**
  718. * Returns a new Vector2 scaled by "scale" from the current Vector2.
  719. */
  720. public scale(scale: number): Vector2 {
  721. return new Vector2(this.x * scale, this.y * scale);
  722. }
  723. /**
  724. * Boolean : True if the passed vector coordinates strictly equal the current Vector2 ones.
  725. */
  726. public equals(otherVector: Vector2): boolean {
  727. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  728. }
  729. /**
  730. * Boolean : True if the passed vector coordinates are close to the current ones by a distance of epsilon.
  731. */
  732. public equalsWithEpsilon(otherVector: Vector2, epsilon: number = Epsilon): boolean {
  733. return otherVector && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon) && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon);
  734. }
  735. // Properties
  736. /**
  737. * Returns the vector length (float).
  738. */
  739. public length(): number {
  740. return Math.sqrt(this.x * this.x + this.y * this.y);
  741. }
  742. /**
  743. * Returns the vector squared length (float);
  744. */
  745. public lengthSquared(): number {
  746. return (this.x * this.x + this.y * this.y);
  747. }
  748. // Methods
  749. /**
  750. * Normalize the vector.
  751. * Returns the updated Vector2.
  752. */
  753. public normalize(): Vector2 {
  754. var len = this.length();
  755. if (len === 0)
  756. return this;
  757. var num = 1.0 / len;
  758. this.x *= num;
  759. this.y *= num;
  760. return this;
  761. }
  762. /**
  763. * Returns a new Vector2 copied from the Vector2.
  764. */
  765. public clone(): Vector2 {
  766. return new Vector2(this.x, this.y);
  767. }
  768. // Statics
  769. /**
  770. * Returns a new Vector2(0, 0)
  771. */
  772. public static Zero(): Vector2 {
  773. return new Vector2(0, 0);
  774. }
  775. /**
  776. * Returns a new Vector2 set from the passed index element of the passed array or Float32Array.
  777. */
  778. public static FromArray(array: number[] | Float32Array, offset: number = 0): Vector2 {
  779. return new Vector2(array[offset], array[offset + 1]);
  780. }
  781. /**
  782. * Sets "result" from the passed index element of the passed array or Float32Array.
  783. * Returns the Vector2.
  784. */
  785. public static FromArrayToRef(array: number[] | Float32Array, offset: number, result: Vector2): void {
  786. result.x = array[offset];
  787. result.y = array[offset + 1];
  788. }
  789. /**
  790. * Retuns a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the passed four Vector2.
  791. */
  792. public static CatmullRom(value1: Vector2, value2: Vector2, value3: Vector2, value4: Vector2, amount: number): Vector2 {
  793. var squared = amount * amount;
  794. var cubed = amount * squared;
  795. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  796. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  797. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  798. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  799. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  800. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  801. return new Vector2(x, y);
  802. }
  803. /**
  804. * Returns a new Vector2 set with same the coordinates than "value" ones if the vector "value" is in the square defined by "min" and "max".
  805. * If a coordinate of "value" is lower than "min" coordinates, the returned Vector2 is given this "min" coordinate.
  806. * If a coordinate of "value" is greater than "max" coordinates, the returned Vector2 is given this "max" coordinate.
  807. */
  808. public static Clamp(value: Vector2, min: Vector2, max: Vector2): Vector2 {
  809. var x = value.x;
  810. x = (x > max.x) ? max.x : x;
  811. x = (x < min.x) ? min.x : x;
  812. var y = value.y;
  813. y = (y > max.y) ? max.y : y;
  814. y = (y < min.y) ? min.y : y;
  815. return new Vector2(x, y);
  816. }
  817. /**
  818. * Returns a new Vecto2 located for "amount" (float) on the Hermite spline defined by the vectors "value1", "value3", "tangent1", "tangent2".
  819. */
  820. public static Hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number): Vector2 {
  821. var squared = amount * amount;
  822. var cubed = amount * squared;
  823. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  824. var part2 = (-2.0 * cubed) + (3.0 * squared);
  825. var part3 = (cubed - (2.0 * squared)) + amount;
  826. var part4 = cubed - squared;
  827. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  828. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  829. return new Vector2(x, y);
  830. }
  831. /**
  832. * Returns a new Vector2 located for "amount" (float) on the linear interpolation between the vector "start" adn the vector "end".
  833. */
  834. public static Lerp(start: Vector2, end: Vector2, amount: number): Vector2 {
  835. var x = start.x + ((end.x - start.x) * amount);
  836. var y = start.y + ((end.y - start.y) * amount);
  837. return new Vector2(x, y);
  838. }
  839. /**
  840. * Returns the dot product (float) of the vector "left" and the vector "right".
  841. */
  842. public static Dot(left: Vector2, right: Vector2): number {
  843. return left.x * right.x + left.y * right.y;
  844. }
  845. /**
  846. * Returns a new Vector2 equal to the normalized passed vector.
  847. */
  848. public static Normalize(vector: Vector2): Vector2 {
  849. var newVector = vector.clone();
  850. newVector.normalize();
  851. return newVector;
  852. }
  853. /**
  854. * Returns a new Vecto2 set with the minimal coordinate values from the "left" and "right" vectors.
  855. */
  856. public static Minimize(left: Vector2, right: Vector2): Vector2 {
  857. var x = (left.x < right.x) ? left.x : right.x;
  858. var y = (left.y < right.y) ? left.y : right.y;
  859. return new Vector2(x, y);
  860. }
  861. /**
  862. * Returns a new Vecto2 set with the maximal coordinate values from the "left" and "right" vectors.
  863. */
  864. public static Maximize(left: Vector2, right: Vector2): Vector2 {
  865. var x = (left.x > right.x) ? left.x : right.x;
  866. var y = (left.y > right.y) ? left.y : right.y;
  867. return new Vector2(x, y);
  868. }
  869. /**
  870. * Returns a new Vecto2 set with the transformed coordinates of the passed vector by the passed transformation matrix.
  871. */
  872. public static Transform(vector: Vector2, transformation: Matrix): Vector2 {
  873. let r = Vector2.Zero();
  874. Vector2.TransformToRef(vector, transformation, r);
  875. return r;
  876. }
  877. /**
  878. * Transforms the passed vector coordinates by the passed transformation matrix and stores the result in the vector "result" coordinates.
  879. */
  880. public static TransformToRef(vector: Vector2, transformation: Matrix, result: Vector2) {
  881. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + transformation.m[12];
  882. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + transformation.m[13];
  883. result.x = x;
  884. result.y = y;
  885. }
  886. /**
  887. * Boolean : True if the point "p" is in the triangle defined by the vertors "p0", "p1", "p2"
  888. */
  889. public static PointInTriangle(p: Vector2, p0: Vector2, p1: Vector2, p2: Vector2) {
  890. let a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  891. let sign = a < 0 ? -1 : 1;
  892. let s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  893. let t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
  894. return s > 0 && t > 0 && (s + t) < 2 * a * sign;
  895. }
  896. /**
  897. * Returns the distance (float) between the vectors "value1" and "value2".
  898. */
  899. public static Distance(value1: Vector2, value2: Vector2): number {
  900. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  901. }
  902. /**
  903. * Returns the squared distance (float) between the vectors "value1" and "value2".
  904. */
  905. public static DistanceSquared(value1: Vector2, value2: Vector2): number {
  906. var x = value1.x - value2.x;
  907. var y = value1.y - value2.y;
  908. return (x * x) + (y * y);
  909. }
  910. /**
  911. * Returns a new Vecto2 located at the center of the vectors "value1" and "value2".
  912. */
  913. public static Center(value1: Vector2, value2: Vector2): Vector2 {
  914. var center = value1.add(value2);
  915. center.scaleInPlace(0.5);
  916. return center;
  917. }
  918. /**
  919. * Returns the shortest distance (float) between the point "p" and the segment defined by the two points "segA" and "segB".
  920. */
  921. public static DistanceOfPointFromSegment(p: Vector2, segA: Vector2, segB: Vector2): number {
  922. let l2 = Vector2.DistanceSquared(segA, segB);
  923. if (l2 === 0.0) {
  924. return Vector2.Distance(p, segA);
  925. }
  926. let v = segB.subtract(segA);
  927. let t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
  928. let proj = segA.add(v.multiplyByFloats(t, t));
  929. return Vector2.Distance(p, proj);
  930. }
  931. }
  932. export class Vector3 {
  933. /**
  934. * Creates a new Vector3 object from the passed x, y, z (floats) coordinates.
  935. * A Vector3 is the main object used in 3D geometry.
  936. * It can represent etiher the coordinates of a point the space, either a direction.
  937. */
  938. constructor(public x: number, public y: number, public z: number) {
  939. }
  940. /**
  941. * Returns a string with the Vector3 coordinates.
  942. */
  943. public toString(): string {
  944. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  945. }
  946. /**
  947. * Returns the string "Vector3"
  948. */
  949. public getClassName(): string {
  950. return "Vector3";
  951. }
  952. /**
  953. * Returns the Vector hash code.
  954. */
  955. public getHashCode(): number {
  956. let hash = this.x || 0;
  957. hash = (hash * 397) ^ (this.y || 0);
  958. hash = (hash * 397) ^ (this.z || 0);
  959. return hash;
  960. }
  961. // Operators
  962. /**
  963. * Returns a new array with three elements : the coordinates the Vector3.
  964. */
  965. public asArray(): number[] {
  966. var result: number[] = [];
  967. this.toArray(result, 0);
  968. return result;
  969. }
  970. /**
  971. * Populates the passed array or Float32Array from the passed index with the successive coordinates of the Vector3.
  972. * Returns the Vector3.
  973. */
  974. public toArray(array: number[] | Float32Array, index: number = 0): Vector3 {
  975. array[index] = this.x;
  976. array[index + 1] = this.y;
  977. array[index + 2] = this.z;
  978. return this;
  979. }
  980. /**
  981. * Returns a new Quaternion object, computed from the Vector3 coordinates.
  982. */
  983. public toQuaternion(): Quaternion {
  984. var result = new Quaternion(0.0, 0.0, 0.0, 1.0);
  985. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  986. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  987. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  988. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  989. var cosy = Math.cos(this.y * 0.5);
  990. var siny = Math.sin(this.y * 0.5);
  991. result.x = coszMinusx * siny;
  992. result.y = -sinzMinusx * siny;
  993. result.z = sinxPlusz * cosy;
  994. result.w = cosxPlusz * cosy;
  995. return result;
  996. }
  997. /**
  998. * Adds the passed vector to the current Vector3.
  999. * Returns the updated Vector3.
  1000. */
  1001. public addInPlace(otherVector: Vector3): Vector3 {
  1002. this.x += otherVector.x;
  1003. this.y += otherVector.y;
  1004. this.z += otherVector.z;
  1005. return this;
  1006. }
  1007. /**
  1008. * Returns a new Vector3, result of the addition the current Vector3 and the passed vector.
  1009. */
  1010. public add(otherVector: Vector3): Vector3 {
  1011. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  1012. }
  1013. /**
  1014. * Adds the current Vector3 to the passed one and stores the result in the vector "result".
  1015. * Returns the current Vector3.
  1016. */
  1017. public addToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1018. result.x = this.x + otherVector.x;
  1019. result.y = this.y + otherVector.y;
  1020. result.z = this.z + otherVector.z;
  1021. return this;
  1022. }
  1023. /**
  1024. * Subtract the passed vector from the current Vector3.
  1025. * Returns the updated Vector3.
  1026. */
  1027. public subtractInPlace(otherVector: Vector3): Vector3 {
  1028. this.x -= otherVector.x;
  1029. this.y -= otherVector.y;
  1030. this.z -= otherVector.z;
  1031. return this;
  1032. }
  1033. /**
  1034. * Returns a new Vector3, result of the subtraction of the passed vector from the current Vector3.
  1035. */
  1036. public subtract(otherVector: Vector3): Vector3 {
  1037. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  1038. }
  1039. /**
  1040. * Subtracts the passed vector from the current Vector3 and stores the result in the vector "result".
  1041. * Returns the current Vector3.
  1042. */
  1043. public subtractToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1044. result.x = this.x - otherVector.x;
  1045. result.y = this.y - otherVector.y;
  1046. result.z = this.z - otherVector.z;
  1047. return this;
  1048. }
  1049. /**
  1050. * Returns a new Vector3 set with the subtraction of the passed floats from the current Vector3 coordinates.
  1051. */
  1052. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  1053. return new Vector3(this.x - x, this.y - y, this.z - z);
  1054. }
  1055. /**
  1056. * Subtracts the passed floats from the current Vector3 coordinates and set the passed vector "result" with this result.
  1057. * Returns the current Vector3.
  1058. */
  1059. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
  1060. result.x = this.x - x;
  1061. result.y = this.y - y;
  1062. result.z = this.z - z;
  1063. return this;
  1064. }
  1065. /**
  1066. * Returns a new Vector3 set with the current Vector3 negated coordinates.
  1067. */
  1068. public negate(): Vector3 {
  1069. return new Vector3(-this.x, -this.y, -this.z);
  1070. }
  1071. /**
  1072. * Multiplies the Vector3 coordinates by the float "scale".
  1073. * Returns the updated Vector3.
  1074. */
  1075. public scaleInPlace(scale: number): Vector3 {
  1076. this.x *= scale;
  1077. this.y *= scale;
  1078. this.z *= scale;
  1079. return this;
  1080. }
  1081. /**
  1082. * Returns a new Vector3 set with the current Vector3 coordinates multiplied by the float "scale".
  1083. */
  1084. public scale(scale: number): Vector3 {
  1085. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  1086. }
  1087. /**
  1088. * Multiplies the current Vector3 coordinates by the float "scale" and stores the result in the passed vector "result" coordinates.
  1089. * Returns the current Vector3.
  1090. */
  1091. public scaleToRef(scale: number, result: Vector3): Vector3 {
  1092. result.x = this.x * scale;
  1093. result.y = this.y * scale;
  1094. result.z = this.z * scale;
  1095. return this;
  1096. }
  1097. /**
  1098. * Boolean : True if the current Vector3 and the passed vector coordinates are strictly equal.
  1099. */
  1100. public equals(otherVector: Vector3): boolean {
  1101. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  1102. }
  1103. /**
  1104. * Boolean : True if the current Vector3 and the passed vector coordinates are distant less than epsilon.
  1105. */
  1106. public equalsWithEpsilon(otherVector: Vector3, epsilon: number = Epsilon): boolean {
  1107. return otherVector && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon) && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon) && MathTools.WithinEpsilon(this.z, otherVector.z, epsilon);
  1108. }
  1109. /**
  1110. * Boolean : True if the current Vector3 coordinate equal the passed floats.
  1111. */
  1112. public equalsToFloats(x: number, y: number, z: number): boolean {
  1113. return this.x === x && this.y === y && this.z === z;
  1114. }
  1115. /**
  1116. * Muliplies the current Vector3 coordinates by the passed ones.
  1117. * Returns the updated Vector3.
  1118. */
  1119. public multiplyInPlace(otherVector: Vector3): Vector3 {
  1120. this.x *= otherVector.x;
  1121. this.y *= otherVector.y;
  1122. this.z *= otherVector.z;
  1123. return this;
  1124. }
  1125. /**
  1126. * Returns a new Vector3, result of the multiplication of the current Vector3 by the passed vector.
  1127. */
  1128. public multiply(otherVector: Vector3): Vector3 {
  1129. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  1130. }
  1131. /**
  1132. * Multiplies the current Vector3 by the passed one and stores the result in the passed vector "result".
  1133. * Returns the current Vector3.
  1134. */
  1135. public multiplyToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1136. result.x = this.x * otherVector.x;
  1137. result.y = this.y * otherVector.y;
  1138. result.z = this.z * otherVector.z;
  1139. return this;
  1140. }
  1141. /**
  1142. * Returns a new Vector3 set witth the result of the mulliplication of the current Vector3 coordinates by the passed floats.
  1143. */
  1144. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  1145. return new Vector3(this.x * x, this.y * y, this.z * z);
  1146. }
  1147. /**
  1148. * Returns a new Vector3 set witth the result of the division of the current Vector3 coordinates by the passed ones.
  1149. */
  1150. public divide(otherVector: Vector3): Vector3 {
  1151. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  1152. }
  1153. /**
  1154. * Divides the current Vector3 coordinates by the passed ones and stores the result in the passed vector "result".
  1155. * Returns the current Vector3.
  1156. */
  1157. public divideToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1158. result.x = this.x / otherVector.x;
  1159. result.y = this.y / otherVector.y;
  1160. result.z = this.z / otherVector.z;
  1161. return this;
  1162. }
  1163. /**
  1164. * Updates the current Vector3 with the minimal coordinate values between its and the passed vector ones.
  1165. * Returns the updated Vector3.
  1166. */
  1167. public MinimizeInPlace(other: Vector3): Vector3 {
  1168. if (other.x < this.x) this.x = other.x;
  1169. if (other.y < this.y) this.y = other.y;
  1170. if (other.z < this.z) this.z = other.z;
  1171. return this;
  1172. }
  1173. /**
  1174. * Updates the current Vector3 with the maximal coordinate values between its and the passed vector ones.
  1175. * Returns the updated Vector3.
  1176. */
  1177. public MaximizeInPlace(other: Vector3): Vector3 {
  1178. if (other.x > this.x) this.x = other.x;
  1179. if (other.y > this.y) this.y = other.y;
  1180. if (other.z > this.z) this.z = other.z;
  1181. return this;
  1182. }
  1183. // Properties
  1184. /**
  1185. * Returns the length of the Vector3 (float).
  1186. */
  1187. public length(): number {
  1188. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  1189. }
  1190. /**
  1191. * Returns the squared length of the Vector3 (float).
  1192. */
  1193. public lengthSquared(): number {
  1194. return (this.x * this.x + this.y * this.y + this.z * this.z);
  1195. }
  1196. // Methods
  1197. /**
  1198. * Normalize the current Vector3.
  1199. * Returns the updated Vector3.
  1200. */
  1201. public normalize(): Vector3 {
  1202. var len = this.length();
  1203. if (len === 0 || len === 1.0)
  1204. return this;
  1205. var num = 1.0 / len;
  1206. this.x *= num;
  1207. this.y *= num;
  1208. this.z *= num;
  1209. return this;
  1210. }
  1211. /**
  1212. * Returns a new Vector3 copied from the current Vector3.
  1213. */
  1214. public clone(): Vector3 {
  1215. return new Vector3(this.x, this.y, this.z);
  1216. }
  1217. /**
  1218. * Copies the passed vector coordinates to the current Vector3 ones.
  1219. * Returns the updated Vector3.
  1220. */
  1221. public copyFrom(source: Vector3): Vector3 {
  1222. this.x = source.x;
  1223. this.y = source.y;
  1224. this.z = source.z;
  1225. return this;
  1226. }
  1227. /**
  1228. * Copies the passed floats to the current Vector3 coordinates.
  1229. * Returns the updated Vector3.
  1230. */
  1231. public copyFromFloats(x: number, y: number, z: number): Vector3 {
  1232. this.x = x;
  1233. this.y = y;
  1234. this.z = z;
  1235. return this;
  1236. }
  1237. /**
  1238. * Copies the passed floats to the current Vector3 coordinates.
  1239. * Returns the updated Vector3.
  1240. */
  1241. public set(x: number, y: number, z: number): Vector3 {
  1242. return this.copyFromFloats(x, y, z);
  1243. }
  1244. // Statics
  1245. /**
  1246. *
  1247. */
  1248. public static GetClipFactor(vector0: Vector3, vector1: Vector3, axis: Vector3, size) {
  1249. var d0 = Vector3.Dot(vector0, axis) - size;
  1250. var d1 = Vector3.Dot(vector1, axis) - size;
  1251. var s = d0 / (d0 - d1);
  1252. return s;
  1253. }
  1254. /**
  1255. * Returns a new Vector3 set from the index "offset" of the passed array or Float32Array.
  1256. */
  1257. public static FromArray(array: number[] | Float32Array, offset?: number): Vector3 {
  1258. if (!offset) {
  1259. offset = 0;
  1260. }
  1261. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  1262. }
  1263. /**
  1264. * Returns a new Vector3 set from the index "offset" of the passed Float32Array.
  1265. */
  1266. public static FromFloatArray(array: Float32Array, offset?: number): Vector3 {
  1267. if (!offset) {
  1268. offset = 0;
  1269. }
  1270. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  1271. }
  1272. /**
  1273. * Sets the passed vector "result" with the element values from the index "offset" of the passed array or Float32Array.
  1274. */
  1275. public static FromArrayToRef(array: number[] | Float32Array, offset: number, result: Vector3): void {
  1276. result.x = array[offset];
  1277. result.y = array[offset + 1];
  1278. result.z = array[offset + 2];
  1279. }
  1280. /**
  1281. * Sets the passed vector "result" with the element values from the index "offset" of the passed Float32Array.
  1282. */
  1283. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector3): void {
  1284. result.x = array[offset];
  1285. result.y = array[offset + 1];
  1286. result.z = array[offset + 2];
  1287. }
  1288. /**
  1289. * Sets the passed vector "result" with the passed floats.
  1290. */
  1291. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  1292. result.x = x;
  1293. result.y = y;
  1294. result.z = z;
  1295. }
  1296. /**
  1297. * Returns a new Vector3 set to (0.0, 0.0, 0.0).
  1298. */
  1299. public static Zero(): Vector3 {
  1300. return new Vector3(0.0, 0.0, 0.0);
  1301. }
  1302. /**
  1303. * Returns a new Vector3 set to (0.0, 1.0, 0.0)
  1304. */
  1305. public static Up(): Vector3 {
  1306. return new Vector3(0.0, 1.0, 0.0);
  1307. }
  1308. /**
  1309. * Returns a new Vector3 set to (0.0, 0.0, 1.0)
  1310. */
  1311. public static Forward(): Vector3 {
  1312. return new Vector3(0.0, 0.0, 1.0);
  1313. }
  1314. /**
  1315. * Returns a new Vector3 set to (1.0, 0.0, 0.0)
  1316. */
  1317. public static Right(): Vector3 {
  1318. return new Vector3(1.0, 0.0, 0.0);
  1319. }
  1320. /**
  1321. * Returns a new Vector3 set to (-1.0, 0.0, 0.0)
  1322. */
  1323. public static Left(): Vector3 {
  1324. return new Vector3(-1.0, 0.0, 0.0);
  1325. }
  1326. /**
  1327. * Returns a new Vector3 set with the result of the transformation by the passed matrix of the passed vector.
  1328. * This method computes tranformed coordinates only, not transformed direction vectors.
  1329. */
  1330. public static TransformCoordinates(vector: Vector3, transformation: Matrix): Vector3 {
  1331. var result = Vector3.Zero();
  1332. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  1333. return result;
  1334. }
  1335. /**
  1336. * Sets the passed vector "result" coordinates with the result of the transformation by the passed matrix of the passed vector.
  1337. * This method computes tranformed coordinates only, not transformed direction vectors.
  1338. */
  1339. public static TransformCoordinatesToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  1340. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  1341. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  1342. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  1343. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  1344. result.x = x / w;
  1345. result.y = y / w;
  1346. result.z = z / w;
  1347. }
  1348. /**
  1349. * Sets the passed vector "result" coordinates with the result of the transformation by the passed matrix of the passed floats (x, y, z).
  1350. * This method computes tranformed coordinates only, not transformed direction vectors.
  1351. */
  1352. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  1353. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  1354. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  1355. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  1356. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  1357. result.x = rx / rw;
  1358. result.y = ry / rw;
  1359. result.z = rz / rw;
  1360. }
  1361. /**
  1362. * Returns a new Vector3 set with the result of the normal transformation by the passed matrix of the passed vector.
  1363. * This methods computes transformed normalized direction vectors only.
  1364. */
  1365. public static TransformNormal(vector: Vector3, transformation: Matrix): Vector3 {
  1366. var result = Vector3.Zero();
  1367. Vector3.TransformNormalToRef(vector, transformation, result);
  1368. return result;
  1369. }
  1370. /**
  1371. * Sets the passed vector "result" with the result of the normal transformation by the passed matrix of the passed vector.
  1372. * This methods computes transformed normalized direction vectors only.
  1373. */
  1374. public static TransformNormalToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  1375. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  1376. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  1377. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  1378. result.x = x;
  1379. result.y = y;
  1380. result.z = z;
  1381. }
  1382. /**
  1383. * Sets the passed vector "result" with the result of the normal transformation by the passed matrix of the passed floats (x, y, z).
  1384. * This methods computes transformed normalized direction vectors only.
  1385. */
  1386. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  1387. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  1388. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  1389. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  1390. }
  1391. /**
  1392. * Returns a new Vector3 located for "amount" on the CatmullRom interpolation spline defined by the vectors "value1", "value2", "value3", "value4".
  1393. */
  1394. public static CatmullRom(value1: Vector3, value2: Vector3, value3: Vector3, value4: Vector3, amount: number): Vector3 {
  1395. var squared = amount * amount;
  1396. var cubed = amount * squared;
  1397. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  1398. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  1399. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  1400. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  1401. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  1402. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  1403. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  1404. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  1405. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  1406. return new Vector3(x, y, z);
  1407. }
  1408. /**
  1409. * Returns a new Vector3 set with the coordinates of "value", if the vector "value" is in the cube defined by the vectors "min" and "max".
  1410. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one.
  1411. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one.
  1412. */
  1413. public static Clamp(value: Vector3, min: Vector3, max: Vector3): Vector3 {
  1414. var x = value.x;
  1415. x = (x > max.x) ? max.x : x;
  1416. x = (x < min.x) ? min.x : x;
  1417. var y = value.y;
  1418. y = (y > max.y) ? max.y : y;
  1419. y = (y < min.y) ? min.y : y;
  1420. var z = value.z;
  1421. z = (z > max.z) ? max.z : z;
  1422. z = (z < min.z) ? min.z : z;
  1423. return new Vector3(x, y, z);
  1424. }
  1425. /**
  1426. * Returns a new Vector3 located for "amount" (float) on the Hermite interpolation spline defined by the vectors "value1", "tangent1", "value2", "tangent2".
  1427. */
  1428. public static Hermite(value1: Vector3, tangent1: Vector3, value2: Vector3, tangent2: Vector3, amount: number): Vector3 {
  1429. var squared = amount * amount;
  1430. var cubed = amount * squared;
  1431. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  1432. var part2 = (-2.0 * cubed) + (3.0 * squared);
  1433. var part3 = (cubed - (2.0 * squared)) + amount;
  1434. var part4 = cubed - squared;
  1435. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  1436. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  1437. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  1438. return new Vector3(x, y, z);
  1439. }
  1440. /**
  1441. * Returns a new Vector3 located for "amount" (float) on the linear interpolation between the vectors "start" and "end".
  1442. */
  1443. public static Lerp(start: Vector3, end: Vector3, amount: number): Vector3 {
  1444. var result = new Vector3(0, 0, 0);
  1445. Vector3.LerpToRef(start, end, amount, result);
  1446. return result;
  1447. }
  1448. /**
  1449. * Sets the passed vector "result" with the result of the linear interpolation from the vector "start" for "amount" to the vector "end".
  1450. */
  1451. public static LerpToRef(start: Vector3, end: Vector3, amount: number, result: Vector3): void {
  1452. result.x = start.x + ((end.x - start.x) * amount);
  1453. result.y = start.y + ((end.y - start.y) * amount);
  1454. result.z = start.z + ((end.z - start.z) * amount);
  1455. }
  1456. /**
  1457. * Returns the dot product (float) between the vectors "left" and "right".
  1458. */
  1459. public static Dot(left: Vector3, right: Vector3): number {
  1460. return (left.x * right.x + left.y * right.y + left.z * right.z);
  1461. }
  1462. /**
  1463. * Returns a new Vector3 as the cross product of the vectors "left" and "right".
  1464. * The cross product is then orthogonal to both "left" and "right".
  1465. */
  1466. public static Cross(left: Vector3, right: Vector3): Vector3 {
  1467. var result = Vector3.Zero();
  1468. Vector3.CrossToRef(left, right, result);
  1469. return result;
  1470. }
  1471. /**
  1472. * Sets the passed vector "result" with the cross product of "left" and "right".
  1473. * The cross product is then orthogonal to both "left" and "right".
  1474. */
  1475. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  1476. MathTmp.Vector3[0].x = left.y * right.z - left.z * right.y;
  1477. MathTmp.Vector3[0].y = left.z * right.x - left.x * right.z;
  1478. MathTmp.Vector3[0].z = left.x * right.y - left.y * right.x;
  1479. result.copyFrom(MathTmp.Vector3[0]);
  1480. }
  1481. /**
  1482. * Returns a new Vector3 as the normalization of the passed vector.
  1483. */
  1484. public static Normalize(vector: Vector3): Vector3 {
  1485. var result = Vector3.Zero();
  1486. Vector3.NormalizeToRef(vector, result);
  1487. return result;
  1488. }
  1489. /**
  1490. * Sets the passed vector "result" with the normalization of the passed first vector.
  1491. */
  1492. public static NormalizeToRef(vector: Vector3, result: Vector3): void {
  1493. result.copyFrom(vector);
  1494. result.normalize();
  1495. }
  1496. private static _viewportMatrixCache: Matrix;
  1497. private static _matrixCache: Matrix;
  1498. public static Project(vector: Vector3, world: Matrix, transform: Matrix, viewport: Viewport): Vector3 {
  1499. var cw = viewport.width;
  1500. var ch = viewport.height;
  1501. var cx = viewport.x;
  1502. var cy = viewport.y;
  1503. var viewportMatrix = Vector3._viewportMatrixCache ? Vector3._viewportMatrixCache : (Vector3._viewportMatrixCache = new Matrix());
  1504. Matrix.FromValuesToRef(
  1505. cw / 2.0, 0, 0, 0,
  1506. 0, -ch / 2.0, 0, 0,
  1507. 0, 0, 0.5, 0,
  1508. cx + cw / 2.0, ch / 2.0 + cy, 0.5, 1, viewportMatrix);
  1509. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  1510. world.multiplyToRef(transform, matrix);
  1511. matrix.multiplyToRef(viewportMatrix, matrix);
  1512. return Vector3.TransformCoordinates(vector, matrix);
  1513. }
  1514. public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, transform: Matrix): Vector3 {
  1515. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  1516. world.multiplyToRef(transform, matrix);
  1517. matrix.invert();
  1518. source.x = source.x / viewportWidth * 2 - 1;
  1519. source.y = -(source.y / viewportHeight * 2 - 1);
  1520. var vector = Vector3.TransformCoordinates(source, matrix);
  1521. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  1522. if (MathTools.WithinEpsilon(num, 1.0)) {
  1523. vector = vector.scale(1.0 / num);
  1524. }
  1525. return vector;
  1526. }
  1527. public static Unproject(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Vector3 {
  1528. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  1529. world.multiplyToRef(view, matrix)
  1530. matrix.multiplyToRef(projection, matrix);
  1531. matrix.invert();
  1532. var screenSource = new Vector3(source.x / viewportWidth * 2 - 1, -(source.y / viewportHeight * 2 - 1), 2 * source.z - 1.0);
  1533. var vector = Vector3.TransformCoordinates(screenSource, matrix);
  1534. var num = screenSource.x * matrix.m[3] + screenSource.y * matrix.m[7] + screenSource.z * matrix.m[11] + matrix.m[15];
  1535. if (MathTools.WithinEpsilon(num, 1.0)) {
  1536. vector = vector.scale(1.0 / num);
  1537. }
  1538. return vector;
  1539. }
  1540. public static Minimize(left: Vector3, right: Vector3): Vector3 {
  1541. var min = left.clone();
  1542. min.MinimizeInPlace(right);
  1543. return min;
  1544. }
  1545. public static Maximize(left: Vector3, right: Vector3): Vector3 {
  1546. var max = left.clone();
  1547. max.MaximizeInPlace(right);
  1548. return max;
  1549. }
  1550. /**
  1551. * Returns the distance (float) between the vectors "value1" and "value2".
  1552. */
  1553. public static Distance(value1: Vector3, value2: Vector3): number {
  1554. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  1555. }
  1556. /**
  1557. * Returns the squared distance (float) between the vectors "value1" and "value2".
  1558. */
  1559. public static DistanceSquared(value1: Vector3, value2: Vector3): number {
  1560. var x = value1.x - value2.x;
  1561. var y = value1.y - value2.y;
  1562. var z = value1.z - value2.z;
  1563. return (x * x) + (y * y) + (z * z);
  1564. }
  1565. /**
  1566. * Returns a new Vector3 located at the center between "value1" and "value2".
  1567. */
  1568. public static Center(value1: Vector3, value2: Vector3): Vector3 {
  1569. var center = value1.add(value2);
  1570. center.scaleInPlace(0.5);
  1571. return center;
  1572. }
  1573. /**
  1574. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  1575. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  1576. * to something in order to rotate it from its local system to the given target system.
  1577. * Note : axis1, axis2 and axis3 are normalized during this operation.
  1578. * Returns a new Vector3.
  1579. */
  1580. public static RotationFromAxis(axis1: Vector3, axis2: Vector3, axis3: Vector3): Vector3 {
  1581. var rotation = Vector3.Zero();
  1582. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  1583. return rotation;
  1584. }
  1585. /**
  1586. * The same than RotationFromAxis but updates the passed ref Vector3 parameter instead of returning a new Vector3.
  1587. */
  1588. public static RotationFromAxisToRef(axis1: Vector3, axis2: Vector3, axis3: Vector3, ref: Vector3): void {
  1589. var quat = MathTmp.Quaternion[0];
  1590. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  1591. quat.toEulerAnglesToRef(ref);
  1592. }
  1593. }
  1594. //Vector4 class created for EulerAngle class conversion to Quaternion
  1595. export class Vector4 {
  1596. /**
  1597. * Creates a Vector4 object from the passed floats.
  1598. */
  1599. constructor(public x: number, public y: number, public z: number, public w: number) { }
  1600. /**
  1601. * Returns the string with the Vector4 coordinates.
  1602. */
  1603. public toString(): string {
  1604. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1605. }
  1606. /**
  1607. * Returns the string "Vector4".
  1608. */
  1609. public getClassName(): string {
  1610. return "Vector4";
  1611. }
  1612. /**
  1613. * Returns the Vector4 hash code.
  1614. */
  1615. public getHashCode(): number {
  1616. let hash = this.x || 0;
  1617. hash = (hash * 397) ^ (this.y || 0);
  1618. hash = (hash * 397) ^ (this.z || 0);
  1619. hash = (hash * 397) ^ (this.w || 0);
  1620. return hash;
  1621. }
  1622. // Operators
  1623. /**
  1624. * Returns a new array populated with 4 elements : the Vector4 coordinates.
  1625. */
  1626. public asArray(): number[] {
  1627. var result = [];
  1628. this.toArray(result, 0);
  1629. return result;
  1630. }
  1631. /**
  1632. * Populates the passed array from the passed index with the Vector4 coordinates.
  1633. * Returns the Vector4.
  1634. */
  1635. public toArray(array: number[], index?: number): Vector4 {
  1636. if (index === undefined) {
  1637. index = 0;
  1638. }
  1639. array[index] = this.x;
  1640. array[index + 1] = this.y;
  1641. array[index + 2] = this.z;
  1642. array[index + 3] = this.w;
  1643. return this;
  1644. }
  1645. /**
  1646. * Adds the passed vector to the current Vector4.
  1647. * Returns the updated Vector4.
  1648. */
  1649. public addInPlace(otherVector: Vector4): Vector4 {
  1650. this.x += otherVector.x;
  1651. this.y += otherVector.y;
  1652. this.z += otherVector.z;
  1653. this.w += otherVector.w;
  1654. return this;
  1655. }
  1656. /**
  1657. * Returns a new Vector4 as the result of the addition of the current Vector4 and the passed one.
  1658. */
  1659. public add(otherVector: Vector4): Vector4 {
  1660. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  1661. }
  1662. /**
  1663. * Updates the passed vector "result" with the result of the addition of the current Vector4 and the passed one.
  1664. * Returns the current Vector4.
  1665. */
  1666. public addToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1667. result.x = this.x + otherVector.x;
  1668. result.y = this.y + otherVector.y;
  1669. result.z = this.z + otherVector.z;
  1670. result.w = this.w + otherVector.w;
  1671. return this;
  1672. }
  1673. /**
  1674. * Subtract in place the passed vector from the current Vector4.
  1675. * Returns the updated Vector4.
  1676. */
  1677. public subtractInPlace(otherVector: Vector4): Vector4 {
  1678. this.x -= otherVector.x;
  1679. this.y -= otherVector.y;
  1680. this.z -= otherVector.z;
  1681. this.w -= otherVector.w;
  1682. return this;
  1683. }
  1684. /**
  1685. * Returns a new Vector4 with the result of the subtraction of the passed vector from the current Vector4.
  1686. */
  1687. public subtract(otherVector: Vector4): Vector4 {
  1688. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  1689. }
  1690. /**
  1691. * Sets the passed vector "result" with the result of the subtraction of the passed vector from the current Vector4.
  1692. * Returns the current Vector4.
  1693. */
  1694. public subtractToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1695. result.x = this.x - otherVector.x;
  1696. result.y = this.y - otherVector.y;
  1697. result.z = this.z - otherVector.z;
  1698. result.w = this.w - otherVector.w;
  1699. return this;
  1700. }
  1701. /**
  1702. * Returns a new Vector4 set with the result of the subtraction of the passed floats from the current Vector4 coordinates.
  1703. */
  1704. public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  1705. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  1706. }
  1707. /**
  1708. * Sets the passed vector "result" set with the result of the subtraction of the passed floats from the current Vector4 coordinates.
  1709. * Returns the current Vector4.
  1710. */
  1711. public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
  1712. result.x = this.x - x;
  1713. result.y = this.y - y;
  1714. result.z = this.z - z;
  1715. result.w = this.w - w;
  1716. return this;
  1717. }
  1718. /**
  1719. * Returns a new Vector4 set with the current Vector4 negated coordinates.
  1720. */
  1721. public negate(): Vector4 {
  1722. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  1723. }
  1724. /**
  1725. * Multiplies the current Vector4 coordinates by scale (float).
  1726. * Returns the updated Vector4.
  1727. */
  1728. public scaleInPlace(scale: number): Vector4 {
  1729. this.x *= scale;
  1730. this.y *= scale;
  1731. this.z *= scale;
  1732. this.w *= scale;
  1733. return this;
  1734. }
  1735. /**
  1736. * Returns a new Vector4 set with the current Vector4 coordinates multiplied by scale (float).
  1737. */
  1738. public scale(scale: number): Vector4 {
  1739. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  1740. }
  1741. /**
  1742. * Sets the passed vector "result" with the current Vector4 coordinates multiplied by scale (float).
  1743. * Returns the current Vector4.
  1744. */
  1745. public scaleToRef(scale: number, result: Vector4): Vector4 {
  1746. result.x = this.x * scale;
  1747. result.y = this.y * scale;
  1748. result.z = this.z * scale;
  1749. result.w = this.w * scale;
  1750. return this;
  1751. }
  1752. /**
  1753. * Boolean : True if the current Vector4 coordinates are stricly equal to the passed ones.
  1754. */
  1755. public equals(otherVector: Vector4): boolean {
  1756. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  1757. }
  1758. /**
  1759. * Boolean : True if the current Vector4 coordinates are each beneath the distance "epsilon" from the passed vector ones.
  1760. */
  1761. public equalsWithEpsilon(otherVector: Vector4, epsilon: number = Epsilon): boolean {
  1762. return otherVector
  1763. && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon)
  1764. && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon)
  1765. && MathTools.WithinEpsilon(this.z, otherVector.z, epsilon)
  1766. && MathTools.WithinEpsilon(this.w, otherVector.w, epsilon);
  1767. }
  1768. /**
  1769. * Boolean : True if the passed floats are strictly equal to the current Vector4 coordinates.
  1770. */
  1771. public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
  1772. return this.x === x && this.y === y && this.z === z && this.w === w;
  1773. }
  1774. /**
  1775. * Multiplies in place the current Vector4 by the passed one.
  1776. * Returns the updated Vector4.
  1777. */
  1778. public multiplyInPlace(otherVector: Vector4): Vector4 {
  1779. this.x *= otherVector.x;
  1780. this.y *= otherVector.y;
  1781. this.z *= otherVector.z;
  1782. this.w *= otherVector.w;
  1783. return this;
  1784. }
  1785. /**
  1786. * Returns a new Vector4 set with the multiplication result of the current Vector4 and the passed one.
  1787. */
  1788. public multiply(otherVector: Vector4): Vector4 {
  1789. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  1790. }
  1791. /**
  1792. * Updates the passed vector "result" with the multiplication result of the current Vector4 and the passed one.
  1793. * Returns the current Vector4.
  1794. */
  1795. public multiplyToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1796. result.x = this.x * otherVector.x;
  1797. result.y = this.y * otherVector.y;
  1798. result.z = this.z * otherVector.z;
  1799. result.w = this.w * otherVector.w;
  1800. return this;
  1801. }
  1802. /**
  1803. * Returns a new Vector4 set with the multiplication result of the passed floats and the current Vector4 coordinates.
  1804. */
  1805. public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
  1806. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  1807. }
  1808. /**
  1809. * Returns a new Vector4 set with the division result of the current Vector4 by the passed one.
  1810. */
  1811. public divide(otherVector: Vector4): Vector4 {
  1812. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  1813. }
  1814. /**
  1815. * Updates the passed vector "result" with the division result of the current Vector4 by the passed one.
  1816. * Returns the current Vector4.
  1817. */
  1818. public divideToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1819. result.x = this.x / otherVector.x;
  1820. result.y = this.y / otherVector.y;
  1821. result.z = this.z / otherVector.z;
  1822. result.w = this.w / otherVector.w;
  1823. return this;
  1824. }
  1825. /**
  1826. * Updates the Vector4 coordinates with the minimum values between its own and the passed vector ones.
  1827. */
  1828. public MinimizeInPlace(other: Vector4): Vector4 {
  1829. if (other.x < this.x) this.x = other.x;
  1830. if (other.y < this.y) this.y = other.y;
  1831. if (other.z < this.z) this.z = other.z;
  1832. if (other.w < this.w) this.w = other.w;
  1833. return this;
  1834. }
  1835. /**
  1836. * Updates the Vector4 coordinates with the maximum values between its own and the passed vector ones.
  1837. */
  1838. public MaximizeInPlace(other: Vector4): Vector4 {
  1839. if (other.x > this.x) this.x = other.x;
  1840. if (other.y > this.y) this.y = other.y;
  1841. if (other.z > this.z) this.z = other.z;
  1842. if (other.w > this.w) this.w = other.w;
  1843. return this;
  1844. }
  1845. // Properties
  1846. /**
  1847. * Returns the Vector4 length (float).
  1848. */
  1849. public length(): number {
  1850. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1851. }
  1852. /**
  1853. * Returns the Vector4 squared length (float).
  1854. */
  1855. public lengthSquared(): number {
  1856. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1857. }
  1858. // Methods
  1859. /**
  1860. * Normalizes in place the Vector4.
  1861. * Returns the updated Vector4.
  1862. */
  1863. public normalize(): Vector4 {
  1864. var len = this.length();
  1865. if (len === 0)
  1866. return this;
  1867. var num = 1.0 / len;
  1868. this.x *= num;
  1869. this.y *= num;
  1870. this.z *= num;
  1871. this.w *= num;
  1872. return this;
  1873. }
  1874. /**
  1875. * Returns a new Vector3 from the Vector4 (x, y, z) coordinates.
  1876. */
  1877. public toVector3(): Vector3 {
  1878. return new Vector3(this.x, this.y, this.z);
  1879. }
  1880. /**
  1881. * Returns a new Vector4 copied from the current one.
  1882. */
  1883. public clone(): Vector4 {
  1884. return new Vector4(this.x, this.y, this.z, this.w);
  1885. }
  1886. /**
  1887. * Updates the current Vector4 with the passed one coordinates.
  1888. * Returns the updated Vector4.
  1889. */
  1890. public copyFrom(source: Vector4): Vector4 {
  1891. this.x = source.x;
  1892. this.y = source.y;
  1893. this.z = source.z;
  1894. this.w = source.w;
  1895. return this;
  1896. }
  1897. /**
  1898. * Updates the current Vector4 coordinates with the passed floats.
  1899. * Returns the updated Vector4.
  1900. */
  1901. public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  1902. this.x = x;
  1903. this.y = y;
  1904. this.z = z;
  1905. this.w = w;
  1906. return this;
  1907. }
  1908. /**
  1909. * Updates the current Vector4 coordinates with the passed floats.
  1910. * Returns the updated Vector4.
  1911. */
  1912. public set(x: number, y: number, z: number, w: number): Vector4 {
  1913. return this.copyFromFloats(x, y, z, w);
  1914. }
  1915. // Statics
  1916. /**
  1917. * Returns a new Vector4 set from the starting index of the passed array.
  1918. */
  1919. public static FromArray(array: number[] | Float32Array, offset?: number): Vector4 {
  1920. if (!offset) {
  1921. offset = 0;
  1922. }
  1923. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1924. }
  1925. /**
  1926. * Updates the passed vector "result" from the starting index of the passed array.
  1927. */
  1928. public static FromArrayToRef(array: number[] | Float32Array, offset: number, result: Vector4): void {
  1929. result.x = array[offset];
  1930. result.y = array[offset + 1];
  1931. result.z = array[offset + 2];
  1932. result.w = array[offset + 3];
  1933. }
  1934. /**
  1935. * Updates the passed vector "result" from the starting index of the passed Float32Array.
  1936. */
  1937. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector4): void {
  1938. result.x = array[offset];
  1939. result.y = array[offset + 1];
  1940. result.z = array[offset + 2];
  1941. result.w = array[offset + 3];
  1942. }
  1943. /**
  1944. * Updates the passed vector "result" coordinates from the passed floats.
  1945. */
  1946. public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
  1947. result.x = x;
  1948. result.y = y;
  1949. result.z = z;
  1950. result.w = w;
  1951. }
  1952. /**
  1953. * Returns a new Vector4 set to (0.0, 0.0, 0.0, 0.0)
  1954. */
  1955. public static Zero(): Vector4 {
  1956. return new Vector4(0.0, 0.0, 0.0, 0.0);
  1957. }
  1958. /**
  1959. * Returns a new normalized Vector4 from the passed one.
  1960. */
  1961. public static Normalize(vector: Vector4): Vector4 {
  1962. var result = Vector4.Zero();
  1963. Vector4.NormalizeToRef(vector, result);
  1964. return result;
  1965. }
  1966. /**
  1967. * Updates the passed vector "result" from the normalization of the passed one.
  1968. */
  1969. public static NormalizeToRef(vector: Vector4, result: Vector4): void {
  1970. result.copyFrom(vector);
  1971. result.normalize();
  1972. }
  1973. public static Minimize(left: Vector4, right: Vector4): Vector4 {
  1974. var min = left.clone();
  1975. min.MinimizeInPlace(right);
  1976. return min;
  1977. }
  1978. public static Maximize(left: Vector4, right: Vector4): Vector4 {
  1979. var max = left.clone();
  1980. max.MaximizeInPlace(right);
  1981. return max;
  1982. }
  1983. /**
  1984. * Returns the distance (float) between the vectors "value1" and "value2".
  1985. */
  1986. public static Distance(value1: Vector4, value2: Vector4): number {
  1987. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1988. }
  1989. /**
  1990. * Returns the squared distance (float) between the vectors "value1" and "value2".
  1991. */
  1992. public static DistanceSquared(value1: Vector4, value2: Vector4): number {
  1993. var x = value1.x - value2.x;
  1994. var y = value1.y - value2.y;
  1995. var z = value1.z - value2.z;
  1996. var w = value1.w - value2.w;
  1997. return (x * x) + (y * y) + (z * z) + (w * w);
  1998. }
  1999. /**
  2000. * Returns a new Vector4 located at the center between the vectors "value1" and "value2".
  2001. */
  2002. public static Center(value1: Vector4, value2: Vector4): Vector4 {
  2003. var center = value1.add(value2);
  2004. center.scaleInPlace(0.5);
  2005. return center;
  2006. }
  2007. /**
  2008. * Returns a new Vector4 set with the result of the normal transformation by the passed matrix of the passed vector.
  2009. * This methods computes transformed normalized direction vectors only.
  2010. */
  2011. public static TransformNormal(vector: Vector4, transformation: Matrix): Vector4 {
  2012. var result = Vector4.Zero();
  2013. Vector4.TransformNormalToRef(vector, transformation, result);
  2014. return result;
  2015. }
  2016. /**
  2017. * Sets the passed vector "result" with the result of the normal transformation by the passed matrix of the passed vector.
  2018. * This methods computes transformed normalized direction vectors only.
  2019. */
  2020. public static TransformNormalToRef(vector: Vector4, transformation: Matrix, result: Vector4): void {
  2021. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  2022. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  2023. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  2024. result.x = x;
  2025. result.y = y;
  2026. result.z = z;
  2027. result.w = vector.w;
  2028. }
  2029. /**
  2030. * Sets the passed vector "result" with the result of the normal transformation by the passed matrix of the passed floats (x, y, z, w).
  2031. * This methods computes transformed normalized direction vectors only.
  2032. */
  2033. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, w: number, transformation: Matrix, result: Vector4): void {
  2034. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  2035. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  2036. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  2037. result.w = w;
  2038. }
  2039. }
  2040. export interface ISize {
  2041. width: number;
  2042. height: number;
  2043. }
  2044. export class Size implements ISize {
  2045. width: number;
  2046. height: number;
  2047. /**
  2048. * Creates a Size object from the passed width and height (floats).
  2049. */
  2050. public constructor(width: number, height: number) {
  2051. this.width = width;
  2052. this.height = height;
  2053. }
  2054. // Returns a string with the Size width and height.
  2055. public toString(): string {
  2056. return `{W: ${this.width}, H: ${this.height}}`;
  2057. }
  2058. /**
  2059. * Returns the string "Size"
  2060. */
  2061. public getClassName(): string {
  2062. return "Size";
  2063. }
  2064. /**
  2065. * Returns the Size hash code.
  2066. */
  2067. public getHashCode(): number {
  2068. let hash = this.width || 0;
  2069. hash = (hash * 397) ^ (this.height || 0);
  2070. return hash;
  2071. }
  2072. /**
  2073. * Updates the current size from the passed one.
  2074. * Returns the updated Size.
  2075. */
  2076. public copyFrom(src: Size) {
  2077. this.width = src.width;
  2078. this.height = src.height;
  2079. }
  2080. /**
  2081. * Updates in place the current Size from the passed floats.
  2082. * Returns the updated Size.
  2083. */
  2084. public copyFromFloats(width: number, height: number): Size {
  2085. this.width = width;
  2086. this.height = height;
  2087. return this;
  2088. }
  2089. /**
  2090. * Updates in place the current Size from the passed floats.
  2091. * Returns the updated Size.
  2092. */
  2093. public set(width: number, height: number): Size {
  2094. return this.copyFromFloats(width, height);
  2095. }
  2096. /**
  2097. * Returns a new Size set with the multiplication result of the current Size and the passed floats.
  2098. */
  2099. public multiplyByFloats(w: number, h: number): Size {
  2100. return new Size(this.width * w, this.height * h);
  2101. }
  2102. /**
  2103. * Returns a new Size copied from the passed one.
  2104. */
  2105. public clone(): Size {
  2106. return new Size(this.width, this.height);
  2107. }
  2108. /**
  2109. * Boolean : True if the current Size and the passed one width and height are strictly equal.
  2110. */
  2111. public equals(other: Size): boolean {
  2112. if (!other) {
  2113. return false;
  2114. }
  2115. return (this.width === other.width) && (this.height === other.height);
  2116. }
  2117. /**
  2118. * Returns the surface of the Size : width * height (float).
  2119. */
  2120. public get surface(): number {
  2121. return this.width * this.height;
  2122. }
  2123. /**
  2124. * Returns a new Size set to (0.0, 0.0)
  2125. */
  2126. public static Zero(): Size {
  2127. return new Size(0.0, 0.0);
  2128. }
  2129. /**
  2130. * Returns a new Size set as the addition result of the current Size and the passed one.
  2131. */
  2132. public add(otherSize: Size): Size {
  2133. let r = new Size(this.width + otherSize.width, this.height + otherSize.height);
  2134. return r;
  2135. }
  2136. /**
  2137. * Returns a new Size set as the subtraction result of the passed one from the current Size.
  2138. */
  2139. public subtract(otherSize: Size): Size {
  2140. let r = new Size(this.width - otherSize.width, this.height - otherSize.height);
  2141. return r;
  2142. }
  2143. /**
  2144. * Returns a new Size set at the linear interpolation "amount" between "start" and "end".
  2145. */
  2146. public static Lerp(start: Size, end: Size, amount: number): Size {
  2147. var w = start.width + ((end.width - start.width) * amount);
  2148. var h = start.height + ((end.height - start.height) * amount);
  2149. return new Size(w, h);
  2150. }
  2151. }
  2152. export class Quaternion {
  2153. /**
  2154. * Creates a new Quaternion from the passed floats.
  2155. */
  2156. constructor(public x: number = 0.0, public y: number = 0.0, public z: number = 0.0, public w: number = 1.0) {
  2157. }
  2158. /**
  2159. * Returns a string with the Quaternion coordinates.
  2160. */
  2161. public toString(): string {
  2162. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  2163. }
  2164. /**
  2165. * Returns the string "Quaternion".
  2166. */
  2167. public getClassName(): string {
  2168. return "Quaternion";
  2169. }
  2170. /**
  2171. * Returns the Quaternion hash code.
  2172. */
  2173. public getHashCode(): number {
  2174. let hash = this.x || 0;
  2175. hash = (hash * 397) ^ (this.y || 0);
  2176. hash = (hash * 397) ^ (this.z || 0);
  2177. hash = (hash * 397) ^ (this.w || 0);
  2178. return hash;
  2179. }
  2180. /**
  2181. * Returns a new array populated with 4 elements : the Quaternion coordinates.
  2182. */
  2183. public asArray(): number[] {
  2184. return [this.x, this.y, this.z, this.w];
  2185. }
  2186. /**
  2187. * Boolean : True if the current Quaterion and the passed one coordinates are strictly equal.
  2188. */
  2189. public equals(otherQuaternion: Quaternion): boolean {
  2190. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  2191. }
  2192. /**
  2193. * Returns a new Quaternion copied from the current one.
  2194. */
  2195. public clone(): Quaternion {
  2196. return new Quaternion(this.x, this.y, this.z, this.w);
  2197. }
  2198. /**
  2199. * Updates the current Quaternion from the passed one coordinates.
  2200. * Returns the updated Quaterion.
  2201. */
  2202. public copyFrom(other: Quaternion): Quaternion {
  2203. this.x = other.x;
  2204. this.y = other.y;
  2205. this.z = other.z;
  2206. this.w = other.w;
  2207. return this;
  2208. }
  2209. /**
  2210. * Updates the current Quaternion from the passed float coordinates.
  2211. * Returns the updated Quaterion.
  2212. */
  2213. public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
  2214. this.x = x;
  2215. this.y = y;
  2216. this.z = z;
  2217. this.w = w;
  2218. return this;
  2219. }
  2220. /**
  2221. * Updates the current Quaternion from the passed float coordinates.
  2222. * Returns the updated Quaterion.
  2223. */
  2224. public set(x: number, y: number, z: number, w: number): Quaternion {
  2225. return this.copyFromFloats(x, y, z, w);
  2226. }
  2227. /**
  2228. * Returns a new Quaternion as the addition result of the passed one and the current Quaternion.
  2229. */
  2230. public add(other: Quaternion): Quaternion {
  2231. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  2232. }
  2233. /**
  2234. * Returns a new Quaternion as the subtraction result of the passed one from the current Quaternion.
  2235. */
  2236. public subtract(other: Quaternion): Quaternion {
  2237. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  2238. }
  2239. /**
  2240. * Returns a new Quaternion set by multiplying the current Quaterion coordinates by the float "scale".
  2241. */
  2242. public scale(value: number): Quaternion {
  2243. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  2244. }
  2245. /**
  2246. * Returns a new Quaternion set as the quaternion mulplication result of the current one with the passed one "q1".
  2247. */
  2248. public multiply(q1: Quaternion): Quaternion {
  2249. var result = new Quaternion(0, 0, 0, 1.0);
  2250. this.multiplyToRef(q1, result);
  2251. return result;
  2252. }
  2253. /**
  2254. * Sets the passed "result" as the quaternion mulplication result of the current one with the passed one "q1".
  2255. * Returns the current Quaternion.
  2256. */
  2257. public multiplyToRef(q1: Quaternion, result: Quaternion): Quaternion {
  2258. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  2259. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  2260. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  2261. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  2262. result.copyFromFloats(x, y, z, w);
  2263. return this;
  2264. }
  2265. /**
  2266. * Updates the current Quaternion with the quaternion mulplication result of itself with the passed one "q1".
  2267. * Returns the updated Quaternion.
  2268. */
  2269. public multiplyInPlace(q1: Quaternion): Quaternion {
  2270. this.multiplyToRef(q1, this);
  2271. return this;
  2272. }
  2273. /**
  2274. * Sets the passed "ref" with the conjugation of the current Quaternion.
  2275. * Returns the current Quaternion.
  2276. */
  2277. public conjugateToRef(ref: Quaternion): Quaternion {
  2278. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  2279. return this;
  2280. }
  2281. /**
  2282. * Conjugates in place the current Quaternion.
  2283. * Returns the updated Quaternion.
  2284. */
  2285. public conjugateInPlace(): Quaternion {
  2286. this.x *= -1;
  2287. this.y *= -1;
  2288. this.z *= -1;
  2289. return this;
  2290. }
  2291. /**
  2292. * Returns a new Quaternion as the conjugate of the current Quaternion.
  2293. */
  2294. public conjugate(): Quaternion {
  2295. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  2296. return result;
  2297. }
  2298. /**
  2299. * Returns the Quaternion length (float).
  2300. */
  2301. public length(): number {
  2302. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  2303. }
  2304. /**
  2305. * Normalize in place the current Quaternion.
  2306. * Returns the updated Quaternion.
  2307. */
  2308. public normalize(): Quaternion {
  2309. var length = 1.0 / this.length();
  2310. this.x *= length;
  2311. this.y *= length;
  2312. this.z *= length;
  2313. this.w *= length;
  2314. return this;
  2315. }
  2316. /**
  2317. * Returns a new Vector3 set with the Euler angles translated from the current Quaternion.
  2318. */
  2319. public toEulerAngles(order = "YZX"): Vector3 {
  2320. var result = Vector3.Zero();
  2321. this.toEulerAnglesToRef(result, order);
  2322. return result;
  2323. }
  2324. /**
  2325. * Sets the passed vector3 "result" with the Euler angles translated from the current Quaternion.
  2326. * Returns the current Quaternion.
  2327. */
  2328. public toEulerAnglesToRef(result: Vector3, order = "YZX"): Quaternion {
  2329. var qz = this.z;
  2330. var qx = this.x;
  2331. var qy = this.y;
  2332. var qw = this.w;
  2333. var sqw = qw * qw;
  2334. var sqz = qz * qz;
  2335. var sqx = qx * qx;
  2336. var sqy = qy * qy;
  2337. var zAxisY = qy * qz - qx * qw;
  2338. var limit = .4999999;
  2339. if (zAxisY < -limit) {
  2340. result.y = 2 * Math.atan2(qy, qw);
  2341. result.x = Math.PI / 2;
  2342. result.z = 0;
  2343. } else if (zAxisY > limit) {
  2344. result.y = 2 * Math.atan2(qy, qw);
  2345. result.x = -Math.PI / 2;
  2346. result.z = 0;
  2347. } else {
  2348. result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
  2349. result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
  2350. result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
  2351. }
  2352. return this;
  2353. }
  2354. /**
  2355. * Updates the passed rotation matrix with the current Quaternion values.
  2356. * Returns the current Quaternion.
  2357. */
  2358. public toRotationMatrix(result: Matrix): Quaternion {
  2359. var xx = this.x * this.x;
  2360. var yy = this.y * this.y;
  2361. var zz = this.z * this.z;
  2362. var xy = this.x * this.y;
  2363. var zw = this.z * this.w;
  2364. var zx = this.z * this.x;
  2365. var yw = this.y * this.w;
  2366. var yz = this.y * this.z;
  2367. var xw = this.x * this.w;
  2368. result.m[0] = 1.0 - (2.0 * (yy + zz));
  2369. result.m[1] = 2.0 * (xy + zw);
  2370. result.m[2] = 2.0 * (zx - yw);
  2371. result.m[3] = 0;
  2372. result.m[4] = 2.0 * (xy - zw);
  2373. result.m[5] = 1.0 - (2.0 * (zz + xx));
  2374. result.m[6] = 2.0 * (yz + xw);
  2375. result.m[7] = 0;
  2376. result.m[8] = 2.0 * (zx + yw);
  2377. result.m[9] = 2.0 * (yz - xw);
  2378. result.m[10] = 1.0 - (2.0 * (yy + xx));
  2379. result.m[11] = 0;
  2380. result.m[12] = 0;
  2381. result.m[13] = 0;
  2382. result.m[14] = 0;
  2383. result.m[15] = 1.0;
  2384. return this;
  2385. }
  2386. /**
  2387. * Updates the current Quaternion from the passed rotation matrix values.
  2388. * Returns the updated Quaternion.
  2389. */
  2390. public fromRotationMatrix(matrix: Matrix): Quaternion {
  2391. Quaternion.FromRotationMatrixToRef(matrix, this);
  2392. return this;
  2393. }
  2394. // Statics
  2395. /**
  2396. * Returns a new Quaternion set from the passed rotation matrix values.
  2397. */
  2398. public static FromRotationMatrix(matrix: Matrix): Quaternion {
  2399. var result = new Quaternion();
  2400. Quaternion.FromRotationMatrixToRef(matrix, result);
  2401. return result;
  2402. }
  2403. /**
  2404. * Updates the passed quaternion "result" with the passed rotation matrix values.
  2405. */
  2406. public static FromRotationMatrixToRef(matrix: Matrix, result: Quaternion): void {
  2407. var data = matrix.m;
  2408. var m11 = data[0], m12 = data[4], m13 = data[8];
  2409. var m21 = data[1], m22 = data[5], m23 = data[9];
  2410. var m31 = data[2], m32 = data[6], m33 = data[10];
  2411. var trace = m11 + m22 + m33;
  2412. var s;
  2413. if (trace > 0) {
  2414. s = 0.5 / Math.sqrt(trace + 1.0);
  2415. result.w = 0.25 / s;
  2416. result.x = (m32 - m23) * s;
  2417. result.y = (m13 - m31) * s;
  2418. result.z = (m21 - m12) * s;
  2419. } else if (m11 > m22 && m11 > m33) {
  2420. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  2421. result.w = (m32 - m23) / s;
  2422. result.x = 0.25 * s;
  2423. result.y = (m12 + m21) / s;
  2424. result.z = (m13 + m31) / s;
  2425. } else if (m22 > m33) {
  2426. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  2427. result.w = (m13 - m31) / s;
  2428. result.x = (m12 + m21) / s;
  2429. result.y = 0.25 * s;
  2430. result.z = (m23 + m32) / s;
  2431. } else {
  2432. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  2433. result.w = (m21 - m12) / s;
  2434. result.x = (m13 + m31) / s;
  2435. result.y = (m23 + m32) / s;
  2436. result.z = 0.25 * s;
  2437. }
  2438. }
  2439. /**
  2440. * Returns a new Quaternion set to (0.0, 0.0, 0.0).
  2441. */
  2442. public static Zero(): Quaternion {
  2443. return new Quaternion(0.0, 0.0, 0.0, 0.0);
  2444. }
  2445. /**
  2446. * Returns a new Quaternion as the inverted current Quaternion.
  2447. */
  2448. public static Inverse(q: Quaternion): Quaternion {
  2449. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  2450. }
  2451. /**
  2452. * Returns the identity Quaternion.
  2453. */
  2454. public static Identity(): Quaternion {
  2455. return new Quaternion(0.0, 0.0, 0.0, 1.0);
  2456. }
  2457. public static IsIdentity(quaternion: Quaternion) {
  2458. return quaternion && quaternion.x === 0 && quaternion.y === 0 && quaternion.z === 0 && quaternion.w === 1;
  2459. }
  2460. /**
  2461. * Returns a new Quaternion set from the passed axis (Vector3) and angle in radians (float).
  2462. */
  2463. public static RotationAxis(axis: Vector3, angle: number): Quaternion {
  2464. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  2465. }
  2466. /**
  2467. * Sets the passed quaternion "result" from the passed axis (Vector3) and angle in radians (float).
  2468. */
  2469. public static RotationAxisToRef(axis: Vector3, angle: number, result: Quaternion): Quaternion {
  2470. var sin = Math.sin(angle / 2);
  2471. axis.normalize();
  2472. result.w = Math.cos(angle / 2);
  2473. result.x = axis.x * sin;
  2474. result.y = axis.y * sin;
  2475. result.z = axis.z * sin;
  2476. return result;
  2477. }
  2478. /**
  2479. * Retuns a new Quaternion set from the starting index of the passed array.
  2480. */
  2481. public static FromArray(array: number[], offset?: number): Quaternion {
  2482. if (!offset) {
  2483. offset = 0;
  2484. }
  2485. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  2486. }
  2487. /**
  2488. * Returns a new Quaternion set from the passed Euler float angles (y, x, z).
  2489. */
  2490. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  2491. var q = new Quaternion();
  2492. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  2493. return q;
  2494. }
  2495. /**
  2496. * Sets the passed quaternion "result" from the passed float Euler angles (y, x, z).
  2497. */
  2498. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  2499. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  2500. var halfRoll = roll * 0.5;
  2501. var halfPitch = pitch * 0.5;
  2502. var halfYaw = yaw * 0.5;
  2503. var sinRoll = Math.sin(halfRoll);
  2504. var cosRoll = Math.cos(halfRoll);
  2505. var sinPitch = Math.sin(halfPitch);
  2506. var cosPitch = Math.cos(halfPitch);
  2507. var sinYaw = Math.sin(halfYaw);
  2508. var cosYaw = Math.cos(halfYaw);
  2509. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  2510. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  2511. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  2512. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  2513. }
  2514. /**
  2515. * Returns a new Quaternion from the passed float Euler angles expressed in z-x-z orientation
  2516. */
  2517. public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
  2518. var result = new Quaternion();
  2519. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  2520. return result;
  2521. }
  2522. /**
  2523. * Sets the passed quaternion "result" from the passed float Euler angles expressed in z-x-z orientation
  2524. */
  2525. public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
  2526. // Produces a quaternion from Euler angles in the z-x-z orientation
  2527. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  2528. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  2529. var halfBeta = beta * 0.5;
  2530. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  2531. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  2532. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  2533. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  2534. }
  2535. /**
  2536. * Returns a new Quaternion as the quaternion rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system.
  2537. * cf to Vector3.RotationFromAxis() documentation.
  2538. * Note : axis1, axis2 and axis3 are normalized during this operation.
  2539. */
  2540. public static RotationQuaternionFromAxis(axis1: Vector3, axis2: Vector3, axis3: Vector3, ref: Quaternion): Quaternion {
  2541. var quat = new Quaternion(0.0, 0.0, 0.0, 0.0);
  2542. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  2543. return quat;
  2544. }
  2545. /**
  2546. * Sets the passed quaternion "ref" with the quaternion rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system.
  2547. * cf to Vector3.RotationFromAxis() documentation.
  2548. * Note : axis1, axis2 and axis3 are normalized during this operation.
  2549. */
  2550. public static RotationQuaternionFromAxisToRef(axis1: Vector3, axis2: Vector3, axis3: Vector3, ref: Quaternion): void {
  2551. var rotMat = MathTmp.Matrix[0];
  2552. BABYLON.Matrix.FromXYZAxesToRef(axis1.normalize(), axis2.normalize(), axis3.normalize(), rotMat);
  2553. BABYLON.Quaternion.FromRotationMatrixToRef(rotMat, ref);
  2554. }
  2555. public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
  2556. var result = Quaternion.Identity();
  2557. Quaternion.SlerpToRef(left, right, amount, result);
  2558. return result;
  2559. }
  2560. public static SlerpToRef(left: Quaternion, right: Quaternion, amount: number, result: Quaternion): void {
  2561. var num2;
  2562. var num3;
  2563. var num = amount;
  2564. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  2565. var flag = false;
  2566. if (num4 < 0) {
  2567. flag = true;
  2568. num4 = -num4;
  2569. }
  2570. if (num4 > 0.999999) {
  2571. num3 = 1 - num;
  2572. num2 = flag ? -num : num;
  2573. }
  2574. else {
  2575. var num5 = Math.acos(num4);
  2576. var num6 = (1.0 / Math.sin(num5));
  2577. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  2578. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  2579. }
  2580. result.x = (num3 * left.x) + (num2 * right.x);
  2581. result.y = (num3 * left.y) + (num2 * right.y);
  2582. result.z = (num3 * left.z) + (num2 * right.z);
  2583. result.w = (num3 * left.w) + (num2 * right.w);
  2584. }
  2585. }
  2586. export class Matrix {
  2587. private static _tempQuaternion: Quaternion = new Quaternion();
  2588. private static _xAxis: Vector3 = Vector3.Zero();
  2589. private static _yAxis: Vector3 = Vector3.Zero();
  2590. private static _zAxis: Vector3 = Vector3.Zero();
  2591. public m: Float32Array = new Float32Array(16);
  2592. // Properties
  2593. /**
  2594. * Boolean : True is the matrix is the identity matrix
  2595. */
  2596. public isIdentity(): boolean {
  2597. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  2598. return false;
  2599. if (this.m[1] !== 0.0 || this.m[2] !== 0.0 || this.m[3] !== 0.0 ||
  2600. this.m[4] !== 0.0 || this.m[6] !== 0.0 || this.m[7] !== 0.0 ||
  2601. this.m[8] !== 0.0 || this.m[9] !== 0.0 || this.m[11] !== 0.0 ||
  2602. this.m[12] !== 0.0 || this.m[13] !== 0.0 || this.m[14] !== 0.0)
  2603. return false;
  2604. return true;
  2605. }
  2606. /**
  2607. * Returns the matrix determinant (float).
  2608. */
  2609. public determinant(): number {
  2610. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  2611. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  2612. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  2613. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  2614. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  2615. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  2616. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  2617. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  2618. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  2619. }
  2620. // Methods
  2621. /**
  2622. * Returns the matrix underlying array.
  2623. */
  2624. public toArray(): Float32Array {
  2625. return this.m;
  2626. }
  2627. /**
  2628. * Returns the matrix underlying array.
  2629. */
  2630. public asArray(): Float32Array {
  2631. return this.toArray();
  2632. }
  2633. /**
  2634. * Inverts in place the Matrix.
  2635. * Returns the Matrix inverted.
  2636. */
  2637. public invert(): Matrix {
  2638. this.invertToRef(this);
  2639. return this;
  2640. }
  2641. /**
  2642. * Sets all the matrix elements to zero.
  2643. * Returns the Matrix.
  2644. */
  2645. public reset(): Matrix {
  2646. for (var index = 0; index < 16; index++) {
  2647. this.m[index] = 0.0;
  2648. }
  2649. return this;
  2650. }
  2651. /**
  2652. * Returns a new Matrix as the addition result of the current Matrix and the passed one.
  2653. */
  2654. public add(other: Matrix): Matrix {
  2655. var result = new Matrix();
  2656. this.addToRef(other, result);
  2657. return result;
  2658. }
  2659. /**
  2660. * Sets the passed matrix "result" with the ddition result of the current Matrix and the passed one.
  2661. * Returns the Matrix.
  2662. */
  2663. public addToRef(other: Matrix, result: Matrix): Matrix {
  2664. for (var index = 0; index < 16; index++) {
  2665. result.m[index] = this.m[index] + other.m[index];
  2666. }
  2667. return this;
  2668. }
  2669. /**
  2670. * Adds in place the passed matrix to the current Matrix.
  2671. * Returns the updated Matrix.
  2672. */
  2673. public addToSelf(other: Matrix): Matrix {
  2674. for (var index = 0; index < 16; index++) {
  2675. this.m[index] += other.m[index];
  2676. }
  2677. return this;
  2678. }
  2679. /**
  2680. * Sets the passed matrix with the current inverted Matrix.
  2681. * Returns the unmodified current Matrix.
  2682. */
  2683. public invertToRef(other: Matrix): Matrix {
  2684. var l1 = this.m[0];
  2685. var l2 = this.m[1];
  2686. var l3 = this.m[2];
  2687. var l4 = this.m[3];
  2688. var l5 = this.m[4];
  2689. var l6 = this.m[5];
  2690. var l7 = this.m[6];
  2691. var l8 = this.m[7];
  2692. var l9 = this.m[8];
  2693. var l10 = this.m[9];
  2694. var l11 = this.m[10];
  2695. var l12 = this.m[11];
  2696. var l13 = this.m[12];
  2697. var l14 = this.m[13];
  2698. var l15 = this.m[14];
  2699. var l16 = this.m[15];
  2700. var l17 = (l11 * l16) - (l12 * l15);
  2701. var l18 = (l10 * l16) - (l12 * l14);
  2702. var l19 = (l10 * l15) - (l11 * l14);
  2703. var l20 = (l9 * l16) - (l12 * l13);
  2704. var l21 = (l9 * l15) - (l11 * l13);
  2705. var l22 = (l9 * l14) - (l10 * l13);
  2706. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  2707. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  2708. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  2709. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  2710. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  2711. var l28 = (l7 * l16) - (l8 * l15);
  2712. var l29 = (l6 * l16) - (l8 * l14);
  2713. var l30 = (l6 * l15) - (l7 * l14);
  2714. var l31 = (l5 * l16) - (l8 * l13);
  2715. var l32 = (l5 * l15) - (l7 * l13);
  2716. var l33 = (l5 * l14) - (l6 * l13);
  2717. var l34 = (l7 * l12) - (l8 * l11);
  2718. var l35 = (l6 * l12) - (l8 * l10);
  2719. var l36 = (l6 * l11) - (l7 * l10);
  2720. var l37 = (l5 * l12) - (l8 * l9);
  2721. var l38 = (l5 * l11) - (l7 * l9);
  2722. var l39 = (l5 * l10) - (l6 * l9);
  2723. other.m[0] = l23 * l27;
  2724. other.m[4] = l24 * l27;
  2725. other.m[8] = l25 * l27;
  2726. other.m[12] = l26 * l27;
  2727. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  2728. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  2729. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  2730. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  2731. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  2732. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  2733. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  2734. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  2735. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  2736. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  2737. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  2738. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  2739. return this;
  2740. }
  2741. /**
  2742. * Inserts the translation vector (using 3 x floats) in the current Matrix.
  2743. * Returns the updated Matrix.
  2744. */
  2745. public setTranslationFromFloats(x: number, y: number, z: number): Matrix {
  2746. this.m[12] = x;
  2747. this.m[13] = y;
  2748. this.m[14] = z;
  2749. return this;
  2750. }
  2751. /**
  2752. * Inserts the translation vector in the current Matrix.
  2753. * Returns the updated Matrix.
  2754. */
  2755. public setTranslation(vector3: Vector3): Matrix {
  2756. this.m[12] = vector3.x;
  2757. this.m[13] = vector3.y;
  2758. this.m[14] = vector3.z;
  2759. return this;
  2760. }
  2761. /**
  2762. * Returns a new Vector3 as the extracted translation from the Matrix.
  2763. */
  2764. public getTranslation(): Vector3 {
  2765. return new Vector3(this.m[12], this.m[13], this.m[14]);
  2766. }
  2767. /**
  2768. * Fill a Vector3 with the extracted translation from the Matrix.
  2769. */
  2770. public getTranslationToRef(result: Vector3): Matrix {
  2771. result.x = this.m[12];
  2772. result.y = this.m[13];
  2773. result.z = this.m[14];
  2774. return this;
  2775. }
  2776. /**
  2777. * Remove rotation and scaling part from the Matrix.
  2778. * Returns the updated Matrix.
  2779. */
  2780. public removeRotationAndScaling(): Matrix {
  2781. this.setRowFromFloats(0, 1, 0, 0, 0);
  2782. this.setRowFromFloats(1, 0, 1, 0, 0);
  2783. this.setRowFromFloats(2, 0, 0, 1, 0);
  2784. return this;
  2785. }
  2786. /**
  2787. * Returns a new Matrix set with the multiplication result of the current Matrix and the passed one.
  2788. */
  2789. public multiply(other: Matrix): Matrix {
  2790. var result = new Matrix();
  2791. this.multiplyToRef(other, result);
  2792. return result;
  2793. }
  2794. /**
  2795. * Updates the current Matrix from the passed one values.
  2796. * Returns the updated Matrix.
  2797. */
  2798. public copyFrom(other: Matrix): Matrix {
  2799. for (var index = 0; index < 16; index++) {
  2800. this.m[index] = other.m[index];
  2801. }
  2802. return this;
  2803. }
  2804. /**
  2805. * Populates the passed array from the starting index with the Matrix values.
  2806. * Returns the Matrix.
  2807. */
  2808. public copyToArray(array: Float32Array, offset: number = 0): Matrix {
  2809. for (var index = 0; index < 16; index++) {
  2810. array[offset + index] = this.m[index];
  2811. }
  2812. return this;
  2813. }
  2814. /**
  2815. * Sets the passed matrix "result" with the multiplication result of the current Matrix and the passed one.
  2816. */
  2817. public multiplyToRef(other: Matrix, result: Matrix): Matrix {
  2818. this.multiplyToArray(other, result.m, 0);
  2819. return this;
  2820. }
  2821. /**
  2822. * Sets the Float32Array "result" from the passed index "offset" with the multiplication result of the current Matrix and the passed one.
  2823. */
  2824. public multiplyToArray(other: Matrix, result: Float32Array, offset: number): Matrix {
  2825. var tm0 = this.m[0];
  2826. var tm1 = this.m[1];
  2827. var tm2 = this.m[2];
  2828. var tm3 = this.m[3];
  2829. var tm4 = this.m[4];
  2830. var tm5 = this.m[5];
  2831. var tm6 = this.m[6];
  2832. var tm7 = this.m[7];
  2833. var tm8 = this.m[8];
  2834. var tm9 = this.m[9];
  2835. var tm10 = this.m[10];
  2836. var tm11 = this.m[11];
  2837. var tm12 = this.m[12];
  2838. var tm13 = this.m[13];
  2839. var tm14 = this.m[14];
  2840. var tm15 = this.m[15];
  2841. var om0 = other.m[0];
  2842. var om1 = other.m[1];
  2843. var om2 = other.m[2];
  2844. var om3 = other.m[3];
  2845. var om4 = other.m[4];
  2846. var om5 = other.m[5];
  2847. var om6 = other.m[6];
  2848. var om7 = other.m[7];
  2849. var om8 = other.m[8];
  2850. var om9 = other.m[9];
  2851. var om10 = other.m[10];
  2852. var om11 = other.m[11];
  2853. var om12 = other.m[12];
  2854. var om13 = other.m[13];
  2855. var om14 = other.m[14];
  2856. var om15 = other.m[15];
  2857. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  2858. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  2859. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  2860. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  2861. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  2862. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  2863. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  2864. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  2865. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  2866. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  2867. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  2868. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  2869. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  2870. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  2871. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  2872. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  2873. return this;
  2874. }
  2875. /**
  2876. * Boolean : True is the current Matrix and the passed one values are strictly equal.
  2877. */
  2878. public equals(value: Matrix): boolean {
  2879. return value &&
  2880. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  2881. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  2882. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  2883. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  2884. }
  2885. /**
  2886. * Returns a new Matrix from the current Matrix.
  2887. */
  2888. public clone(): Matrix {
  2889. return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  2890. this.m[4], this.m[5], this.m[6], this.m[7],
  2891. this.m[8], this.m[9], this.m[10], this.m[11],
  2892. this.m[12], this.m[13], this.m[14], this.m[15]);
  2893. }
  2894. /**
  2895. * Returns the string "Matrix"
  2896. */
  2897. public getClassName(): string {
  2898. return "Matrix";
  2899. }
  2900. /**
  2901. * Returns the Matrix hash code.
  2902. */
  2903. public getHashCode(): number {
  2904. let hash = this.m[0] || 0;
  2905. for (let i = 1; i < 16; i++) {
  2906. hash = (hash * 397) ^ (this.m[i] || 0);
  2907. }
  2908. return hash;
  2909. }
  2910. /**
  2911. * Decomposes the current Matrix into :
  2912. * - a scale vector3 passed as a reference to update,
  2913. * - a rotation quaternion passed as a reference to update,
  2914. * - a translation vector3 passed as a reference to update.
  2915. * Returns the boolean `true`.
  2916. */
  2917. public decompose(scale: Vector3, rotation: Quaternion, translation: Vector3): boolean {
  2918. translation.x = this.m[12];
  2919. translation.y = this.m[13];
  2920. translation.z = this.m[14];
  2921. var xs = MathTools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  2922. var ys = MathTools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  2923. var zs = MathTools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  2924. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  2925. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  2926. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  2927. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  2928. rotation.x = 0;
  2929. rotation.y = 0;
  2930. rotation.z = 0;
  2931. rotation.w = 1;
  2932. return false;
  2933. }
  2934. Matrix.FromValuesToRef(
  2935. this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0,
  2936. this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0,
  2937. this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0,
  2938. 0, 0, 0, 1, MathTmp.Matrix[0]);
  2939. Quaternion.FromRotationMatrixToRef(MathTmp.Matrix[0], rotation);
  2940. return true;
  2941. }
  2942. /**
  2943. * Returns a new Matrix as the extracted rotation matrix from the current one.
  2944. */
  2945. public getRotationMatrix(): Matrix {
  2946. var result = Matrix.Identity();
  2947. this.getRotationMatrixToRef(result);
  2948. return result;
  2949. }
  2950. /**
  2951. * Extracts the rotation matrix from the current one and sets it as the passed "result".
  2952. * Returns the current Matrix.
  2953. */
  2954. public getRotationMatrixToRef(result: Matrix): Matrix {
  2955. var m = this.m;
  2956. var xs = m[0] * m[1] * m[2] * m[3] < 0 ? -1 : 1;
  2957. var ys = m[4] * m[5] * m[6] * m[7] < 0 ? -1 : 1;
  2958. var zs = m[8] * m[9] * m[10] * m[11] < 0 ? -1 : 1;
  2959. var sx = xs * Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
  2960. var sy = ys * Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
  2961. var sz = zs * Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
  2962. Matrix.FromValuesToRef(
  2963. m[0] / sx, m[1] / sx, m[2] / sx, 0,
  2964. m[4] / sy, m[5] / sy, m[6] / sy, 0,
  2965. m[8] / sz, m[9] / sz, m[10] / sz, 0,
  2966. 0, 0, 0, 1, result);
  2967. return this;
  2968. }
  2969. // Statics
  2970. /**
  2971. * Returns a new Matrix set from the starting index of the passed array.
  2972. */
  2973. public static FromArray(array: number[], offset?: number): Matrix {
  2974. var result = new Matrix();
  2975. if (!offset) {
  2976. offset = 0;
  2977. }
  2978. Matrix.FromArrayToRef(array, offset, result);
  2979. return result;
  2980. }
  2981. /**
  2982. * Sets the passed "result" matrix from the starting index of the passed array.
  2983. */
  2984. public static FromArrayToRef(array: number[], offset: number, result: Matrix) {
  2985. for (var index = 0; index < 16; index++) {
  2986. result.m[index] = array[index + offset];
  2987. }
  2988. }
  2989. /**
  2990. * Sets the passed "result" matrix from the starting index of the passed Float32Array by multiplying each element by the float "scale".
  2991. */
  2992. public static FromFloat32ArrayToRefScaled(array: Float32Array, offset: number, scale: number, result: Matrix) {
  2993. for (var index = 0; index < 16; index++) {
  2994. result.m[index] = array[index + offset] * scale;
  2995. }
  2996. }
  2997. /**
  2998. * Sets the passed matrix "result" with the 16 passed floats.
  2999. */
  3000. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  3001. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  3002. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  3003. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  3004. result.m[0] = initialM11;
  3005. result.m[1] = initialM12;
  3006. result.m[2] = initialM13;
  3007. result.m[3] = initialM14;
  3008. result.m[4] = initialM21;
  3009. result.m[5] = initialM22;
  3010. result.m[6] = initialM23;
  3011. result.m[7] = initialM24;
  3012. result.m[8] = initialM31;
  3013. result.m[9] = initialM32;
  3014. result.m[10] = initialM33;
  3015. result.m[11] = initialM34;
  3016. result.m[12] = initialM41;
  3017. result.m[13] = initialM42;
  3018. result.m[14] = initialM43;
  3019. result.m[15] = initialM44;
  3020. }
  3021. /**
  3022. * Returns the index-th row of the current matrix as a new Vector4.
  3023. */
  3024. public getRow(index: number): Vector4 {
  3025. if (index < 0 || index > 3) {
  3026. return null;
  3027. }
  3028. var i = index * 4;
  3029. return new Vector4(this.m[i + 0], this.m[i + 1], this.m[i + 2], this.m[i + 3]);
  3030. }
  3031. /**
  3032. * Sets the index-th row of the current matrix with the passed Vector4 values.
  3033. * Returns the updated Matrix.
  3034. */
  3035. public setRow(index: number, row: Vector4): Matrix {
  3036. if (index < 0 || index > 3) {
  3037. return this;
  3038. }
  3039. var i = index * 4;
  3040. this.m[i + 0] = row.x;
  3041. this.m[i + 1] = row.y;
  3042. this.m[i + 2] = row.z;
  3043. this.m[i + 3] = row.w;
  3044. return this;
  3045. }
  3046. /**
  3047. * Sets the index-th row of the current matrix with the passed 4 x float values.
  3048. * Returns the updated Matrix.
  3049. */
  3050. public setRowFromFloats(index: number, x: number, y: number, z: number, w: number): Matrix {
  3051. if (index < 0 || index > 3) {
  3052. return this;
  3053. }
  3054. var i = index * 4;
  3055. this.m[i + 0] = x;
  3056. this.m[i + 1] = y;
  3057. this.m[i + 2] = z;
  3058. this.m[i + 3] = w;
  3059. return this;
  3060. }
  3061. /**
  3062. * Returns a new Matrix set from the 16 passed floats.
  3063. */
  3064. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  3065. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  3066. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  3067. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  3068. var result = new Matrix();
  3069. result.m[0] = initialM11;
  3070. result.m[1] = initialM12;
  3071. result.m[2] = initialM13;
  3072. result.m[3] = initialM14;
  3073. result.m[4] = initialM21;
  3074. result.m[5] = initialM22;
  3075. result.m[6] = initialM23;
  3076. result.m[7] = initialM24;
  3077. result.m[8] = initialM31;
  3078. result.m[9] = initialM32;
  3079. result.m[10] = initialM33;
  3080. result.m[11] = initialM34;
  3081. result.m[12] = initialM41;
  3082. result.m[13] = initialM42;
  3083. result.m[14] = initialM43;
  3084. result.m[15] = initialM44;
  3085. return result;
  3086. }
  3087. /**
  3088. * Returns a new Matrix composed by the passed scale (vector3), rotation (quaternion) and translation (vector3).
  3089. */
  3090. public static Compose(scale: Vector3, rotation: Quaternion, translation: Vector3): Matrix {
  3091. var result = Matrix.Identity();
  3092. Matrix.ComposeToRef(scale, rotation, translation, result);
  3093. return result;
  3094. }
  3095. /**
  3096. * Update a Matrix with values composed by the passed scale (vector3), rotation (quaternion) and translation (vector3).
  3097. */
  3098. public static ComposeToRef(scale: Vector3, rotation: Quaternion, translation: Vector3, result: Matrix): void {
  3099. Matrix.FromValuesToRef(scale.x, 0, 0, 0,
  3100. 0, scale.y, 0, 0,
  3101. 0, 0, scale.z, 0,
  3102. 0, 0, 0, 1, MathTmp.Matrix[1]);
  3103. rotation.toRotationMatrix(MathTmp.Matrix[0]);
  3104. MathTmp.Matrix[1].multiplyToRef(MathTmp.Matrix[0], result);
  3105. result.setTranslation(translation);
  3106. }
  3107. /**
  3108. * Returns a new indentity Matrix.
  3109. */
  3110. public static Identity(): Matrix {
  3111. return Matrix.FromValues(1.0, 0.0, 0.0, 0.0,
  3112. 0.0, 1.0, 0.0, 0.0,
  3113. 0.0, 0.0, 1.0, 0.0,
  3114. 0.0, 0.0, 0.0, 1.0);
  3115. }
  3116. /**
  3117. * Sets the passed "result" as an identity matrix.
  3118. */
  3119. public static IdentityToRef(result: Matrix): void {
  3120. Matrix.FromValuesToRef(1.0, 0.0, 0.0, 0.0,
  3121. 0.0, 1.0, 0.0, 0.0,
  3122. 0.0, 0.0, 1.0, 0.0,
  3123. 0.0, 0.0, 0.0, 1.0, result);
  3124. }
  3125. /**
  3126. * Returns a new zero Matrix.
  3127. */
  3128. public static Zero(): Matrix {
  3129. return Matrix.FromValues(0.0, 0.0, 0.0, 0.0,
  3130. 0.0, 0.0, 0.0, 0.0,
  3131. 0.0, 0.0, 0.0, 0.0,
  3132. 0.0, 0.0, 0.0, 0.0);
  3133. }
  3134. /**
  3135. * Returns a new rotation matrix for "angle" radians around the X axis.
  3136. */
  3137. public static RotationX(angle: number): Matrix {
  3138. var result = new Matrix();
  3139. Matrix.RotationXToRef(angle, result);
  3140. return result;
  3141. }
  3142. /**
  3143. * Returns a new Matrix as the passed inverted one.
  3144. */
  3145. public static Invert(source: Matrix): Matrix {
  3146. var result = new Matrix();
  3147. source.invertToRef(result);
  3148. return result;
  3149. }
  3150. /**
  3151. * Sets the passed matrix "result" as a rotation matrix for "angle" radians around the X axis.
  3152. */
  3153. public static RotationXToRef(angle: number, result: Matrix): void {
  3154. var s = Math.sin(angle);
  3155. var c = Math.cos(angle);
  3156. result.m[0] = 1.0;
  3157. result.m[15] = 1.0;
  3158. result.m[5] = c;
  3159. result.m[10] = c;
  3160. result.m[9] = -s;
  3161. result.m[6] = s;
  3162. result.m[1] = 0.0;
  3163. result.m[2] = 0.0;
  3164. result.m[3] = 0.0;
  3165. result.m[4] = 0.0;
  3166. result.m[7] = 0.0;
  3167. result.m[8] = 0.0;
  3168. result.m[11] = 0.0;
  3169. result.m[12] = 0.0;
  3170. result.m[13] = 0.0;
  3171. result.m[14] = 0.0;
  3172. }
  3173. /**
  3174. * Returns a new rotation matrix for "angle" radians around the Y axis.
  3175. */
  3176. public static RotationY(angle: number): Matrix {
  3177. var result = new Matrix();
  3178. Matrix.RotationYToRef(angle, result);
  3179. return result;
  3180. }
  3181. /**
  3182. * Sets the passed matrix "result" as a rotation matrix for "angle" radians around the Y axis.
  3183. */
  3184. public static RotationYToRef(angle: number, result: Matrix): void {
  3185. var s = Math.sin(angle);
  3186. var c = Math.cos(angle);
  3187. result.m[5] = 1.0;
  3188. result.m[15] = 1.0;
  3189. result.m[0] = c;
  3190. result.m[2] = -s;
  3191. result.m[8] = s;
  3192. result.m[10] = c;
  3193. result.m[1] = 0.0;
  3194. result.m[3] = 0.0;
  3195. result.m[4] = 0.0;
  3196. result.m[6] = 0.0;
  3197. result.m[7] = 0.0;
  3198. result.m[9] = 0.0;
  3199. result.m[11] = 0.0;
  3200. result.m[12] = 0.0;
  3201. result.m[13] = 0.0;
  3202. result.m[14] = 0.0;
  3203. }
  3204. /**
  3205. * Returns a new rotation matrix for "angle" radians around the Z axis.
  3206. */
  3207. public static RotationZ(angle: number): Matrix {
  3208. var result = new Matrix();
  3209. Matrix.RotationZToRef(angle, result);
  3210. return result;
  3211. }
  3212. /**
  3213. * Sets the passed matrix "result" as a rotation matrix for "angle" radians around the Z axis.
  3214. */
  3215. public static RotationZToRef(angle: number, result: Matrix): void {
  3216. var s = Math.sin(angle);
  3217. var c = Math.cos(angle);
  3218. result.m[10] = 1.0;
  3219. result.m[15] = 1.0;
  3220. result.m[0] = c;
  3221. result.m[1] = s;
  3222. result.m[4] = -s;
  3223. result.m[5] = c;
  3224. result.m[2] = 0.0;
  3225. result.m[3] = 0.0;
  3226. result.m[6] = 0.0;
  3227. result.m[7] = 0.0;
  3228. result.m[8] = 0.0;
  3229. result.m[9] = 0.0;
  3230. result.m[11] = 0.0;
  3231. result.m[12] = 0.0;
  3232. result.m[13] = 0.0;
  3233. result.m[14] = 0.0;
  3234. }
  3235. /**
  3236. * Returns a new rotation matrix for "angle" radians around the passed axis.
  3237. */
  3238. public static RotationAxis(axis: Vector3, angle: number): Matrix {
  3239. var result = Matrix.Zero();
  3240. Matrix.RotationAxisToRef(axis, angle, result);
  3241. return result;
  3242. }
  3243. /**
  3244. * Sets the passed matrix "result" as a rotation matrix for "angle" radians around the passed axis.
  3245. */
  3246. public static RotationAxisToRef(axis: Vector3, angle: number, result: Matrix): void {
  3247. var s = Math.sin(-angle);
  3248. var c = Math.cos(-angle);
  3249. var c1 = 1 - c;
  3250. axis.normalize();
  3251. result.m[0] = (axis.x * axis.x) * c1 + c;
  3252. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  3253. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  3254. result.m[3] = 0.0;
  3255. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  3256. result.m[5] = (axis.y * axis.y) * c1 + c;
  3257. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  3258. result.m[7] = 0.0;
  3259. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  3260. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  3261. result.m[10] = (axis.z * axis.z) * c1 + c;
  3262. result.m[11] = 0.0;
  3263. result.m[15] = 1.0;
  3264. }
  3265. /**
  3266. * Returns a new Matrix as a rotation matrix from the Euler angles (y, x, z).
  3267. */
  3268. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  3269. var result = new Matrix();
  3270. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  3271. return result;
  3272. }
  3273. /**
  3274. * Sets the passed matrix "result" as a rotation matrix from the Euler angles (y, x, z).
  3275. */
  3276. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  3277. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  3278. this._tempQuaternion.toRotationMatrix(result);
  3279. }
  3280. /**
  3281. * Returns a new Matrix as a scaling matrix from the passed floats (x, y, z).
  3282. */
  3283. public static Scaling(x: number, y: number, z: number): Matrix {
  3284. var result = Matrix.Zero();
  3285. Matrix.ScalingToRef(x, y, z, result);
  3286. return result;
  3287. }
  3288. /**
  3289. * Sets the passed matrix "result" as a scaling matrix from the passed floats (x, y, z).
  3290. */
  3291. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  3292. result.m[0] = x;
  3293. result.m[1] = 0.0;
  3294. result.m[2] = 0.0;
  3295. result.m[3] = 0.0;
  3296. result.m[4] = 0.0;
  3297. result.m[5] = y;
  3298. result.m[6] = 0.0;
  3299. result.m[7] = 0.0;
  3300. result.m[8] = 0.0;
  3301. result.m[9] = 0.0;
  3302. result.m[10] = z;
  3303. result.m[11] = 0.0;
  3304. result.m[12] = 0.0;
  3305. result.m[13] = 0.0;
  3306. result.m[14] = 0.0;
  3307. result.m[15] = 1.0;
  3308. }
  3309. /**
  3310. * Returns a new Matrix as a translation matrix from the passed floats (x, y, z).
  3311. */
  3312. public static Translation(x: number, y: number, z: number): Matrix {
  3313. var result = Matrix.Identity();
  3314. Matrix.TranslationToRef(x, y, z, result);
  3315. return result;
  3316. }
  3317. /**
  3318. * Sets the passed matrix "result" as a translation matrix from the passed floats (x, y, z).
  3319. */
  3320. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  3321. Matrix.FromValuesToRef(1.0, 0.0, 0.0, 0.0,
  3322. 0.0, 1.0, 0.0, 0.0,
  3323. 0.0, 0.0, 1.0, 0.0,
  3324. x, y, z, 1.0, result);
  3325. }
  3326. /**
  3327. * Returns a new Matrix whose values are the interpolated values for "gradien" (float) between the ones of the matrices "startValue" and "endValue".
  3328. */
  3329. public static Lerp(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  3330. var result = Matrix.Zero();
  3331. for (var index = 0; index < 16; index++) {
  3332. result.m[index] = startValue.m[index] * (1.0 - gradient) + endValue.m[index] * gradient;
  3333. }
  3334. return result;
  3335. }
  3336. /**
  3337. * Returns a new Matrix whose values are computed by :
  3338. * - decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices,
  3339. * - interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end,
  3340. * - recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices.
  3341. */
  3342. public static DecomposeLerp(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  3343. var startScale = new Vector3(0, 0, 0);
  3344. var startRotation = new Quaternion();
  3345. var startTranslation = new Vector3(0, 0, 0);
  3346. startValue.decompose(startScale, startRotation, startTranslation);
  3347. var endScale = new Vector3(0, 0, 0);
  3348. var endRotation = new Quaternion();
  3349. var endTranslation = new Vector3(0, 0, 0);
  3350. endValue.decompose(endScale, endRotation, endTranslation);
  3351. var resultScale = Vector3.Lerp(startScale, endScale, gradient);
  3352. var resultRotation = Quaternion.Slerp(startRotation, endRotation, gradient);
  3353. var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
  3354. return Matrix.Compose(resultScale, resultRotation, resultTranslation);
  3355. }
  3356. /**
  3357. * Returns a new rotation Matrix used to rotate a mesh so as it looks at the target Vector3, from the eye Vector3, the UP vector3 being orientated like "up".
  3358. * This methods works for a Left-Handed system.
  3359. */
  3360. public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  3361. var result = Matrix.Zero();
  3362. Matrix.LookAtLHToRef(eye, target, up, result);
  3363. return result;
  3364. }
  3365. /**
  3366. * Sets the passed "result" Matrix as a rotation matrix used to rotate a mesh so as it looks at the target Vector3, from the eye Vector3, the UP vector3 being orientated like "up".
  3367. * This methods works for a Left-Handed system.
  3368. */
  3369. public static LookAtLHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  3370. // Z axis
  3371. target.subtractToRef(eye, this._zAxis);
  3372. this._zAxis.normalize();
  3373. // X axis
  3374. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  3375. if (this._xAxis.lengthSquared() === 0) {
  3376. this._xAxis.x = 1.0;
  3377. } else {
  3378. this._xAxis.normalize();
  3379. }
  3380. // Y axis
  3381. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  3382. this._yAxis.normalize();
  3383. // Eye angles
  3384. var ex = -Vector3.Dot(this._xAxis, eye);
  3385. var ey = -Vector3.Dot(this._yAxis, eye);
  3386. var ez = -Vector3.Dot(this._zAxis, eye);
  3387. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  3388. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  3389. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  3390. ex, ey, ez, 1, result);
  3391. }
  3392. /**
  3393. * Returns a new rotation Matrix used to rotate a mesh so as it looks at the target Vector3, from the eye Vector3, the UP vector3 being orientated like "up".
  3394. * This methods works for a Right-Handed system.
  3395. */
  3396. public static LookAtRH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  3397. var result = Matrix.Zero();
  3398. Matrix.LookAtRHToRef(eye, target, up, result);
  3399. return result;
  3400. }
  3401. /**
  3402. * Sets the passed "result" Matrix as a rotation matrix used to rotate a mesh so as it looks at the target Vector3, from the eye Vector3, the UP vector3 being orientated like "up".
  3403. * This methods works for a Left-Handed system.
  3404. */
  3405. public static LookAtRHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  3406. // Z axis
  3407. eye.subtractToRef(target, this._zAxis);
  3408. this._zAxis.normalize();
  3409. // X axis
  3410. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  3411. if (this._xAxis.lengthSquared() === 0) {
  3412. this._xAxis.x = 1.0;
  3413. } else {
  3414. this._xAxis.normalize();
  3415. }
  3416. // Y axis
  3417. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  3418. this._yAxis.normalize();
  3419. // Eye angles
  3420. var ex = -Vector3.Dot(this._xAxis, eye);
  3421. var ey = -Vector3.Dot(this._yAxis, eye);
  3422. var ez = -Vector3.Dot(this._zAxis, eye);
  3423. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  3424. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  3425. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  3426. ex, ey, ez, 1, result);
  3427. }
  3428. /**
  3429. * Returns a new Matrix as a left-handed orthographic projection matrix computed from the passed floats : width and height of the projection plane, z near and far limits.
  3430. */
  3431. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  3432. var matrix = Matrix.Zero();
  3433. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  3434. return matrix;
  3435. }
  3436. /**
  3437. * Sets the passed matrix "result" as a left-handed orthographic projection matrix computed from the passed floats : width and height of the projection plane, z near and far limits.
  3438. */
  3439. public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
  3440. let n = znear;
  3441. let f = zfar;
  3442. let a = 2.0 / width;
  3443. let b = 2.0 / height;
  3444. let c = 2.0 / (f - n);
  3445. let d = -(f + n) / (f - n);
  3446. BABYLON.Matrix.FromValuesToRef(
  3447. a, 0.0, 0.0, 0.0,
  3448. 0.0, b, 0.0, 0.0,
  3449. 0.0, 0.0, c, 0.0,
  3450. 0.0, 0.0, d, 1.0,
  3451. result
  3452. );
  3453. }
  3454. /**
  3455. * Returns a new Matrix as a left-handed orthographic projection matrix computed from the passed floats : left, right, top and bottom being the coordinates of the projection plane, z near and far limits.
  3456. */
  3457. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  3458. var matrix = Matrix.Zero();
  3459. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  3460. return matrix;
  3461. }
  3462. /**
  3463. * Sets the passed matrix "result" as a left-handed orthographic projection matrix computed from the passed floats : left, right, top and bottom being the coordinates of the projection plane, z near and far limits.
  3464. */
  3465. public static OrthoOffCenterLHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  3466. let n = znear;
  3467. let f = zfar;
  3468. let a = 2.0 / (right - left);
  3469. let b = 2.0 / (top - bottom);
  3470. let c = 2.0 / (f - n);
  3471. let d = -(f + n) / (f - n);
  3472. let i0 = (left + right) / (left - right);
  3473. let i1 = (top + bottom) / (bottom - top);
  3474. BABYLON.Matrix.FromValuesToRef(
  3475. a, 0.0, 0.0, 0.0,
  3476. 0.0, b, 0.0, 0.0,
  3477. 0.0, 0.0, c, 0.0,
  3478. i0, i1, d, 1.0,
  3479. result
  3480. );
  3481. }
  3482. /**
  3483. * Returns a new Matrix as a right-handed orthographic projection matrix computed from the passed floats : left, right, top and bottom being the coordinates of the projection plane, z near and far limits.
  3484. */
  3485. public static OrthoOffCenterRH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  3486. var matrix = Matrix.Zero();
  3487. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  3488. return matrix;
  3489. }
  3490. /**
  3491. * Sets the passed matrix "result" as a right-handed orthographic projection matrix computed from the passed floats : left, right, top and bottom being the coordinates of the projection plane, z near and far limits.
  3492. */
  3493. public static OrthoOffCenterRHToRef(left: number, right, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  3494. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  3495. result.m[10] *= -1.0;
  3496. }
  3497. /**
  3498. * Returns a new Matrix as a left-handed perspective projection matrix computed from the passed floats : width and height of the projection plane, z near and far limits.
  3499. */
  3500. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  3501. var matrix = Matrix.Zero();
  3502. let n = znear;
  3503. let f = zfar;
  3504. let a = 2.0 * n / width;
  3505. let b = 2.0 * n / height;
  3506. let c = (f + n) / (f - n);
  3507. let d = -2.0 * f * n / (f - n);
  3508. BABYLON.Matrix.FromValuesToRef(
  3509. a, 0.0, 0.0, 0.0,
  3510. 0.0, b, 0.0, 0.0,
  3511. 0.0, 0.0, c, 1.0,
  3512. 0.0, 0.0, d, 0.0,
  3513. matrix
  3514. );
  3515. return matrix;
  3516. }
  3517. /**
  3518. * Returns a new Matrix as a left-handed perspective projection matrix computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3519. */
  3520. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  3521. var matrix = Matrix.Zero();
  3522. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  3523. return matrix;
  3524. }
  3525. /**
  3526. * Sets the passed matrix "result" as a left-handed perspective projection matrix computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3527. */
  3528. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  3529. let n = znear;
  3530. let f = zfar;
  3531. let t = 1.0 / (Math.tan(fov * 0.5));
  3532. let a = isVerticalFovFixed ? (t / aspect) : t;
  3533. let b = isVerticalFovFixed ? t : (t * aspect);
  3534. let c = (f + n) / (f - n);
  3535. let d = -2.0 * f * n / (f - n);
  3536. BABYLON.Matrix.FromValuesToRef(
  3537. a, 0.0, 0.0, 0.0,
  3538. 0.0, b, 0.0, 0.0,
  3539. 0.0, 0.0, c, 1.0,
  3540. 0.0, 0.0, d, 0.0,
  3541. result
  3542. );
  3543. }
  3544. /**
  3545. * Returns a new Matrix as a right-handed perspective projection matrix computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3546. */
  3547. public static PerspectiveFovRH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  3548. var matrix = Matrix.Zero();
  3549. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  3550. return matrix;
  3551. }
  3552. /**
  3553. * Sets the passed matrix "result" as a right-handed perspective projection matrix computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3554. */
  3555. public static PerspectiveFovRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  3556. //alternatively this could be expressed as:
  3557. // m = PerspectiveFovLHToRef
  3558. // m[10] *= -1.0;
  3559. // m[11] *= -1.0;
  3560. let n = znear;
  3561. let f = zfar;
  3562. let t = 1.0 / (Math.tan(fov * 0.5));
  3563. let a = isVerticalFovFixed ? (t / aspect) : t;
  3564. let b = isVerticalFovFixed ? t : (t * aspect);
  3565. let c = -(f + n) / (f - n);
  3566. let d = -2 * f * n / (f - n);
  3567. BABYLON.Matrix.FromValuesToRef(
  3568. a, 0.0, 0.0, 0.0,
  3569. 0.0, b, 0.0, 0.0,
  3570. 0.0, 0.0, c, -1.0,
  3571. 0.0, 0.0, d, 0.0,
  3572. result
  3573. );
  3574. }
  3575. /**
  3576. * Sets the passed matrix "result" as a left-handed perspective projection matrix for WebVR computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3577. */
  3578. public static PerspectiveFovWebVRToRef(fov, znear: number, zfar: number, result: Matrix, rightHanded = false): void {
  3579. var rightHandedFactor = rightHanded ? -1 : 1;
  3580. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  3581. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  3582. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  3583. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  3584. var xScale = 2.0 / (leftTan + rightTan);
  3585. var yScale = 2.0 / (upTan + downTan);
  3586. result.m[0] = xScale;
  3587. result.m[1] = result.m[2] = result.m[3] = result.m[4] = 0.0;
  3588. result.m[5] = yScale;
  3589. result.m[6] = result.m[7] = 0.0;
  3590. result.m[8] = ((leftTan - rightTan) * xScale * 0.5) * rightHandedFactor;
  3591. result.m[9] = -((upTan - downTan) * yScale * 0.5) * rightHandedFactor;
  3592. result.m[10] = -(znear + zfar) / (zfar - znear) * rightHandedFactor;
  3593. // result.m[10] = -zfar / (znear - zfar);
  3594. result.m[11] = 1.0 * rightHandedFactor;
  3595. result.m[12] = result.m[13] = result.m[15] = 0.0;
  3596. result.m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  3597. // result.m[14] = (znear * zfar) / (znear - zfar);
  3598. }
  3599. /**
  3600. * Returns the final transformation matrix : world * view * projection * viewport
  3601. */
  3602. public static GetFinalMatrix(viewport: Viewport, world: Matrix, view: Matrix, projection: Matrix, zmin: number, zmax: number): Matrix {
  3603. var cw = viewport.width;
  3604. var ch = viewport.height;
  3605. var cx = viewport.x;
  3606. var cy = viewport.y;
  3607. var viewportMatrix = Matrix.FromValues(cw / 2.0, 0.0, 0.0, 0.0,
  3608. 0.0, -ch / 2.0, 0.0, 0.0,
  3609. 0.0, 0.0, zmax - zmin, 0.0,
  3610. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  3611. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  3612. }
  3613. /**
  3614. * Returns a new Float32Array array with 4 elements : the 2x2 matrix extracted from the passed Matrix.
  3615. */
  3616. public static GetAsMatrix2x2(matrix: Matrix): Float32Array {
  3617. return new Float32Array([
  3618. matrix.m[0], matrix.m[1],
  3619. matrix.m[4], matrix.m[5]
  3620. ]);
  3621. }
  3622. /**
  3623. * Returns a new Float32Array array with 9 elements : the 3x3 matrix extracted from the passed Matrix.
  3624. */
  3625. public static GetAsMatrix3x3(matrix: Matrix): Float32Array {
  3626. return new Float32Array([
  3627. matrix.m[0], matrix.m[1], matrix.m[2],
  3628. matrix.m[4], matrix.m[5], matrix.m[6],
  3629. matrix.m[8], matrix.m[9], matrix.m[10]
  3630. ]);
  3631. }
  3632. /**
  3633. * Compute the transpose of the passed Matrix.
  3634. * Returns a new Matrix.
  3635. */
  3636. public static Transpose(matrix: Matrix): Matrix {
  3637. var result = new Matrix();
  3638. result.m[0] = matrix.m[0];
  3639. result.m[1] = matrix.m[4];
  3640. result.m[2] = matrix.m[8];
  3641. result.m[3] = matrix.m[12];
  3642. result.m[4] = matrix.m[1];
  3643. result.m[5] = matrix.m[5];
  3644. result.m[6] = matrix.m[9];
  3645. result.m[7] = matrix.m[13];
  3646. result.m[8] = matrix.m[2];
  3647. result.m[9] = matrix.m[6];
  3648. result.m[10] = matrix.m[10];
  3649. result.m[11] = matrix.m[14];
  3650. result.m[12] = matrix.m[3];
  3651. result.m[13] = matrix.m[7];
  3652. result.m[14] = matrix.m[11];
  3653. result.m[15] = matrix.m[15];
  3654. return result;
  3655. }
  3656. /**
  3657. * Returns a new Matrix as the reflection matrix across the passed plane.
  3658. */
  3659. public static Reflection(plane: Plane): Matrix {
  3660. var matrix = new Matrix();
  3661. Matrix.ReflectionToRef(plane, matrix);
  3662. return matrix;
  3663. }
  3664. /**
  3665. * Sets the passed matrix "result" as the reflection matrix across the passed plane.
  3666. */
  3667. public static ReflectionToRef(plane: Plane, result: Matrix): void {
  3668. plane.normalize();
  3669. var x = plane.normal.x;
  3670. var y = plane.normal.y;
  3671. var z = plane.normal.z;
  3672. var temp = -2 * x;
  3673. var temp2 = -2 * y;
  3674. var temp3 = -2 * z;
  3675. result.m[0] = (temp * x) + 1;
  3676. result.m[1] = temp2 * x;
  3677. result.m[2] = temp3 * x;
  3678. result.m[3] = 0.0;
  3679. result.m[4] = temp * y;
  3680. result.m[5] = (temp2 * y) + 1;
  3681. result.m[6] = temp3 * y;
  3682. result.m[7] = 0.0;
  3683. result.m[8] = temp * z;
  3684. result.m[9] = temp2 * z;
  3685. result.m[10] = (temp3 * z) + 1;
  3686. result.m[11] = 0.0;
  3687. result.m[12] = temp * plane.d;
  3688. result.m[13] = temp2 * plane.d;
  3689. result.m[14] = temp3 * plane.d;
  3690. result.m[15] = 1.0;
  3691. }
  3692. /**
  3693. * Sets the passed matrix "mat" as a rotation matrix composed from the 3 passed left handed axis.
  3694. */
  3695. public static FromXYZAxesToRef(xaxis: Vector3, yaxis: Vector3, zaxis: Vector3, mat: Matrix) {
  3696. mat.m[0] = xaxis.x;
  3697. mat.m[1] = xaxis.y;
  3698. mat.m[2] = xaxis.z;
  3699. mat.m[3] = 0.0;
  3700. mat.m[4] = yaxis.x;
  3701. mat.m[5] = yaxis.y;
  3702. mat.m[6] = yaxis.z;
  3703. mat.m[7] = 0.0;
  3704. mat.m[8] = zaxis.x;
  3705. mat.m[9] = zaxis.y;
  3706. mat.m[10] = zaxis.z;
  3707. mat.m[11] = 0.0;
  3708. mat.m[12] = 0.0;
  3709. mat.m[13] = 0.0;
  3710. mat.m[14] = 0.0;
  3711. mat.m[15] = 1.0;
  3712. }
  3713. /**
  3714. * Sets the passed matrix "result" as a rotation matrix according to the passed quaternion.
  3715. */
  3716. public static FromQuaternionToRef(quat: Quaternion, result: Matrix) {
  3717. var xx = quat.x * quat.x;
  3718. var yy = quat.y * quat.y;
  3719. var zz = quat.z * quat.z;
  3720. var xy = quat.x * quat.y;
  3721. var zw = quat.z * quat.w;
  3722. var zx = quat.z * quat.x;
  3723. var yw = quat.y * quat.w;
  3724. var yz = quat.y * quat.z;
  3725. var xw = quat.x * quat.w;
  3726. result.m[0] = 1.0 - (2.0 * (yy + zz));
  3727. result.m[1] = 2.0 * (xy + zw);
  3728. result.m[2] = 2.0 * (zx - yw);
  3729. result.m[3] = 0.0;
  3730. result.m[4] = 2.0 * (xy - zw);
  3731. result.m[5] = 1.0 - (2.0 * (zz + xx));
  3732. result.m[6] = 2.0 * (yz + xw);
  3733. result.m[7] = 0.0;
  3734. result.m[8] = 2.0 * (zx + yw);
  3735. result.m[9] = 2.0 * (yz - xw);
  3736. result.m[10] = 1.0 - (2.0 * (yy + xx));
  3737. result.m[11] = 0.0;
  3738. result.m[12] = 0.0;
  3739. result.m[13] = 0.0;
  3740. result.m[14] = 0.0;
  3741. result.m[15] = 1.0;
  3742. }
  3743. }
  3744. export class Plane {
  3745. public normal: Vector3;
  3746. public d: number;
  3747. /**
  3748. * Creates a Plane object according to the passed floats a, b, c, d and the plane equation : ax + by + cz + d = 0
  3749. */
  3750. constructor(a: number, b: number, c: number, d: number) {
  3751. this.normal = new Vector3(a, b, c);
  3752. this.d = d;
  3753. }
  3754. /**
  3755. * Returns the plane coordinates as a new array of 4 elements [a, b, c, d].
  3756. */
  3757. public asArray(): number[] {
  3758. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  3759. }
  3760. // Methods
  3761. /**
  3762. * Returns a new plane copied from the current Plane.
  3763. */
  3764. public clone(): Plane {
  3765. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  3766. }
  3767. /**
  3768. * Returns the string "Plane".
  3769. */
  3770. public getClassName(): string {
  3771. return "Plane";
  3772. }
  3773. /**
  3774. * Returns the Plane hash code.
  3775. */
  3776. public getHashCode(): number {
  3777. let hash = this.normal.getHashCode();
  3778. hash = (hash * 397) ^ (this.d || 0);
  3779. return hash;
  3780. }
  3781. /**
  3782. * Normalize the current Plane in place.
  3783. * Returns the updated Plane.
  3784. */
  3785. public normalize(): Plane {
  3786. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  3787. var magnitude = 0.0;
  3788. if (norm !== 0) {
  3789. magnitude = 1.0 / norm;
  3790. }
  3791. this.normal.x *= magnitude;
  3792. this.normal.y *= magnitude;
  3793. this.normal.z *= magnitude;
  3794. this.d *= magnitude;
  3795. return this;
  3796. }
  3797. /**
  3798. * Returns a new Plane as the result of the transformation of the current Plane by the passed matrix.
  3799. */
  3800. public transform(transformation: Matrix): Plane {
  3801. var transposedMatrix = Matrix.Transpose(transformation);
  3802. var x = this.normal.x;
  3803. var y = this.normal.y;
  3804. var z = this.normal.z;
  3805. var d = this.d;
  3806. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  3807. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  3808. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  3809. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  3810. return new Plane(normalX, normalY, normalZ, finalD);
  3811. }
  3812. /**
  3813. * Returns the dot product (float) of the point coordinates and the plane normal.
  3814. */
  3815. public dotCoordinate(point): number {
  3816. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  3817. }
  3818. /**
  3819. * Updates the current Plane from the plane defined by the three passed points.
  3820. * Returns the updated Plane.
  3821. */
  3822. public copyFromPoints(point1: Vector3, point2: Vector3, point3: Vector3): Plane {
  3823. var x1 = point2.x - point1.x;
  3824. var y1 = point2.y - point1.y;
  3825. var z1 = point2.z - point1.z;
  3826. var x2 = point3.x - point1.x;
  3827. var y2 = point3.y - point1.y;
  3828. var z2 = point3.z - point1.z;
  3829. var yz = (y1 * z2) - (z1 * y2);
  3830. var xz = (z1 * x2) - (x1 * z2);
  3831. var xy = (x1 * y2) - (y1 * x2);
  3832. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  3833. var invPyth;
  3834. if (pyth !== 0) {
  3835. invPyth = 1.0 / pyth;
  3836. }
  3837. else {
  3838. invPyth = 0.0;
  3839. }
  3840. this.normal.x = yz * invPyth;
  3841. this.normal.y = xz * invPyth;
  3842. this.normal.z = xy * invPyth;
  3843. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  3844. return this;
  3845. }
  3846. /**
  3847. * Boolean : True is the vector "direction" is the same side than the plane normal.
  3848. */
  3849. public isFrontFacingTo(direction: Vector3, epsilon: number): boolean {
  3850. var dot = Vector3.Dot(this.normal, direction);
  3851. return (dot <= epsilon);
  3852. }
  3853. /**
  3854. * Returns the signed distance (float) from the passed point to the Plane.
  3855. */
  3856. public signedDistanceTo(point: Vector3): number {
  3857. return Vector3.Dot(point, this.normal) + this.d;
  3858. }
  3859. // Statics
  3860. /**
  3861. * Returns a new Plane from the passed array.
  3862. */
  3863. static FromArray(array: number[]): Plane {
  3864. return new Plane(array[0], array[1], array[2], array[3]);
  3865. }
  3866. /**
  3867. * Returns a new Plane defined by the three passed points.
  3868. */
  3869. static FromPoints(point1, point2, point3): Plane {
  3870. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  3871. result.copyFromPoints(point1, point2, point3);
  3872. return result;
  3873. }
  3874. /**
  3875. * Returns a new Plane the normal vector to this plane at the passed origin point.
  3876. * Note : the vector "normal" is updated because normalized.
  3877. */
  3878. static FromPositionAndNormal(origin: Vector3, normal: Vector3): Plane {
  3879. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  3880. normal.normalize();
  3881. result.normal = normal;
  3882. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  3883. return result;
  3884. }
  3885. /**
  3886. * Returns the signed distance between the plane defined by the normal vector at the "origin"" point and the passed other point.
  3887. */
  3888. static SignedDistanceToPlaneFromPositionAndNormal(origin: Vector3, normal: Vector3, point: Vector3): number {
  3889. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  3890. return Vector3.Dot(point, normal) + d;
  3891. }
  3892. }
  3893. export class Viewport {
  3894. /**
  3895. * Creates a Viewport object located at (x, y) and sized (width, height).
  3896. */
  3897. constructor(public x: number, public y: number, public width: number, public height: number) {
  3898. }
  3899. public toGlobal(renderWidth: number, renderHeight: number): Viewport {
  3900. return new Viewport(this.x * renderWidth, this.y * renderHeight, this.width * renderWidth, this.height * renderHeight);
  3901. }
  3902. /**
  3903. * Returns a new Viewport copied from the current one.
  3904. */
  3905. public clone(): Viewport {
  3906. return new Viewport(this.x, this.y, this.width, this.height);
  3907. }
  3908. }
  3909. export class Frustum {
  3910. /**
  3911. * Returns a new array of 6 Frustum planes computed by the passed transformation matrix.
  3912. */
  3913. public static GetPlanes(transform: Matrix): Plane[] {
  3914. var frustumPlanes = [];
  3915. for (var index = 0; index < 6; index++) {
  3916. frustumPlanes.push(new Plane(0.0, 0.0, 0.0, 0.0));
  3917. }
  3918. Frustum.GetPlanesToRef(transform, frustumPlanes);
  3919. return frustumPlanes;
  3920. }
  3921. /**
  3922. * Sets the passed array "frustumPlanes" with the 6 Frustum planes computed by the passed transformation matrix.
  3923. */
  3924. public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
  3925. // Near
  3926. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  3927. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  3928. frustumPlanes[0].normal.z = transform.m[11] + transform.m[10];
  3929. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  3930. frustumPlanes[0].normalize();
  3931. // Far
  3932. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  3933. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  3934. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  3935. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  3936. frustumPlanes[1].normalize();
  3937. // Left
  3938. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  3939. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  3940. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  3941. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  3942. frustumPlanes[2].normalize();
  3943. // Right
  3944. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  3945. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  3946. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  3947. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  3948. frustumPlanes[3].normalize();
  3949. // Top
  3950. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  3951. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  3952. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  3953. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  3954. frustumPlanes[4].normalize();
  3955. // Bottom
  3956. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  3957. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  3958. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  3959. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  3960. frustumPlanes[5].normalize();
  3961. }
  3962. }
  3963. export enum Space {
  3964. LOCAL = 0,
  3965. WORLD = 1,
  3966. BONE = 2
  3967. }
  3968. export class Axis {
  3969. public static X: Vector3 = new Vector3(1.0, 0.0, 0.0);
  3970. public static Y: Vector3 = new Vector3(0.0, 1.0, 0.0);
  3971. public static Z: Vector3 = new Vector3(0.0, 0.0, 1.0);
  3972. };
  3973. export class BezierCurve {
  3974. /**
  3975. * Returns the cubic Bezier interpolated value (float) at "t" (float) from the passed x1, y1, x2, y2 floats.
  3976. */
  3977. public static interpolate(t: number, x1: number, y1: number, x2: number, y2: number): number {
  3978. // Extract X (which is equal to time here)
  3979. var f0 = 1 - 3 * x2 + 3 * x1;
  3980. var f1 = 3 * x2 - 6 * x1;
  3981. var f2 = 3 * x1;
  3982. var refinedT = t;
  3983. for (var i = 0; i < 5; i++) {
  3984. var refinedT2 = refinedT * refinedT;
  3985. var refinedT3 = refinedT2 * refinedT;
  3986. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  3987. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  3988. refinedT -= (x - t) * slope;
  3989. refinedT = Math.min(1, Math.max(0, refinedT));
  3990. }
  3991. // Resolve cubic bezier for the given x
  3992. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
  3993. 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
  3994. Math.pow(refinedT, 3);
  3995. }
  3996. }
  3997. export enum Orientation {
  3998. CW = 0,
  3999. CCW = 1
  4000. }
  4001. export class Angle {
  4002. private _radians: number;
  4003. /**
  4004. * Creates an Angle object of "radians" radians (float).
  4005. */
  4006. constructor(radians: number) {
  4007. this._radians = radians;
  4008. if (this._radians < 0.0) this._radians += (2.0 * Math.PI);
  4009. }
  4010. /**
  4011. * Returns the Angle value in degrees (float).
  4012. */
  4013. public degrees = () => this._radians * 180.0 / Math.PI;
  4014. /**
  4015. * Returns the Angle value in radians (float).
  4016. */
  4017. public radians = () => this._radians;
  4018. /**
  4019. * Returns a new Angle object valued with the angle value in radians between the two passed vectors.
  4020. */
  4021. public static BetweenTwoPoints(a: Vector2, b: Vector2): Angle {
  4022. var delta = b.subtract(a);
  4023. var theta = Math.atan2(delta.y, delta.x);
  4024. return new Angle(theta);
  4025. }
  4026. /**
  4027. * Returns a new Angle object from the passed float in radians.
  4028. */
  4029. public static FromRadians(radians: number): Angle {
  4030. return new Angle(radians);
  4031. }
  4032. /**
  4033. * Returns a new Angle object from the passed float in degrees.
  4034. */
  4035. public static FromDegrees(degrees: number): Angle {
  4036. return new Angle(degrees * Math.PI / 180.0);
  4037. }
  4038. }
  4039. export class Arc2 {
  4040. centerPoint: Vector2;
  4041. radius: number;
  4042. angle: Angle;
  4043. startAngle: Angle;
  4044. orientation: Orientation;
  4045. /**
  4046. * Creates an Arc object from the three passed points : start, middle and end.
  4047. */
  4048. constructor(public startPoint: Vector2, public midPoint: Vector2, public endPoint: Vector2) {
  4049. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  4050. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  4051. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  4052. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  4053. this.centerPoint = new Vector2(
  4054. (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
  4055. ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
  4056. );
  4057. this.radius = this.centerPoint.subtract(this.startPoint).length();
  4058. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  4059. var a1 = this.startAngle.degrees();
  4060. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  4061. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  4062. // angles correction
  4063. if (a2 - a1 > +180.0) a2 -= 360.0;
  4064. if (a2 - a1 < -180.0) a2 += 360.0;
  4065. if (a3 - a2 > +180.0) a3 -= 360.0;
  4066. if (a3 - a2 < -180.0) a3 += 360.0;
  4067. this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
  4068. this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
  4069. }
  4070. }
  4071. export class Path2 {
  4072. private _points = new Array<Vector2>();
  4073. private _length = 0.0;
  4074. public closed = false;
  4075. /**
  4076. * Creates a Path2 object from the starting 2D coordinates x and y.
  4077. */
  4078. constructor(x: number, y: number) {
  4079. this._points.push(new Vector2(x, y));
  4080. }
  4081. /**
  4082. * Adds a new segment until the passed coordinates (x, y) to the current Path2.
  4083. * Returns the updated Path2.
  4084. */
  4085. public addLineTo(x: number, y: number): Path2 {
  4086. if (closed) {
  4087. //Tools.Error("cannot add lines to closed paths");
  4088. return this;
  4089. }
  4090. var newPoint = new Vector2(x, y);
  4091. var previousPoint = this._points[this._points.length - 1];
  4092. this._points.push(newPoint);
  4093. this._length += newPoint.subtract(previousPoint).length();
  4094. return this;
  4095. }
  4096. /**
  4097. * Adds _numberOfSegments_ segments according to the arc definition (middle point coordinates, end point coordinates, the arc start point being the current Path2 last point) to the current Path2.
  4098. * Returns the updated Path2.
  4099. */
  4100. public addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36): Path2 {
  4101. if (closed) {
  4102. //Tools.Error("cannot add arcs to closed paths");
  4103. return this;
  4104. }
  4105. var startPoint = this._points[this._points.length - 1];
  4106. var midPoint = new Vector2(midX, midY);
  4107. var endPoint = new Vector2(endX, endY);
  4108. var arc = new Arc2(startPoint, midPoint, endPoint);
  4109. var increment = arc.angle.radians() / numberOfSegments;
  4110. if (arc.orientation === Orientation.CW) increment *= -1;
  4111. var currentAngle = arc.startAngle.radians() + increment;
  4112. for (var i = 0; i < numberOfSegments; i++) {
  4113. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  4114. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  4115. this.addLineTo(x, y);
  4116. currentAngle += increment;
  4117. }
  4118. return this;
  4119. }
  4120. /**
  4121. * Closes the Path2.
  4122. * Returns the Path2.
  4123. */
  4124. public close(): Path2 {
  4125. this.closed = true;
  4126. return this;
  4127. }
  4128. /**
  4129. * Returns the Path2 total length (float).
  4130. */
  4131. public length(): number {
  4132. var result = this._length;
  4133. if (!this.closed) {
  4134. var lastPoint = this._points[this._points.length - 1];
  4135. var firstPoint = this._points[0];
  4136. result += (firstPoint.subtract(lastPoint).length());
  4137. }
  4138. return result;
  4139. }
  4140. /**
  4141. * Returns the Path2 internal array of points.
  4142. */
  4143. public getPoints(): Vector2[] {
  4144. return this._points;
  4145. }
  4146. /**
  4147. * Returns a new Vector2 located at a percentage of the Path2 total length on this path.
  4148. */
  4149. public getPointAtLengthPosition(normalizedLengthPosition: number): Vector2 {
  4150. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  4151. //Tools.Error("normalized length position should be between 0 and 1.");
  4152. return Vector2.Zero();
  4153. }
  4154. var lengthPosition = normalizedLengthPosition * this.length();
  4155. var previousOffset = 0;
  4156. for (var i = 0; i < this._points.length; i++) {
  4157. var j = (i + 1) % this._points.length;
  4158. var a = this._points[i];
  4159. var b = this._points[j];
  4160. var bToA = b.subtract(a);
  4161. var nextOffset = (bToA.length() + previousOffset);
  4162. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  4163. var dir = bToA.normalize();
  4164. var localOffset = lengthPosition - previousOffset;
  4165. return new Vector2(
  4166. a.x + (dir.x * localOffset),
  4167. a.y + (dir.y * localOffset)
  4168. );
  4169. }
  4170. previousOffset = nextOffset;
  4171. }
  4172. //Tools.Error("internal error");
  4173. return Vector2.Zero();
  4174. }
  4175. /**
  4176. * Returns a new Path2 starting at the coordinates (x, y).
  4177. */
  4178. public static StartingAt(x: number, y: number): Path2 {
  4179. return new Path2(x, y);
  4180. }
  4181. }
  4182. export class Path3D {
  4183. private _curve = new Array<Vector3>();
  4184. private _distances = new Array<number>();
  4185. private _tangents = new Array<Vector3>();
  4186. private _normals = new Array<Vector3>();
  4187. private _binormals = new Array<Vector3>();
  4188. private _raw: boolean;
  4189. /**
  4190. * new Path3D(path, normal, raw)
  4191. * Creates a Path3D. A Path3D is a logical math object, so not a mesh.
  4192. * please read the description in the tutorial : http://doc.babylonjs.com/tutorials/How_to_use_Path3D
  4193. * path : an array of Vector3, the curve axis of the Path3D
  4194. * normal (optional) : Vector3, the first wanted normal to the curve. Ex (0, 1, 0) for a vertical normal.
  4195. * raw (optional, default false) : boolean, if true the returned Path3D isn't normalized. Useful to depict path acceleration or speed.
  4196. */
  4197. constructor(public path: Vector3[], firstNormal?: Vector3, raw?: boolean) {
  4198. for (var p = 0; p < path.length; p++) {
  4199. this._curve[p] = path[p].clone(); // hard copy
  4200. }
  4201. this._raw = raw || false;
  4202. this._compute(firstNormal);
  4203. }
  4204. /**
  4205. * Returns the Path3D array of successive Vector3 designing its curve.
  4206. */
  4207. public getCurve(): Vector3[] {
  4208. return this._curve;
  4209. }
  4210. /**
  4211. * Returns an array populated with tangent vectors on each Path3D curve point.
  4212. */
  4213. public getTangents(): Vector3[] {
  4214. return this._tangents;
  4215. }
  4216. /**
  4217. * Returns an array populated with normal vectors on each Path3D curve point.
  4218. */
  4219. public getNormals(): Vector3[] {
  4220. return this._normals;
  4221. }
  4222. /**
  4223. * Returns an array populated with binormal vectors on each Path3D curve point.
  4224. */
  4225. public getBinormals(): Vector3[] {
  4226. return this._binormals;
  4227. }
  4228. /**
  4229. * Returns an array populated with distances (float) of the i-th point from the first curve point.
  4230. */
  4231. public getDistances(): number[] {
  4232. return this._distances;
  4233. }
  4234. /**
  4235. * Forces the Path3D tangent, normal, binormal and distance recomputation.
  4236. * Returns the same object updated.
  4237. */
  4238. public update(path: Vector3[], firstNormal?: Vector3): Path3D {
  4239. for (var p = 0; p < path.length; p++) {
  4240. this._curve[p].x = path[p].x;
  4241. this._curve[p].y = path[p].y;
  4242. this._curve[p].z = path[p].z;
  4243. }
  4244. this._compute(firstNormal);
  4245. return this;
  4246. }
  4247. // private function compute() : computes tangents, normals and binormals
  4248. private _compute(firstNormal) {
  4249. var l = this._curve.length;
  4250. // first and last tangents
  4251. this._tangents[0] = this._getFirstNonNullVector(0);
  4252. if (!this._raw) {
  4253. this._tangents[0].normalize();
  4254. }
  4255. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  4256. if (!this._raw) {
  4257. this._tangents[l - 1].normalize();
  4258. }
  4259. // normals and binormals at first point : arbitrary vector with _normalVector()
  4260. var tg0 = this._tangents[0];
  4261. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  4262. this._normals[0] = pp0;
  4263. if (!this._raw) {
  4264. this._normals[0].normalize();
  4265. }
  4266. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  4267. if (!this._raw) {
  4268. this._binormals[0].normalize();
  4269. }
  4270. this._distances[0] = 0.0;
  4271. // normals and binormals : next points
  4272. var prev: Vector3; // previous vector (segment)
  4273. var cur: Vector3; // current vector (segment)
  4274. var curTang: Vector3; // current tangent
  4275. // previous normal
  4276. var prevBinor: Vector3; // previous binormal
  4277. for (var i = 1; i < l; i++) {
  4278. // tangents
  4279. prev = this._getLastNonNullVector(i);
  4280. if (i < l - 1) {
  4281. cur = this._getFirstNonNullVector(i);
  4282. this._tangents[i] = prev.add(cur);
  4283. this._tangents[i].normalize();
  4284. }
  4285. this._distances[i] = this._distances[i - 1] + prev.length();
  4286. // normals and binormals
  4287. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  4288. curTang = this._tangents[i];
  4289. prevBinor = this._binormals[i - 1];
  4290. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  4291. if (!this._raw) {
  4292. this._normals[i].normalize();
  4293. }
  4294. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  4295. if (!this._raw) {
  4296. this._binormals[i].normalize();
  4297. }
  4298. }
  4299. }
  4300. // private function getFirstNonNullVector(index)
  4301. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  4302. private _getFirstNonNullVector(index: number): Vector3 {
  4303. var i = 1;
  4304. var nNVector: Vector3 = this._curve[index + i].subtract(this._curve[index]);
  4305. while (nNVector.length() === 0 && index + i + 1 < this._curve.length) {
  4306. i++;
  4307. nNVector = this._curve[index + i].subtract(this._curve[index]);
  4308. }
  4309. return nNVector;
  4310. }
  4311. // private function getLastNonNullVector(index)
  4312. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  4313. private _getLastNonNullVector(index: number): Vector3 {
  4314. var i = 1;
  4315. var nLVector: Vector3 = this._curve[index].subtract(this._curve[index - i]);
  4316. while (nLVector.length() === 0 && index > i + 1) {
  4317. i++;
  4318. nLVector = this._curve[index].subtract(this._curve[index - i]);
  4319. }
  4320. return nLVector;
  4321. }
  4322. // private function normalVector(v0, vt, va) :
  4323. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  4324. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  4325. private _normalVector(v0: Vector3, vt: Vector3, va: Vector3): Vector3 {
  4326. var normal0: Vector3;
  4327. var tgl = vt.length();
  4328. if (tgl === 0.0) {
  4329. tgl = 1.0;
  4330. }
  4331. if (va === undefined || va === null) {
  4332. var point: Vector3;
  4333. if (!MathTools.WithinEpsilon(Math.abs(vt.y) / tgl, 1.0, Epsilon)) { // search for a point in the plane
  4334. point = new Vector3(0.0, -1.0, 0.0);
  4335. }
  4336. else if (!MathTools.WithinEpsilon(Math.abs(vt.x) / tgl, 1.0, Epsilon)) {
  4337. point = new Vector3(1.0, 0.0, 0.0);
  4338. }
  4339. else if (!MathTools.WithinEpsilon(Math.abs(vt.z) / tgl, 1.0, Epsilon)) {
  4340. point = new Vector3(0.0, 0.0, 1.0);
  4341. }
  4342. normal0 = Vector3.Cross(vt, point);
  4343. }
  4344. else {
  4345. normal0 = Vector3.Cross(vt, va);
  4346. Vector3.CrossToRef(normal0, vt, normal0);
  4347. }
  4348. normal0.normalize();
  4349. return normal0;
  4350. }
  4351. }
  4352. export class Curve3 {
  4353. private _points: Vector3[];
  4354. private _length: number = 0.0;
  4355. /**
  4356. * Returns a Curve3 object along a Quadratic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#quadratic-bezier-curve
  4357. * @param v0 (Vector3) the origin point of the Quadratic Bezier
  4358. * @param v1 (Vector3) the control point
  4359. * @param v2 (Vector3) the end point of the Quadratic Bezier
  4360. * @param nbPoints (integer) the wanted number of points in the curve
  4361. */
  4362. public static CreateQuadraticBezier(v0: Vector3, v1: Vector3, v2: Vector3, nbPoints: number): Curve3 {
  4363. nbPoints = nbPoints > 2 ? nbPoints : 3;
  4364. var bez = new Array<Vector3>();
  4365. var equation = (t: number, val0: number, val1: number, val2: number) => {
  4366. var res = (1.0 - t) * (1.0 - t) * val0 + 2.0 * t * (1.0 - t) * val1 + t * t * val2;
  4367. return res;
  4368. }
  4369. for (var i = 0; i <= nbPoints; i++) {
  4370. 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)));
  4371. }
  4372. return new Curve3(bez);
  4373. }
  4374. /**
  4375. * Returns a Curve3 object along a Cubic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#cubic-bezier-curve
  4376. * @param v0 (Vector3) the origin point of the Cubic Bezier
  4377. * @param v1 (Vector3) the first control point
  4378. * @param v2 (Vector3) the second control point
  4379. * @param v3 (Vector3) the end point of the Cubic Bezier
  4380. * @param nbPoints (integer) the wanted number of points in the curve
  4381. */
  4382. public static CreateCubicBezier(v0: Vector3, v1: Vector3, v2: Vector3, v3: Vector3, nbPoints: number): Curve3 {
  4383. nbPoints = nbPoints > 3 ? nbPoints : 4;
  4384. var bez = new Array<Vector3>();
  4385. var equation = (t: number, val0: number, val1: number, val2: number, val3: number) => {
  4386. 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;
  4387. return res;
  4388. }
  4389. for (var i = 0; i <= nbPoints; i++) {
  4390. 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)));
  4391. }
  4392. return new Curve3(bez);
  4393. }
  4394. /**
  4395. * Returns a Curve3 object along a Hermite Spline curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#hermite-spline
  4396. * @param p1 (Vector3) the origin point of the Hermite Spline
  4397. * @param t1 (Vector3) the tangent vector at the origin point
  4398. * @param p2 (Vector3) the end point of the Hermite Spline
  4399. * @param t2 (Vector3) the tangent vector at the end point
  4400. * @param nbPoints (integer) the wanted number of points in the curve
  4401. */
  4402. public static CreateHermiteSpline(p1: Vector3, t1: Vector3, p2: Vector3, t2: Vector3, nbPoints: number): Curve3 {
  4403. var hermite = new Array<Vector3>();
  4404. var step = 1.0 / nbPoints;
  4405. for (var i = 0; i <= nbPoints; i++) {
  4406. hermite.push(Vector3.Hermite(p1, t1, p2, t2, i * step));
  4407. }
  4408. return new Curve3(hermite);
  4409. }
  4410. /**
  4411. * Returns a Curve3 object along a CatmullRom Spline curve :
  4412. * @param points (array of Vector3) the points the spline must pass through. At least, four points required.
  4413. * @param nbPoints (integer) the wanted number of points between each curve control points.
  4414. */
  4415. public static CreateCatmullRomSpline(points: Vector3[], nbPoints: number): Curve3 {
  4416. var totalPoints = new Array<Vector3>();
  4417. totalPoints.push(points[0].clone());
  4418. Array.prototype.push.apply(totalPoints, points);
  4419. totalPoints.push(points[points.length - 1].clone());
  4420. var catmullRom = new Array<Vector3>();
  4421. var step = 1.0 / nbPoints;
  4422. for (var i = 0; i < totalPoints.length - 3; i++) {
  4423. var amount = 0.0;
  4424. for (var c = 0; c < nbPoints; c++) {
  4425. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  4426. amount += step
  4427. }
  4428. }
  4429. i--;
  4430. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  4431. return new Curve3(catmullRom);
  4432. }
  4433. /**
  4434. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  4435. * A Curve3 is designed from a series of successive Vector3.
  4436. * Tuto : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#curve3-object
  4437. */
  4438. constructor(points: Vector3[]) {
  4439. this._points = points;
  4440. this._length = this._computeLength(points);
  4441. }
  4442. /**
  4443. * Returns the Curve3 stored array of successive Vector3
  4444. */
  4445. public getPoints() {
  4446. return this._points;
  4447. }
  4448. /**
  4449. * Returns the computed length (float) of the curve.
  4450. */
  4451. public length() {
  4452. return this._length;
  4453. }
  4454. /**
  4455. * Returns a new instance of Curve3 object : var curve = curveA.continue(curveB);
  4456. * This new Curve3 is built by translating and sticking the curveB at the end of the curveA.
  4457. * curveA and curveB keep unchanged.
  4458. */
  4459. public continue(curve: Curve3): Curve3 {
  4460. var lastPoint = this._points[this._points.length - 1];
  4461. var continuedPoints = this._points.slice();
  4462. var curvePoints = curve.getPoints();
  4463. for (var i = 1; i < curvePoints.length; i++) {
  4464. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  4465. }
  4466. var continuedCurve = new Curve3(continuedPoints);
  4467. return continuedCurve;
  4468. }
  4469. private _computeLength(path: Vector3[]): number {
  4470. var l = 0;
  4471. for (var i = 1; i < path.length; i++) {
  4472. l += (path[i].subtract(path[i - 1])).length();
  4473. }
  4474. return l;
  4475. }
  4476. }
  4477. // SphericalHarmonics
  4478. export class SphericalHarmonics {
  4479. public L00: Vector3 = Vector3.Zero();
  4480. public L1_1: Vector3 = Vector3.Zero();
  4481. public L10: Vector3 = Vector3.Zero();
  4482. public L11: Vector3 = Vector3.Zero();
  4483. public L2_2: Vector3 = Vector3.Zero();
  4484. public L2_1: Vector3 = Vector3.Zero();
  4485. public L20: Vector3 = Vector3.Zero();
  4486. public L21: Vector3 = Vector3.Zero();
  4487. public L22: Vector3 = Vector3.Zero();
  4488. public addLight(direction: Vector3, color: Color3, deltaSolidAngle: number): void {
  4489. var colorVector = new Vector3(color.r, color.g, color.b);
  4490. var c = colorVector.scale(deltaSolidAngle);
  4491. this.L00 = this.L00.add(c.scale(0.282095));
  4492. this.L1_1 = this.L1_1.add(c.scale(0.488603 * direction.y));
  4493. this.L10 = this.L10.add(c.scale(0.488603 * direction.z));
  4494. this.L11 = this.L11.add(c.scale(0.488603 * direction.x));
  4495. this.L2_2 = this.L2_2.add(c.scale(1.092548 * direction.x * direction.y));
  4496. this.L2_1 = this.L2_1.add(c.scale(1.092548 * direction.y * direction.z));
  4497. this.L21 = this.L21.add(c.scale(1.092548 * direction.x * direction.z));
  4498. this.L20 = this.L20.add(c.scale(0.315392 * (3.0 * direction.z * direction.z - 1.0)));
  4499. this.L22 = this.L22.add(c.scale(0.546274 * (direction.x * direction.x - direction.y * direction.y)));
  4500. }
  4501. public scale(scale: number): void {
  4502. this.L00 = this.L00.scale(scale);
  4503. this.L1_1 = this.L1_1.scale(scale);
  4504. this.L10 = this.L10.scale(scale);
  4505. this.L11 = this.L11.scale(scale);
  4506. this.L2_2 = this.L2_2.scale(scale);
  4507. this.L2_1 = this.L2_1.scale(scale);
  4508. this.L20 = this.L20.scale(scale);
  4509. this.L21 = this.L21.scale(scale);
  4510. this.L22 = this.L22.scale(scale);
  4511. }
  4512. }
  4513. // SphericalPolynomial
  4514. export class SphericalPolynomial {
  4515. public x: Vector3 = Vector3.Zero();
  4516. public y: Vector3 = Vector3.Zero();
  4517. public z: Vector3 = Vector3.Zero();
  4518. public xx: Vector3 = Vector3.Zero();
  4519. public yy: Vector3 = Vector3.Zero();
  4520. public zz: Vector3 = Vector3.Zero();
  4521. public xy: Vector3 = Vector3.Zero();
  4522. public yz: Vector3 = Vector3.Zero();
  4523. public zx: Vector3 = Vector3.Zero();
  4524. public addAmbient(color: Color3): void {
  4525. var colorVector = new Vector3(color.r, color.g, color.b);
  4526. this.xx = this.xx.add(colorVector);
  4527. this.yy = this.yy.add(colorVector);
  4528. this.zz = this.zz.add(colorVector);
  4529. }
  4530. public static getSphericalPolynomialFromHarmonics(harmonics: SphericalHarmonics): SphericalPolynomial {
  4531. var result = new SphericalPolynomial();
  4532. result.x = harmonics.L11.scale(1.02333);
  4533. result.y = harmonics.L1_1.scale(1.02333);
  4534. result.z = harmonics.L10.scale(1.02333);
  4535. result.xx = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).add(harmonics.L22.scale(0.429043));
  4536. result.yy = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).subtract(harmonics.L22.scale(0.429043));
  4537. result.zz = harmonics.L00.scale(0.886277).add(harmonics.L20.scale(0.495417));
  4538. result.yz = harmonics.L2_1.scale(0.858086);
  4539. result.zx = harmonics.L21.scale(0.858086);
  4540. result.xy = harmonics.L2_2.scale(0.858086);
  4541. return result;
  4542. }
  4543. }
  4544. // Vertex formats
  4545. export class PositionNormalVertex {
  4546. constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up()) {
  4547. }
  4548. public clone(): PositionNormalVertex {
  4549. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  4550. }
  4551. }
  4552. export class PositionNormalTextureVertex {
  4553. constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up(), public uv: Vector2 = Vector2.Zero()) {
  4554. }
  4555. public clone(): PositionNormalTextureVertex {
  4556. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  4557. }
  4558. }
  4559. // Temporary pre-allocated objects for engine internal use
  4560. // usage in any internal function :
  4561. // var tmp = Tmp.Vector3[0]; <= gets access to the first pre-created Vector3
  4562. // There's a Tmp array per object type : int, float, Vector2, Vector3, Vector4, Quaternion, Matrix
  4563. export class Tmp {
  4564. public static Color3: Color3[] = [Color3.Black(), Color3.Black(), Color3.Black()];
  4565. public static Vector2: Vector2[] = [Vector2.Zero(), Vector2.Zero(), Vector2.Zero()]; // 3 temp Vector2 at once should be enough
  4566. public static Vector3: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(),
  4567. Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()]; // 9 temp Vector3 at once should be enough
  4568. public static Vector4: Vector4[] = [Vector4.Zero(), Vector4.Zero(), Vector4.Zero()]; // 3 temp Vector4 at once should be enough
  4569. public static Quaternion: Quaternion[] = [Quaternion.Zero(), Quaternion.Zero()]; // 2 temp Quaternion at once should be enough
  4570. public static Matrix: Matrix[] = [Matrix.Zero(), Matrix.Zero(),
  4571. Matrix.Zero(), Matrix.Zero(),
  4572. Matrix.Zero(), Matrix.Zero(),
  4573. Matrix.Zero(), Matrix.Zero()]; // 6 temp Matrices at once should be enough
  4574. }
  4575. // Same as Tmp but not exported to keep it onyl for math functions to avoid conflicts
  4576. class MathTmp {
  4577. public static Vector3: Vector3[] = [Vector3.Zero()];
  4578. public static Matrix: Matrix[] = [Matrix.Zero(), Matrix.Zero()];
  4579. public static Quaternion: Quaternion[] = [Quaternion.Zero()];
  4580. }
  4581. }