babylon.math.ts 302 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386
  1. module BABYLON {
  2. /**
  3. * Constant used to convert a value to gamma space
  4. * @ignorenaming
  5. */
  6. export const ToGammaSpace = 1 / 2.2;
  7. /**
  8. * Constant used to convert a value to linear space
  9. * @ignorenaming
  10. */
  11. export const ToLinearSpace = 2.2;
  12. /**
  13. * Constant used to define the minimal number value in Babylon.js
  14. * @ignorenaming
  15. */
  16. export const Epsilon = 0.001;
  17. /**
  18. * Class used to hold a RBG color
  19. */
  20. export class Color3 {
  21. /**
  22. * Creates a new Color3 object from red, green, blue values, all between 0 and 1
  23. * @param r defines the red component (between 0 and 1, default is 0)
  24. * @param g defines the green component (between 0 and 1, default is 0)
  25. * @param b defines the blue component (between 0 and 1, default is 0)
  26. */
  27. constructor(
  28. /**
  29. * Defines the red component (between 0 and 1, default is 0)
  30. */
  31. public r: number = 0,
  32. /**
  33. * Defines the green component (between 0 and 1, default is 0)
  34. */
  35. public g: number = 0,
  36. /**
  37. * Defines the blue component (between 0 and 1, default is 0)
  38. */
  39. public b: number = 0) {
  40. }
  41. /**
  42. * Creates a string with the Color3 current values
  43. * @returns the string representation of the Color3 object
  44. */
  45. public toString(): string {
  46. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  47. }
  48. /**
  49. * Returns the string "Color3"
  50. * @returns "Color3"
  51. */
  52. public getClassName(): string {
  53. return "Color3";
  54. }
  55. /**
  56. * Compute the Color3 hash code
  57. * @returns an unique number that can be used to hash Color3 objects
  58. */
  59. public getHashCode(): number {
  60. let hash = this.r || 0;
  61. hash = (hash * 397) ^ (this.g || 0);
  62. hash = (hash * 397) ^ (this.b || 0);
  63. return hash;
  64. }
  65. // Operators
  66. /**
  67. * Stores in the given array from the given starting index the red, green, blue values as successive elements
  68. * @param array defines the array where to store the r,g,b components
  69. * @param index defines an optional index in the target array to define where to start storing values
  70. * @returns the current Color3 object
  71. */
  72. public toArray(array: FloatArray, index: number = 0): Color3 {
  73. array[index] = this.r;
  74. array[index + 1] = this.g;
  75. array[index + 2] = this.b;
  76. return this;
  77. }
  78. /**
  79. * Returns a new Color4 object from the current Color3 and the given alpha
  80. * @param alpha defines the alpha component on the new Color4 object (default is 1)
  81. * @returns a new Color4 object
  82. */
  83. public toColor4(alpha: number = 1): Color4 {
  84. return new Color4(this.r, this.g, this.b, alpha);
  85. }
  86. /**
  87. * Returns a new array populated with 3 numeric elements : red, green and blue values
  88. * @returns the new array
  89. */
  90. public asArray(): number[] {
  91. var result = new Array<number>();
  92. this.toArray(result, 0);
  93. return result;
  94. }
  95. /**
  96. * Returns the luminance value
  97. * @returns a float value
  98. */
  99. public toLuminance(): number {
  100. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  101. }
  102. /**
  103. * Multiply each Color3 rgb values by the given Color3 rgb values in a new Color3 object
  104. * @param otherColor defines the second operand
  105. * @returns the new Color3 object
  106. */
  107. public multiply(otherColor: DeepImmutable<Color3>): Color3 {
  108. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  109. }
  110. /**
  111. * Multiply the rgb values of the Color3 and the given Color3 and stores the result in the object "result"
  112. * @param otherColor defines the second operand
  113. * @param result defines the Color3 object where to store the result
  114. * @returns the current Color3
  115. */
  116. public multiplyToRef(otherColor: DeepImmutable<Color3>, result: Color3): Color3 {
  117. result.r = this.r * otherColor.r;
  118. result.g = this.g * otherColor.g;
  119. result.b = this.b * otherColor.b;
  120. return this;
  121. }
  122. /**
  123. * Determines equality between Color3 objects
  124. * @param otherColor defines the second operand
  125. * @returns true if the rgb values are equal to the given ones
  126. */
  127. public equals(otherColor: DeepImmutable<Color3>): boolean {
  128. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  129. }
  130. /**
  131. * Determines equality between the current Color3 object and a set of r,b,g values
  132. * @param r defines the red component to check
  133. * @param g defines the green component to check
  134. * @param b defines the blue component to check
  135. * @returns true if the rgb values are equal to the given ones
  136. */
  137. public equalsFloats(r: number, g: number, b: number): boolean {
  138. return this.r === r && this.g === g && this.b === b;
  139. }
  140. /**
  141. * Multiplies in place each rgb value by scale
  142. * @param scale defines the scaling factor
  143. * @returns the updated Color3
  144. */
  145. public scale(scale: number): Color3 {
  146. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  147. }
  148. /**
  149. * Multiplies the rgb values by scale and stores the result into "result"
  150. * @param scale defines the scaling factor
  151. * @param result defines the Color3 object where to store the result
  152. * @returns the unmodified current Color3
  153. */
  154. public scaleToRef(scale: number, result: Color3): Color3 {
  155. result.r = this.r * scale;
  156. result.g = this.g * scale;
  157. result.b = this.b * scale;
  158. return this;
  159. }
  160. /**
  161. * Scale the current Color3 values by a factor and add the result to a given Color3
  162. * @param scale defines the scale factor
  163. * @param result defines color to store the result into
  164. * @returns the unmodified current Color3
  165. */
  166. public scaleAndAddToRef(scale: number, result: Color3): Color3 {
  167. result.r += this.r * scale;
  168. result.g += this.g * scale;
  169. result.b += this.b * scale;
  170. return this;
  171. }
  172. /**
  173. * Clamps the rgb values by the min and max values and stores the result into "result"
  174. * @param min defines minimum clamping value (default is 0)
  175. * @param max defines maximum clamping value (default is 1)
  176. * @param result defines color to store the result into
  177. * @returns the original Color3
  178. */
  179. public clampToRef(min: number = 0, max: number = 1, result: Color3): Color3 {
  180. result.r = Scalar.Clamp(this.r, min, max);
  181. result.g = Scalar.Clamp(this.g, min, max);
  182. result.b = Scalar.Clamp(this.b, min, max);
  183. return this;
  184. }
  185. /**
  186. * Creates a new Color3 set with the added values of the current Color3 and of the given one
  187. * @param otherColor defines the second operand
  188. * @returns the new Color3
  189. */
  190. public add(otherColor: DeepImmutable<Color3>): Color3 {
  191. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  192. }
  193. /**
  194. * Stores the result of the addition of the current Color3 and given one rgb values into "result"
  195. * @param otherColor defines the second operand
  196. * @param result defines Color3 object to store the result into
  197. * @returns the unmodified current Color3
  198. */
  199. public addToRef(otherColor: DeepImmutable<Color3>, result: Color3): Color3 {
  200. result.r = this.r + otherColor.r;
  201. result.g = this.g + otherColor.g;
  202. result.b = this.b + otherColor.b;
  203. return this;
  204. }
  205. /**
  206. * Returns a new Color3 set with the subtracted values of the given one from the current Color3
  207. * @param otherColor defines the second operand
  208. * @returns the new Color3
  209. */
  210. public subtract(otherColor: DeepImmutable<Color3>): Color3 {
  211. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  212. }
  213. /**
  214. * Stores the result of the subtraction of given one from the current Color3 rgb values into "result"
  215. * @param otherColor defines the second operand
  216. * @param result defines Color3 object to store the result into
  217. * @returns the unmodified current Color3
  218. */
  219. public subtractToRef(otherColor: DeepImmutable<Color3>, result: Color3): Color3 {
  220. result.r = this.r - otherColor.r;
  221. result.g = this.g - otherColor.g;
  222. result.b = this.b - otherColor.b;
  223. return this;
  224. }
  225. /**
  226. * Copy the current object
  227. * @returns a new Color3 copied the current one
  228. */
  229. public clone(): Color3 {
  230. return new Color3(this.r, this.g, this.b);
  231. }
  232. /**
  233. * Copies the rgb values from the source in the current Color3
  234. * @param source defines the source Color3 object
  235. * @returns the updated Color3 object
  236. */
  237. public copyFrom(source: DeepImmutable<Color3>): Color3 {
  238. this.r = source.r;
  239. this.g = source.g;
  240. this.b = source.b;
  241. return this;
  242. }
  243. /**
  244. * Updates the Color3 rgb values from the given floats
  245. * @param r defines the red component to read from
  246. * @param g defines the green component to read from
  247. * @param b defines the blue component to read from
  248. * @returns the current Color3 object
  249. */
  250. public copyFromFloats(r: number, g: number, b: number): Color3 {
  251. this.r = r;
  252. this.g = g;
  253. this.b = b;
  254. return this;
  255. }
  256. /**
  257. * Updates the Color3 rgb values from the given floats
  258. * @param r defines the red component to read from
  259. * @param g defines the green component to read from
  260. * @param b defines the blue component to read from
  261. * @returns the current Color3 object
  262. */
  263. public set(r: number, g: number, b: number): Color3 {
  264. return this.copyFromFloats(r, g, b);
  265. }
  266. /**
  267. * Compute the Color3 hexadecimal code as a string
  268. * @returns a string containing the hexadecimal representation of the Color3 object
  269. */
  270. public toHexString(): string {
  271. var intR = (this.r * 255) | 0;
  272. var intG = (this.g * 255) | 0;
  273. var intB = (this.b * 255) | 0;
  274. return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB);
  275. }
  276. /**
  277. * Computes a new Color3 converted from the current one to linear space
  278. * @returns a new Color3 object
  279. */
  280. public toLinearSpace(): Color3 {
  281. var convertedColor = new Color3();
  282. this.toLinearSpaceToRef(convertedColor);
  283. return convertedColor;
  284. }
  285. /**
  286. * Converts the Color3 values to linear space and stores the result in "convertedColor"
  287. * @param convertedColor defines the Color3 object where to store the linear space version
  288. * @returns the unmodified Color3
  289. */
  290. public toLinearSpaceToRef(convertedColor: Color3): Color3 {
  291. convertedColor.r = Math.pow(this.r, ToLinearSpace);
  292. convertedColor.g = Math.pow(this.g, ToLinearSpace);
  293. convertedColor.b = Math.pow(this.b, ToLinearSpace);
  294. return this;
  295. }
  296. /**
  297. * Computes a new Color3 converted from the current one to gamma space
  298. * @returns a new Color3 object
  299. */
  300. public toGammaSpace(): Color3 {
  301. var convertedColor = new Color3();
  302. this.toGammaSpaceToRef(convertedColor);
  303. return convertedColor;
  304. }
  305. /**
  306. * Converts the Color3 values to gamma space and stores the result in "convertedColor"
  307. * @param convertedColor defines the Color3 object where to store the gamma space version
  308. * @returns the unmodified Color3
  309. */
  310. public toGammaSpaceToRef(convertedColor: Color3): Color3 {
  311. convertedColor.r = Math.pow(this.r, ToGammaSpace);
  312. convertedColor.g = Math.pow(this.g, ToGammaSpace);
  313. convertedColor.b = Math.pow(this.b, ToGammaSpace);
  314. return this;
  315. }
  316. // Statics
  317. /**
  318. * Creates a new Color3 from the string containing valid hexadecimal values
  319. * @param hex defines a string containing valid hexadecimal values
  320. * @returns a new Color3 object
  321. */
  322. public static FromHexString(hex: string): Color3 {
  323. if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
  324. return new Color3(0, 0, 0);
  325. }
  326. var r = parseInt(hex.substring(1, 3), 16);
  327. var g = parseInt(hex.substring(3, 5), 16);
  328. var b = parseInt(hex.substring(5, 7), 16);
  329. return Color3.FromInts(r, g, b);
  330. }
  331. /**
  332. * Creates a new Vector3 from the starting index of the given array
  333. * @param array defines the source array
  334. * @param offset defines an offset in the source array
  335. * @returns a new Color3 object
  336. */
  337. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Color3 {
  338. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  339. }
  340. /**
  341. * Creates a new Color3 from integer values (< 256)
  342. * @param r defines the red component to read from (value between 0 and 255)
  343. * @param g defines the green component to read from (value between 0 and 255)
  344. * @param b defines the blue component to read from (value between 0 and 255)
  345. * @returns a new Color3 object
  346. */
  347. public static FromInts(r: number, g: number, b: number): Color3 {
  348. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  349. }
  350. /**
  351. * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
  352. * @param start defines the start Color3 value
  353. * @param end defines the end Color3 value
  354. * @param amount defines the gradient value between start and end
  355. * @returns a new Color3 object
  356. */
  357. public static Lerp(start: DeepImmutable<Color3>, end: DeepImmutable<Color3>, amount: number): Color3 {
  358. var result = new Color3(0.0, 0.0, 0.0);
  359. Color3.LerpToRef(start, end, amount, result);
  360. return result;
  361. }
  362. /**
  363. * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
  364. * @param left defines the start value
  365. * @param right defines the end value
  366. * @param amount defines the gradient factor
  367. * @param result defines the Color3 object where to store the result
  368. */
  369. public static LerpToRef(left: DeepImmutable<Color3>, right: DeepImmutable<Color3>, amount: number, result: Color3): void {
  370. result.r = left.r + ((right.r - left.r) * amount);
  371. result.g = left.g + ((right.g - left.g) * amount);
  372. result.b = left.b + ((right.b - left.b) * amount);
  373. }
  374. /**
  375. * Returns a Color3 value containing a red color
  376. * @returns a new Color3 object
  377. */
  378. public static Red(): Color3 { return new Color3(1, 0, 0); }
  379. /**
  380. * Returns a Color3 value containing a green color
  381. * @returns a new Color3 object
  382. */
  383. public static Green(): Color3 { return new Color3(0, 1, 0); }
  384. /**
  385. * Returns a Color3 value containing a blue color
  386. * @returns a new Color3 object
  387. */
  388. public static Blue(): Color3 { return new Color3(0, 0, 1); }
  389. /**
  390. * Returns a Color3 value containing a black color
  391. * @returns a new Color3 object
  392. */
  393. public static Black(): Color3 { return new Color3(0, 0, 0); }
  394. /**
  395. * Returns a Color3 value containing a white color
  396. * @returns a new Color3 object
  397. */
  398. public static White(): Color3 { return new Color3(1, 1, 1); }
  399. /**
  400. * Returns a Color3 value containing a purple color
  401. * @returns a new Color3 object
  402. */
  403. public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
  404. /**
  405. * Returns a Color3 value containing a magenta color
  406. * @returns a new Color3 object
  407. */
  408. public static Magenta(): Color3 { return new Color3(1, 0, 1); }
  409. /**
  410. * Returns a Color3 value containing a yellow color
  411. * @returns a new Color3 object
  412. */
  413. public static Yellow(): Color3 { return new Color3(1, 1, 0); }
  414. /**
  415. * Returns a Color3 value containing a gray color
  416. * @returns a new Color3 object
  417. */
  418. public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
  419. /**
  420. * Returns a Color3 value containing a teal color
  421. * @returns a new Color3 object
  422. */
  423. public static Teal(): Color3 { return new Color3(0, 1.0, 1.0); }
  424. /**
  425. * Returns a Color3 value containing a random color
  426. * @returns a new Color3 object
  427. */
  428. public static Random(): Color3 { return new Color3(Math.random(), Math.random(), Math.random()); }
  429. }
  430. /**
  431. * Class used to hold a RBGA color
  432. */
  433. export class Color4 {
  434. /**
  435. * Creates a new Color4 object from red, green, blue values, all between 0 and 1
  436. * @param r defines the red component (between 0 and 1, default is 0)
  437. * @param g defines the green component (between 0 and 1, default is 0)
  438. * @param b defines the blue component (between 0 and 1, default is 0)
  439. * @param a defines the alpha component (between 0 and 1, default is 1)
  440. */
  441. constructor(
  442. /**
  443. * Defines the red component (between 0 and 1, default is 0)
  444. */
  445. public r: number = 0,
  446. /**
  447. * Defines the green component (between 0 and 1, default is 0)
  448. */
  449. public g: number = 0,
  450. /**
  451. * Defines the blue component (between 0 and 1, default is 0)
  452. */
  453. public b: number = 0,
  454. /**
  455. * Defines the alpha component (between 0 and 1, default is 1)
  456. */
  457. public a: number = 1) {
  458. }
  459. // Operators
  460. /**
  461. * Adds in place the given Color4 values to the current Color4 object
  462. * @param right defines the second operand
  463. * @returns the current updated Color4 object
  464. */
  465. public addInPlace(right: DeepImmutable<Color4>): Color4 {
  466. this.r += right.r;
  467. this.g += right.g;
  468. this.b += right.b;
  469. this.a += right.a;
  470. return this;
  471. }
  472. /**
  473. * Creates a new array populated with 4 numeric elements : red, green, blue, alpha values
  474. * @returns the new array
  475. */
  476. public asArray(): number[] {
  477. var result = new Array<number>();
  478. this.toArray(result, 0);
  479. return result;
  480. }
  481. /**
  482. * Stores from the starting index in the given array the Color4 successive values
  483. * @param array defines the array where to store the r,g,b components
  484. * @param index defines an optional index in the target array to define where to start storing values
  485. * @returns the current Color4 object
  486. */
  487. public toArray(array: number[], index: number = 0): Color4 {
  488. array[index] = this.r;
  489. array[index + 1] = this.g;
  490. array[index + 2] = this.b;
  491. array[index + 3] = this.a;
  492. return this;
  493. }
  494. /**
  495. * Determines equality between Color4 objects
  496. * @param otherColor defines the second operand
  497. * @returns true if the rgba values are equal to the given ones
  498. */
  499. public equals(otherColor: DeepImmutable<Color4>): boolean {
  500. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b && this.a === otherColor.a;
  501. }
  502. /**
  503. * Creates a new Color4 set with the added values of the current Color4 and of the given one
  504. * @param right defines the second operand
  505. * @returns a new Color4 object
  506. */
  507. public add(right: DeepImmutable<Color4>): Color4 {
  508. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  509. }
  510. /**
  511. * Creates a new Color4 set with the subtracted values of the given one from the current Color4
  512. * @param right defines the second operand
  513. * @returns a new Color4 object
  514. */
  515. public subtract(right: DeepImmutable<Color4>): Color4 {
  516. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  517. }
  518. /**
  519. * Subtracts the given ones from the current Color4 values and stores the results in "result"
  520. * @param right defines the second operand
  521. * @param result defines the Color4 object where to store the result
  522. * @returns the current Color4 object
  523. */
  524. public subtractToRef(right: DeepImmutable<Color4>, result: Color4): Color4 {
  525. result.r = this.r - right.r;
  526. result.g = this.g - right.g;
  527. result.b = this.b - right.b;
  528. result.a = this.a - right.a;
  529. return this;
  530. }
  531. /**
  532. * Creates a new Color4 with the current Color4 values multiplied by scale
  533. * @param scale defines the scaling factor to apply
  534. * @returns a new Color4 object
  535. */
  536. public scale(scale: number): Color4 {
  537. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  538. }
  539. /**
  540. * Multiplies the current Color4 values by scale and stores the result in "result"
  541. * @param scale defines the scaling factor to apply
  542. * @param result defines the Color4 object where to store the result
  543. * @returns the current unmodified Color4
  544. */
  545. public scaleToRef(scale: number, result: Color4): Color4 {
  546. result.r = this.r * scale;
  547. result.g = this.g * scale;
  548. result.b = this.b * scale;
  549. result.a = this.a * scale;
  550. return this;
  551. }
  552. /**
  553. * Scale the current Color4 values by a factor and add the result to a given Color4
  554. * @param scale defines the scale factor
  555. * @param result defines the Color4 object where to store the result
  556. * @returns the unmodified current Color4
  557. */
  558. public scaleAndAddToRef(scale: number, result: Color4): Color4 {
  559. result.r += this.r * scale;
  560. result.g += this.g * scale;
  561. result.b += this.b * scale;
  562. result.a += this.a * scale;
  563. return this;
  564. }
  565. /**
  566. * Clamps the rgb values by the min and max values and stores the result into "result"
  567. * @param min defines minimum clamping value (default is 0)
  568. * @param max defines maximum clamping value (default is 1)
  569. * @param result defines color to store the result into.
  570. * @returns the cuurent Color4
  571. */
  572. public clampToRef(min: number = 0, max: number = 1, result: Color4): Color4 {
  573. result.r = Scalar.Clamp(this.r, min, max);
  574. result.g = Scalar.Clamp(this.g, min, max);
  575. result.b = Scalar.Clamp(this.b, min, max);
  576. result.a = Scalar.Clamp(this.a, min, max);
  577. return this;
  578. }
  579. /**
  580. * Multipy an Color4 value by another and return a new Color4 object
  581. * @param color defines the Color4 value to multiply by
  582. * @returns a new Color4 object
  583. */
  584. public multiply(color: Color4): Color4 {
  585. return new Color4(this.r * color.r, this.g * color.g, this.b * color.b, this.a * color.a);
  586. }
  587. /**
  588. * Multipy a Color4 value by another and push the result in a reference value
  589. * @param color defines the Color4 value to multiply by
  590. * @param result defines the Color4 to fill the result in
  591. * @returns the result Color4
  592. */
  593. public multiplyToRef(color: Color4, result: Color4): Color4 {
  594. result.r = this.r * color.r;
  595. result.g = this.g * color.g;
  596. result.b = this.b * color.b;
  597. result.a = this.a * color.a;
  598. return result;
  599. }
  600. /**
  601. * Creates a string with the Color4 current values
  602. * @returns the string representation of the Color4 object
  603. */
  604. public toString(): string {
  605. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  606. }
  607. /**
  608. * Returns the string "Color4"
  609. * @returns "Color4"
  610. */
  611. public getClassName(): string {
  612. return "Color4";
  613. }
  614. /**
  615. * Compute the Color4 hash code
  616. * @returns an unique number that can be used to hash Color4 objects
  617. */
  618. public getHashCode(): number {
  619. let hash = this.r || 0;
  620. hash = (hash * 397) ^ (this.g || 0);
  621. hash = (hash * 397) ^ (this.b || 0);
  622. hash = (hash * 397) ^ (this.a || 0);
  623. return hash;
  624. }
  625. /**
  626. * Creates a new Color4 copied from the current one
  627. * @returns a new Color4 object
  628. */
  629. public clone(): Color4 {
  630. return new Color4(this.r, this.g, this.b, this.a);
  631. }
  632. /**
  633. * Copies the given Color4 values into the current one
  634. * @param source defines the source Color4 object
  635. * @returns the current updated Color4 object
  636. */
  637. public copyFrom(source: Color4): Color4 {
  638. this.r = source.r;
  639. this.g = source.g;
  640. this.b = source.b;
  641. this.a = source.a;
  642. return this;
  643. }
  644. /**
  645. * Copies the given float values into the current one
  646. * @param r defines the red component to read from
  647. * @param g defines the green component to read from
  648. * @param b defines the blue component to read from
  649. * @param a defines the alpha component to read from
  650. * @returns the current updated Color4 object
  651. */
  652. public copyFromFloats(r: number, g: number, b: number, a: number): Color4 {
  653. this.r = r;
  654. this.g = g;
  655. this.b = b;
  656. this.a = a;
  657. return this;
  658. }
  659. /**
  660. * Copies the given float values into the current one
  661. * @param r defines the red component to read from
  662. * @param g defines the green component to read from
  663. * @param b defines the blue component to read from
  664. * @param a defines the alpha component to read from
  665. * @returns the current updated Color4 object
  666. */
  667. public set(r: number, g: number, b: number, a: number): Color4 {
  668. return this.copyFromFloats(r, g, b, a);
  669. }
  670. /**
  671. * Compute the Color4 hexadecimal code as a string
  672. * @returns a string containing the hexadecimal representation of the Color4 object
  673. */
  674. public toHexString(): string {
  675. var intR = (this.r * 255) | 0;
  676. var intG = (this.g * 255) | 0;
  677. var intB = (this.b * 255) | 0;
  678. var intA = (this.a * 255) | 0;
  679. return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB) + Scalar.ToHex(intA);
  680. }
  681. /**
  682. * Computes a new Color4 converted from the current one to linear space
  683. * @returns a new Color4 object
  684. */
  685. public toLinearSpace(): Color4 {
  686. var convertedColor = new Color4();
  687. this.toLinearSpaceToRef(convertedColor);
  688. return convertedColor;
  689. }
  690. /**
  691. * Converts the Color4 values to linear space and stores the result in "convertedColor"
  692. * @param convertedColor defines the Color4 object where to store the linear space version
  693. * @returns the unmodified Color4
  694. */
  695. public toLinearSpaceToRef(convertedColor: Color4): Color4 {
  696. convertedColor.r = Math.pow(this.r, ToLinearSpace);
  697. convertedColor.g = Math.pow(this.g, ToLinearSpace);
  698. convertedColor.b = Math.pow(this.b, ToLinearSpace);
  699. convertedColor.a = this.a;
  700. return this;
  701. }
  702. /**
  703. * Computes a new Color4 converted from the current one to gamma space
  704. * @returns a new Color4 object
  705. */
  706. public toGammaSpace(): Color4 {
  707. var convertedColor = new Color4();
  708. this.toGammaSpaceToRef(convertedColor);
  709. return convertedColor;
  710. }
  711. /**
  712. * Converts the Color4 values to gamma space and stores the result in "convertedColor"
  713. * @param convertedColor defines the Color4 object where to store the gamma space version
  714. * @returns the unmodified Color4
  715. */
  716. public toGammaSpaceToRef(convertedColor: Color4): Color4 {
  717. convertedColor.r = Math.pow(this.r, ToGammaSpace);
  718. convertedColor.g = Math.pow(this.g, ToGammaSpace);
  719. convertedColor.b = Math.pow(this.b, ToGammaSpace);
  720. convertedColor.a = this.a;
  721. return this;
  722. }
  723. // Statics
  724. /**
  725. * Creates a new Color4 from the string containing valid hexadecimal values
  726. * @param hex defines a string containing valid hexadecimal values
  727. * @returns a new Color4 object
  728. */
  729. public static FromHexString(hex: string): Color4 {
  730. if (hex.substring(0, 1) !== "#" || hex.length !== 9) {
  731. return new Color4(0.0, 0.0, 0.0, 0.0);
  732. }
  733. var r = parseInt(hex.substring(1, 3), 16);
  734. var g = parseInt(hex.substring(3, 5), 16);
  735. var b = parseInt(hex.substring(5, 7), 16);
  736. var a = parseInt(hex.substring(7, 9), 16);
  737. return Color4.FromInts(r, g, b, a);
  738. }
  739. /**
  740. * Creates a new Color4 object set with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
  741. * @param left defines the start value
  742. * @param right defines the end value
  743. * @param amount defines the gradient factor
  744. * @returns a new Color4 object
  745. */
  746. public static Lerp(left: DeepImmutable<Color4>, right: DeepImmutable<Color4>, amount: number): Color4 {
  747. var result = new Color4(0.0, 0.0, 0.0, 0.0);
  748. Color4.LerpToRef(left, right, amount, result);
  749. return result;
  750. }
  751. /**
  752. * Set the given "result" with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
  753. * @param left defines the start value
  754. * @param right defines the end value
  755. * @param amount defines the gradient factor
  756. * @param result defines the Color4 object where to store data
  757. */
  758. public static LerpToRef(left: DeepImmutable<Color4>, right: DeepImmutable<Color4>, amount: number, result: Color4): void {
  759. result.r = left.r + (right.r - left.r) * amount;
  760. result.g = left.g + (right.g - left.g) * amount;
  761. result.b = left.b + (right.b - left.b) * amount;
  762. result.a = left.a + (right.a - left.a) * amount;
  763. }
  764. /**
  765. * Creates a new Color4 from a Color3 and an alpha value
  766. * @param color3 defines the source Color3 to read from
  767. * @param alpha defines the alpha component (1.0 by default)
  768. * @returns a new Color4 object
  769. */
  770. public static FromColor3(color3: DeepImmutable<Color3>, alpha: number = 1.0): Color4 {
  771. return new Color4(color3.r, color3.g, color3.b, alpha);
  772. }
  773. /**
  774. * Creates a new Color4 from the starting index element of the given array
  775. * @param array defines the source array to read from
  776. * @param offset defines the offset in the source array
  777. * @returns a new Color4 object
  778. */
  779. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Color4 {
  780. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  781. }
  782. /**
  783. * Creates a new Color3 from integer values (< 256)
  784. * @param r defines the red component to read from (value between 0 and 255)
  785. * @param g defines the green component to read from (value between 0 and 255)
  786. * @param b defines the blue component to read from (value between 0 and 255)
  787. * @param a defines the alpha component to read from (value between 0 and 255)
  788. * @returns a new Color3 object
  789. */
  790. public static FromInts(r: number, g: number, b: number, a: number): Color4 {
  791. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  792. }
  793. /**
  794. * Check the content of a given array and convert it to an array containing RGBA data
  795. * If the original array was already containing count * 4 values then it is returned directly
  796. * @param colors defines the array to check
  797. * @param count defines the number of RGBA data to expect
  798. * @returns an array containing count * 4 values (RGBA)
  799. */
  800. public static CheckColors4(colors: number[], count: number): number[] {
  801. // Check if color3 was used
  802. if (colors.length === count * 3) {
  803. var colors4 = [];
  804. for (var index = 0; index < colors.length; index += 3) {
  805. var newIndex = (index / 3) * 4;
  806. colors4[newIndex] = colors[index];
  807. colors4[newIndex + 1] = colors[index + 1];
  808. colors4[newIndex + 2] = colors[index + 2];
  809. colors4[newIndex + 3] = 1.0;
  810. }
  811. return colors4;
  812. }
  813. return colors;
  814. }
  815. }
  816. /**
  817. * Class representing a vector containing 2 coordinates
  818. */
  819. export class Vector2 {
  820. /**
  821. * Creates a new Vector2 from the given x and y coordinates
  822. * @param x defines the first coordinate
  823. * @param y defines the second coordinate
  824. */
  825. constructor(
  826. /** defines the first coordinate */
  827. public x: number = 0,
  828. /** defines the second coordinate */
  829. public y: number = 0) {
  830. }
  831. /**
  832. * Gets a string with the Vector2 coordinates
  833. * @returns a string with the Vector2 coordinates
  834. */
  835. public toString(): string {
  836. return "{X: " + this.x + " Y:" + this.y + "}";
  837. }
  838. /**
  839. * Gets class name
  840. * @returns the string "Vector2"
  841. */
  842. public getClassName(): string {
  843. return "Vector2";
  844. }
  845. /**
  846. * Gets current vector hash code
  847. * @returns the Vector2 hash code as a number
  848. */
  849. public getHashCode(): number {
  850. let hash = this.x || 0;
  851. hash = (hash * 397) ^ (this.y || 0);
  852. return hash;
  853. }
  854. // Operators
  855. /**
  856. * Sets the Vector2 coordinates in the given array or Float32Array from the given index.
  857. * @param array defines the source array
  858. * @param index defines the offset in source array
  859. * @returns the current Vector2
  860. */
  861. public toArray(array: FloatArray, index: number = 0): Vector2 {
  862. array[index] = this.x;
  863. array[index + 1] = this.y;
  864. return this;
  865. }
  866. /**
  867. * Copy the current vector to an array
  868. * @returns a new array with 2 elements: the Vector2 coordinates.
  869. */
  870. public asArray(): number[] {
  871. var result = new Array<number>();
  872. this.toArray(result, 0);
  873. return result;
  874. }
  875. /**
  876. * Sets the Vector2 coordinates with the given Vector2 coordinates
  877. * @param source defines the source Vector2
  878. * @returns the current updated Vector2
  879. */
  880. public copyFrom(source: DeepImmutable<Vector2>): Vector2 {
  881. this.x = source.x;
  882. this.y = source.y;
  883. return this;
  884. }
  885. /**
  886. * Sets the Vector2 coordinates with the given floats
  887. * @param x defines the first coordinate
  888. * @param y defines the second coordinate
  889. * @returns the current updated Vector2
  890. */
  891. public copyFromFloats(x: number, y: number): Vector2 {
  892. this.x = x;
  893. this.y = y;
  894. return this;
  895. }
  896. /**
  897. * Sets the Vector2 coordinates with the given floats
  898. * @param x defines the first coordinate
  899. * @param y defines the second coordinate
  900. * @returns the current updated Vector2
  901. */
  902. public set(x: number, y: number): Vector2 {
  903. return this.copyFromFloats(x, y);
  904. }
  905. /**
  906. * Add another vector with the current one
  907. * @param otherVector defines the other vector
  908. * @returns a new Vector2 set with the addition of the current Vector2 and the given one coordinates
  909. */
  910. public add(otherVector: DeepImmutable<Vector2>): Vector2 {
  911. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  912. }
  913. /**
  914. * Sets the "result" coordinates with the addition of the current Vector2 and the given one coordinates
  915. * @param otherVector defines the other vector
  916. * @param result defines the target vector
  917. * @returns the unmodified current Vector2
  918. */
  919. public addToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  920. result.x = this.x + otherVector.x;
  921. result.y = this.y + otherVector.y;
  922. return this;
  923. }
  924. /**
  925. * Set the Vector2 coordinates by adding the given Vector2 coordinates
  926. * @param otherVector defines the other vector
  927. * @returns the current updated Vector2
  928. */
  929. public addInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  930. this.x += otherVector.x;
  931. this.y += otherVector.y;
  932. return this;
  933. }
  934. /**
  935. * Gets a new Vector2 by adding the current Vector2 coordinates to the given Vector3 x, y coordinates
  936. * @param otherVector defines the other vector
  937. * @returns a new Vector2
  938. */
  939. public addVector3(otherVector: Vector3): Vector2 {
  940. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  941. }
  942. /**
  943. * Gets a new Vector2 set with the subtracted coordinates of the given one from the current Vector2
  944. * @param otherVector defines the other vector
  945. * @returns a new Vector2
  946. */
  947. public subtract(otherVector: Vector2): Vector2 {
  948. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  949. }
  950. /**
  951. * Sets the "result" coordinates with the subtraction of the given one from the current Vector2 coordinates.
  952. * @param otherVector defines the other vector
  953. * @param result defines the target vector
  954. * @returns the unmodified current Vector2
  955. */
  956. public subtractToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  957. result.x = this.x - otherVector.x;
  958. result.y = this.y - otherVector.y;
  959. return this;
  960. }
  961. /**
  962. * Sets the current Vector2 coordinates by subtracting from it the given one coordinates
  963. * @param otherVector defines the other vector
  964. * @returns the current updated Vector2
  965. */
  966. public subtractInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  967. this.x -= otherVector.x;
  968. this.y -= otherVector.y;
  969. return this;
  970. }
  971. /**
  972. * Multiplies in place the current Vector2 coordinates by the given ones
  973. * @param otherVector defines the other vector
  974. * @returns the current updated Vector2
  975. */
  976. public multiplyInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  977. this.x *= otherVector.x;
  978. this.y *= otherVector.y;
  979. return this;
  980. }
  981. /**
  982. * Returns a new Vector2 set with the multiplication of the current Vector2 and the given one coordinates
  983. * @param otherVector defines the other vector
  984. * @returns a new Vector2
  985. */
  986. public multiply(otherVector: DeepImmutable<Vector2>): Vector2 {
  987. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  988. }
  989. /**
  990. * Sets "result" coordinates with the multiplication of the current Vector2 and the given one coordinates
  991. * @param otherVector defines the other vector
  992. * @param result defines the target vector
  993. * @returns the unmodified current Vector2
  994. */
  995. public multiplyToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  996. result.x = this.x * otherVector.x;
  997. result.y = this.y * otherVector.y;
  998. return this;
  999. }
  1000. /**
  1001. * Gets a new Vector2 set with the Vector2 coordinates multiplied by the given floats
  1002. * @param x defines the first coordinate
  1003. * @param y defines the second coordinate
  1004. * @returns a new Vector2
  1005. */
  1006. public multiplyByFloats(x: number, y: number): Vector2 {
  1007. return new Vector2(this.x * x, this.y * y);
  1008. }
  1009. /**
  1010. * Returns a new Vector2 set with the Vector2 coordinates divided by the given one coordinates
  1011. * @param otherVector defines the other vector
  1012. * @returns a new Vector2
  1013. */
  1014. public divide(otherVector: Vector2): Vector2 {
  1015. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  1016. }
  1017. /**
  1018. * Sets the "result" coordinates with the Vector2 divided by the given one coordinates
  1019. * @param otherVector defines the other vector
  1020. * @param result defines the target vector
  1021. * @returns the unmodified current Vector2
  1022. */
  1023. public divideToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  1024. result.x = this.x / otherVector.x;
  1025. result.y = this.y / otherVector.y;
  1026. return this;
  1027. }
  1028. /**
  1029. * Divides the current Vector2 coordinates by the given ones
  1030. * @param otherVector defines the other vector
  1031. * @returns the current updated Vector2
  1032. */
  1033. public divideInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  1034. return this.divideToRef(otherVector, this);
  1035. }
  1036. /**
  1037. * Gets a new Vector2 with current Vector2 negated coordinates
  1038. * @returns a new Vector2
  1039. */
  1040. public negate(): Vector2 {
  1041. return new Vector2(-this.x, -this.y);
  1042. }
  1043. /**
  1044. * Multiply the Vector2 coordinates by scale
  1045. * @param scale defines the scaling factor
  1046. * @returns the current updated Vector2
  1047. */
  1048. public scaleInPlace(scale: number): Vector2 {
  1049. this.x *= scale;
  1050. this.y *= scale;
  1051. return this;
  1052. }
  1053. /**
  1054. * Returns a new Vector2 scaled by "scale" from the current Vector2
  1055. * @param scale defines the scaling factor
  1056. * @returns a new Vector2
  1057. */
  1058. public scale(scale: number): Vector2 {
  1059. let result = new Vector2(0, 0);
  1060. this.scaleToRef(scale, result);
  1061. return result;
  1062. }
  1063. /**
  1064. * Scale the current Vector2 values by a factor to a given Vector2
  1065. * @param scale defines the scale factor
  1066. * @param result defines the Vector2 object where to store the result
  1067. * @returns the unmodified current Vector2
  1068. */
  1069. public scaleToRef(scale: number, result: Vector2): Vector2 {
  1070. result.x = this.x * scale;
  1071. result.y = this.y * scale;
  1072. return this;
  1073. }
  1074. /**
  1075. * Scale the current Vector2 values by a factor and add the result to a given Vector2
  1076. * @param scale defines the scale factor
  1077. * @param result defines the Vector2 object where to store the result
  1078. * @returns the unmodified current Vector2
  1079. */
  1080. public scaleAndAddToRef(scale: number, result: Vector2): Vector2 {
  1081. result.x += this.x * scale;
  1082. result.y += this.y * scale;
  1083. return this;
  1084. }
  1085. /**
  1086. * Gets a boolean if two vectors are equals
  1087. * @param otherVector defines the other vector
  1088. * @returns true if the given vector coordinates strictly equal the current Vector2 ones
  1089. */
  1090. public equals(otherVector: DeepImmutable<Vector2>): boolean {
  1091. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  1092. }
  1093. /**
  1094. * Gets a boolean if two vectors are equals (using an epsilon value)
  1095. * @param otherVector defines the other vector
  1096. * @param epsilon defines the minimal distance to consider equality
  1097. * @returns true if the given vector coordinates are close to the current ones by a distance of epsilon.
  1098. */
  1099. public equalsWithEpsilon(otherVector: DeepImmutable<Vector2>, epsilon: number = Epsilon): boolean {
  1100. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon);
  1101. }
  1102. /**
  1103. * Gets a new Vector2 from current Vector2 floored values
  1104. * @returns a new Vector2
  1105. */
  1106. public floor(): Vector2 {
  1107. return new Vector2(Math.floor(this.x), Math.floor(this.y));
  1108. }
  1109. /**
  1110. * Gets a new Vector2 from current Vector2 floored values
  1111. * @returns a new Vector2
  1112. */
  1113. public fract(): Vector2 {
  1114. return new Vector2(this.x - Math.floor(this.x), this.y - Math.floor(this.y));
  1115. }
  1116. // Properties
  1117. /**
  1118. * Gets the length of the vector
  1119. * @returns the vector length (float)
  1120. */
  1121. public length(): number {
  1122. return Math.sqrt(this.x * this.x + this.y * this.y);
  1123. }
  1124. /**
  1125. * Gets the vector squared length
  1126. * @returns the vector squared length (float)
  1127. */
  1128. public lengthSquared(): number {
  1129. return (this.x * this.x + this.y * this.y);
  1130. }
  1131. // Methods
  1132. /**
  1133. * Normalize the vector
  1134. * @returns the current updated Vector2
  1135. */
  1136. public normalize(): Vector2 {
  1137. var len = this.length();
  1138. if (len === 0) {
  1139. return this;
  1140. }
  1141. var num = 1.0 / len;
  1142. this.x *= num;
  1143. this.y *= num;
  1144. return this;
  1145. }
  1146. /**
  1147. * Gets a new Vector2 copied from the Vector2
  1148. * @returns a new Vector2
  1149. */
  1150. public clone(): Vector2 {
  1151. return new Vector2(this.x, this.y);
  1152. }
  1153. // Statics
  1154. /**
  1155. * Gets a new Vector2(0, 0)
  1156. * @returns a new Vector2
  1157. */
  1158. public static Zero(): Vector2 {
  1159. return new Vector2(0, 0);
  1160. }
  1161. /**
  1162. * Gets a new Vector2(1, 1)
  1163. * @returns a new Vector2
  1164. */
  1165. public static One(): Vector2 {
  1166. return new Vector2(1, 1);
  1167. }
  1168. /**
  1169. * Gets a new Vector2 set from the given index element of the given array
  1170. * @param array defines the data source
  1171. * @param offset defines the offset in the data source
  1172. * @returns a new Vector2
  1173. */
  1174. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Vector2 {
  1175. return new Vector2(array[offset], array[offset + 1]);
  1176. }
  1177. /**
  1178. * Sets "result" from the given index element of the given array
  1179. * @param array defines the data source
  1180. * @param offset defines the offset in the data source
  1181. * @param result defines the target vector
  1182. */
  1183. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector2): void {
  1184. result.x = array[offset];
  1185. result.y = array[offset + 1];
  1186. }
  1187. /**
  1188. * Gets a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the given four Vector2
  1189. * @param value1 defines 1st point of control
  1190. * @param value2 defines 2nd point of control
  1191. * @param value3 defines 3rd point of control
  1192. * @param value4 defines 4th point of control
  1193. * @param amount defines the interpolation factor
  1194. * @returns a new Vector2
  1195. */
  1196. public static CatmullRom(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, value3: DeepImmutable<Vector2>, value4: DeepImmutable<Vector2>, amount: number): Vector2 {
  1197. var squared = amount * amount;
  1198. var cubed = amount * squared;
  1199. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  1200. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  1201. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  1202. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  1203. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  1204. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  1205. return new Vector2(x, y);
  1206. }
  1207. /**
  1208. * 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".
  1209. * If a coordinate of "value" is lower than "min" coordinates, the returned Vector2 is given this "min" coordinate.
  1210. * If a coordinate of "value" is greater than "max" coordinates, the returned Vector2 is given this "max" coordinate
  1211. * @param value defines the value to clamp
  1212. * @param min defines the lower limit
  1213. * @param max defines the upper limit
  1214. * @returns a new Vector2
  1215. */
  1216. public static Clamp(value: DeepImmutable<Vector2>, min: DeepImmutable<Vector2>, max: DeepImmutable<Vector2>): Vector2 {
  1217. var x = value.x;
  1218. x = (x > max.x) ? max.x : x;
  1219. x = (x < min.x) ? min.x : x;
  1220. var y = value.y;
  1221. y = (y > max.y) ? max.y : y;
  1222. y = (y < min.y) ? min.y : y;
  1223. return new Vector2(x, y);
  1224. }
  1225. /**
  1226. * Returns a new Vector2 located for "amount" (float) on the Hermite spline defined by the vectors "value1", "value3", "tangent1", "tangent2"
  1227. * @param value1 defines the 1st control point
  1228. * @param tangent1 defines the outgoing tangent
  1229. * @param value2 defines the 2nd control point
  1230. * @param tangent2 defines the incoming tangent
  1231. * @param amount defines the interpolation factor
  1232. * @returns a new Vector2
  1233. */
  1234. public static Hermite(value1: DeepImmutable<Vector2>, tangent1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, tangent2: DeepImmutable<Vector2>, amount: number): Vector2 {
  1235. var squared = amount * amount;
  1236. var cubed = amount * squared;
  1237. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  1238. var part2 = (-2.0 * cubed) + (3.0 * squared);
  1239. var part3 = (cubed - (2.0 * squared)) + amount;
  1240. var part4 = cubed - squared;
  1241. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  1242. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  1243. return new Vector2(x, y);
  1244. }
  1245. /**
  1246. * Returns a new Vector2 located for "amount" (float) on the linear interpolation between the vector "start" adn the vector "end".
  1247. * @param start defines the start vector
  1248. * @param end defines the end vector
  1249. * @param amount defines the interpolation factor
  1250. * @returns a new Vector2
  1251. */
  1252. public static Lerp(start: DeepImmutable<Vector2>, end: DeepImmutable<Vector2>, amount: number): Vector2 {
  1253. var x = start.x + ((end.x - start.x) * amount);
  1254. var y = start.y + ((end.y - start.y) * amount);
  1255. return new Vector2(x, y);
  1256. }
  1257. /**
  1258. * Gets the dot product of the vector "left" and the vector "right"
  1259. * @param left defines first vector
  1260. * @param right defines second vector
  1261. * @returns the dot product (float)
  1262. */
  1263. public static Dot(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): number {
  1264. return left.x * right.x + left.y * right.y;
  1265. }
  1266. /**
  1267. * Returns a new Vector2 equal to the normalized given vector
  1268. * @param vector defines the vector to normalize
  1269. * @returns a new Vector2
  1270. */
  1271. public static Normalize(vector: DeepImmutable<Vector2>): Vector2 {
  1272. var newVector = vector.clone();
  1273. newVector.normalize();
  1274. return newVector;
  1275. }
  1276. /**
  1277. * Gets a new Vector2 set with the minimal coordinate values from the "left" and "right" vectors
  1278. * @param left defines 1st vector
  1279. * @param right defines 2nd vector
  1280. * @returns a new Vector2
  1281. */
  1282. public static Minimize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
  1283. var x = (left.x < right.x) ? left.x : right.x;
  1284. var y = (left.y < right.y) ? left.y : right.y;
  1285. return new Vector2(x, y);
  1286. }
  1287. /**
  1288. * Gets a new Vecto2 set with the maximal coordinate values from the "left" and "right" vectors
  1289. * @param left defines 1st vector
  1290. * @param right defines 2nd vector
  1291. * @returns a new Vector2
  1292. */
  1293. public static Maximize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
  1294. var x = (left.x > right.x) ? left.x : right.x;
  1295. var y = (left.y > right.y) ? left.y : right.y;
  1296. return new Vector2(x, y);
  1297. }
  1298. /**
  1299. * Gets a new Vector2 set with the transformed coordinates of the given vector by the given transformation matrix
  1300. * @param vector defines the vector to transform
  1301. * @param transformation defines the matrix to apply
  1302. * @returns a new Vector2
  1303. */
  1304. public static Transform(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>): Vector2 {
  1305. let r = Vector2.Zero();
  1306. Vector2.TransformToRef(vector, transformation, r);
  1307. return r;
  1308. }
  1309. /**
  1310. * Transforms the given vector coordinates by the given transformation matrix and stores the result in the vector "result" coordinates
  1311. * @param vector defines the vector to transform
  1312. * @param transformation defines the matrix to apply
  1313. * @param result defines the target vector
  1314. */
  1315. public static TransformToRef(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>, result: Vector2) {
  1316. const m = transformation.m;
  1317. var x = (vector.x * m[0]) + (vector.y * m[4]) + m[12];
  1318. var y = (vector.x * m[1]) + (vector.y * m[5]) + m[13];
  1319. result.x = x;
  1320. result.y = y;
  1321. }
  1322. /**
  1323. * Determines if a given vector is included in a triangle
  1324. * @param p defines the vector to test
  1325. * @param p0 defines 1st triangle point
  1326. * @param p1 defines 2nd triangle point
  1327. * @param p2 defines 3rd triangle point
  1328. * @returns true if the point "p" is in the triangle defined by the vertors "p0", "p1", "p2"
  1329. */
  1330. public static PointInTriangle(p: DeepImmutable<Vector2>, p0: DeepImmutable<Vector2>, p1: DeepImmutable<Vector2>, p2: DeepImmutable<Vector2>) {
  1331. let a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  1332. let sign = a < 0 ? -1 : 1;
  1333. let s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  1334. let t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
  1335. return s > 0 && t > 0 && (s + t) < 2 * a * sign;
  1336. }
  1337. /**
  1338. * Gets the distance between the vectors "value1" and "value2"
  1339. * @param value1 defines first vector
  1340. * @param value2 defines second vector
  1341. * @returns the distance between vectors
  1342. */
  1343. public static Distance(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
  1344. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  1345. }
  1346. /**
  1347. * Returns the squared distance between the vectors "value1" and "value2"
  1348. * @param value1 defines first vector
  1349. * @param value2 defines second vector
  1350. * @returns the squared distance between vectors
  1351. */
  1352. public static DistanceSquared(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
  1353. var x = value1.x - value2.x;
  1354. var y = value1.y - value2.y;
  1355. return (x * x) + (y * y);
  1356. }
  1357. /**
  1358. * Gets a new Vector2 located at the center of the vectors "value1" and "value2"
  1359. * @param value1 defines first vector
  1360. * @param value2 defines second vector
  1361. * @returns a new Vector2
  1362. */
  1363. public static Center(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): Vector2 {
  1364. var center = value1.add(value2);
  1365. center.scaleInPlace(0.5);
  1366. return center;
  1367. }
  1368. /**
  1369. * Gets the shortest distance (float) between the point "p" and the segment defined by the two points "segA" and "segB".
  1370. * @param p defines the middle point
  1371. * @param segA defines one point of the segment
  1372. * @param segB defines the other point of the segment
  1373. * @returns the shortest distance
  1374. */
  1375. public static DistanceOfPointFromSegment(p: DeepImmutable<Vector2>, segA: DeepImmutable<Vector2>, segB: DeepImmutable<Vector2>): number {
  1376. let l2 = Vector2.DistanceSquared(segA, segB);
  1377. if (l2 === 0.0) {
  1378. return Vector2.Distance(p, segA);
  1379. }
  1380. let v = segB.subtract(segA);
  1381. let t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
  1382. let proj = segA.add(v.multiplyByFloats(t, t));
  1383. return Vector2.Distance(p, proj);
  1384. }
  1385. }
  1386. /**
  1387. * Classed used to store (x,y,z) vector representation
  1388. * A Vector3 is the main object used in 3D geometry
  1389. * It can represent etiher the coordinates of a point the space, either a direction
  1390. * Reminder: Babylon.js uses a left handed forward facing system
  1391. */
  1392. export class Vector3 {
  1393. private static _UpReadOnly = Vector3.Up() as DeepImmutable<Vector3>;
  1394. /**
  1395. * Creates a new Vector3 object from the given x, y, z (floats) coordinates.
  1396. * @param x defines the first coordinates (on X axis)
  1397. * @param y defines the second coordinates (on Y axis)
  1398. * @param z defines the third coordinates (on Z axis)
  1399. */
  1400. constructor(
  1401. /**
  1402. * Defines the first coordinates (on X axis)
  1403. */
  1404. public x: number = 0,
  1405. /**
  1406. * Defines the second coordinates (on Y axis)
  1407. */
  1408. public y: number = 0,
  1409. /**
  1410. * Defines the third coordinates (on Z axis)
  1411. */
  1412. public z: number = 0
  1413. ) {
  1414. }
  1415. /**
  1416. * Creates a string representation of the Vector3
  1417. * @returns a string with the Vector3 coordinates.
  1418. */
  1419. public toString(): string {
  1420. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  1421. }
  1422. /**
  1423. * Gets the class name
  1424. * @returns the string "Vector3"
  1425. */
  1426. public getClassName(): string {
  1427. return "Vector3";
  1428. }
  1429. /**
  1430. * Creates the Vector3 hash code
  1431. * @returns a number which tends to be unique between Vector3 instances
  1432. */
  1433. public getHashCode(): number {
  1434. let hash = this.x || 0;
  1435. hash = (hash * 397) ^ (this.y || 0);
  1436. hash = (hash * 397) ^ (this.z || 0);
  1437. return hash;
  1438. }
  1439. // Operators
  1440. /**
  1441. * Creates an array containing three elements : the coordinates of the Vector3
  1442. * @returns a new array of numbers
  1443. */
  1444. public asArray(): number[] {
  1445. var result: number[] = [];
  1446. this.toArray(result, 0);
  1447. return result;
  1448. }
  1449. /**
  1450. * Populates the given array or Float32Array from the given index with the successive coordinates of the Vector3
  1451. * @param array defines the destination array
  1452. * @param index defines the offset in the destination array
  1453. * @returns the current Vector3
  1454. */
  1455. public toArray(array: FloatArray, index: number = 0): Vector3 {
  1456. array[index] = this.x;
  1457. array[index + 1] = this.y;
  1458. array[index + 2] = this.z;
  1459. return this;
  1460. }
  1461. /**
  1462. * Converts the current Vector3 into a quaternion (considering that the Vector3 contains Euler angles representation of a rotation)
  1463. * @returns a new Quaternion object, computed from the Vector3 coordinates
  1464. */
  1465. public toQuaternion(): Quaternion {
  1466. return BABYLON.Quaternion.RotationYawPitchRoll(this.y, this.x, this.z);
  1467. }
  1468. /**
  1469. * Adds the given vector to the current Vector3
  1470. * @param otherVector defines the second operand
  1471. * @returns the current updated Vector3
  1472. */
  1473. public addInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  1474. return this.addInPlaceFromFloats(otherVector.x, otherVector.y, otherVector.z);
  1475. }
  1476. /**
  1477. * Adds the given coordinates to the current Vector3
  1478. * @param x defines the x coordinate of the operand
  1479. * @param y defines the y coordinate of the operand
  1480. * @param z defines the z coordinate of the operand
  1481. * @returns the current updated Vector3
  1482. */
  1483. public addInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  1484. this.x += x;
  1485. this.y += y;
  1486. this.z += z;
  1487. return this;
  1488. }
  1489. /**
  1490. * Gets a new Vector3, result of the addition the current Vector3 and the given vector
  1491. * @param otherVector defines the second operand
  1492. * @returns the resulting Vector3
  1493. */
  1494. public add(otherVector: DeepImmutable<Vector3>): Vector3 {
  1495. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  1496. }
  1497. /**
  1498. * Adds the current Vector3 to the given one and stores the result in the vector "result"
  1499. * @param otherVector defines the second operand
  1500. * @param result defines the Vector3 object where to store the result
  1501. * @returns the current Vector3
  1502. */
  1503. public addToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  1504. return result.copyFromFloats(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  1505. }
  1506. /**
  1507. * Subtract the given vector from the current Vector3
  1508. * @param otherVector defines the second operand
  1509. * @returns the current updated Vector3
  1510. */
  1511. public subtractInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  1512. this.x -= otherVector.x;
  1513. this.y -= otherVector.y;
  1514. this.z -= otherVector.z;
  1515. return this;
  1516. }
  1517. /**
  1518. * Returns a new Vector3, result of the subtraction of the given vector from the current Vector3
  1519. * @param otherVector defines the second operand
  1520. * @returns the resulting Vector3
  1521. */
  1522. public subtract(otherVector: DeepImmutable<Vector3>): Vector3 {
  1523. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  1524. }
  1525. /**
  1526. * Subtracts the given vector from the current Vector3 and stores the result in the vector "result".
  1527. * @param otherVector defines the second operand
  1528. * @param result defines the Vector3 object where to store the result
  1529. * @returns the current Vector3
  1530. */
  1531. public subtractToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  1532. return this.subtractFromFloatsToRef(otherVector.x, otherVector.y, otherVector.z, result);
  1533. }
  1534. /**
  1535. * Returns a new Vector3 set with the subtraction of the given floats from the current Vector3 coordinates
  1536. * @param x defines the x coordinate of the operand
  1537. * @param y defines the y coordinate of the operand
  1538. * @param z defines the z coordinate of the operand
  1539. * @returns the resulting Vector3
  1540. */
  1541. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  1542. return new Vector3(this.x - x, this.y - y, this.z - z);
  1543. }
  1544. /**
  1545. * Subtracts the given floats from the current Vector3 coordinates and set the given vector "result" with this result
  1546. * @param x defines the x coordinate of the operand
  1547. * @param y defines the y coordinate of the operand
  1548. * @param z defines the z coordinate of the operand
  1549. * @param result defines the Vector3 object where to store the result
  1550. * @returns the current Vector3
  1551. */
  1552. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
  1553. return result.copyFromFloats(this.x - x, this.y - y, this.z - z);
  1554. }
  1555. /**
  1556. * Gets a new Vector3 set with the current Vector3 negated coordinates
  1557. * @returns a new Vector3
  1558. */
  1559. public negate(): Vector3 {
  1560. return new Vector3(-this.x, -this.y, -this.z);
  1561. }
  1562. /**
  1563. * Multiplies the Vector3 coordinates by the float "scale"
  1564. * @param scale defines the multiplier factor
  1565. * @returns the current updated Vector3
  1566. */
  1567. public scaleInPlace(scale: number): Vector3 {
  1568. this.x *= scale;
  1569. this.y *= scale;
  1570. this.z *= scale;
  1571. return this;
  1572. }
  1573. /**
  1574. * Returns a new Vector3 set with the current Vector3 coordinates multiplied by the float "scale"
  1575. * @param scale defines the multiplier factor
  1576. * @returns a new Vector3
  1577. */
  1578. public scale(scale: number): Vector3 {
  1579. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  1580. }
  1581. /**
  1582. * Multiplies the current Vector3 coordinates by the float "scale" and stores the result in the given vector "result" coordinates
  1583. * @param scale defines the multiplier factor
  1584. * @param result defines the Vector3 object where to store the result
  1585. * @returns the current Vector3
  1586. */
  1587. public scaleToRef(scale: number, result: Vector3): Vector3 {
  1588. return result.copyFromFloats(this.x * scale, this.y * scale, this.z * scale);
  1589. }
  1590. /**
  1591. * Scale the current Vector3 values by a factor and add the result to a given Vector3
  1592. * @param scale defines the scale factor
  1593. * @param result defines the Vector3 object where to store the result
  1594. * @returns the unmodified current Vector3
  1595. */
  1596. public scaleAndAddToRef(scale: number, result: Vector3): Vector3 {
  1597. return result.addInPlaceFromFloats(this.x * scale, this.y * scale, this.z * scale);
  1598. }
  1599. /**
  1600. * Returns true if the current Vector3 and the given vector coordinates are strictly equal
  1601. * @param otherVector defines the second operand
  1602. * @returns true if both vectors are equals
  1603. */
  1604. public equals(otherVector: DeepImmutable<Vector3>): boolean {
  1605. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  1606. }
  1607. /**
  1608. * Returns true if the current Vector3 and the given vector coordinates are distant less than epsilon
  1609. * @param otherVector defines the second operand
  1610. * @param epsilon defines the minimal distance to define values as equals
  1611. * @returns true if both vectors are distant less than epsilon
  1612. */
  1613. public equalsWithEpsilon(otherVector: DeepImmutable<Vector3>, epsilon: number = Epsilon): boolean {
  1614. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon) && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon);
  1615. }
  1616. /**
  1617. * Returns true if the current Vector3 coordinates equals the given floats
  1618. * @param x defines the x coordinate of the operand
  1619. * @param y defines the y coordinate of the operand
  1620. * @param z defines the z coordinate of the operand
  1621. * @returns true if both vectors are equals
  1622. */
  1623. public equalsToFloats(x: number, y: number, z: number): boolean {
  1624. return this.x === x && this.y === y && this.z === z;
  1625. }
  1626. /**
  1627. * Multiplies the current Vector3 coordinates by the given ones
  1628. * @param otherVector defines the second operand
  1629. * @returns the current updated Vector3
  1630. */
  1631. public multiplyInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  1632. this.x *= otherVector.x;
  1633. this.y *= otherVector.y;
  1634. this.z *= otherVector.z;
  1635. return this;
  1636. }
  1637. /**
  1638. * Returns a new Vector3, result of the multiplication of the current Vector3 by the given vector
  1639. * @param otherVector defines the second operand
  1640. * @returns the new Vector3
  1641. */
  1642. public multiply(otherVector: DeepImmutable<Vector3>): Vector3 {
  1643. return this.multiplyByFloats(otherVector.x, otherVector.y, otherVector.z);
  1644. }
  1645. /**
  1646. * Multiplies the current Vector3 by the given one and stores the result in the given vector "result"
  1647. * @param otherVector defines the second operand
  1648. * @param result defines the Vector3 object where to store the result
  1649. * @returns the current Vector3
  1650. */
  1651. public multiplyToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  1652. return result.copyFromFloats(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  1653. }
  1654. /**
  1655. * Returns a new Vector3 set with the result of the mulliplication of the current Vector3 coordinates by the given floats
  1656. * @param x defines the x coordinate of the operand
  1657. * @param y defines the y coordinate of the operand
  1658. * @param z defines the z coordinate of the operand
  1659. * @returns the new Vector3
  1660. */
  1661. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  1662. return new Vector3(this.x * x, this.y * y, this.z * z);
  1663. }
  1664. /**
  1665. * Returns a new Vector3 set with the result of the division of the current Vector3 coordinates by the given ones
  1666. * @param otherVector defines the second operand
  1667. * @returns the new Vector3
  1668. */
  1669. public divide(otherVector: DeepImmutable<Vector3>): Vector3 {
  1670. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  1671. }
  1672. /**
  1673. * Divides the current Vector3 coordinates by the given ones and stores the result in the given vector "result"
  1674. * @param otherVector defines the second operand
  1675. * @param result defines the Vector3 object where to store the result
  1676. * @returns the current Vector3
  1677. */
  1678. public divideToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  1679. return result.copyFromFloats(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  1680. }
  1681. /**
  1682. * Divides the current Vector3 coordinates by the given ones.
  1683. * @param otherVector defines the second operand
  1684. * @returns the current updated Vector3
  1685. */
  1686. public divideInPlace(otherVector: Vector3): Vector3 {
  1687. return this.divideToRef(otherVector, this);
  1688. }
  1689. /**
  1690. * Updates the current Vector3 with the minimal coordinate values between its and the given vector ones
  1691. * @param other defines the second operand
  1692. * @returns the current updated Vector3
  1693. */
  1694. public minimizeInPlace(other: DeepImmutable<Vector3>): Vector3 {
  1695. return this.minimizeInPlaceFromFloats(other.x, other.y, other.z);
  1696. }
  1697. /**
  1698. * Updates the current Vector3 with the maximal coordinate values between its and the given vector ones.
  1699. * @param other defines the second operand
  1700. * @returns the current updated Vector3
  1701. */
  1702. public maximizeInPlace(other: DeepImmutable<Vector3>): Vector3 {
  1703. return this.maximizeInPlaceFromFloats(other.x, other.y, other.z);
  1704. }
  1705. /**
  1706. * Updates the current Vector3 with the minimal coordinate values between its and the given coordinates
  1707. * @param x defines the x coordinate of the operand
  1708. * @param y defines the y coordinate of the operand
  1709. * @param z defines the z coordinate of the operand
  1710. * @returns the current updated Vector3
  1711. */
  1712. public minimizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  1713. if (x < this.x) { this.x = x; }
  1714. if (y < this.y) { this.y = y; }
  1715. if (z < this.z) { this.z = z; }
  1716. return this;
  1717. }
  1718. /**
  1719. * Updates the current Vector3 with the maximal coordinate values between its and the given coordinates.
  1720. * @param x defines the x coordinate of the operand
  1721. * @param y defines the y coordinate of the operand
  1722. * @param z defines the z coordinate of the operand
  1723. * @returns the current updated Vector3
  1724. */
  1725. public maximizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  1726. if (x > this.x) { this.x = x; }
  1727. if (y > this.y) { this.y = y; }
  1728. if (z > this.z) { this.z = z; }
  1729. return this;
  1730. }
  1731. /**
  1732. * Gets a boolean indicating that the vector is non uniform meaning x, y or z are not all the same
  1733. */
  1734. public get isNonUniform(): boolean {
  1735. let absX = Math.abs(this.x);
  1736. let absY = Math.abs(this.y);
  1737. if (absX !== absY) {
  1738. return true;
  1739. }
  1740. let absZ = Math.abs(this.z);
  1741. if (absX !== absZ) {
  1742. return true;
  1743. }
  1744. if (absY !== absZ) {
  1745. return true;
  1746. }
  1747. return false;
  1748. }
  1749. /**
  1750. * Gets a new Vector3 from current Vector3 floored values
  1751. * @returns a new Vector3
  1752. */
  1753. public floor(): Vector3 {
  1754. return new Vector3(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
  1755. }
  1756. /**
  1757. * Gets a new Vector3 from current Vector3 floored values
  1758. * @returns a new Vector3
  1759. */
  1760. public fract(): Vector3 {
  1761. return new Vector3(this.x - Math.floor(this.x), this.y - Math.floor(this.y), this.z - Math.floor(this.z));
  1762. }
  1763. // Properties
  1764. /**
  1765. * Gets the length of the Vector3
  1766. * @returns the length of the Vecto3
  1767. */
  1768. public length(): number {
  1769. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  1770. }
  1771. /**
  1772. * Gets the squared length of the Vector3
  1773. * @returns squared length of the Vector3
  1774. */
  1775. public lengthSquared(): number {
  1776. return (this.x * this.x + this.y * this.y + this.z * this.z);
  1777. }
  1778. /**
  1779. * Normalize the current Vector3.
  1780. * Please note that this is an in place operation.
  1781. * @returns the current updated Vector3
  1782. */
  1783. public normalize(): Vector3 {
  1784. return this.normalizeFromLength(this.length());
  1785. }
  1786. /**
  1787. * Reorders the x y z properties of the vector in place
  1788. * @param order new ordering of the properties (eg. for vector 1,2,3 with "ZYX" will produce 3,2,1)
  1789. * @returns the current updated vector
  1790. */
  1791. public reorderInPlace(order: string) {
  1792. order = order.toLowerCase();
  1793. if (order === "xyz") {
  1794. return this;
  1795. }
  1796. MathTmp.Vector3[0].copyFrom(this);
  1797. ["x", "y", "z"].forEach((val, i) => {
  1798. (<any>this)[val] = (<any>MathTmp.Vector3[0])[order[i]];
  1799. });
  1800. return this;
  1801. }
  1802. /**
  1803. * Rotates the vector around 0,0,0 by a quaternion
  1804. * @param quaternion the rotation quaternion
  1805. * @param result vector to store the result
  1806. * @returns the resulting vector
  1807. */
  1808. public rotateByQuaternionToRef(quaternion: Quaternion, result: Vector3) {
  1809. quaternion.toRotationMatrix(MathTmp.Matrix[0]);
  1810. Vector3.TransformCoordinatesToRef(this, MathTmp.Matrix[0], result);
  1811. return result;
  1812. }
  1813. /**
  1814. * Rotates a vector around a given point
  1815. * @param quaternion the rotation quaternion
  1816. * @param point the point to rotate around
  1817. * @param result vector to store the result
  1818. * @returns the resulting vector
  1819. */
  1820. public rotateByQuaternionAroundPointToRef(quaternion: Quaternion, point: Vector3, result: Vector3) {
  1821. this.subtractToRef(point, MathTmp.Vector3[0]);
  1822. MathTmp.Vector3[0].rotateByQuaternionToRef(quaternion, MathTmp.Vector3[0]);
  1823. point.addToRef(MathTmp.Vector3[0], result);
  1824. return result;
  1825. }
  1826. /**
  1827. * Normalize the current Vector3 with the given input length.
  1828. * Please note that this is an in place operation.
  1829. * @param len the length of the vector
  1830. * @returns the current updated Vector3
  1831. */
  1832. public normalizeFromLength(len: number): Vector3 {
  1833. if (len === 0 || len === 1.0) {
  1834. return this;
  1835. }
  1836. return this.scaleInPlace(1.0 / len);
  1837. }
  1838. /**
  1839. * Normalize the current Vector3 to a new vector
  1840. * @returns the new Vector3
  1841. */
  1842. public normalizeToNew(): Vector3 {
  1843. const normalized = new Vector3(0, 0, 0);
  1844. this.normalizeToRef(normalized);
  1845. return normalized;
  1846. }
  1847. /**
  1848. * Normalize the current Vector3 to the reference
  1849. * @param reference define the Vector3 to update
  1850. * @returns the updated Vector3
  1851. */
  1852. public normalizeToRef(reference: DeepImmutable<Vector3>): Vector3 {
  1853. var len = this.length();
  1854. if (len === 0 || len === 1.0) {
  1855. return reference.copyFromFloats(this.x, this.y, this.z);
  1856. }
  1857. return this.scaleToRef(1.0 / len, reference);
  1858. }
  1859. /**
  1860. * Creates a new Vector3 copied from the current Vector3
  1861. * @returns the new Vector3
  1862. */
  1863. public clone(): Vector3 {
  1864. return new Vector3(this.x, this.y, this.z);
  1865. }
  1866. /**
  1867. * Copies the given vector coordinates to the current Vector3 ones
  1868. * @param source defines the source Vector3
  1869. * @returns the current updated Vector3
  1870. */
  1871. public copyFrom(source: DeepImmutable<Vector3>): Vector3 {
  1872. return this.copyFromFloats(source.x, source.y, source.z);
  1873. }
  1874. /**
  1875. * Copies the given floats to the current Vector3 coordinates
  1876. * @param x defines the x coordinate of the operand
  1877. * @param y defines the y coordinate of the operand
  1878. * @param z defines the z coordinate of the operand
  1879. * @returns the current updated Vector3
  1880. */
  1881. public copyFromFloats(x: number, y: number, z: number): Vector3 {
  1882. this.x = x;
  1883. this.y = y;
  1884. this.z = z;
  1885. return this;
  1886. }
  1887. /**
  1888. * Copies the given floats to the current Vector3 coordinates
  1889. * @param x defines the x coordinate of the operand
  1890. * @param y defines the y coordinate of the operand
  1891. * @param z defines the z coordinate of the operand
  1892. * @returns the current updated Vector3
  1893. */
  1894. public set(x: number, y: number, z: number): Vector3 {
  1895. return this.copyFromFloats(x, y, z);
  1896. }
  1897. /**
  1898. * Copies the given float to the current Vector3 coordinates
  1899. * @param v defines the x, y and z coordinates of the operand
  1900. * @returns the current updated Vector3
  1901. */
  1902. public setAll(v: number): Vector3 {
  1903. this.x = this.y = this.z = v;
  1904. return this;
  1905. }
  1906. // Statics
  1907. /**
  1908. * Get the clip factor between two vectors
  1909. * @param vector0 defines the first operand
  1910. * @param vector1 defines the second operand
  1911. * @param axis defines the axis to use
  1912. * @param size defines the size along the axis
  1913. * @returns the clip factor
  1914. */
  1915. public static GetClipFactor(vector0: DeepImmutable<Vector3>, vector1: DeepImmutable<Vector3>, axis: DeepImmutable<Vector3>, size: number) {
  1916. var d0 = Vector3.Dot(vector0, axis) - size;
  1917. var d1 = Vector3.Dot(vector1, axis) - size;
  1918. var s = d0 / (d0 - d1);
  1919. return s;
  1920. }
  1921. /**
  1922. * Get angle between two vectors
  1923. * @param vector0 angle between vector0 and vector1
  1924. * @param vector1 angle between vector0 and vector1
  1925. * @param normal direction of the normal
  1926. * @return the angle between vector0 and vector1
  1927. */
  1928. public static GetAngleBetweenVectors(vector0: DeepImmutable<Vector3>, vector1: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>): number {
  1929. const v0: Vector3 = vector0.normalizeToRef(MathTmp.Vector3[1]);
  1930. const v1: Vector3 = vector1.normalizeToRef(MathTmp.Vector3[2]);
  1931. const dot: number = Vector3.Dot(v0, v1);
  1932. const n = MathTmp.Vector3[3];
  1933. Vector3.CrossToRef(v0, v1, n);
  1934. if (Vector3.Dot(n, normal) > 0) {
  1935. return Math.acos(dot);
  1936. }
  1937. return -Math.acos(dot);
  1938. }
  1939. /**
  1940. * Returns a new Vector3 set from the index "offset" of the given array
  1941. * @param array defines the source array
  1942. * @param offset defines the offset in the source array
  1943. * @returns the new Vector3
  1944. */
  1945. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Vector3 {
  1946. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  1947. }
  1948. /**
  1949. * Returns a new Vector3 set from the index "offset" of the given Float32Array
  1950. * This function is deprecated. Use FromArray instead
  1951. * @param array defines the source array
  1952. * @param offset defines the offset in the source array
  1953. * @returns the new Vector3
  1954. */
  1955. public static FromFloatArray(array: DeepImmutable<Float32Array>, offset?: number): Vector3 {
  1956. return Vector3.FromArray(array, offset);
  1957. }
  1958. /**
  1959. * Sets the given vector "result" with the element values from the index "offset" of the given array
  1960. * @param array defines the source array
  1961. * @param offset defines the offset in the source array
  1962. * @param result defines the Vector3 where to store the result
  1963. */
  1964. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector3): void {
  1965. result.x = array[offset];
  1966. result.y = array[offset + 1];
  1967. result.z = array[offset + 2];
  1968. }
  1969. /**
  1970. * Sets the given vector "result" with the element values from the index "offset" of the given Float32Array
  1971. * This function is deprecated. Use FromArrayToRef instead.
  1972. * @param array defines the source array
  1973. * @param offset defines the offset in the source array
  1974. * @param result defines the Vector3 where to store the result
  1975. */
  1976. public static FromFloatArrayToRef(array: DeepImmutable<Float32Array>, offset: number, result: Vector3): void {
  1977. return Vector3.FromArrayToRef(array, offset, result);
  1978. }
  1979. /**
  1980. * Sets the given vector "result" with the given floats.
  1981. * @param x defines the x coordinate of the source
  1982. * @param y defines the y coordinate of the source
  1983. * @param z defines the z coordinate of the source
  1984. * @param result defines the Vector3 where to store the result
  1985. */
  1986. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  1987. result.copyFromFloats(x, y, z);
  1988. }
  1989. /**
  1990. * Returns a new Vector3 set to (0.0, 0.0, 0.0)
  1991. * @returns a new empty Vector3
  1992. */
  1993. public static Zero(): Vector3 {
  1994. return new Vector3(0.0, 0.0, 0.0);
  1995. }
  1996. /**
  1997. * Returns a new Vector3 set to (1.0, 1.0, 1.0)
  1998. * @returns a new unit Vector3
  1999. */
  2000. public static One(): Vector3 {
  2001. return new Vector3(1.0, 1.0, 1.0);
  2002. }
  2003. /**
  2004. * Returns a new Vector3 set to (0.0, 1.0, 0.0)
  2005. * @returns a new up Vector3
  2006. */
  2007. public static Up(): Vector3 {
  2008. return new Vector3(0.0, 1.0, 0.0);
  2009. }
  2010. /**
  2011. * Gets a up Vector3 that must not be updated
  2012. */
  2013. public static get UpReadOnly(): DeepImmutable<Vector3> {
  2014. return Vector3._UpReadOnly;
  2015. }
  2016. /**
  2017. * Returns a new Vector3 set to (0.0, -1.0, 0.0)
  2018. * @returns a new down Vector3
  2019. */
  2020. public static Down(): Vector3 {
  2021. return new Vector3(0.0, -1.0, 0.0);
  2022. }
  2023. /**
  2024. * Returns a new Vector3 set to (0.0, 0.0, 1.0)
  2025. * @returns a new forward Vector3
  2026. */
  2027. public static Forward(): Vector3 {
  2028. return new Vector3(0.0, 0.0, 1.0);
  2029. }
  2030. /**
  2031. * Returns a new Vector3 set to (0.0, 0.0, -1.0)
  2032. * @returns a new forward Vector3
  2033. */
  2034. public static Backward(): Vector3 {
  2035. return new Vector3(0.0, 0.0, -1.0);
  2036. }
  2037. /**
  2038. * Returns a new Vector3 set to (1.0, 0.0, 0.0)
  2039. * @returns a new right Vector3
  2040. */
  2041. public static Right(): Vector3 {
  2042. return new Vector3(1.0, 0.0, 0.0);
  2043. }
  2044. /**
  2045. * Returns a new Vector3 set to (-1.0, 0.0, 0.0)
  2046. * @returns a new left Vector3
  2047. */
  2048. public static Left(): Vector3 {
  2049. return new Vector3(-1.0, 0.0, 0.0);
  2050. }
  2051. /**
  2052. * Returns a new Vector3 set with the result of the transformation by the given matrix of the given vector.
  2053. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  2054. * @param vector defines the Vector3 to transform
  2055. * @param transformation defines the transformation matrix
  2056. * @returns the transformed Vector3
  2057. */
  2058. public static TransformCoordinates(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>): Vector3 {
  2059. var result = Vector3.Zero();
  2060. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  2061. return result;
  2062. }
  2063. /**
  2064. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given vector
  2065. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  2066. * @param vector defines the Vector3 to transform
  2067. * @param transformation defines the transformation matrix
  2068. * @param result defines the Vector3 where to store the result
  2069. */
  2070. public static TransformCoordinatesToRef(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  2071. Vector3.TransformCoordinatesFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  2072. }
  2073. /**
  2074. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given floats (x, y, z)
  2075. * This method computes tranformed coordinates only, not transformed direction vectors
  2076. * @param x define the x coordinate of the source vector
  2077. * @param y define the y coordinate of the source vector
  2078. * @param z define the z coordinate of the source vector
  2079. * @param transformation defines the transformation matrix
  2080. * @param result defines the Vector3 where to store the result
  2081. */
  2082. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  2083. const m = transformation.m;
  2084. var rx = x * m[0] + y * m[4] + z * m[8] + m[12];
  2085. var ry = x * m[1] + y * m[5] + z * m[9] + m[13];
  2086. var rz = x * m[2] + y * m[6] + z * m[10] + m[14];
  2087. var rw = 1 / (x * m[3] + y * m[7] + z * m[11] + m[15]);
  2088. result.x = rx * rw;
  2089. result.y = ry * rw;
  2090. result.z = rz * rw;
  2091. }
  2092. /**
  2093. * Returns a new Vector3 set with the result of the normal transformation by the given matrix of the given vector
  2094. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  2095. * @param vector defines the Vector3 to transform
  2096. * @param transformation defines the transformation matrix
  2097. * @returns the new Vector3
  2098. */
  2099. public static TransformNormal(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>): Vector3 {
  2100. var result = Vector3.Zero();
  2101. Vector3.TransformNormalToRef(vector, transformation, result);
  2102. return result;
  2103. }
  2104. /**
  2105. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector
  2106. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  2107. * @param vector defines the Vector3 to transform
  2108. * @param transformation defines the transformation matrix
  2109. * @param result defines the Vector3 where to store the result
  2110. */
  2111. public static TransformNormalToRef(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  2112. this.TransformNormalFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  2113. }
  2114. /**
  2115. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z)
  2116. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  2117. * @param x define the x coordinate of the source vector
  2118. * @param y define the y coordinate of the source vector
  2119. * @param z define the z coordinate of the source vector
  2120. * @param transformation defines the transformation matrix
  2121. * @param result defines the Vector3 where to store the result
  2122. */
  2123. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  2124. const m = transformation.m;
  2125. result.x = x * m[0] + y * m[4] + z * m[8];
  2126. result.y = x * m[1] + y * m[5] + z * m[9];
  2127. result.z = x * m[2] + y * m[6] + z * m[10];
  2128. }
  2129. /**
  2130. * Returns a new Vector3 located for "amount" on the CatmullRom interpolation spline defined by the vectors "value1", "value2", "value3", "value4"
  2131. * @param value1 defines the first control point
  2132. * @param value2 defines the second control point
  2133. * @param value3 defines the third control point
  2134. * @param value4 defines the fourth control point
  2135. * @param amount defines the amount on the spline to use
  2136. * @returns the new Vector3
  2137. */
  2138. public static CatmullRom(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>, value3: DeepImmutable<Vector3>, value4: DeepImmutable<Vector3>, amount: number): Vector3 {
  2139. var squared = amount * amount;
  2140. var cubed = amount * squared;
  2141. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  2142. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  2143. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  2144. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  2145. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  2146. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  2147. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  2148. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  2149. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  2150. return new Vector3(x, y, z);
  2151. }
  2152. /**
  2153. * 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"
  2154. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  2155. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  2156. * @param value defines the current value
  2157. * @param min defines the lower range value
  2158. * @param max defines the upper range value
  2159. * @returns the new Vector3
  2160. */
  2161. public static Clamp(value: DeepImmutable<Vector3>, min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>): Vector3 {
  2162. const v = new Vector3();
  2163. Vector3.ClampToRef(value, min, max, v);
  2164. return v;
  2165. }
  2166. /**
  2167. * Sets the given vector "result" with the coordinates of "value", if the vector "value" is in the cube defined by the vectors "min" and "max"
  2168. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  2169. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  2170. * @param value defines the current value
  2171. * @param min defines the lower range value
  2172. * @param max defines the upper range value
  2173. * @param result defines the Vector3 where to store the result
  2174. */
  2175. public static ClampToRef(value: DeepImmutable<Vector3>, min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, result: Vector3): void {
  2176. var x = value.x;
  2177. x = (x > max.x) ? max.x : x;
  2178. x = (x < min.x) ? min.x : x;
  2179. var y = value.y;
  2180. y = (y > max.y) ? max.y : y;
  2181. y = (y < min.y) ? min.y : y;
  2182. var z = value.z;
  2183. z = (z > max.z) ? max.z : z;
  2184. z = (z < min.z) ? min.z : z;
  2185. result.copyFromFloats(x, y, z);
  2186. }
  2187. /**
  2188. * Returns a new Vector3 located for "amount" (float) on the Hermite interpolation spline defined by the vectors "value1", "tangent1", "value2", "tangent2"
  2189. * @param value1 defines the first control point
  2190. * @param tangent1 defines the first tangent vector
  2191. * @param value2 defines the second control point
  2192. * @param tangent2 defines the second tangent vector
  2193. * @param amount defines the amount on the interpolation spline (between 0 and 1)
  2194. * @returns the new Vector3
  2195. */
  2196. public static Hermite(value1: DeepImmutable<Vector3>, tangent1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>, tangent2: DeepImmutable<Vector3>, amount: number): Vector3 {
  2197. var squared = amount * amount;
  2198. var cubed = amount * squared;
  2199. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  2200. var part2 = (-2.0 * cubed) + (3.0 * squared);
  2201. var part3 = (cubed - (2.0 * squared)) + amount;
  2202. var part4 = cubed - squared;
  2203. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  2204. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  2205. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  2206. return new Vector3(x, y, z);
  2207. }
  2208. /**
  2209. * Returns a new Vector3 located for "amount" (float) on the linear interpolation between the vectors "start" and "end"
  2210. * @param start defines the start value
  2211. * @param end defines the end value
  2212. * @param amount max defines amount between both (between 0 and 1)
  2213. * @returns the new Vector3
  2214. */
  2215. public static Lerp(start: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, amount: number): Vector3 {
  2216. var result = new Vector3(0, 0, 0);
  2217. Vector3.LerpToRef(start, end, amount, result);
  2218. return result;
  2219. }
  2220. /**
  2221. * Sets the given vector "result" with the result of the linear interpolation from the vector "start" for "amount" to the vector "end"
  2222. * @param start defines the start value
  2223. * @param end defines the end value
  2224. * @param amount max defines amount between both (between 0 and 1)
  2225. * @param result defines the Vector3 where to store the result
  2226. */
  2227. public static LerpToRef(start: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, amount: number, result: Vector3): void {
  2228. result.x = start.x + ((end.x - start.x) * amount);
  2229. result.y = start.y + ((end.y - start.y) * amount);
  2230. result.z = start.z + ((end.z - start.z) * amount);
  2231. }
  2232. /**
  2233. * Returns the dot product (float) between the vectors "left" and "right"
  2234. * @param left defines the left operand
  2235. * @param right defines the right operand
  2236. * @returns the dot product
  2237. */
  2238. public static Dot(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): number {
  2239. return (left.x * right.x + left.y * right.y + left.z * right.z);
  2240. }
  2241. /**
  2242. * Returns a new Vector3 as the cross product of the vectors "left" and "right"
  2243. * The cross product is then orthogonal to both "left" and "right"
  2244. * @param left defines the left operand
  2245. * @param right defines the right operand
  2246. * @returns the cross product
  2247. */
  2248. public static Cross(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  2249. var result = Vector3.Zero();
  2250. Vector3.CrossToRef(left, right, result);
  2251. return result;
  2252. }
  2253. /**
  2254. * Sets the given vector "result" with the cross product of "left" and "right"
  2255. * The cross product is then orthogonal to both "left" and "right"
  2256. * @param left defines the left operand
  2257. * @param right defines the right operand
  2258. * @param result defines the Vector3 where to store the result
  2259. */
  2260. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  2261. const x = left.y * right.z - left.z * right.y;
  2262. const y = left.z * right.x - left.x * right.z;
  2263. const z = left.x * right.y - left.y * right.x;
  2264. result.copyFromFloats(x, y, z);
  2265. }
  2266. /**
  2267. * Returns a new Vector3 as the normalization of the given vector
  2268. * @param vector defines the Vector3 to normalize
  2269. * @returns the new Vector3
  2270. */
  2271. public static Normalize(vector: DeepImmutable<Vector3>): Vector3 {
  2272. var result = Vector3.Zero();
  2273. Vector3.NormalizeToRef(vector, result);
  2274. return result;
  2275. }
  2276. /**
  2277. * Sets the given vector "result" with the normalization of the given first vector
  2278. * @param vector defines the Vector3 to normalize
  2279. * @param result defines the Vector3 where to store the result
  2280. */
  2281. public static NormalizeToRef(vector: DeepImmutable<Vector3>, result: Vector3): void {
  2282. vector.normalizeToRef(result);
  2283. }
  2284. /**
  2285. * Project a Vector3 onto screen space
  2286. * @param vector defines the Vector3 to project
  2287. * @param world defines the world matrix to use
  2288. * @param transform defines the transform (view x projection) matrix to use
  2289. * @param viewport defines the screen viewport to use
  2290. * @returns the new Vector3
  2291. */
  2292. public static Project(vector: DeepImmutable<Vector3>, world: DeepImmutable<Matrix>, transform: DeepImmutable<Matrix>, viewport: DeepImmutable<Viewport>): Vector3 {
  2293. var cw = viewport.width;
  2294. var ch = viewport.height;
  2295. var cx = viewport.x;
  2296. var cy = viewport.y;
  2297. var viewportMatrix = MathTmp.Matrix[1];
  2298. Matrix.FromValuesToRef(
  2299. cw / 2.0, 0, 0, 0,
  2300. 0, -ch / 2.0, 0, 0,
  2301. 0, 0, 0.5, 0,
  2302. cx + cw / 2.0, ch / 2.0 + cy, 0.5, 1, viewportMatrix);
  2303. var matrix = MathTmp.Matrix[0];
  2304. world.multiplyToRef(transform, matrix);
  2305. matrix.multiplyToRef(viewportMatrix, matrix);
  2306. return Vector3.TransformCoordinates(vector, matrix);
  2307. }
  2308. /** @hidden */
  2309. private static UnprojectFromInvertedMatrixToRef(source: DeepImmutable<Vector3>, matrix: DeepImmutable<Matrix>, result: Vector3) {
  2310. Vector3.TransformCoordinatesToRef(source, matrix, result);
  2311. const m = matrix.m;
  2312. var num = source.x * m[3] + source.y * m[7] + source.z * m[11] + m[15];
  2313. if (Scalar.WithinEpsilon(num, 1.0)) {
  2314. result.scaleInPlace(1.0 / num);
  2315. }
  2316. }
  2317. /**
  2318. * Unproject from screen space to object space
  2319. * @param source defines the screen space Vector3 to use
  2320. * @param viewportWidth defines the current width of the viewport
  2321. * @param viewportHeight defines the current height of the viewport
  2322. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2323. * @param transform defines the transform (view x projection) matrix to use
  2324. * @returns the new Vector3
  2325. */
  2326. public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, transform: DeepImmutable<Matrix>): Vector3 {
  2327. var matrix = MathTmp.Matrix[0];
  2328. world.multiplyToRef(transform, matrix);
  2329. matrix.invert();
  2330. source.x = source.x / viewportWidth * 2 - 1;
  2331. source.y = -(source.y / viewportHeight * 2 - 1);
  2332. const vector = new Vector3();
  2333. Vector3.UnprojectFromInvertedMatrixToRef(source, matrix, vector);
  2334. return vector;
  2335. }
  2336. /**
  2337. * Unproject from screen space to object space
  2338. * @param source defines the screen space Vector3 to use
  2339. * @param viewportWidth defines the current width of the viewport
  2340. * @param viewportHeight defines the current height of the viewport
  2341. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2342. * @param view defines the view matrix to use
  2343. * @param projection defines the projection matrix to use
  2344. * @returns the new Vector3
  2345. */
  2346. public static Unproject(source: DeepImmutable<Vector3>, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>): Vector3 {
  2347. let result = Vector3.Zero();
  2348. Vector3.UnprojectToRef(source, viewportWidth, viewportHeight, world, view, projection, result);
  2349. return result;
  2350. }
  2351. /**
  2352. * Unproject from screen space to object space
  2353. * @param source defines the screen space Vector3 to use
  2354. * @param viewportWidth defines the current width of the viewport
  2355. * @param viewportHeight defines the current height of the viewport
  2356. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2357. * @param view defines the view matrix to use
  2358. * @param projection defines the projection matrix to use
  2359. * @param result defines the Vector3 where to store the result
  2360. */
  2361. public static UnprojectToRef(source: DeepImmutable<Vector3>, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, result: Vector3): void {
  2362. Vector3.UnprojectFloatsToRef(source.x, source.y, source.z, viewportWidth, viewportHeight, world, view, projection, result);
  2363. }
  2364. /**
  2365. * Unproject from screen space to object space
  2366. * @param sourceX defines the screen space x coordinate to use
  2367. * @param sourceY defines the screen space y coordinate to use
  2368. * @param sourceZ defines the screen space z coordinate to use
  2369. * @param viewportWidth defines the current width of the viewport
  2370. * @param viewportHeight defines the current height of the viewport
  2371. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2372. * @param view defines the view matrix to use
  2373. * @param projection defines the projection matrix to use
  2374. * @param result defines the Vector3 where to store the result
  2375. */
  2376. public static UnprojectFloatsToRef(sourceX: float, sourceY: float, sourceZ: float, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, result: Vector3): void {
  2377. var matrix = MathTmp.Matrix[0];
  2378. world.multiplyToRef(view, matrix);
  2379. matrix.multiplyToRef(projection, matrix);
  2380. matrix.invert();
  2381. var screenSource = MathTmp.Vector3[0];
  2382. screenSource.x = sourceX / viewportWidth * 2 - 1;
  2383. screenSource.y = -(sourceY / viewportHeight * 2 - 1);
  2384. screenSource.z = 2 * sourceZ - 1.0;
  2385. Vector3.UnprojectFromInvertedMatrixToRef(screenSource, matrix, result);
  2386. }
  2387. /**
  2388. * Unproject a ray from screen space to object space
  2389. * @param sourceX defines the screen space x coordinate to use
  2390. * @param sourceY defines the screen space y coordinate to use
  2391. * @param viewportWidth defines the current width of the viewport
  2392. * @param viewportHeight defines the current height of the viewport
  2393. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2394. * @param view defines the view matrix to use
  2395. * @param projection defines the projection matrix to use
  2396. * @param ray defines the Ray where to store the result
  2397. */
  2398. public static UnprojectRayToRef(sourceX: float, sourceY: float, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, ray: Ray): void {
  2399. var matrix = MathTmp.Matrix[0];
  2400. world.multiplyToRef(view, matrix);
  2401. matrix.multiplyToRef(projection, matrix);
  2402. matrix.invert();
  2403. var nearScreenSource = MathTmp.Vector3[0];
  2404. nearScreenSource.x = sourceX / viewportWidth * 2 - 1;
  2405. nearScreenSource.y = -(sourceY / viewportHeight * 2 - 1);
  2406. nearScreenSource.z = -1.0;
  2407. var farScreenSource = MathTmp.Vector3[1].copyFromFloats(nearScreenSource.x, nearScreenSource.y, 1.0);
  2408. const nearVec3 = MathTmp.Vector3[2];
  2409. const farVec3 = MathTmp.Vector3[3];
  2410. Vector3.UnprojectFromInvertedMatrixToRef(nearScreenSource, matrix, nearVec3);
  2411. Vector3.UnprojectFromInvertedMatrixToRef(farScreenSource, matrix, farVec3);
  2412. ray.origin.copyFrom(nearVec3);
  2413. farVec3.subtractToRef(nearVec3, ray.direction);
  2414. ray.direction.normalize();
  2415. }
  2416. /**
  2417. * Gets the minimal coordinate values between two Vector3
  2418. * @param left defines the first operand
  2419. * @param right defines the second operand
  2420. * @returns the new Vector3
  2421. */
  2422. public static Minimize(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  2423. var min = left.clone();
  2424. min.minimizeInPlace(right);
  2425. return min;
  2426. }
  2427. /**
  2428. * Gets the maximal coordinate values between two Vector3
  2429. * @param left defines the first operand
  2430. * @param right defines the second operand
  2431. * @returns the new Vector3
  2432. */
  2433. public static Maximize(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  2434. var max = left.clone();
  2435. max.maximizeInPlace(right);
  2436. return max;
  2437. }
  2438. /**
  2439. * Returns the distance between the vectors "value1" and "value2"
  2440. * @param value1 defines the first operand
  2441. * @param value2 defines the second operand
  2442. * @returns the distance
  2443. */
  2444. public static Distance(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): number {
  2445. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  2446. }
  2447. /**
  2448. * Returns the squared distance between the vectors "value1" and "value2"
  2449. * @param value1 defines the first operand
  2450. * @param value2 defines the second operand
  2451. * @returns the squared distance
  2452. */
  2453. public static DistanceSquared(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): number {
  2454. var x = value1.x - value2.x;
  2455. var y = value1.y - value2.y;
  2456. var z = value1.z - value2.z;
  2457. return (x * x) + (y * y) + (z * z);
  2458. }
  2459. /**
  2460. * Returns a new Vector3 located at the center between "value1" and "value2"
  2461. * @param value1 defines the first operand
  2462. * @param value2 defines the second operand
  2463. * @returns the new Vector3
  2464. */
  2465. public static Center(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): Vector3 {
  2466. var center = value1.add(value2);
  2467. center.scaleInPlace(0.5);
  2468. return center;
  2469. }
  2470. /**
  2471. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  2472. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  2473. * to something in order to rotate it from its local system to the given target system
  2474. * Note: axis1, axis2 and axis3 are normalized during this operation
  2475. * @param axis1 defines the first axis
  2476. * @param axis2 defines the second axis
  2477. * @param axis3 defines the third axis
  2478. * @returns a new Vector3
  2479. */
  2480. public static RotationFromAxis(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>): Vector3 {
  2481. var rotation = Vector3.Zero();
  2482. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  2483. return rotation;
  2484. }
  2485. /**
  2486. * The same than RotationFromAxis but updates the given ref Vector3 parameter instead of returning a new Vector3
  2487. * @param axis1 defines the first axis
  2488. * @param axis2 defines the second axis
  2489. * @param axis3 defines the third axis
  2490. * @param ref defines the Vector3 where to store the result
  2491. */
  2492. public static RotationFromAxisToRef(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>, ref: Vector3): void {
  2493. var quat = MathTmp.Quaternion[0];
  2494. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  2495. quat.toEulerAnglesToRef(ref);
  2496. }
  2497. }
  2498. /**
  2499. * Vector4 class created for EulerAngle class conversion to Quaternion
  2500. */
  2501. export class Vector4 {
  2502. /**
  2503. * Creates a Vector4 object from the given floats.
  2504. * @param x x value of the vector
  2505. * @param y y value of the vector
  2506. * @param z z value of the vector
  2507. * @param w w value of the vector
  2508. */
  2509. constructor(
  2510. /** x value of the vector */
  2511. public x: number,
  2512. /** y value of the vector */
  2513. public y: number,
  2514. /** z value of the vector */
  2515. public z: number,
  2516. /** w value of the vector */
  2517. public w: number
  2518. ) { }
  2519. /**
  2520. * Returns the string with the Vector4 coordinates.
  2521. * @returns a string containing all the vector values
  2522. */
  2523. public toString(): string {
  2524. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  2525. }
  2526. /**
  2527. * Returns the string "Vector4".
  2528. * @returns "Vector4"
  2529. */
  2530. public getClassName(): string {
  2531. return "Vector4";
  2532. }
  2533. /**
  2534. * Returns the Vector4 hash code.
  2535. * @returns a unique hash code
  2536. */
  2537. public getHashCode(): number {
  2538. let hash = this.x || 0;
  2539. hash = (hash * 397) ^ (this.y || 0);
  2540. hash = (hash * 397) ^ (this.z || 0);
  2541. hash = (hash * 397) ^ (this.w || 0);
  2542. return hash;
  2543. }
  2544. // Operators
  2545. /**
  2546. * Returns a new array populated with 4 elements : the Vector4 coordinates.
  2547. * @returns the resulting array
  2548. */
  2549. public asArray(): number[] {
  2550. var result = new Array<number>();
  2551. this.toArray(result, 0);
  2552. return result;
  2553. }
  2554. /**
  2555. * Populates the given array from the given index with the Vector4 coordinates.
  2556. * @param array array to populate
  2557. * @param index index of the array to start at (default: 0)
  2558. * @returns the Vector4.
  2559. */
  2560. public toArray(array: FloatArray, index?: number): Vector4 {
  2561. if (index === undefined) {
  2562. index = 0;
  2563. }
  2564. array[index] = this.x;
  2565. array[index + 1] = this.y;
  2566. array[index + 2] = this.z;
  2567. array[index + 3] = this.w;
  2568. return this;
  2569. }
  2570. /**
  2571. * Adds the given vector to the current Vector4.
  2572. * @param otherVector the vector to add
  2573. * @returns the updated Vector4.
  2574. */
  2575. public addInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  2576. this.x += otherVector.x;
  2577. this.y += otherVector.y;
  2578. this.z += otherVector.z;
  2579. this.w += otherVector.w;
  2580. return this;
  2581. }
  2582. /**
  2583. * Returns a new Vector4 as the result of the addition of the current Vector4 and the given one.
  2584. * @param otherVector the vector to add
  2585. * @returns the resulting vector
  2586. */
  2587. public add(otherVector: DeepImmutable<Vector4>): Vector4 {
  2588. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  2589. }
  2590. /**
  2591. * Updates the given vector "result" with the result of the addition of the current Vector4 and the given one.
  2592. * @param otherVector the vector to add
  2593. * @param result the vector to store the result
  2594. * @returns the current Vector4.
  2595. */
  2596. public addToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2597. result.x = this.x + otherVector.x;
  2598. result.y = this.y + otherVector.y;
  2599. result.z = this.z + otherVector.z;
  2600. result.w = this.w + otherVector.w;
  2601. return this;
  2602. }
  2603. /**
  2604. * Subtract in place the given vector from the current Vector4.
  2605. * @param otherVector the vector to subtract
  2606. * @returns the updated Vector4.
  2607. */
  2608. public subtractInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  2609. this.x -= otherVector.x;
  2610. this.y -= otherVector.y;
  2611. this.z -= otherVector.z;
  2612. this.w -= otherVector.w;
  2613. return this;
  2614. }
  2615. /**
  2616. * Returns a new Vector4 with the result of the subtraction of the given vector from the current Vector4.
  2617. * @param otherVector the vector to add
  2618. * @returns the new vector with the result
  2619. */
  2620. public subtract(otherVector: DeepImmutable<Vector4>): Vector4 {
  2621. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  2622. }
  2623. /**
  2624. * Sets the given vector "result" with the result of the subtraction of the given vector from the current Vector4.
  2625. * @param otherVector the vector to subtract
  2626. * @param result the vector to store the result
  2627. * @returns the current Vector4.
  2628. */
  2629. public subtractToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2630. result.x = this.x - otherVector.x;
  2631. result.y = this.y - otherVector.y;
  2632. result.z = this.z - otherVector.z;
  2633. result.w = this.w - otherVector.w;
  2634. return this;
  2635. }
  2636. /**
  2637. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  2638. */
  2639. /**
  2640. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  2641. * @param x value to subtract
  2642. * @param y value to subtract
  2643. * @param z value to subtract
  2644. * @param w value to subtract
  2645. * @returns new vector containing the result
  2646. */
  2647. public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  2648. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  2649. }
  2650. /**
  2651. * Sets the given vector "result" set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  2652. * @param x value to subtract
  2653. * @param y value to subtract
  2654. * @param z value to subtract
  2655. * @param w value to subtract
  2656. * @param result the vector to store the result in
  2657. * @returns the current Vector4.
  2658. */
  2659. public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
  2660. result.x = this.x - x;
  2661. result.y = this.y - y;
  2662. result.z = this.z - z;
  2663. result.w = this.w - w;
  2664. return this;
  2665. }
  2666. /**
  2667. * Returns a new Vector4 set with the current Vector4 negated coordinates.
  2668. * @returns a new vector with the negated values
  2669. */
  2670. public negate(): Vector4 {
  2671. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  2672. }
  2673. /**
  2674. * Multiplies the current Vector4 coordinates by scale (float).
  2675. * @param scale the number to scale with
  2676. * @returns the updated Vector4.
  2677. */
  2678. public scaleInPlace(scale: number): Vector4 {
  2679. this.x *= scale;
  2680. this.y *= scale;
  2681. this.z *= scale;
  2682. this.w *= scale;
  2683. return this;
  2684. }
  2685. /**
  2686. * Returns a new Vector4 set with the current Vector4 coordinates multiplied by scale (float).
  2687. * @param scale the number to scale with
  2688. * @returns a new vector with the result
  2689. */
  2690. public scale(scale: number): Vector4 {
  2691. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  2692. }
  2693. /**
  2694. * Sets the given vector "result" with the current Vector4 coordinates multiplied by scale (float).
  2695. * @param scale the number to scale with
  2696. * @param result a vector to store the result in
  2697. * @returns the current Vector4.
  2698. */
  2699. public scaleToRef(scale: number, result: Vector4): Vector4 {
  2700. result.x = this.x * scale;
  2701. result.y = this.y * scale;
  2702. result.z = this.z * scale;
  2703. result.w = this.w * scale;
  2704. return this;
  2705. }
  2706. /**
  2707. * Scale the current Vector4 values by a factor and add the result to a given Vector4
  2708. * @param scale defines the scale factor
  2709. * @param result defines the Vector4 object where to store the result
  2710. * @returns the unmodified current Vector4
  2711. */
  2712. public scaleAndAddToRef(scale: number, result: Vector4): Vector4 {
  2713. result.x += this.x * scale;
  2714. result.y += this.y * scale;
  2715. result.z += this.z * scale;
  2716. result.w += this.w * scale;
  2717. return this;
  2718. }
  2719. /**
  2720. * Boolean : True if the current Vector4 coordinates are stricly equal to the given ones.
  2721. * @param otherVector the vector to compare against
  2722. * @returns true if they are equal
  2723. */
  2724. public equals(otherVector: DeepImmutable<Vector4>): boolean {
  2725. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  2726. }
  2727. /**
  2728. * Boolean : True if the current Vector4 coordinates are each beneath the distance "epsilon" from the given vector ones.
  2729. * @param otherVector vector to compare against
  2730. * @param epsilon (Default: very small number)
  2731. * @returns true if they are equal
  2732. */
  2733. public equalsWithEpsilon(otherVector: DeepImmutable<Vector4>, epsilon: number = Epsilon): boolean {
  2734. return otherVector
  2735. && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon)
  2736. && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon)
  2737. && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon)
  2738. && Scalar.WithinEpsilon(this.w, otherVector.w, epsilon);
  2739. }
  2740. /**
  2741. * Boolean : True if the given floats are strictly equal to the current Vector4 coordinates.
  2742. * @param x x value to compare against
  2743. * @param y y value to compare against
  2744. * @param z z value to compare against
  2745. * @param w w value to compare against
  2746. * @returns true if equal
  2747. */
  2748. public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
  2749. return this.x === x && this.y === y && this.z === z && this.w === w;
  2750. }
  2751. /**
  2752. * Multiplies in place the current Vector4 by the given one.
  2753. * @param otherVector vector to multiple with
  2754. * @returns the updated Vector4.
  2755. */
  2756. public multiplyInPlace(otherVector: Vector4): Vector4 {
  2757. this.x *= otherVector.x;
  2758. this.y *= otherVector.y;
  2759. this.z *= otherVector.z;
  2760. this.w *= otherVector.w;
  2761. return this;
  2762. }
  2763. /**
  2764. * Returns a new Vector4 set with the multiplication result of the current Vector4 and the given one.
  2765. * @param otherVector vector to multiple with
  2766. * @returns resulting new vector
  2767. */
  2768. public multiply(otherVector: DeepImmutable<Vector4>): Vector4 {
  2769. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  2770. }
  2771. /**
  2772. * Updates the given vector "result" with the multiplication result of the current Vector4 and the given one.
  2773. * @param otherVector vector to multiple with
  2774. * @param result vector to store the result
  2775. * @returns the current Vector4.
  2776. */
  2777. public multiplyToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2778. result.x = this.x * otherVector.x;
  2779. result.y = this.y * otherVector.y;
  2780. result.z = this.z * otherVector.z;
  2781. result.w = this.w * otherVector.w;
  2782. return this;
  2783. }
  2784. /**
  2785. * Returns a new Vector4 set with the multiplication result of the given floats and the current Vector4 coordinates.
  2786. * @param x x value multiply with
  2787. * @param y y value multiply with
  2788. * @param z z value multiply with
  2789. * @param w w value multiply with
  2790. * @returns resulting new vector
  2791. */
  2792. public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
  2793. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  2794. }
  2795. /**
  2796. * Returns a new Vector4 set with the division result of the current Vector4 by the given one.
  2797. * @param otherVector vector to devide with
  2798. * @returns resulting new vector
  2799. */
  2800. public divide(otherVector: DeepImmutable<Vector4>): Vector4 {
  2801. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  2802. }
  2803. /**
  2804. * Updates the given vector "result" with the division result of the current Vector4 by the given one.
  2805. * @param otherVector vector to devide with
  2806. * @param result vector to store the result
  2807. * @returns the current Vector4.
  2808. */
  2809. public divideToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2810. result.x = this.x / otherVector.x;
  2811. result.y = this.y / otherVector.y;
  2812. result.z = this.z / otherVector.z;
  2813. result.w = this.w / otherVector.w;
  2814. return this;
  2815. }
  2816. /**
  2817. * Divides the current Vector3 coordinates by the given ones.
  2818. * @param otherVector vector to devide with
  2819. * @returns the updated Vector3.
  2820. */
  2821. public divideInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  2822. return this.divideToRef(otherVector, this);
  2823. }
  2824. /**
  2825. * Updates the Vector4 coordinates with the minimum values between its own and the given vector ones
  2826. * @param other defines the second operand
  2827. * @returns the current updated Vector4
  2828. */
  2829. public minimizeInPlace(other: DeepImmutable<Vector4>): Vector4 {
  2830. if (other.x < this.x) { this.x = other.x; }
  2831. if (other.y < this.y) { this.y = other.y; }
  2832. if (other.z < this.z) { this.z = other.z; }
  2833. if (other.w < this.w) { this.w = other.w; }
  2834. return this;
  2835. }
  2836. /**
  2837. * Updates the Vector4 coordinates with the maximum values between its own and the given vector ones
  2838. * @param other defines the second operand
  2839. * @returns the current updated Vector4
  2840. */
  2841. public maximizeInPlace(other: DeepImmutable<Vector4>): Vector4 {
  2842. if (other.x > this.x) { this.x = other.x; }
  2843. if (other.y > this.y) { this.y = other.y; }
  2844. if (other.z > this.z) { this.z = other.z; }
  2845. if (other.w > this.w) { this.w = other.w; }
  2846. return this;
  2847. }
  2848. /**
  2849. * Gets a new Vector4 from current Vector4 floored values
  2850. * @returns a new Vector4
  2851. */
  2852. public floor(): Vector4 {
  2853. return new Vector4(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z), Math.floor(this.w));
  2854. }
  2855. /**
  2856. * Gets a new Vector4 from current Vector3 floored values
  2857. * @returns a new Vector4
  2858. */
  2859. public fract(): Vector4 {
  2860. return new Vector4(this.x - Math.floor(this.x), this.y - Math.floor(this.y), this.z - Math.floor(this.z), this.w - Math.floor(this.w));
  2861. }
  2862. // Properties
  2863. /**
  2864. * Returns the Vector4 length (float).
  2865. * @returns the length
  2866. */
  2867. public length(): number {
  2868. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2869. }
  2870. /**
  2871. * Returns the Vector4 squared length (float).
  2872. * @returns the length squared
  2873. */
  2874. public lengthSquared(): number {
  2875. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2876. }
  2877. // Methods
  2878. /**
  2879. * Normalizes in place the Vector4.
  2880. * @returns the updated Vector4.
  2881. */
  2882. public normalize(): Vector4 {
  2883. var len = this.length();
  2884. if (len === 0) {
  2885. return this;
  2886. }
  2887. return this.scaleInPlace(1.0 / len);
  2888. }
  2889. /**
  2890. * Returns a new Vector3 from the Vector4 (x, y, z) coordinates.
  2891. * @returns this converted to a new vector3
  2892. */
  2893. public toVector3(): Vector3 {
  2894. return new Vector3(this.x, this.y, this.z);
  2895. }
  2896. /**
  2897. * Returns a new Vector4 copied from the current one.
  2898. * @returns the new cloned vector
  2899. */
  2900. public clone(): Vector4 {
  2901. return new Vector4(this.x, this.y, this.z, this.w);
  2902. }
  2903. /**
  2904. * Updates the current Vector4 with the given one coordinates.
  2905. * @param source the source vector to copy from
  2906. * @returns the updated Vector4.
  2907. */
  2908. public copyFrom(source: DeepImmutable<Vector4>): Vector4 {
  2909. this.x = source.x;
  2910. this.y = source.y;
  2911. this.z = source.z;
  2912. this.w = source.w;
  2913. return this;
  2914. }
  2915. /**
  2916. * Updates the current Vector4 coordinates with the given floats.
  2917. * @param x float to copy from
  2918. * @param y float to copy from
  2919. * @param z float to copy from
  2920. * @param w float to copy from
  2921. * @returns the updated Vector4.
  2922. */
  2923. public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  2924. this.x = x;
  2925. this.y = y;
  2926. this.z = z;
  2927. this.w = w;
  2928. return this;
  2929. }
  2930. /**
  2931. * Updates the current Vector4 coordinates with the given floats.
  2932. * @param x float to set from
  2933. * @param y float to set from
  2934. * @param z float to set from
  2935. * @param w float to set from
  2936. * @returns the updated Vector4.
  2937. */
  2938. public set(x: number, y: number, z: number, w: number): Vector4 {
  2939. return this.copyFromFloats(x, y, z, w);
  2940. }
  2941. /**
  2942. * Copies the given float to the current Vector3 coordinates
  2943. * @param v defines the x, y, z and w coordinates of the operand
  2944. * @returns the current updated Vector3
  2945. */
  2946. public setAll(v: number): Vector4 {
  2947. this.x = this.y = this.z = this.w = v;
  2948. return this;
  2949. }
  2950. // Statics
  2951. /**
  2952. * Returns a new Vector4 set from the starting index of the given array.
  2953. * @param array the array to pull values from
  2954. * @param offset the offset into the array to start at
  2955. * @returns the new vector
  2956. */
  2957. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset?: number): Vector4 {
  2958. if (!offset) {
  2959. offset = 0;
  2960. }
  2961. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  2962. }
  2963. /**
  2964. * Updates the given vector "result" from the starting index of the given array.
  2965. * @param array the array to pull values from
  2966. * @param offset the offset into the array to start at
  2967. * @param result the vector to store the result in
  2968. */
  2969. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector4): void {
  2970. result.x = array[offset];
  2971. result.y = array[offset + 1];
  2972. result.z = array[offset + 2];
  2973. result.w = array[offset + 3];
  2974. }
  2975. /**
  2976. * Updates the given vector "result" from the starting index of the given Float32Array.
  2977. * @param array the array to pull values from
  2978. * @param offset the offset into the array to start at
  2979. * @param result the vector to store the result in
  2980. */
  2981. public static FromFloatArrayToRef(array: DeepImmutable<Float32Array>, offset: number, result: Vector4): void {
  2982. Vector4.FromArrayToRef(array, offset, result);
  2983. }
  2984. /**
  2985. * Updates the given vector "result" coordinates from the given floats.
  2986. * @param x float to set from
  2987. * @param y float to set from
  2988. * @param z float to set from
  2989. * @param w float to set from
  2990. * @param result the vector to the floats in
  2991. */
  2992. public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
  2993. result.x = x;
  2994. result.y = y;
  2995. result.z = z;
  2996. result.w = w;
  2997. }
  2998. /**
  2999. * Returns a new Vector4 set to (0.0, 0.0, 0.0, 0.0)
  3000. * @returns the new vector
  3001. */
  3002. public static Zero(): Vector4 {
  3003. return new Vector4(0.0, 0.0, 0.0, 0.0);
  3004. }
  3005. /**
  3006. * Returns a new Vector4 set to (1.0, 1.0, 1.0, 1.0)
  3007. * @returns the new vector
  3008. */
  3009. public static One(): Vector4 {
  3010. return new Vector4(1.0, 1.0, 1.0, 1.0);
  3011. }
  3012. /**
  3013. * Returns a new normalized Vector4 from the given one.
  3014. * @param vector the vector to normalize
  3015. * @returns the vector
  3016. */
  3017. public static Normalize(vector: DeepImmutable<Vector4>): Vector4 {
  3018. var result = Vector4.Zero();
  3019. Vector4.NormalizeToRef(vector, result);
  3020. return result;
  3021. }
  3022. /**
  3023. * Updates the given vector "result" from the normalization of the given one.
  3024. * @param vector the vector to normalize
  3025. * @param result the vector to store the result in
  3026. */
  3027. public static NormalizeToRef(vector: DeepImmutable<Vector4>, result: Vector4): void {
  3028. result.copyFrom(vector);
  3029. result.normalize();
  3030. }
  3031. /**
  3032. * Returns a vector with the minimum values from the left and right vectors
  3033. * @param left left vector to minimize
  3034. * @param right right vector to minimize
  3035. * @returns a new vector with the minimum of the left and right vector values
  3036. */
  3037. public static Minimize(left: DeepImmutable<Vector4>, right: DeepImmutable<Vector4>): Vector4 {
  3038. var min = left.clone();
  3039. min.minimizeInPlace(right);
  3040. return min;
  3041. }
  3042. /**
  3043. * Returns a vector with the maximum values from the left and right vectors
  3044. * @param left left vector to maximize
  3045. * @param right right vector to maximize
  3046. * @returns a new vector with the maximum of the left and right vector values
  3047. */
  3048. public static Maximize(left: DeepImmutable<Vector4>, right: DeepImmutable<Vector4>): Vector4 {
  3049. var max = left.clone();
  3050. max.maximizeInPlace(right);
  3051. return max;
  3052. }
  3053. /**
  3054. * Returns the distance (float) between the vectors "value1" and "value2".
  3055. * @param value1 value to calulate the distance between
  3056. * @param value2 value to calulate the distance between
  3057. * @return the distance between the two vectors
  3058. */
  3059. public static Distance(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): number {
  3060. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  3061. }
  3062. /**
  3063. * Returns the squared distance (float) between the vectors "value1" and "value2".
  3064. * @param value1 value to calulate the distance between
  3065. * @param value2 value to calulate the distance between
  3066. * @return the distance between the two vectors squared
  3067. */
  3068. public static DistanceSquared(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): number {
  3069. var x = value1.x - value2.x;
  3070. var y = value1.y - value2.y;
  3071. var z = value1.z - value2.z;
  3072. var w = value1.w - value2.w;
  3073. return (x * x) + (y * y) + (z * z) + (w * w);
  3074. }
  3075. /**
  3076. * Returns a new Vector4 located at the center between the vectors "value1" and "value2".
  3077. * @param value1 value to calulate the center between
  3078. * @param value2 value to calulate the center between
  3079. * @return the center between the two vectors
  3080. */
  3081. public static Center(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): Vector4 {
  3082. var center = value1.add(value2);
  3083. center.scaleInPlace(0.5);
  3084. return center;
  3085. }
  3086. /**
  3087. * Returns a new Vector4 set with the result of the normal transformation by the given matrix of the given vector.
  3088. * This methods computes transformed normalized direction vectors only.
  3089. * @param vector the vector to transform
  3090. * @param transformation the transformation matrix to apply
  3091. * @returns the new vector
  3092. */
  3093. public static TransformNormal(vector: DeepImmutable<Vector4>, transformation: DeepImmutable<Matrix>): Vector4 {
  3094. var result = Vector4.Zero();
  3095. Vector4.TransformNormalToRef(vector, transformation, result);
  3096. return result;
  3097. }
  3098. /**
  3099. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector.
  3100. * This methods computes transformed normalized direction vectors only.
  3101. * @param vector the vector to transform
  3102. * @param transformation the transformation matrix to apply
  3103. * @param result the vector to store the result in
  3104. */
  3105. public static TransformNormalToRef(vector: DeepImmutable<Vector4>, transformation: DeepImmutable<Matrix>, result: Vector4): void {
  3106. const m = transformation.m;
  3107. var x = (vector.x * m[0]) + (vector.y * m[4]) + (vector.z * m[8]);
  3108. var y = (vector.x * m[1]) + (vector.y * m[5]) + (vector.z * m[9]);
  3109. var z = (vector.x * m[2]) + (vector.y * m[6]) + (vector.z * m[10]);
  3110. result.x = x;
  3111. result.y = y;
  3112. result.z = z;
  3113. result.w = vector.w;
  3114. }
  3115. /**
  3116. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z, w).
  3117. * This methods computes transformed normalized direction vectors only.
  3118. * @param x value to transform
  3119. * @param y value to transform
  3120. * @param z value to transform
  3121. * @param w value to transform
  3122. * @param transformation the transformation matrix to apply
  3123. * @param result the vector to store the results in
  3124. */
  3125. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, w: number, transformation: DeepImmutable<Matrix>, result: Vector4): void {
  3126. const m = transformation.m;
  3127. result.x = (x * m[0]) + (y * m[4]) + (z * m[8]);
  3128. result.y = (x * m[1]) + (y * m[5]) + (z * m[9]);
  3129. result.z = (x * m[2]) + (y * m[6]) + (z * m[10]);
  3130. result.w = w;
  3131. }
  3132. /**
  3133. * Creates a new Vector4 from a Vector3
  3134. * @param source defines the source data
  3135. * @param w defines the 4th component (default is 0)
  3136. * @returns a new Vector4
  3137. */
  3138. public static FromVector3(source: Vector3, w: number = 0) {
  3139. return new Vector4(source.x, source.y, source.z, w);
  3140. }
  3141. }
  3142. /**
  3143. * Interface for the size containing width and height
  3144. */
  3145. export interface ISize {
  3146. /**
  3147. * Width
  3148. */
  3149. width: number;
  3150. /**
  3151. * Heighht
  3152. */
  3153. height: number;
  3154. }
  3155. /**
  3156. * Size containing widht and height
  3157. */
  3158. export class Size implements ISize {
  3159. /**
  3160. * Width
  3161. */
  3162. public width: number;
  3163. /**
  3164. * Height
  3165. */
  3166. public height: number;
  3167. /**
  3168. * Creates a Size object from the given width and height (floats).
  3169. * @param width width of the new size
  3170. * @param height height of the new size
  3171. */
  3172. public constructor(width: number, height: number) {
  3173. this.width = width;
  3174. this.height = height;
  3175. }
  3176. /**
  3177. * Returns a string with the Size width and height
  3178. * @returns a string with the Size width and height
  3179. */
  3180. public toString(): string {
  3181. return `{W: ${this.width}, H: ${this.height}}`;
  3182. }
  3183. /**
  3184. * "Size"
  3185. * @returns the string "Size"
  3186. */
  3187. public getClassName(): string {
  3188. return "Size";
  3189. }
  3190. /**
  3191. * Returns the Size hash code.
  3192. * @returns a hash code for a unique width and height
  3193. */
  3194. public getHashCode(): number {
  3195. let hash = this.width || 0;
  3196. hash = (hash * 397) ^ (this.height || 0);
  3197. return hash;
  3198. }
  3199. /**
  3200. * Updates the current size from the given one.
  3201. * @param src the given size
  3202. */
  3203. public copyFrom(src: Size) {
  3204. this.width = src.width;
  3205. this.height = src.height;
  3206. }
  3207. /**
  3208. * Updates in place the current Size from the given floats.
  3209. * @param width width of the new size
  3210. * @param height height of the new size
  3211. * @returns the updated Size.
  3212. */
  3213. public copyFromFloats(width: number, height: number): Size {
  3214. this.width = width;
  3215. this.height = height;
  3216. return this;
  3217. }
  3218. /**
  3219. * Updates in place the current Size from the given floats.
  3220. * @param width width to set
  3221. * @param height height to set
  3222. * @returns the updated Size.
  3223. */
  3224. public set(width: number, height: number): Size {
  3225. return this.copyFromFloats(width, height);
  3226. }
  3227. /**
  3228. * Multiplies the width and height by numbers
  3229. * @param w factor to multiple the width by
  3230. * @param h factor to multiple the height by
  3231. * @returns a new Size set with the multiplication result of the current Size and the given floats.
  3232. */
  3233. public multiplyByFloats(w: number, h: number): Size {
  3234. return new Size(this.width * w, this.height * h);
  3235. }
  3236. /**
  3237. * Clones the size
  3238. * @returns a new Size copied from the given one.
  3239. */
  3240. public clone(): Size {
  3241. return new Size(this.width, this.height);
  3242. }
  3243. /**
  3244. * True if the current Size and the given one width and height are strictly equal.
  3245. * @param other the other size to compare against
  3246. * @returns True if the current Size and the given one width and height are strictly equal.
  3247. */
  3248. public equals(other: Size): boolean {
  3249. if (!other) {
  3250. return false;
  3251. }
  3252. return (this.width === other.width) && (this.height === other.height);
  3253. }
  3254. /**
  3255. * The surface of the Size : width * height (float).
  3256. */
  3257. public get surface(): number {
  3258. return this.width * this.height;
  3259. }
  3260. /**
  3261. * Create a new size of zero
  3262. * @returns a new Size set to (0.0, 0.0)
  3263. */
  3264. public static Zero(): Size {
  3265. return new Size(0.0, 0.0);
  3266. }
  3267. /**
  3268. * Sums the width and height of two sizes
  3269. * @param otherSize size to add to this size
  3270. * @returns a new Size set as the addition result of the current Size and the given one.
  3271. */
  3272. public add(otherSize: Size): Size {
  3273. let r = new Size(this.width + otherSize.width, this.height + otherSize.height);
  3274. return r;
  3275. }
  3276. /**
  3277. * Subtracts the width and height of two
  3278. * @param otherSize size to subtract to this size
  3279. * @returns a new Size set as the subtraction result of the given one from the current Size.
  3280. */
  3281. public subtract(otherSize: Size): Size {
  3282. let r = new Size(this.width - otherSize.width, this.height - otherSize.height);
  3283. return r;
  3284. }
  3285. /**
  3286. * Creates a new Size set at the linear interpolation "amount" between "start" and "end"
  3287. * @param start starting size to lerp between
  3288. * @param end end size to lerp between
  3289. * @param amount amount to lerp between the start and end values
  3290. * @returns a new Size set at the linear interpolation "amount" between "start" and "end"
  3291. */
  3292. public static Lerp(start: Size, end: Size, amount: number): Size {
  3293. var w = start.width + ((end.width - start.width) * amount);
  3294. var h = start.height + ((end.height - start.height) * amount);
  3295. return new Size(w, h);
  3296. }
  3297. }
  3298. /**
  3299. * Class used to store quaternion data
  3300. * @see https://en.wikipedia.org/wiki/Quaternion
  3301. * @see http://doc.babylonjs.com/features/position,_rotation,_scaling
  3302. */
  3303. export class Quaternion {
  3304. /**
  3305. * Creates a new Quaternion from the given floats
  3306. * @param x defines the first component (0 by default)
  3307. * @param y defines the second component (0 by default)
  3308. * @param z defines the third component (0 by default)
  3309. * @param w defines the fourth component (1.0 by default)
  3310. */
  3311. constructor(
  3312. /** defines the first component (0 by default) */
  3313. public x: number = 0.0,
  3314. /** defines the second component (0 by default) */
  3315. public y: number = 0.0,
  3316. /** defines the third component (0 by default) */
  3317. public z: number = 0.0,
  3318. /** defines the fourth component (1.0 by default) */
  3319. public w: number = 1.0) {
  3320. }
  3321. /**
  3322. * Gets a string representation for the current quaternion
  3323. * @returns a string with the Quaternion coordinates
  3324. */
  3325. public toString(): string {
  3326. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  3327. }
  3328. /**
  3329. * Gets the class name of the quaternion
  3330. * @returns the string "Quaternion"
  3331. */
  3332. public getClassName(): string {
  3333. return "Quaternion";
  3334. }
  3335. /**
  3336. * Gets a hash code for this quaternion
  3337. * @returns the quaternion hash code
  3338. */
  3339. public getHashCode(): number {
  3340. let hash = this.x || 0;
  3341. hash = (hash * 397) ^ (this.y || 0);
  3342. hash = (hash * 397) ^ (this.z || 0);
  3343. hash = (hash * 397) ^ (this.w || 0);
  3344. return hash;
  3345. }
  3346. /**
  3347. * Copy the quaternion to an array
  3348. * @returns a new array populated with 4 elements from the quaternion coordinates
  3349. */
  3350. public asArray(): number[] {
  3351. return [this.x, this.y, this.z, this.w];
  3352. }
  3353. /**
  3354. * Check if two quaternions are equals
  3355. * @param otherQuaternion defines the second operand
  3356. * @return true if the current quaternion and the given one coordinates are strictly equals
  3357. */
  3358. public equals(otherQuaternion: DeepImmutable<Quaternion>): boolean {
  3359. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  3360. }
  3361. /**
  3362. * Clone the current quaternion
  3363. * @returns a new quaternion copied from the current one
  3364. */
  3365. public clone(): Quaternion {
  3366. return new Quaternion(this.x, this.y, this.z, this.w);
  3367. }
  3368. /**
  3369. * Copy a quaternion to the current one
  3370. * @param other defines the other quaternion
  3371. * @returns the updated current quaternion
  3372. */
  3373. public copyFrom(other: DeepImmutable<Quaternion>): Quaternion {
  3374. this.x = other.x;
  3375. this.y = other.y;
  3376. this.z = other.z;
  3377. this.w = other.w;
  3378. return this;
  3379. }
  3380. /**
  3381. * Updates the current quaternion with the given float coordinates
  3382. * @param x defines the x coordinate
  3383. * @param y defines the y coordinate
  3384. * @param z defines the z coordinate
  3385. * @param w defines the w coordinate
  3386. * @returns the updated current quaternion
  3387. */
  3388. public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
  3389. this.x = x;
  3390. this.y = y;
  3391. this.z = z;
  3392. this.w = w;
  3393. return this;
  3394. }
  3395. /**
  3396. * Updates the current quaternion from the given float coordinates
  3397. * @param x defines the x coordinate
  3398. * @param y defines the y coordinate
  3399. * @param z defines the z coordinate
  3400. * @param w defines the w coordinate
  3401. * @returns the updated current quaternion
  3402. */
  3403. public set(x: number, y: number, z: number, w: number): Quaternion {
  3404. return this.copyFromFloats(x, y, z, w);
  3405. }
  3406. /**
  3407. * Adds two quaternions
  3408. * @param other defines the second operand
  3409. * @returns a new quaternion as the addition result of the given one and the current quaternion
  3410. */
  3411. public add(other: DeepImmutable<Quaternion>): Quaternion {
  3412. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  3413. }
  3414. /**
  3415. * Add a quaternion to the current one
  3416. * @param other defines the quaternion to add
  3417. * @returns the current quaternion
  3418. */
  3419. public addInPlace(other: DeepImmutable<Quaternion>): Quaternion {
  3420. this.x += other.x;
  3421. this.y += other.y;
  3422. this.z += other.z;
  3423. this.w += other.w;
  3424. return this;
  3425. }
  3426. /**
  3427. * Subtract two quaternions
  3428. * @param other defines the second operand
  3429. * @returns a new quaternion as the subtraction result of the given one from the current one
  3430. */
  3431. public subtract(other: Quaternion): Quaternion {
  3432. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  3433. }
  3434. /**
  3435. * Multiplies the current quaternion by a scale factor
  3436. * @param value defines the scale factor
  3437. * @returns a new quaternion set by multiplying the current quaternion coordinates by the float "scale"
  3438. */
  3439. public scale(value: number): Quaternion {
  3440. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  3441. }
  3442. /**
  3443. * Scale the current quaternion values by a factor and stores the result to a given quaternion
  3444. * @param scale defines the scale factor
  3445. * @param result defines the Quaternion object where to store the result
  3446. * @returns the unmodified current quaternion
  3447. */
  3448. public scaleToRef(scale: number, result: Quaternion): Quaternion {
  3449. result.x = this.x * scale;
  3450. result.y = this.y * scale;
  3451. result.z = this.z * scale;
  3452. result.w = this.w * scale;
  3453. return this;
  3454. }
  3455. /**
  3456. * Multiplies in place the current quaternion by a scale factor
  3457. * @param value defines the scale factor
  3458. * @returns the current modified quaternion
  3459. */
  3460. public scaleInPlace(value: number): Quaternion {
  3461. this.x *= value;
  3462. this.y *= value;
  3463. this.z *= value;
  3464. this.w *= value;
  3465. return this;
  3466. }
  3467. /**
  3468. * Scale the current quaternion values by a factor and add the result to a given quaternion
  3469. * @param scale defines the scale factor
  3470. * @param result defines the Quaternion object where to store the result
  3471. * @returns the unmodified current quaternion
  3472. */
  3473. public scaleAndAddToRef(scale: number, result: Quaternion): Quaternion {
  3474. result.x += this.x * scale;
  3475. result.y += this.y * scale;
  3476. result.z += this.z * scale;
  3477. result.w += this.w * scale;
  3478. return this;
  3479. }
  3480. /**
  3481. * Multiplies two quaternions
  3482. * @param q1 defines the second operand
  3483. * @returns a new quaternion set as the multiplication result of the current one with the given one "q1"
  3484. */
  3485. public multiply(q1: DeepImmutable<Quaternion>): Quaternion {
  3486. var result = new Quaternion(0, 0, 0, 1.0);
  3487. this.multiplyToRef(q1, result);
  3488. return result;
  3489. }
  3490. /**
  3491. * Sets the given "result" as the the multiplication result of the current one with the given one "q1"
  3492. * @param q1 defines the second operand
  3493. * @param result defines the target quaternion
  3494. * @returns the current quaternion
  3495. */
  3496. public multiplyToRef(q1: DeepImmutable<Quaternion>, result: Quaternion): Quaternion {
  3497. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  3498. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  3499. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  3500. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  3501. result.copyFromFloats(x, y, z, w);
  3502. return this;
  3503. }
  3504. /**
  3505. * Updates the current quaternion with the multiplication of itself with the given one "q1"
  3506. * @param q1 defines the second operand
  3507. * @returns the currentupdated quaternion
  3508. */
  3509. public multiplyInPlace(q1: DeepImmutable<Quaternion>): Quaternion {
  3510. this.multiplyToRef(q1, this);
  3511. return this;
  3512. }
  3513. /**
  3514. * Conjugates (1-q) the current quaternion and stores the result in the given quaternion
  3515. * @param ref defines the target quaternion
  3516. * @returns the current quaternion
  3517. */
  3518. public conjugateToRef(ref: Quaternion): Quaternion {
  3519. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  3520. return this;
  3521. }
  3522. /**
  3523. * Conjugates in place (1-q) the current quaternion
  3524. * @returns the current updated quaternion
  3525. */
  3526. public conjugateInPlace(): Quaternion {
  3527. this.x *= -1;
  3528. this.y *= -1;
  3529. this.z *= -1;
  3530. return this;
  3531. }
  3532. /**
  3533. * Conjugates in place (1-q) the current quaternion
  3534. * @returns a new quaternion
  3535. */
  3536. public conjugate(): Quaternion {
  3537. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  3538. return result;
  3539. }
  3540. /**
  3541. * Gets length of current quaternion
  3542. * @returns the quaternion length (float)
  3543. */
  3544. public length(): number {
  3545. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  3546. }
  3547. /**
  3548. * Normalize in place the current quaternion
  3549. * @returns the current updated quaternion
  3550. */
  3551. public normalize(): Quaternion {
  3552. var length = 1.0 / this.length();
  3553. this.x *= length;
  3554. this.y *= length;
  3555. this.z *= length;
  3556. this.w *= length;
  3557. return this;
  3558. }
  3559. /**
  3560. * Returns a new Vector3 set with the Euler angles translated from the current quaternion
  3561. * @param order is a reserved parameter and is ignore for now
  3562. * @returns a new Vector3 containing the Euler angles
  3563. */
  3564. public toEulerAngles(order = "YZX"): Vector3 {
  3565. var result = Vector3.Zero();
  3566. this.toEulerAnglesToRef(result, order);
  3567. return result;
  3568. }
  3569. /**
  3570. * Sets the given vector3 "result" with the Euler angles translated from the current quaternion
  3571. * @param result defines the vector which will be filled with the Euler angles
  3572. * @param order is a reserved parameter and is ignore for now
  3573. * @returns the current unchanged quaternion
  3574. */
  3575. public toEulerAnglesToRef(result: Vector3, order = "YZX"): Quaternion {
  3576. var qz = this.z;
  3577. var qx = this.x;
  3578. var qy = this.y;
  3579. var qw = this.w;
  3580. var sqw = qw * qw;
  3581. var sqz = qz * qz;
  3582. var sqx = qx * qx;
  3583. var sqy = qy * qy;
  3584. var zAxisY = qy * qz - qx * qw;
  3585. var limit = .4999999;
  3586. if (zAxisY < -limit) {
  3587. result.y = 2 * Math.atan2(qy, qw);
  3588. result.x = Math.PI / 2;
  3589. result.z = 0;
  3590. } else if (zAxisY > limit) {
  3591. result.y = 2 * Math.atan2(qy, qw);
  3592. result.x = -Math.PI / 2;
  3593. result.z = 0;
  3594. } else {
  3595. result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
  3596. result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
  3597. result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
  3598. }
  3599. return this;
  3600. }
  3601. /**
  3602. * Updates the given rotation matrix with the current quaternion values
  3603. * @param result defines the target matrix
  3604. * @returns the current unchanged quaternion
  3605. */
  3606. public toRotationMatrix(result: Matrix): Quaternion {
  3607. Matrix.FromQuaternionToRef(this, result);
  3608. return this;
  3609. }
  3610. /**
  3611. * Updates the current quaternion from the given rotation matrix values
  3612. * @param matrix defines the source matrix
  3613. * @returns the current updated quaternion
  3614. */
  3615. public fromRotationMatrix(matrix: DeepImmutable<Matrix>): Quaternion {
  3616. Quaternion.FromRotationMatrixToRef(matrix, this);
  3617. return this;
  3618. }
  3619. // Statics
  3620. /**
  3621. * Creates a new quaternion from a rotation matrix
  3622. * @param matrix defines the source matrix
  3623. * @returns a new quaternion created from the given rotation matrix values
  3624. */
  3625. public static FromRotationMatrix(matrix: DeepImmutable<Matrix>): Quaternion {
  3626. var result = new Quaternion();
  3627. Quaternion.FromRotationMatrixToRef(matrix, result);
  3628. return result;
  3629. }
  3630. /**
  3631. * Updates the given quaternion with the given rotation matrix values
  3632. * @param matrix defines the source matrix
  3633. * @param result defines the target quaternion
  3634. */
  3635. public static FromRotationMatrixToRef(matrix: DeepImmutable<Matrix>, result: Quaternion): void {
  3636. var data = matrix.m;
  3637. var m11 = data[0], m12 = data[4], m13 = data[8];
  3638. var m21 = data[1], m22 = data[5], m23 = data[9];
  3639. var m31 = data[2], m32 = data[6], m33 = data[10];
  3640. var trace = m11 + m22 + m33;
  3641. var s;
  3642. if (trace > 0) {
  3643. s = 0.5 / Math.sqrt(trace + 1.0);
  3644. result.w = 0.25 / s;
  3645. result.x = (m32 - m23) * s;
  3646. result.y = (m13 - m31) * s;
  3647. result.z = (m21 - m12) * s;
  3648. } else if (m11 > m22 && m11 > m33) {
  3649. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  3650. result.w = (m32 - m23) / s;
  3651. result.x = 0.25 * s;
  3652. result.y = (m12 + m21) / s;
  3653. result.z = (m13 + m31) / s;
  3654. } else if (m22 > m33) {
  3655. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  3656. result.w = (m13 - m31) / s;
  3657. result.x = (m12 + m21) / s;
  3658. result.y = 0.25 * s;
  3659. result.z = (m23 + m32) / s;
  3660. } else {
  3661. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  3662. result.w = (m21 - m12) / s;
  3663. result.x = (m13 + m31) / s;
  3664. result.y = (m23 + m32) / s;
  3665. result.z = 0.25 * s;
  3666. }
  3667. }
  3668. /**
  3669. * Returns the dot product (float) between the quaternions "left" and "right"
  3670. * @param left defines the left operand
  3671. * @param right defines the right operand
  3672. * @returns the dot product
  3673. */
  3674. public static Dot(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>): number {
  3675. return (left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w);
  3676. }
  3677. /**
  3678. * Checks if the two quaternions are close to each other
  3679. * @param quat0 defines the first quaternion to check
  3680. * @param quat1 defines the second quaternion to check
  3681. * @returns true if the two quaternions are close to each other
  3682. */
  3683. public static AreClose(quat0: DeepImmutable<Quaternion>, quat1: DeepImmutable<Quaternion>): boolean {
  3684. let dot = Quaternion.Dot(quat0, quat1);
  3685. return dot >= 0;
  3686. }
  3687. /**
  3688. * Creates an empty quaternion
  3689. * @returns a new quaternion set to (0.0, 0.0, 0.0)
  3690. */
  3691. public static Zero(): Quaternion {
  3692. return new Quaternion(0.0, 0.0, 0.0, 0.0);
  3693. }
  3694. /**
  3695. * Inverse a given quaternion
  3696. * @param q defines the source quaternion
  3697. * @returns a new quaternion as the inverted current quaternion
  3698. */
  3699. public static Inverse(q: DeepImmutable<Quaternion>): Quaternion {
  3700. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  3701. }
  3702. /**
  3703. * Inverse a given quaternion
  3704. * @param q defines the source quaternion
  3705. * @param result the quaternion the result will be stored in
  3706. * @returns the result quaternion
  3707. */
  3708. public static InverseToRef(q: Quaternion, result: Quaternion): Quaternion {
  3709. result.set(-q.x, -q.y, -q.z, q.w);
  3710. return result;
  3711. }
  3712. /**
  3713. * Creates an identity quaternion
  3714. * @returns the identity quaternion
  3715. */
  3716. public static Identity(): Quaternion {
  3717. return new Quaternion(0.0, 0.0, 0.0, 1.0);
  3718. }
  3719. /**
  3720. * Gets a boolean indicating if the given quaternion is identity
  3721. * @param quaternion defines the quaternion to check
  3722. * @returns true if the quaternion is identity
  3723. */
  3724. public static IsIdentity(quaternion: DeepImmutable<Quaternion>): boolean {
  3725. return quaternion && quaternion.x === 0 && quaternion.y === 0 && quaternion.z === 0 && quaternion.w === 1;
  3726. }
  3727. /**
  3728. * Creates a quaternion from a rotation around an axis
  3729. * @param axis defines the axis to use
  3730. * @param angle defines the angle to use
  3731. * @returns a new quaternion created from the given axis (Vector3) and angle in radians (float)
  3732. */
  3733. public static RotationAxis(axis: DeepImmutable<Vector3>, angle: number): Quaternion {
  3734. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  3735. }
  3736. /**
  3737. * Creates a rotation around an axis and stores it into the given quaternion
  3738. * @param axis defines the axis to use
  3739. * @param angle defines the angle to use
  3740. * @param result defines the target quaternion
  3741. * @returns the target quaternion
  3742. */
  3743. public static RotationAxisToRef(axis: DeepImmutable<Vector3>, angle: number, result: Quaternion): Quaternion {
  3744. var sin = Math.sin(angle / 2);
  3745. axis.normalize();
  3746. result.w = Math.cos(angle / 2);
  3747. result.x = axis.x * sin;
  3748. result.y = axis.y * sin;
  3749. result.z = axis.z * sin;
  3750. return result;
  3751. }
  3752. /**
  3753. * Creates a new quaternion from data stored into an array
  3754. * @param array defines the data source
  3755. * @param offset defines the offset in the source array where the data starts
  3756. * @returns a new quaternion
  3757. */
  3758. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset?: number): Quaternion {
  3759. if (!offset) {
  3760. offset = 0;
  3761. }
  3762. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  3763. }
  3764. /**
  3765. * Create a quaternion from Euler rotation angles
  3766. * @param x Pitch
  3767. * @param y Yaw
  3768. * @param z Roll
  3769. * @returns the new Quaternion
  3770. */
  3771. public static FromEulerAngles(x: number, y: number, z: number): Quaternion {
  3772. var q = new Quaternion();
  3773. Quaternion.RotationYawPitchRollToRef(y, x, z, q);
  3774. return q;
  3775. }
  3776. /**
  3777. * Updates a quaternion from Euler rotation angles
  3778. * @param x Pitch
  3779. * @param y Yaw
  3780. * @param z Roll
  3781. * @param result the quaternion to store the result
  3782. * @returns the updated quaternion
  3783. */
  3784. public static FromEulerAnglesToRef(x: number, y: number, z: number, result: Quaternion): Quaternion {
  3785. Quaternion.RotationYawPitchRollToRef(y, x, z, result);
  3786. return result;
  3787. }
  3788. /**
  3789. * Create a quaternion from Euler rotation vector
  3790. * @param vec the Euler vector (x Pitch, y Yaw, z Roll)
  3791. * @returns the new Quaternion
  3792. */
  3793. public static FromEulerVector(vec: DeepImmutable<Vector3>): Quaternion {
  3794. var q = new Quaternion();
  3795. Quaternion.RotationYawPitchRollToRef(vec.y, vec.x, vec.z, q);
  3796. return q;
  3797. }
  3798. /**
  3799. * Updates a quaternion from Euler rotation vector
  3800. * @param vec the Euler vector (x Pitch, y Yaw, z Roll)
  3801. * @param result the quaternion to store the result
  3802. * @returns the updated quaternion
  3803. */
  3804. public static FromEulerVectorToRef(vec: DeepImmutable<Vector3>, result: Quaternion): Quaternion {
  3805. Quaternion.RotationYawPitchRollToRef(vec.y, vec.x, vec.z, result);
  3806. return result;
  3807. }
  3808. /**
  3809. * Creates a new quaternion from the given Euler float angles (y, x, z)
  3810. * @param yaw defines the rotation around Y axis
  3811. * @param pitch defines the rotation around X axis
  3812. * @param roll defines the rotation around Z axis
  3813. * @returns the new quaternion
  3814. */
  3815. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  3816. var q = new Quaternion();
  3817. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  3818. return q;
  3819. }
  3820. /**
  3821. * Creates a new rotation from the given Euler float angles (y, x, z) and stores it in the target quaternion
  3822. * @param yaw defines the rotation around Y axis
  3823. * @param pitch defines the rotation around X axis
  3824. * @param roll defines the rotation around Z axis
  3825. * @param result defines the target quaternion
  3826. */
  3827. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  3828. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  3829. var halfRoll = roll * 0.5;
  3830. var halfPitch = pitch * 0.5;
  3831. var halfYaw = yaw * 0.5;
  3832. var sinRoll = Math.sin(halfRoll);
  3833. var cosRoll = Math.cos(halfRoll);
  3834. var sinPitch = Math.sin(halfPitch);
  3835. var cosPitch = Math.cos(halfPitch);
  3836. var sinYaw = Math.sin(halfYaw);
  3837. var cosYaw = Math.cos(halfYaw);
  3838. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  3839. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  3840. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  3841. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  3842. }
  3843. /**
  3844. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation
  3845. * @param alpha defines the rotation around first axis
  3846. * @param beta defines the rotation around second axis
  3847. * @param gamma defines the rotation around third axis
  3848. * @returns the new quaternion
  3849. */
  3850. public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
  3851. var result = new Quaternion();
  3852. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  3853. return result;
  3854. }
  3855. /**
  3856. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation and stores it in the target quaternion
  3857. * @param alpha defines the rotation around first axis
  3858. * @param beta defines the rotation around second axis
  3859. * @param gamma defines the rotation around third axis
  3860. * @param result defines the target quaternion
  3861. */
  3862. public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
  3863. // Produces a quaternion from Euler angles in the z-x-z orientation
  3864. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  3865. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  3866. var halfBeta = beta * 0.5;
  3867. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  3868. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  3869. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  3870. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  3871. }
  3872. /**
  3873. * Creates a new quaternion containing the rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system (axis1, axis2 and axis3 are normalized during this operation)
  3874. * @param axis1 defines the first axis
  3875. * @param axis2 defines the second axis
  3876. * @param axis3 defines the third axis
  3877. * @returns the new quaternion
  3878. */
  3879. public static RotationQuaternionFromAxis(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>): Quaternion {
  3880. var quat = new Quaternion(0.0, 0.0, 0.0, 0.0);
  3881. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  3882. return quat;
  3883. }
  3884. /**
  3885. * Creates a rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system (axis1, axis2 and axis3 are normalized during this operation) and stores it in the target quaternion
  3886. * @param axis1 defines the first axis
  3887. * @param axis2 defines the second axis
  3888. * @param axis3 defines the third axis
  3889. * @param ref defines the target quaternion
  3890. */
  3891. public static RotationQuaternionFromAxisToRef(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>, ref: Quaternion): void {
  3892. var rotMat = MathTmp.Matrix[0];
  3893. Matrix.FromXYZAxesToRef(axis1.normalize(), axis2.normalize(), axis3.normalize(), rotMat);
  3894. Quaternion.FromRotationMatrixToRef(rotMat, ref);
  3895. }
  3896. /**
  3897. * Interpolates between two quaternions
  3898. * @param left defines first quaternion
  3899. * @param right defines second quaternion
  3900. * @param amount defines the gradient to use
  3901. * @returns the new interpolated quaternion
  3902. */
  3903. public static Slerp(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>, amount: number): Quaternion {
  3904. var result = Quaternion.Identity();
  3905. Quaternion.SlerpToRef(left, right, amount, result);
  3906. return result;
  3907. }
  3908. /**
  3909. * Interpolates between two quaternions and stores it into a target quaternion
  3910. * @param left defines first quaternion
  3911. * @param right defines second quaternion
  3912. * @param amount defines the gradient to use
  3913. * @param result defines the target quaternion
  3914. */
  3915. public static SlerpToRef(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>, amount: number, result: Quaternion): void {
  3916. var num2;
  3917. var num3;
  3918. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  3919. var flag = false;
  3920. if (num4 < 0) {
  3921. flag = true;
  3922. num4 = -num4;
  3923. }
  3924. if (num4 > 0.999999) {
  3925. num3 = 1 - amount;
  3926. num2 = flag ? -amount : amount;
  3927. }
  3928. else {
  3929. var num5 = Math.acos(num4);
  3930. var num6 = (1.0 / Math.sin(num5));
  3931. num3 = (Math.sin((1.0 - amount) * num5)) * num6;
  3932. num2 = flag ? ((-Math.sin(amount * num5)) * num6) : ((Math.sin(amount * num5)) * num6);
  3933. }
  3934. result.x = (num3 * left.x) + (num2 * right.x);
  3935. result.y = (num3 * left.y) + (num2 * right.y);
  3936. result.z = (num3 * left.z) + (num2 * right.z);
  3937. result.w = (num3 * left.w) + (num2 * right.w);
  3938. }
  3939. /**
  3940. * Interpolate between two quaternions using Hermite interpolation
  3941. * @param value1 defines first quaternion
  3942. * @param tangent1 defines the incoming tangent
  3943. * @param value2 defines second quaternion
  3944. * @param tangent2 defines the outgoing tangent
  3945. * @param amount defines the target quaternion
  3946. * @returns the new interpolated quaternion
  3947. */
  3948. public static Hermite(value1: DeepImmutable<Quaternion>, tangent1: DeepImmutable<Quaternion>, value2: DeepImmutable<Quaternion>, tangent2: DeepImmutable<Quaternion>, amount: number): Quaternion {
  3949. var squared = amount * amount;
  3950. var cubed = amount * squared;
  3951. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  3952. var part2 = (-2.0 * cubed) + (3.0 * squared);
  3953. var part3 = (cubed - (2.0 * squared)) + amount;
  3954. var part4 = cubed - squared;
  3955. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  3956. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  3957. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  3958. var w = (((value1.w * part1) + (value2.w * part2)) + (tangent1.w * part3)) + (tangent2.w * part4);
  3959. return new Quaternion(x, y, z, w);
  3960. }
  3961. }
  3962. /**
  3963. * Class used to store matrix data (4x4)
  3964. */
  3965. export class Matrix {
  3966. private static _updateFlagSeed = 0;
  3967. private static _identityReadOnly = Matrix.Identity() as DeepImmutable<Matrix>;
  3968. private _isIdentity = false;
  3969. private _isIdentityDirty = true;
  3970. private _isIdentity3x2 = true;
  3971. private _isIdentity3x2Dirty = true;
  3972. /**
  3973. * Gets the update flag of the matrix which is an unique number for the matrix.
  3974. * It will be incremented every time the matrix data change.
  3975. * You can use it to speed the comparison between two versions of the same matrix.
  3976. */
  3977. public updateFlag: number;
  3978. private readonly _m: Float32Array = new Float32Array(16);
  3979. /**
  3980. * Gets the internal data of the matrix
  3981. */
  3982. public get m(): DeepImmutable<Float32Array> { return this._m; }
  3983. /** @hidden */
  3984. public _markAsUpdated() {
  3985. this.updateFlag = Matrix._updateFlagSeed++;
  3986. this._isIdentity = false;
  3987. this._isIdentity3x2 = false;
  3988. this._isIdentityDirty = true;
  3989. this._isIdentity3x2Dirty = true;
  3990. }
  3991. /** @hidden */
  3992. private _updateIdentityStatus(isIdentity: boolean, isIdentityDirty: boolean = false, isIdentity3x2: boolean = false, isIdentity3x2Dirty: boolean = true) {
  3993. this.updateFlag = Matrix._updateFlagSeed++;
  3994. this._isIdentity = isIdentity;
  3995. this._isIdentity3x2 = isIdentity || isIdentity3x2;
  3996. this._isIdentityDirty = this._isIdentity ? false : isIdentityDirty;
  3997. this._isIdentity3x2Dirty = this._isIdentity3x2 ? false : isIdentity3x2Dirty;
  3998. }
  3999. /**
  4000. * Creates an empty matrix (filled with zeros)
  4001. */
  4002. public constructor() {
  4003. this._updateIdentityStatus(false);
  4004. }
  4005. // Properties
  4006. /**
  4007. * Check if the current matrix is identity
  4008. * @returns true is the matrix is the identity matrix
  4009. */
  4010. public isIdentity(): boolean {
  4011. if (this._isIdentityDirty) {
  4012. this._isIdentityDirty = false;
  4013. const m = this._m;
  4014. this._isIdentity = (
  4015. m[0] === 1.0 && m[1] === 0.0 && m[2] === 0.0 && m[3] === 0.0 &&
  4016. m[4] === 0.0 && m[5] === 1.0 && m[6] === 0.0 && m[7] === 0.0 &&
  4017. m[8] === 0.0 && m[9] === 0.0 && m[10] === 1.0 && m[11] === 0.0 &&
  4018. m[12] === 0.0 && m[13] === 0.0 && m[14] === 0.0 && m[15] === 1.0
  4019. );
  4020. }
  4021. return this._isIdentity;
  4022. }
  4023. /**
  4024. * Check if the current matrix is identity as a texture matrix (3x2 store in 4x4)
  4025. * @returns true is the matrix is the identity matrix
  4026. */
  4027. public isIdentityAs3x2(): boolean {
  4028. if (this._isIdentity3x2Dirty) {
  4029. this._isIdentity3x2Dirty = false;
  4030. if (this._m[0] !== 1.0 || this._m[5] !== 1.0 || this._m[15] !== 1.0) {
  4031. this._isIdentity3x2 = false;
  4032. } else if (this._m[1] !== 0.0 || this._m[2] !== 0.0 || this._m[3] !== 0.0 ||
  4033. this._m[4] !== 0.0 || this._m[6] !== 0.0 || this._m[7] !== 0.0 ||
  4034. this._m[8] !== 0.0 || this._m[9] !== 0.0 || this._m[10] !== 0.0 || this._m[11] !== 0.0 ||
  4035. this._m[12] !== 0.0 || this._m[13] !== 0.0 || this._m[14] !== 0.0) {
  4036. this._isIdentity3x2 = false;
  4037. } else {
  4038. this._isIdentity3x2 = true;
  4039. }
  4040. }
  4041. return this._isIdentity3x2;
  4042. }
  4043. /**
  4044. * Gets the determinant of the matrix
  4045. * @returns the matrix determinant
  4046. */
  4047. public determinant(): number {
  4048. if (this._isIdentity === true) {
  4049. return 1;
  4050. }
  4051. const m = this._m;
  4052. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  4053. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  4054. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  4055. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  4056. // https://en.wikipedia.org/wiki/Laplace_expansion
  4057. // to compute the deterrminant of a 4x4 Matrix we compute the cofactors of any row or column,
  4058. // then we multiply each Cofactor by its corresponding matrix value and sum them all to get the determinant
  4059. // Cofactor(i, j) = sign(i,j) * det(Minor(i, j))
  4060. // where
  4061. // - sign(i,j) = (i+j) % 2 === 0 ? 1 : -1
  4062. // - Minor(i, j) is the 3x3 matrix we get by removing row i and column j from current Matrix
  4063. //
  4064. // Here we do that for the 1st row.
  4065. const det_22_33 = m22 * m33 - m32 * m23;
  4066. const det_21_33 = m21 * m33 - m31 * m23;
  4067. const det_21_32 = m21 * m32 - m31 * m22;
  4068. const det_20_33 = m20 * m33 - m30 * m23;
  4069. const det_20_32 = m20 * m32 - m22 * m30;
  4070. const det_20_31 = m20 * m31 - m30 * m21;
  4071. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  4072. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  4073. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  4074. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  4075. return m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  4076. }
  4077. // Methods
  4078. /**
  4079. * Returns the matrix as a Float32Array
  4080. * @returns the matrix underlying array
  4081. */
  4082. public toArray(): DeepImmutable<Float32Array> {
  4083. return this._m;
  4084. }
  4085. /**
  4086. * Returns the matrix as a Float32Array
  4087. * @returns the matrix underlying array.
  4088. */
  4089. public asArray(): DeepImmutable<Float32Array> {
  4090. return this._m;
  4091. }
  4092. /**
  4093. * Inverts the current matrix in place
  4094. * @returns the current inverted matrix
  4095. */
  4096. public invert(): Matrix {
  4097. this.invertToRef(this);
  4098. return this;
  4099. }
  4100. /**
  4101. * Sets all the matrix elements to zero
  4102. * @returns the current matrix
  4103. */
  4104. public reset(): Matrix {
  4105. Matrix.FromValuesToRef(
  4106. 0.0, 0.0, 0.0, 0.0,
  4107. 0.0, 0.0, 0.0, 0.0,
  4108. 0.0, 0.0, 0.0, 0.0,
  4109. 0.0, 0.0, 0.0, 0.0,
  4110. this
  4111. );
  4112. this._updateIdentityStatus(false);
  4113. return this;
  4114. }
  4115. /**
  4116. * Adds the current matrix with a second one
  4117. * @param other defines the matrix to add
  4118. * @returns a new matrix as the addition of the current matrix and the given one
  4119. */
  4120. public add(other: DeepImmutable<Matrix>): Matrix {
  4121. var result = new Matrix();
  4122. this.addToRef(other, result);
  4123. return result;
  4124. }
  4125. /**
  4126. * Sets the given matrix "result" to the addition of the current matrix and the given one
  4127. * @param other defines the matrix to add
  4128. * @param result defines the target matrix
  4129. * @returns the current matrix
  4130. */
  4131. public addToRef(other: DeepImmutable<Matrix>, result: Matrix): Matrix {
  4132. const m = this._m;
  4133. const resultM = result._m;
  4134. const otherM = other.m;
  4135. for (var index = 0; index < 16; index++) {
  4136. resultM[index] = m[index] + otherM[index];
  4137. }
  4138. result._markAsUpdated();
  4139. return this;
  4140. }
  4141. /**
  4142. * Adds in place the given matrix to the current matrix
  4143. * @param other defines the second operand
  4144. * @returns the current updated matrix
  4145. */
  4146. public addToSelf(other: DeepImmutable<Matrix>): Matrix {
  4147. const m = this._m;
  4148. const otherM = other.m;
  4149. for (var index = 0; index < 16; index++) {
  4150. m[index] += otherM[index];
  4151. }
  4152. this._markAsUpdated();
  4153. return this;
  4154. }
  4155. /**
  4156. * Sets the given matrix to the current inverted Matrix
  4157. * @param other defines the target matrix
  4158. * @returns the unmodified current matrix
  4159. */
  4160. public invertToRef(other: Matrix): Matrix {
  4161. if (this._isIdentity === true) {
  4162. Matrix.IdentityToRef(other);
  4163. return this;
  4164. }
  4165. // the inverse of a Matrix is the transpose of cofactor matrix divided by the determinant
  4166. const m = this._m;
  4167. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  4168. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  4169. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  4170. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  4171. const det_22_33 = m22 * m33 - m32 * m23;
  4172. const det_21_33 = m21 * m33 - m31 * m23;
  4173. const det_21_32 = m21 * m32 - m31 * m22;
  4174. const det_20_33 = m20 * m33 - m30 * m23;
  4175. const det_20_32 = m20 * m32 - m22 * m30;
  4176. const det_20_31 = m20 * m31 - m30 * m21;
  4177. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  4178. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  4179. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  4180. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  4181. const det = m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  4182. if (det === 0) {
  4183. // not invertible
  4184. other.copyFrom(this);
  4185. return this;
  4186. }
  4187. const detInv = 1 / det;
  4188. const det_12_33 = m12 * m33 - m32 * m13;
  4189. const det_11_33 = m11 * m33 - m31 * m13;
  4190. const det_11_32 = m11 * m32 - m31 * m12;
  4191. const det_10_33 = m10 * m33 - m30 * m13;
  4192. const det_10_32 = m10 * m32 - m30 * m12;
  4193. const det_10_31 = m10 * m31 - m30 * m11;
  4194. const det_12_23 = m12 * m23 - m22 * m13;
  4195. const det_11_23 = m11 * m23 - m21 * m13;
  4196. const det_11_22 = m11 * m22 - m21 * m12;
  4197. const det_10_23 = m10 * m23 - m20 * m13;
  4198. const det_10_22 = m10 * m22 - m20 * m12;
  4199. const det_10_21 = m10 * m21 - m20 * m11;
  4200. const cofact_10 = -(m01 * det_22_33 - m02 * det_21_33 + m03 * det_21_32);
  4201. const cofact_11 = +(m00 * det_22_33 - m02 * det_20_33 + m03 * det_20_32);
  4202. const cofact_12 = -(m00 * det_21_33 - m01 * det_20_33 + m03 * det_20_31);
  4203. const cofact_13 = +(m00 * det_21_32 - m01 * det_20_32 + m02 * det_20_31);
  4204. const cofact_20 = +(m01 * det_12_33 - m02 * det_11_33 + m03 * det_11_32);
  4205. const cofact_21 = -(m00 * det_12_33 - m02 * det_10_33 + m03 * det_10_32);
  4206. const cofact_22 = +(m00 * det_11_33 - m01 * det_10_33 + m03 * det_10_31);
  4207. const cofact_23 = -(m00 * det_11_32 - m01 * det_10_32 + m02 * det_10_31);
  4208. const cofact_30 = -(m01 * det_12_23 - m02 * det_11_23 + m03 * det_11_22);
  4209. const cofact_31 = +(m00 * det_12_23 - m02 * det_10_23 + m03 * det_10_22);
  4210. const cofact_32 = -(m00 * det_11_23 - m01 * det_10_23 + m03 * det_10_21);
  4211. const cofact_33 = +(m00 * det_11_22 - m01 * det_10_22 + m02 * det_10_21);
  4212. Matrix.FromValuesToRef(
  4213. cofact_00 * detInv, cofact_10 * detInv, cofact_20 * detInv, cofact_30 * detInv,
  4214. cofact_01 * detInv, cofact_11 * detInv, cofact_21 * detInv, cofact_31 * detInv,
  4215. cofact_02 * detInv, cofact_12 * detInv, cofact_22 * detInv, cofact_32 * detInv,
  4216. cofact_03 * detInv, cofact_13 * detInv, cofact_23 * detInv, cofact_33 * detInv,
  4217. other
  4218. );
  4219. return this;
  4220. }
  4221. /**
  4222. * add a value at the specified position in the current Matrix
  4223. * @param index the index of the value within the matrix. between 0 and 15.
  4224. * @param value the value to be added
  4225. * @returns the current updated matrix
  4226. */
  4227. public addAtIndex(index: number, value: number): Matrix {
  4228. this._m[index] += value;
  4229. this._markAsUpdated();
  4230. return this;
  4231. }
  4232. /**
  4233. * mutiply the specified position in the current Matrix by a value
  4234. * @param index the index of the value within the matrix. between 0 and 15.
  4235. * @param value the value to be added
  4236. * @returns the current updated matrix
  4237. */
  4238. public multiplyAtIndex(index: number, value: number): Matrix {
  4239. this._m[index] *= value;
  4240. this._markAsUpdated();
  4241. return this;
  4242. }
  4243. /**
  4244. * Inserts the translation vector (using 3 floats) in the current matrix
  4245. * @param x defines the 1st component of the translation
  4246. * @param y defines the 2nd component of the translation
  4247. * @param z defines the 3rd component of the translation
  4248. * @returns the current updated matrix
  4249. */
  4250. public setTranslationFromFloats(x: number, y: number, z: number): Matrix {
  4251. this._m[12] = x;
  4252. this._m[13] = y;
  4253. this._m[14] = z;
  4254. this._markAsUpdated();
  4255. return this;
  4256. }
  4257. /**
  4258. * Inserts the translation vector in the current matrix
  4259. * @param vector3 defines the translation to insert
  4260. * @returns the current updated matrix
  4261. */
  4262. public setTranslation(vector3: DeepImmutable<Vector3>): Matrix {
  4263. return this.setTranslationFromFloats(vector3.x, vector3.y, vector3.z);
  4264. }
  4265. /**
  4266. * Gets the translation value of the current matrix
  4267. * @returns a new Vector3 as the extracted translation from the matrix
  4268. */
  4269. public getTranslation(): Vector3 {
  4270. return new Vector3(this._m[12], this._m[13], this._m[14]);
  4271. }
  4272. /**
  4273. * Fill a Vector3 with the extracted translation from the matrix
  4274. * @param result defines the Vector3 where to store the translation
  4275. * @returns the current matrix
  4276. */
  4277. public getTranslationToRef(result: Vector3): Matrix {
  4278. result.x = this._m[12];
  4279. result.y = this._m[13];
  4280. result.z = this._m[14];
  4281. return this;
  4282. }
  4283. /**
  4284. * Remove rotation and scaling part from the matrix
  4285. * @returns the updated matrix
  4286. */
  4287. public removeRotationAndScaling(): Matrix {
  4288. const m = this.m;
  4289. Matrix.FromValuesToRef(
  4290. 1.0, 0.0, 0.0, 0.0,
  4291. 0.0, 1.0, 0.0, 0.0,
  4292. 0.0, 0.0, 1.0, 0.0,
  4293. m[12], m[13], m[14], m[15],
  4294. this
  4295. );
  4296. this._updateIdentityStatus(m[12] === 0 && m[13] === 0 && m[14] === 0 && m[15] === 1);
  4297. return this;
  4298. }
  4299. /**
  4300. * Multiply two matrices
  4301. * @param other defines the second operand
  4302. * @returns a new matrix set with the multiplication result of the current Matrix and the given one
  4303. */
  4304. public multiply(other: DeepImmutable<Matrix>): Matrix {
  4305. var result = new Matrix();
  4306. this.multiplyToRef(other, result);
  4307. return result;
  4308. }
  4309. /**
  4310. * Copy the current matrix from the given one
  4311. * @param other defines the source matrix
  4312. * @returns the current updated matrix
  4313. */
  4314. public copyFrom(other: DeepImmutable<Matrix>): Matrix {
  4315. other.copyToArray(this._m);
  4316. const o = (other as Matrix);
  4317. this._updateIdentityStatus(o._isIdentity, o._isIdentityDirty, o._isIdentity3x2, o._isIdentity3x2Dirty);
  4318. return this;
  4319. }
  4320. /**
  4321. * Populates the given array from the starting index with the current matrix values
  4322. * @param array defines the target array
  4323. * @param offset defines the offset in the target array where to start storing values
  4324. * @returns the current matrix
  4325. */
  4326. public copyToArray(array: Float32Array, offset: number = 0): Matrix {
  4327. for (var index = 0; index < 16; index++) {
  4328. array[offset + index] = this._m[index];
  4329. }
  4330. return this;
  4331. }
  4332. /**
  4333. * Sets the given matrix "result" with the multiplication result of the current Matrix and the given one
  4334. * @param other defines the second operand
  4335. * @param result defines the matrix where to store the multiplication
  4336. * @returns the current matrix
  4337. */
  4338. public multiplyToRef(other: DeepImmutable<Matrix>, result: Matrix): Matrix {
  4339. if (this._isIdentity) {
  4340. result.copyFrom(other);
  4341. return this;
  4342. }
  4343. if ((other as Matrix)._isIdentity) {
  4344. result.copyFrom(this);
  4345. return this;
  4346. }
  4347. this.multiplyToArray(other, result._m, 0);
  4348. result._markAsUpdated();
  4349. return this;
  4350. }
  4351. /**
  4352. * Sets the Float32Array "result" from the given index "offset" with the multiplication of the current matrix and the given one
  4353. * @param other defines the second operand
  4354. * @param result defines the array where to store the multiplication
  4355. * @param offset defines the offset in the target array where to start storing values
  4356. * @returns the current matrix
  4357. */
  4358. public multiplyToArray(other: DeepImmutable<Matrix>, result: Float32Array, offset: number): Matrix {
  4359. const m = this._m;
  4360. const otherM = other.m;
  4361. var tm0 = m[0], tm1 = m[1], tm2 = m[2], tm3 = m[3];
  4362. var tm4 = m[4], tm5 = m[5], tm6 = m[6], tm7 = m[7];
  4363. var tm8 = m[8], tm9 = m[9], tm10 = m[10], tm11 = m[11];
  4364. var tm12 = m[12], tm13 = m[13], tm14 = m[14], tm15 = m[15];
  4365. var om0 = otherM[0], om1 = otherM[1], om2 = otherM[2], om3 = otherM[3];
  4366. var om4 = otherM[4], om5 = otherM[5], om6 = otherM[6], om7 = otherM[7];
  4367. var om8 = otherM[8], om9 = otherM[9], om10 = otherM[10], om11 = otherM[11];
  4368. var om12 = otherM[12], om13 = otherM[13], om14 = otherM[14], om15 = otherM[15];
  4369. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  4370. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  4371. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  4372. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  4373. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  4374. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  4375. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  4376. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  4377. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  4378. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  4379. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  4380. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  4381. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  4382. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  4383. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  4384. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  4385. return this;
  4386. }
  4387. /**
  4388. * Check equality between this matrix and a second one
  4389. * @param value defines the second matrix to compare
  4390. * @returns true is the current matrix and the given one values are strictly equal
  4391. */
  4392. public equals(value: DeepImmutable<Matrix>): boolean {
  4393. const other = (value as Matrix);
  4394. if (!other) {
  4395. return false;
  4396. }
  4397. if (this._isIdentity || other._isIdentity) {
  4398. if (!this._isIdentityDirty && !other._isIdentityDirty) {
  4399. return this._isIdentity && other._isIdentity;
  4400. }
  4401. }
  4402. const m = this.m;
  4403. const om = other.m;
  4404. return (
  4405. m[0] === om[0] && m[1] === om[1] && m[2] === om[2] && m[3] === om[3] &&
  4406. m[4] === om[4] && m[5] === om[5] && m[6] === om[6] && m[7] === om[7] &&
  4407. m[8] === om[8] && m[9] === om[9] && m[10] === om[10] && m[11] === om[11] &&
  4408. m[12] === om[12] && m[13] === om[13] && m[14] === om[14] && m[15] === om[15]
  4409. );
  4410. }
  4411. /**
  4412. * Clone the current matrix
  4413. * @returns a new matrix from the current matrix
  4414. */
  4415. public clone(): Matrix {
  4416. const matrix = new Matrix();
  4417. matrix.copyFrom(this);
  4418. return matrix;
  4419. }
  4420. /**
  4421. * Returns the name of the current matrix class
  4422. * @returns the string "Matrix"
  4423. */
  4424. public getClassName(): string {
  4425. return "Matrix";
  4426. }
  4427. /**
  4428. * Gets the hash code of the current matrix
  4429. * @returns the hash code
  4430. */
  4431. public getHashCode(): number {
  4432. let hash = this._m[0] || 0;
  4433. for (let i = 1; i < 16; i++) {
  4434. hash = (hash * 397) ^ (this._m[i] || 0);
  4435. }
  4436. return hash;
  4437. }
  4438. /**
  4439. * Decomposes the current Matrix into a translation, rotation and scaling components
  4440. * @param scale defines the scale vector3 given as a reference to update
  4441. * @param rotation defines the rotation quaternion given as a reference to update
  4442. * @param translation defines the translation vector3 given as a reference to update
  4443. * @returns true if operation was successful
  4444. */
  4445. public decompose(scale?: Vector3, rotation?: Quaternion, translation?: Vector3): boolean {
  4446. if (this._isIdentity) {
  4447. if (translation) {
  4448. translation.setAll(0);
  4449. }
  4450. if (scale) {
  4451. scale.setAll(1);
  4452. }
  4453. if (rotation) {
  4454. rotation.copyFromFloats(0, 0, 0, 1);
  4455. }
  4456. return true;
  4457. }
  4458. const m = this._m;
  4459. if (translation) {
  4460. translation.copyFromFloats(m[12], m[13], m[14]);
  4461. }
  4462. scale = scale || MathTmp.Vector3[0];
  4463. scale.x = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
  4464. scale.y = Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
  4465. scale.z = Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
  4466. if (this.determinant() <= 0) {
  4467. scale.y *= -1;
  4468. }
  4469. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  4470. if (rotation) {
  4471. rotation.copyFromFloats(0.0, 0.0, 0.0, 1.0);
  4472. }
  4473. return false;
  4474. }
  4475. if (rotation) {
  4476. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  4477. Matrix.FromValuesToRef(
  4478. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  4479. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  4480. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  4481. 0.0, 0.0, 0.0, 1.0,
  4482. MathTmp.Matrix[0]
  4483. );
  4484. Quaternion.FromRotationMatrixToRef(MathTmp.Matrix[0], rotation);
  4485. }
  4486. return true;
  4487. }
  4488. /**
  4489. * Gets specific row of the matrix
  4490. * @param index defines the number of the row to get
  4491. * @returns the index-th row of the current matrix as a new Vector4
  4492. */
  4493. public getRow(index: number): Nullable<Vector4> {
  4494. if (index < 0 || index > 3) {
  4495. return null;
  4496. }
  4497. var i = index * 4;
  4498. return new Vector4(this._m[i + 0], this._m[i + 1], this._m[i + 2], this._m[i + 3]);
  4499. }
  4500. /**
  4501. * Sets the index-th row of the current matrix to the vector4 values
  4502. * @param index defines the number of the row to set
  4503. * @param row defines the target vector4
  4504. * @returns the updated current matrix
  4505. */
  4506. public setRow(index: number, row: Vector4): Matrix {
  4507. return this.setRowFromFloats(index, row.x, row.y, row.z, row.w);
  4508. }
  4509. /**
  4510. * Compute the transpose of the matrix
  4511. * @returns the new transposed matrix
  4512. */
  4513. public transpose(): Matrix {
  4514. return Matrix.Transpose(this);
  4515. }
  4516. /**
  4517. * Compute the transpose of the matrix and store it in a given matrix
  4518. * @param result defines the target matrix
  4519. * @returns the current matrix
  4520. */
  4521. public transposeToRef(result: Matrix): Matrix {
  4522. Matrix.TransposeToRef(this, result);
  4523. return this;
  4524. }
  4525. /**
  4526. * Sets the index-th row of the current matrix with the given 4 x float values
  4527. * @param index defines the row index
  4528. * @param x defines the x component to set
  4529. * @param y defines the y component to set
  4530. * @param z defines the z component to set
  4531. * @param w defines the w component to set
  4532. * @returns the updated current matrix
  4533. */
  4534. public setRowFromFloats(index: number, x: number, y: number, z: number, w: number): Matrix {
  4535. if (index < 0 || index > 3) {
  4536. return this;
  4537. }
  4538. var i = index * 4;
  4539. this._m[i + 0] = x;
  4540. this._m[i + 1] = y;
  4541. this._m[i + 2] = z;
  4542. this._m[i + 3] = w;
  4543. this._markAsUpdated();
  4544. return this;
  4545. }
  4546. /**
  4547. * Compute a new matrix set with the current matrix values multiplied by scale (float)
  4548. * @param scale defines the scale factor
  4549. * @returns a new matrix
  4550. */
  4551. public scale(scale: number): Matrix {
  4552. var result = new Matrix();
  4553. this.scaleToRef(scale, result);
  4554. return result;
  4555. }
  4556. /**
  4557. * Scale the current matrix values by a factor to a given result matrix
  4558. * @param scale defines the scale factor
  4559. * @param result defines the matrix to store the result
  4560. * @returns the current matrix
  4561. */
  4562. public scaleToRef(scale: number, result: Matrix): Matrix {
  4563. for (var index = 0; index < 16; index++) {
  4564. result._m[index] = this._m[index] * scale;
  4565. }
  4566. result._markAsUpdated();
  4567. return this;
  4568. }
  4569. /**
  4570. * Scale the current matrix values by a factor and add the result to a given matrix
  4571. * @param scale defines the scale factor
  4572. * @param result defines the Matrix to store the result
  4573. * @returns the current matrix
  4574. */
  4575. public scaleAndAddToRef(scale: number, result: Matrix): Matrix {
  4576. for (var index = 0; index < 16; index++) {
  4577. result._m[index] += this._m[index] * scale;
  4578. }
  4579. result._markAsUpdated();
  4580. return this;
  4581. }
  4582. /**
  4583. * Writes to the given matrix a normal matrix, computed from this one (using values from identity matrix for fourth row and column).
  4584. * @param ref matrix to store the result
  4585. */
  4586. public toNormalMatrix(ref: Matrix): void {
  4587. const tmp = MathTmp.Matrix[0];
  4588. this.invertToRef(tmp);
  4589. tmp.transposeToRef(ref);
  4590. var m = ref._m;
  4591. Matrix.FromValuesToRef(
  4592. m[0], m[1], m[2], 0.0,
  4593. m[4], m[5], m[6], 0.0,
  4594. m[8], m[9], m[10], 0.0,
  4595. 0.0, 0.0, 0.0, 1.0,
  4596. ref
  4597. );
  4598. }
  4599. /**
  4600. * Gets only rotation part of the current matrix
  4601. * @returns a new matrix sets to the extracted rotation matrix from the current one
  4602. */
  4603. public getRotationMatrix(): Matrix {
  4604. var result = new Matrix();
  4605. this.getRotationMatrixToRef(result);
  4606. return result;
  4607. }
  4608. /**
  4609. * Extracts the rotation matrix from the current one and sets it as the given "result"
  4610. * @param result defines the target matrix to store data to
  4611. * @returns the current matrix
  4612. */
  4613. public getRotationMatrixToRef(result: Matrix): Matrix {
  4614. const scale = MathTmp.Vector3[0];
  4615. if (!this.decompose(scale)) {
  4616. Matrix.IdentityToRef(result);
  4617. return this;
  4618. }
  4619. const m = this._m;
  4620. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  4621. Matrix.FromValuesToRef(
  4622. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  4623. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  4624. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  4625. 0.0, 0.0, 0.0, 1.0,
  4626. result
  4627. );
  4628. return this;
  4629. }
  4630. /**
  4631. * Toggles model matrix from being right handed to left handed in place and vice versa
  4632. */
  4633. public toggleModelMatrixHandInPlace() {
  4634. const m = this._m;
  4635. m[2] *= -1;
  4636. m[6] *= -1;
  4637. m[8] *= -1;
  4638. m[9] *= -1;
  4639. m[14] *= -1;
  4640. this._markAsUpdated();
  4641. }
  4642. /**
  4643. * Toggles projection matrix from being right handed to left handed in place and vice versa
  4644. */
  4645. public toggleProjectionMatrixHandInPlace() {
  4646. var m = this._m;
  4647. m[8] *= -1;
  4648. m[9] *= -1;
  4649. m[10] *= -1;
  4650. m[11] *= -1;
  4651. this._markAsUpdated();
  4652. }
  4653. // Statics
  4654. /**
  4655. * Creates a matrix from an array
  4656. * @param array defines the source array
  4657. * @param offset defines an offset in the source array
  4658. * @returns a new Matrix set from the starting index of the given array
  4659. */
  4660. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Matrix {
  4661. var result = new Matrix();
  4662. Matrix.FromArrayToRef(array, offset, result);
  4663. return result;
  4664. }
  4665. /**
  4666. * Copy the content of an array into a given matrix
  4667. * @param array defines the source array
  4668. * @param offset defines an offset in the source array
  4669. * @param result defines the target matrix
  4670. */
  4671. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Matrix) {
  4672. for (var index = 0; index < 16; index++) {
  4673. result._m[index] = array[index + offset];
  4674. }
  4675. result._markAsUpdated();
  4676. }
  4677. /**
  4678. * Stores an array into a matrix after having multiplied each component by a given factor
  4679. * @param array defines the source array
  4680. * @param offset defines the offset in the source array
  4681. * @param scale defines the scaling factor
  4682. * @param result defines the target matrix
  4683. */
  4684. public static FromFloat32ArrayToRefScaled(array: DeepImmutable<Float32Array>, offset: number, scale: number, result: Matrix) {
  4685. for (var index = 0; index < 16; index++) {
  4686. result._m[index] = array[index + offset] * scale;
  4687. }
  4688. result._markAsUpdated();
  4689. }
  4690. /**
  4691. * Gets an identity matrix that must not be updated
  4692. */
  4693. public static get IdentityReadOnly(): DeepImmutable<Matrix> {
  4694. return Matrix._identityReadOnly;
  4695. }
  4696. /**
  4697. * Stores a list of values (16) inside a given matrix
  4698. * @param initialM11 defines 1st value of 1st row
  4699. * @param initialM12 defines 2nd value of 1st row
  4700. * @param initialM13 defines 3rd value of 1st row
  4701. * @param initialM14 defines 4th value of 1st row
  4702. * @param initialM21 defines 1st value of 2nd row
  4703. * @param initialM22 defines 2nd value of 2nd row
  4704. * @param initialM23 defines 3rd value of 2nd row
  4705. * @param initialM24 defines 4th value of 2nd row
  4706. * @param initialM31 defines 1st value of 3rd row
  4707. * @param initialM32 defines 2nd value of 3rd row
  4708. * @param initialM33 defines 3rd value of 3rd row
  4709. * @param initialM34 defines 4th value of 3rd row
  4710. * @param initialM41 defines 1st value of 4th row
  4711. * @param initialM42 defines 2nd value of 4th row
  4712. * @param initialM43 defines 3rd value of 4th row
  4713. * @param initialM44 defines 4th value of 4th row
  4714. * @param result defines the target matrix
  4715. */
  4716. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  4717. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  4718. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  4719. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  4720. const m = result._m;
  4721. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  4722. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  4723. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  4724. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  4725. result._markAsUpdated();
  4726. }
  4727. /**
  4728. * Creates new matrix from a list of values (16)
  4729. * @param initialM11 defines 1st value of 1st row
  4730. * @param initialM12 defines 2nd value of 1st row
  4731. * @param initialM13 defines 3rd value of 1st row
  4732. * @param initialM14 defines 4th value of 1st row
  4733. * @param initialM21 defines 1st value of 2nd row
  4734. * @param initialM22 defines 2nd value of 2nd row
  4735. * @param initialM23 defines 3rd value of 2nd row
  4736. * @param initialM24 defines 4th value of 2nd row
  4737. * @param initialM31 defines 1st value of 3rd row
  4738. * @param initialM32 defines 2nd value of 3rd row
  4739. * @param initialM33 defines 3rd value of 3rd row
  4740. * @param initialM34 defines 4th value of 3rd row
  4741. * @param initialM41 defines 1st value of 4th row
  4742. * @param initialM42 defines 2nd value of 4th row
  4743. * @param initialM43 defines 3rd value of 4th row
  4744. * @param initialM44 defines 4th value of 4th row
  4745. * @returns the new matrix
  4746. */
  4747. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  4748. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  4749. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  4750. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  4751. var result = new Matrix();
  4752. const m = result._m;
  4753. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  4754. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  4755. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  4756. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  4757. result._markAsUpdated();
  4758. return result;
  4759. }
  4760. /**
  4761. * Creates a new matrix composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  4762. * @param scale defines the scale vector3
  4763. * @param rotation defines the rotation quaternion
  4764. * @param translation defines the translation vector3
  4765. * @returns a new matrix
  4766. */
  4767. public static Compose(scale: DeepImmutable<Vector3>, rotation: DeepImmutable<Quaternion>, translation: DeepImmutable<Vector3>): Matrix {
  4768. var result = new Matrix();
  4769. Matrix.ComposeToRef(scale, rotation, translation, result);
  4770. return result;
  4771. }
  4772. /**
  4773. * Sets a matrix to a value composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  4774. * @param scale defines the scale vector3
  4775. * @param rotation defines the rotation quaternion
  4776. * @param translation defines the translation vector3
  4777. * @param result defines the target matrix
  4778. */
  4779. public static ComposeToRef(scale: DeepImmutable<Vector3>, rotation: DeepImmutable<Quaternion>, translation: DeepImmutable<Vector3>, result: Matrix): void {
  4780. Matrix.ScalingToRef(scale.x, scale.y, scale.z, MathTmp.Matrix[1]);
  4781. rotation.toRotationMatrix(MathTmp.Matrix[0]);
  4782. MathTmp.Matrix[1].multiplyToRef(MathTmp.Matrix[0], result);
  4783. result.setTranslation(translation);
  4784. }
  4785. /**
  4786. * Creates a new identity matrix
  4787. * @returns a new identity matrix
  4788. */
  4789. public static Identity(): Matrix {
  4790. const identity = Matrix.FromValues(
  4791. 1.0, 0.0, 0.0, 0.0,
  4792. 0.0, 1.0, 0.0, 0.0,
  4793. 0.0, 0.0, 1.0, 0.0,
  4794. 0.0, 0.0, 0.0, 1.0);
  4795. identity._updateIdentityStatus(true);
  4796. return identity;
  4797. }
  4798. /**
  4799. * Creates a new identity matrix and stores the result in a given matrix
  4800. * @param result defines the target matrix
  4801. */
  4802. public static IdentityToRef(result: Matrix): void {
  4803. Matrix.FromValuesToRef(
  4804. 1.0, 0.0, 0.0, 0.0,
  4805. 0.0, 1.0, 0.0, 0.0,
  4806. 0.0, 0.0, 1.0, 0.0,
  4807. 0.0, 0.0, 0.0, 1.0,
  4808. result
  4809. );
  4810. result._updateIdentityStatus(true);
  4811. }
  4812. /**
  4813. * Creates a new zero matrix
  4814. * @returns a new zero matrix
  4815. */
  4816. public static Zero(): Matrix {
  4817. const zero = Matrix.FromValues(
  4818. 0.0, 0.0, 0.0, 0.0,
  4819. 0.0, 0.0, 0.0, 0.0,
  4820. 0.0, 0.0, 0.0, 0.0,
  4821. 0.0, 0.0, 0.0, 0.0);
  4822. zero._updateIdentityStatus(false);
  4823. return zero;
  4824. }
  4825. /**
  4826. * Creates a new rotation matrix for "angle" radians around the X axis
  4827. * @param angle defines the angle (in radians) to use
  4828. * @return the new matrix
  4829. */
  4830. public static RotationX(angle: number): Matrix {
  4831. var result = new Matrix();
  4832. Matrix.RotationXToRef(angle, result);
  4833. return result;
  4834. }
  4835. /**
  4836. * Creates a new matrix as the invert of a given matrix
  4837. * @param source defines the source matrix
  4838. * @returns the new matrix
  4839. */
  4840. public static Invert(source: DeepImmutable<Matrix>): Matrix {
  4841. var result = new Matrix();
  4842. source.invertToRef(result);
  4843. return result;
  4844. }
  4845. /**
  4846. * Creates a new rotation matrix for "angle" radians around the X axis and stores it in a given matrix
  4847. * @param angle defines the angle (in radians) to use
  4848. * @param result defines the target matrix
  4849. */
  4850. public static RotationXToRef(angle: number, result: Matrix): void {
  4851. var s = Math.sin(angle);
  4852. var c = Math.cos(angle);
  4853. Matrix.FromValuesToRef(
  4854. 1.0, 0.0, 0.0, 0.0,
  4855. 0.0, c, s, 0.0,
  4856. 0.0, -s, c, 0.0,
  4857. 0.0, 0.0, 0.0, 1.0,
  4858. result
  4859. );
  4860. result._updateIdentityStatus(c === 1 && s === 0);
  4861. }
  4862. /**
  4863. * Creates a new rotation matrix for "angle" radians around the Y axis
  4864. * @param angle defines the angle (in radians) to use
  4865. * @return the new matrix
  4866. */
  4867. public static RotationY(angle: number): Matrix {
  4868. var result = new Matrix();
  4869. Matrix.RotationYToRef(angle, result);
  4870. return result;
  4871. }
  4872. /**
  4873. * Creates a new rotation matrix for "angle" radians around the Y axis and stores it in a given matrix
  4874. * @param angle defines the angle (in radians) to use
  4875. * @param result defines the target matrix
  4876. */
  4877. public static RotationYToRef(angle: number, result: Matrix): void {
  4878. var s = Math.sin(angle);
  4879. var c = Math.cos(angle);
  4880. Matrix.FromValuesToRef(
  4881. c, 0.0, -s, 0.0,
  4882. 0.0, 1.0, 0.0, 0.0,
  4883. s, 0.0, c, 0.0,
  4884. 0.0, 0.0, 0.0, 1.0,
  4885. result
  4886. );
  4887. result._updateIdentityStatus(c === 1 && s === 0);
  4888. }
  4889. /**
  4890. * Creates a new rotation matrix for "angle" radians around the Z axis
  4891. * @param angle defines the angle (in radians) to use
  4892. * @return the new matrix
  4893. */
  4894. public static RotationZ(angle: number): Matrix {
  4895. var result = new Matrix();
  4896. Matrix.RotationZToRef(angle, result);
  4897. return result;
  4898. }
  4899. /**
  4900. * Creates a new rotation matrix for "angle" radians around the Z axis and stores it in a given matrix
  4901. * @param angle defines the angle (in radians) to use
  4902. * @param result defines the target matrix
  4903. */
  4904. public static RotationZToRef(angle: number, result: Matrix): void {
  4905. var s = Math.sin(angle);
  4906. var c = Math.cos(angle);
  4907. Matrix.FromValuesToRef(
  4908. c, s, 0.0, 0.0,
  4909. -s, c, 0.0, 0.0,
  4910. 0.0, 0.0, 1.0, 0.0,
  4911. 0.0, 0.0, 0.0, 1.0,
  4912. result
  4913. );
  4914. result._updateIdentityStatus(c === 1 && s === 0);
  4915. }
  4916. /**
  4917. * Creates a new rotation matrix for "angle" radians around the given axis
  4918. * @param axis defines the axis to use
  4919. * @param angle defines the angle (in radians) to use
  4920. * @return the new matrix
  4921. */
  4922. public static RotationAxis(axis: DeepImmutable<Vector3>, angle: number): Matrix {
  4923. var result = new Matrix();
  4924. Matrix.RotationAxisToRef(axis, angle, result);
  4925. return result;
  4926. }
  4927. /**
  4928. * Creates a new rotation matrix for "angle" radians around the given axis and stores it in a given matrix
  4929. * @param axis defines the axis to use
  4930. * @param angle defines the angle (in radians) to use
  4931. * @param result defines the target matrix
  4932. */
  4933. public static RotationAxisToRef(axis: DeepImmutable<Vector3>, angle: number, result: Matrix): void {
  4934. var s = Math.sin(-angle);
  4935. var c = Math.cos(-angle);
  4936. var c1 = 1 - c;
  4937. axis.normalize();
  4938. const m = result._m;
  4939. m[0] = (axis.x * axis.x) * c1 + c;
  4940. m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  4941. m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  4942. m[3] = 0.0;
  4943. m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  4944. m[5] = (axis.y * axis.y) * c1 + c;
  4945. m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  4946. m[7] = 0.0;
  4947. m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  4948. m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  4949. m[10] = (axis.z * axis.z) * c1 + c;
  4950. m[11] = 0.0;
  4951. m[12] = 0.0;
  4952. m[13] = 0.0;
  4953. m[14] = 0.0;
  4954. m[15] = 1.0;
  4955. result._markAsUpdated();
  4956. }
  4957. /**
  4958. * Creates a rotation matrix
  4959. * @param yaw defines the yaw angle in radians (Y axis)
  4960. * @param pitch defines the pitch angle in radians (X axis)
  4961. * @param roll defines the roll angle in radians (X axis)
  4962. * @returns the new rotation matrix
  4963. */
  4964. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  4965. var result = new Matrix();
  4966. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  4967. return result;
  4968. }
  4969. /**
  4970. * Creates a rotation matrix and stores it in a given matrix
  4971. * @param yaw defines the yaw angle in radians (Y axis)
  4972. * @param pitch defines the pitch angle in radians (X axis)
  4973. * @param roll defines the roll angle in radians (X axis)
  4974. * @param result defines the target matrix
  4975. */
  4976. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  4977. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, MathTmp.Quaternion[0]);
  4978. MathTmp.Quaternion[0].toRotationMatrix(result);
  4979. }
  4980. /**
  4981. * Creates a scaling matrix
  4982. * @param x defines the scale factor on X axis
  4983. * @param y defines the scale factor on Y axis
  4984. * @param z defines the scale factor on Z axis
  4985. * @returns the new matrix
  4986. */
  4987. public static Scaling(x: number, y: number, z: number): Matrix {
  4988. var result = new Matrix();
  4989. Matrix.ScalingToRef(x, y, z, result);
  4990. return result;
  4991. }
  4992. /**
  4993. * Creates a scaling matrix and stores it in a given matrix
  4994. * @param x defines the scale factor on X axis
  4995. * @param y defines the scale factor on Y axis
  4996. * @param z defines the scale factor on Z axis
  4997. * @param result defines the target matrix
  4998. */
  4999. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  5000. Matrix.FromValuesToRef(
  5001. x, 0.0, 0.0, 0.0,
  5002. 0.0, y, 0.0, 0.0,
  5003. 0.0, 0.0, z, 0.0,
  5004. 0.0, 0.0, 0.0, 1.0,
  5005. result
  5006. );
  5007. result._updateIdentityStatus(x === 1 && y === 1 && z === 1);
  5008. }
  5009. /**
  5010. * Creates a translation matrix
  5011. * @param x defines the translation on X axis
  5012. * @param y defines the translation on Y axis
  5013. * @param z defines the translationon Z axis
  5014. * @returns the new matrix
  5015. */
  5016. public static Translation(x: number, y: number, z: number): Matrix {
  5017. var result = new Matrix();
  5018. Matrix.TranslationToRef(x, y, z, result);
  5019. return result;
  5020. }
  5021. /**
  5022. * Creates a translation matrix and stores it in a given matrix
  5023. * @param x defines the translation on X axis
  5024. * @param y defines the translation on Y axis
  5025. * @param z defines the translationon Z axis
  5026. * @param result defines the target matrix
  5027. */
  5028. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  5029. Matrix.FromValuesToRef(
  5030. 1.0, 0.0, 0.0, 0.0,
  5031. 0.0, 1.0, 0.0, 0.0,
  5032. 0.0, 0.0, 1.0, 0.0,
  5033. x, y, z, 1.0,
  5034. result
  5035. );
  5036. result._updateIdentityStatus(x === 0 && y === 0 && z === 0);
  5037. }
  5038. /**
  5039. * Returns a new Matrix whose values are the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  5040. * @param startValue defines the start value
  5041. * @param endValue defines the end value
  5042. * @param gradient defines the gradient factor
  5043. * @returns the new matrix
  5044. */
  5045. public static Lerp(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number): Matrix {
  5046. var result = new Matrix();
  5047. Matrix.LerpToRef(startValue, endValue, gradient, result);
  5048. return result;
  5049. }
  5050. /**
  5051. * Set the given matrix "result" as the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  5052. * @param startValue defines the start value
  5053. * @param endValue defines the end value
  5054. * @param gradient defines the gradient factor
  5055. * @param result defines the Matrix object where to store data
  5056. */
  5057. public static LerpToRef(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number, result: Matrix): void {
  5058. const resultM = result._m;
  5059. const startM = startValue.m;
  5060. const endM = endValue.m;
  5061. for (var index = 0; index < 16; index++) {
  5062. resultM[index] = startM[index] * (1.0 - gradient) + endM[index] * gradient;
  5063. }
  5064. result._markAsUpdated();
  5065. }
  5066. /**
  5067. * Builds a new matrix whose values are computed by:
  5068. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  5069. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  5070. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  5071. * @param startValue defines the first matrix
  5072. * @param endValue defines the second matrix
  5073. * @param gradient defines the gradient between the two matrices
  5074. * @returns the new matrix
  5075. */
  5076. public static DecomposeLerp(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number): Matrix {
  5077. var result = new Matrix();
  5078. Matrix.DecomposeLerpToRef(startValue, endValue, gradient, result);
  5079. return result;
  5080. }
  5081. /**
  5082. * Update a matrix to values which are computed by:
  5083. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  5084. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  5085. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  5086. * @param startValue defines the first matrix
  5087. * @param endValue defines the second matrix
  5088. * @param gradient defines the gradient between the two matrices
  5089. * @param result defines the target matrix
  5090. */
  5091. public static DecomposeLerpToRef(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number, result: Matrix) {
  5092. var startScale = MathTmp.Vector3[0];
  5093. var startRotation = MathTmp.Quaternion[0];
  5094. var startTranslation = MathTmp.Vector3[1];
  5095. startValue.decompose(startScale, startRotation, startTranslation);
  5096. var endScale = MathTmp.Vector3[2];
  5097. var endRotation = MathTmp.Quaternion[1];
  5098. var endTranslation = MathTmp.Vector3[3];
  5099. endValue.decompose(endScale, endRotation, endTranslation);
  5100. var resultScale = MathTmp.Vector3[4];
  5101. Vector3.LerpToRef(startScale, endScale, gradient, resultScale);
  5102. var resultRotation = MathTmp.Quaternion[2];
  5103. Quaternion.SlerpToRef(startRotation, endRotation, gradient, resultRotation);
  5104. var resultTranslation = MathTmp.Vector3[5];
  5105. Vector3.LerpToRef(startTranslation, endTranslation, gradient, resultTranslation);
  5106. Matrix.ComposeToRef(resultScale, resultRotation, resultTranslation, result);
  5107. }
  5108. /**
  5109. * Gets a new rotation matrix used to rotate an entity so as it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up"
  5110. * This function works in left handed mode
  5111. * @param eye defines the final position of the entity
  5112. * @param target defines where the entity should look at
  5113. * @param up defines the up vector for the entity
  5114. * @returns the new matrix
  5115. */
  5116. public static LookAtLH(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>): Matrix {
  5117. var result = new Matrix();
  5118. Matrix.LookAtLHToRef(eye, target, up, result);
  5119. return result;
  5120. }
  5121. /**
  5122. * Sets the given "result" Matrix to a rotation matrix used to rotate an entity so that it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up".
  5123. * This function works in left handed mode
  5124. * @param eye defines the final position of the entity
  5125. * @param target defines where the entity should look at
  5126. * @param up defines the up vector for the entity
  5127. * @param result defines the target matrix
  5128. */
  5129. public static LookAtLHToRef(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: Matrix): void {
  5130. const xAxis = MathTmp.Vector3[0];
  5131. const yAxis = MathTmp.Vector3[1];
  5132. const zAxis = MathTmp.Vector3[2];
  5133. // Z axis
  5134. target.subtractToRef(eye, zAxis);
  5135. zAxis.normalize();
  5136. // X axis
  5137. Vector3.CrossToRef(up, zAxis, xAxis);
  5138. const xSquareLength = xAxis.lengthSquared();
  5139. if (xSquareLength === 0) {
  5140. xAxis.x = 1.0;
  5141. } else {
  5142. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  5143. }
  5144. // Y axis
  5145. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  5146. yAxis.normalize();
  5147. // Eye angles
  5148. var ex = -Vector3.Dot(xAxis, eye);
  5149. var ey = -Vector3.Dot(yAxis, eye);
  5150. var ez = -Vector3.Dot(zAxis, eye);
  5151. Matrix.FromValuesToRef(
  5152. xAxis.x, yAxis.x, zAxis.x, 0.0,
  5153. xAxis.y, yAxis.y, zAxis.y, 0.0,
  5154. xAxis.z, yAxis.z, zAxis.z, 0.0,
  5155. ex, ey, ez, 1.0,
  5156. result
  5157. );
  5158. }
  5159. /**
  5160. * Gets a new rotation matrix used to rotate an entity so as it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up"
  5161. * This function works in right handed mode
  5162. * @param eye defines the final position of the entity
  5163. * @param target defines where the entity should look at
  5164. * @param up defines the up vector for the entity
  5165. * @returns the new matrix
  5166. */
  5167. public static LookAtRH(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>): Matrix {
  5168. var result = new Matrix();
  5169. Matrix.LookAtRHToRef(eye, target, up, result);
  5170. return result;
  5171. }
  5172. /**
  5173. * Sets the given "result" Matrix to a rotation matrix used to rotate an entity so that it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up".
  5174. * This function works in right handed mode
  5175. * @param eye defines the final position of the entity
  5176. * @param target defines where the entity should look at
  5177. * @param up defines the up vector for the entity
  5178. * @param result defines the target matrix
  5179. */
  5180. public static LookAtRHToRef(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: Matrix): void {
  5181. const xAxis = MathTmp.Vector3[0];
  5182. const yAxis = MathTmp.Vector3[1];
  5183. const zAxis = MathTmp.Vector3[2];
  5184. // Z axis
  5185. eye.subtractToRef(target, zAxis);
  5186. zAxis.normalize();
  5187. // X axis
  5188. Vector3.CrossToRef(up, zAxis, xAxis);
  5189. const xSquareLength = xAxis.lengthSquared();
  5190. if (xSquareLength === 0) {
  5191. xAxis.x = 1.0;
  5192. } else {
  5193. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  5194. }
  5195. // Y axis
  5196. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  5197. yAxis.normalize();
  5198. // Eye angles
  5199. var ex = -Vector3.Dot(xAxis, eye);
  5200. var ey = -Vector3.Dot(yAxis, eye);
  5201. var ez = -Vector3.Dot(zAxis, eye);
  5202. Matrix.FromValuesToRef(
  5203. xAxis.x, yAxis.x, zAxis.x, 0.0,
  5204. xAxis.y, yAxis.y, zAxis.y, 0.0,
  5205. xAxis.z, yAxis.z, zAxis.z, 0.0,
  5206. ex, ey, ez, 1.0,
  5207. result
  5208. );
  5209. }
  5210. /**
  5211. * Create a left-handed orthographic projection matrix
  5212. * @param width defines the viewport width
  5213. * @param height defines the viewport height
  5214. * @param znear defines the near clip plane
  5215. * @param zfar defines the far clip plane
  5216. * @returns a new matrix as a left-handed orthographic projection matrix
  5217. */
  5218. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  5219. var matrix = new Matrix();
  5220. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  5221. return matrix;
  5222. }
  5223. /**
  5224. * Store a left-handed orthographic projection to a given matrix
  5225. * @param width defines the viewport width
  5226. * @param height defines the viewport height
  5227. * @param znear defines the near clip plane
  5228. * @param zfar defines the far clip plane
  5229. * @param result defines the target matrix
  5230. */
  5231. public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
  5232. let n = znear;
  5233. let f = zfar;
  5234. let a = 2.0 / width;
  5235. let b = 2.0 / height;
  5236. let c = 2.0 / (f - n);
  5237. let d = -(f + n) / (f - n);
  5238. Matrix.FromValuesToRef(
  5239. a, 0.0, 0.0, 0.0,
  5240. 0.0, b, 0.0, 0.0,
  5241. 0.0, 0.0, c, 0.0,
  5242. 0.0, 0.0, d, 1.0,
  5243. result
  5244. );
  5245. result._updateIdentityStatus(a === 1 && b === 1 && c === 1 && d === 0);
  5246. }
  5247. /**
  5248. * Create a left-handed orthographic projection matrix
  5249. * @param left defines the viewport left coordinate
  5250. * @param right defines the viewport right coordinate
  5251. * @param bottom defines the viewport bottom coordinate
  5252. * @param top defines the viewport top coordinate
  5253. * @param znear defines the near clip plane
  5254. * @param zfar defines the far clip plane
  5255. * @returns a new matrix as a left-handed orthographic projection matrix
  5256. */
  5257. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  5258. var matrix = new Matrix();
  5259. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  5260. return matrix;
  5261. }
  5262. /**
  5263. * Stores a left-handed orthographic projection into a given matrix
  5264. * @param left defines the viewport left coordinate
  5265. * @param right defines the viewport right coordinate
  5266. * @param bottom defines the viewport bottom coordinate
  5267. * @param top defines the viewport top coordinate
  5268. * @param znear defines the near clip plane
  5269. * @param zfar defines the far clip plane
  5270. * @param result defines the target matrix
  5271. */
  5272. public static OrthoOffCenterLHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  5273. let n = znear;
  5274. let f = zfar;
  5275. let a = 2.0 / (right - left);
  5276. let b = 2.0 / (top - bottom);
  5277. let c = 2.0 / (f - n);
  5278. let d = -(f + n) / (f - n);
  5279. let i0 = (left + right) / (left - right);
  5280. let i1 = (top + bottom) / (bottom - top);
  5281. Matrix.FromValuesToRef(
  5282. a, 0.0, 0.0, 0.0,
  5283. 0.0, b, 0.0, 0.0,
  5284. 0.0, 0.0, c, 0.0,
  5285. i0, i1, d, 1.0,
  5286. result
  5287. );
  5288. result._markAsUpdated();
  5289. }
  5290. /**
  5291. * Creates a right-handed orthographic projection matrix
  5292. * @param left defines the viewport left coordinate
  5293. * @param right defines the viewport right coordinate
  5294. * @param bottom defines the viewport bottom coordinate
  5295. * @param top defines the viewport top coordinate
  5296. * @param znear defines the near clip plane
  5297. * @param zfar defines the far clip plane
  5298. * @returns a new matrix as a right-handed orthographic projection matrix
  5299. */
  5300. public static OrthoOffCenterRH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  5301. var matrix = new Matrix();
  5302. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  5303. return matrix;
  5304. }
  5305. /**
  5306. * Stores a right-handed orthographic projection into a given matrix
  5307. * @param left defines the viewport left coordinate
  5308. * @param right defines the viewport right coordinate
  5309. * @param bottom defines the viewport bottom coordinate
  5310. * @param top defines the viewport top coordinate
  5311. * @param znear defines the near clip plane
  5312. * @param zfar defines the far clip plane
  5313. * @param result defines the target matrix
  5314. */
  5315. public static OrthoOffCenterRHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  5316. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  5317. result._m[10] *= -1; // No need to call _markAsUpdated as previous function already called it and let _isIdentityDirty to true
  5318. }
  5319. /**
  5320. * Creates a left-handed perspective projection matrix
  5321. * @param width defines the viewport width
  5322. * @param height defines the viewport height
  5323. * @param znear defines the near clip plane
  5324. * @param zfar defines the far clip plane
  5325. * @returns a new matrix as a left-handed perspective projection matrix
  5326. */
  5327. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  5328. var matrix = new Matrix();
  5329. let n = znear;
  5330. let f = zfar;
  5331. let a = 2.0 * n / width;
  5332. let b = 2.0 * n / height;
  5333. let c = (f + n) / (f - n);
  5334. let d = -2.0 * f * n / (f - n);
  5335. Matrix.FromValuesToRef(
  5336. a, 0.0, 0.0, 0.0,
  5337. 0.0, b, 0.0, 0.0,
  5338. 0.0, 0.0, c, 1.0,
  5339. 0.0, 0.0, d, 0.0,
  5340. matrix
  5341. );
  5342. matrix._updateIdentityStatus(false);
  5343. return matrix;
  5344. }
  5345. /**
  5346. * Creates a left-handed perspective projection matrix
  5347. * @param fov defines the horizontal field of view
  5348. * @param aspect defines the aspect ratio
  5349. * @param znear defines the near clip plane
  5350. * @param zfar defines the far clip plane
  5351. * @returns a new matrix as a left-handed perspective projection matrix
  5352. */
  5353. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  5354. var matrix = new Matrix();
  5355. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  5356. return matrix;
  5357. }
  5358. /**
  5359. * Stores a left-handed perspective projection into a given matrix
  5360. * @param fov defines the horizontal field of view
  5361. * @param aspect defines the aspect ratio
  5362. * @param znear defines the near clip plane
  5363. * @param zfar defines the far clip plane
  5364. * @param result defines the target matrix
  5365. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  5366. */
  5367. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  5368. let n = znear;
  5369. let f = zfar;
  5370. let t = 1.0 / (Math.tan(fov * 0.5));
  5371. let a = isVerticalFovFixed ? (t / aspect) : t;
  5372. let b = isVerticalFovFixed ? t : (t * aspect);
  5373. let c = (f + n) / (f - n);
  5374. let d = -2.0 * f * n / (f - n);
  5375. Matrix.FromValuesToRef(
  5376. a, 0.0, 0.0, 0.0,
  5377. 0.0, b, 0.0, 0.0,
  5378. 0.0, 0.0, c, 1.0,
  5379. 0.0, 0.0, d, 0.0,
  5380. result
  5381. );
  5382. result._updateIdentityStatus(false);
  5383. }
  5384. /**
  5385. * Creates a right-handed perspective projection matrix
  5386. * @param fov defines the horizontal field of view
  5387. * @param aspect defines the aspect ratio
  5388. * @param znear defines the near clip plane
  5389. * @param zfar defines the far clip plane
  5390. * @returns a new matrix as a right-handed perspective projection matrix
  5391. */
  5392. public static PerspectiveFovRH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  5393. var matrix = new Matrix();
  5394. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  5395. return matrix;
  5396. }
  5397. /**
  5398. * Stores a right-handed perspective projection into a given matrix
  5399. * @param fov defines the horizontal field of view
  5400. * @param aspect defines the aspect ratio
  5401. * @param znear defines the near clip plane
  5402. * @param zfar defines the far clip plane
  5403. * @param result defines the target matrix
  5404. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  5405. */
  5406. public static PerspectiveFovRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  5407. //alternatively this could be expressed as:
  5408. // m = PerspectiveFovLHToRef
  5409. // m[10] *= -1.0;
  5410. // m[11] *= -1.0;
  5411. let n = znear;
  5412. let f = zfar;
  5413. let t = 1.0 / (Math.tan(fov * 0.5));
  5414. let a = isVerticalFovFixed ? (t / aspect) : t;
  5415. let b = isVerticalFovFixed ? t : (t * aspect);
  5416. let c = -(f + n) / (f - n);
  5417. let d = -2 * f * n / (f - n);
  5418. Matrix.FromValuesToRef(
  5419. a, 0.0, 0.0, 0.0,
  5420. 0.0, b, 0.0, 0.0,
  5421. 0.0, 0.0, c, -1.0,
  5422. 0.0, 0.0, d, 0.0,
  5423. result
  5424. );
  5425. result._updateIdentityStatus(false);
  5426. }
  5427. /**
  5428. * Stores a perspective projection for WebVR info a given matrix
  5429. * @param fov defines the field of view
  5430. * @param znear defines the near clip plane
  5431. * @param zfar defines the far clip plane
  5432. * @param result defines the target matrix
  5433. * @param rightHanded defines if the matrix must be in right-handed mode (false by default)
  5434. */
  5435. public static PerspectiveFovWebVRToRef(fov: { upDegrees: number, downDegrees: number, leftDegrees: number, rightDegrees: number }, znear: number, zfar: number, result: Matrix, rightHanded = false): void {
  5436. var rightHandedFactor = rightHanded ? -1 : 1;
  5437. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  5438. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  5439. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  5440. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  5441. var xScale = 2.0 / (leftTan + rightTan);
  5442. var yScale = 2.0 / (upTan + downTan);
  5443. const m = result._m;
  5444. m[0] = xScale;
  5445. m[1] = m[2] = m[3] = m[4] = 0.0;
  5446. m[5] = yScale;
  5447. m[6] = m[7] = 0.0;
  5448. m[8] = ((leftTan - rightTan) * xScale * 0.5);
  5449. m[9] = -((upTan - downTan) * yScale * 0.5);
  5450. m[10] = -zfar / (znear - zfar);
  5451. m[11] = 1.0 * rightHandedFactor;
  5452. m[12] = m[13] = m[15] = 0.0;
  5453. m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  5454. result._markAsUpdated();
  5455. }
  5456. /**
  5457. * Computes a complete transformation matrix
  5458. * @param viewport defines the viewport to use
  5459. * @param world defines the world matrix
  5460. * @param view defines the view matrix
  5461. * @param projection defines the projection matrix
  5462. * @param zmin defines the near clip plane
  5463. * @param zmax defines the far clip plane
  5464. * @returns the transformation matrix
  5465. */
  5466. public static GetFinalMatrix(viewport: DeepImmutable<Viewport>, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, zmin: number, zmax: number): Matrix {
  5467. var cw = viewport.width;
  5468. var ch = viewport.height;
  5469. var cx = viewport.x;
  5470. var cy = viewport.y;
  5471. var viewportMatrix = Matrix.FromValues(
  5472. cw / 2.0, 0.0, 0.0, 0.0,
  5473. 0.0, -ch / 2.0, 0.0, 0.0,
  5474. 0.0, 0.0, zmax - zmin, 0.0,
  5475. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1.0);
  5476. var matrix = MathTmp.Matrix[0];
  5477. world.multiplyToRef(view, matrix);
  5478. matrix.multiplyToRef(projection, matrix);
  5479. return matrix.multiply(viewportMatrix);
  5480. }
  5481. /**
  5482. * Extracts a 2x2 matrix from a given matrix and store the result in a Float32Array
  5483. * @param matrix defines the matrix to use
  5484. * @returns a new Float32Array array with 4 elements : the 2x2 matrix extracted from the given matrix
  5485. */
  5486. public static GetAsMatrix2x2(matrix: DeepImmutable<Matrix>): Float32Array {
  5487. const m = matrix.m;
  5488. return new Float32Array([m[0], m[1], m[4], m[5]]);
  5489. }
  5490. /**
  5491. * Extracts a 3x3 matrix from a given matrix and store the result in a Float32Array
  5492. * @param matrix defines the matrix to use
  5493. * @returns a new Float32Array array with 9 elements : the 3x3 matrix extracted from the given matrix
  5494. */
  5495. public static GetAsMatrix3x3(matrix: DeepImmutable<Matrix>): Float32Array {
  5496. const m = matrix.m;
  5497. return new Float32Array([
  5498. m[0], m[1], m[2],
  5499. m[4], m[5], m[6],
  5500. m[8], m[9], m[10]
  5501. ]);
  5502. }
  5503. /**
  5504. * Compute the transpose of a given matrix
  5505. * @param matrix defines the matrix to transpose
  5506. * @returns the new matrix
  5507. */
  5508. public static Transpose(matrix: DeepImmutable<Matrix>): Matrix {
  5509. var result = new Matrix();
  5510. Matrix.TransposeToRef(matrix, result);
  5511. return result;
  5512. }
  5513. /**
  5514. * Compute the transpose of a matrix and store it in a target matrix
  5515. * @param matrix defines the matrix to transpose
  5516. * @param result defines the target matrix
  5517. */
  5518. public static TransposeToRef(matrix: DeepImmutable<Matrix>, result: Matrix): void {
  5519. const rm = result._m;
  5520. const mm = matrix.m;
  5521. rm[0] = mm[0];
  5522. rm[1] = mm[4];
  5523. rm[2] = mm[8];
  5524. rm[3] = mm[12];
  5525. rm[4] = mm[1];
  5526. rm[5] = mm[5];
  5527. rm[6] = mm[9];
  5528. rm[7] = mm[13];
  5529. rm[8] = mm[2];
  5530. rm[9] = mm[6];
  5531. rm[10] = mm[10];
  5532. rm[11] = mm[14];
  5533. rm[12] = mm[3];
  5534. rm[13] = mm[7];
  5535. rm[14] = mm[11];
  5536. rm[15] = mm[15];
  5537. // identity-ness does not change when transposing
  5538. result._updateIdentityStatus((matrix as Matrix)._isIdentity, (matrix as Matrix)._isIdentityDirty);
  5539. }
  5540. /**
  5541. * Computes a reflection matrix from a plane
  5542. * @param plane defines the reflection plane
  5543. * @returns a new matrix
  5544. */
  5545. public static Reflection(plane: DeepImmutable<Plane>): Matrix {
  5546. var matrix = new Matrix();
  5547. Matrix.ReflectionToRef(plane, matrix);
  5548. return matrix;
  5549. }
  5550. /**
  5551. * Computes a reflection matrix from a plane
  5552. * @param plane defines the reflection plane
  5553. * @param result defines the target matrix
  5554. */
  5555. public static ReflectionToRef(plane: DeepImmutable<Plane>, result: Matrix): void {
  5556. plane.normalize();
  5557. var x = plane.normal.x;
  5558. var y = plane.normal.y;
  5559. var z = plane.normal.z;
  5560. var temp = -2 * x;
  5561. var temp2 = -2 * y;
  5562. var temp3 = -2 * z;
  5563. Matrix.FromValuesToRef(
  5564. temp * x + 1, temp2 * x, temp3 * x, 0.0,
  5565. temp * y, temp2 * y + 1, temp3 * y, 0.0,
  5566. temp * z, temp2 * z, temp3 * z + 1, 0.0,
  5567. temp * plane.d, temp2 * plane.d, temp3 * plane.d, 1.0,
  5568. result
  5569. );
  5570. }
  5571. /**
  5572. * Sets the given matrix as a rotation matrix composed from the 3 left handed axes
  5573. * @param xaxis defines the value of the 1st axis
  5574. * @param yaxis defines the value of the 2nd axis
  5575. * @param zaxis defines the value of the 3rd axis
  5576. * @param result defines the target matrix
  5577. */
  5578. public static FromXYZAxesToRef(xaxis: DeepImmutable<Vector3>, yaxis: DeepImmutable<Vector3>, zaxis: DeepImmutable<Vector3>, result: Matrix) {
  5579. Matrix.FromValuesToRef(
  5580. xaxis.x, xaxis.y, xaxis.z, 0.0,
  5581. yaxis.x, yaxis.y, yaxis.z, 0.0,
  5582. zaxis.x, zaxis.y, zaxis.z, 0.0,
  5583. 0.0, 0.0, 0.0, 1.0,
  5584. result
  5585. );
  5586. }
  5587. /**
  5588. * Creates a rotation matrix from a quaternion and stores it in a target matrix
  5589. * @param quat defines the quaternion to use
  5590. * @param result defines the target matrix
  5591. */
  5592. public static FromQuaternionToRef(quat: DeepImmutable<Quaternion>, result: Matrix) {
  5593. var xx = quat.x * quat.x;
  5594. var yy = quat.y * quat.y;
  5595. var zz = quat.z * quat.z;
  5596. var xy = quat.x * quat.y;
  5597. var zw = quat.z * quat.w;
  5598. var zx = quat.z * quat.x;
  5599. var yw = quat.y * quat.w;
  5600. var yz = quat.y * quat.z;
  5601. var xw = quat.x * quat.w;
  5602. result._m[0] = 1.0 - (2.0 * (yy + zz));
  5603. result._m[1] = 2.0 * (xy + zw);
  5604. result._m[2] = 2.0 * (zx - yw);
  5605. result._m[3] = 0.0;
  5606. result._m[4] = 2.0 * (xy - zw);
  5607. result._m[5] = 1.0 - (2.0 * (zz + xx));
  5608. result._m[6] = 2.0 * (yz + xw);
  5609. result._m[7] = 0.0;
  5610. result._m[8] = 2.0 * (zx + yw);
  5611. result._m[9] = 2.0 * (yz - xw);
  5612. result._m[10] = 1.0 - (2.0 * (yy + xx));
  5613. result._m[11] = 0.0;
  5614. result._m[12] = 0.0;
  5615. result._m[13] = 0.0;
  5616. result._m[14] = 0.0;
  5617. result._m[15] = 1.0;
  5618. result._markAsUpdated();
  5619. }
  5620. }
  5621. /**
  5622. * Represens a plane by the equation ax + by + cz + d = 0
  5623. */
  5624. export class Plane {
  5625. /**
  5626. * Normal of the plane (a,b,c)
  5627. */
  5628. public normal: Vector3;
  5629. /**
  5630. * d component of the plane
  5631. */
  5632. public d: number;
  5633. /**
  5634. * Creates a Plane object according to the given floats a, b, c, d and the plane equation : ax + by + cz + d = 0
  5635. * @param a a component of the plane
  5636. * @param b b component of the plane
  5637. * @param c c component of the plane
  5638. * @param d d component of the plane
  5639. */
  5640. constructor(a: number, b: number, c: number, d: number) {
  5641. this.normal = new Vector3(a, b, c);
  5642. this.d = d;
  5643. }
  5644. /**
  5645. * @returns the plane coordinates as a new array of 4 elements [a, b, c, d].
  5646. */
  5647. public asArray(): number[] {
  5648. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  5649. }
  5650. // Methods
  5651. /**
  5652. * @returns a new plane copied from the current Plane.
  5653. */
  5654. public clone(): Plane {
  5655. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  5656. }
  5657. /**
  5658. * @returns the string "Plane".
  5659. */
  5660. public getClassName(): string {
  5661. return "Plane";
  5662. }
  5663. /**
  5664. * @returns the Plane hash code.
  5665. */
  5666. public getHashCode(): number {
  5667. let hash = this.normal.getHashCode();
  5668. hash = (hash * 397) ^ (this.d || 0);
  5669. return hash;
  5670. }
  5671. /**
  5672. * Normalize the current Plane in place.
  5673. * @returns the updated Plane.
  5674. */
  5675. public normalize(): Plane {
  5676. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  5677. var magnitude = 0.0;
  5678. if (norm !== 0) {
  5679. magnitude = 1.0 / norm;
  5680. }
  5681. this.normal.x *= magnitude;
  5682. this.normal.y *= magnitude;
  5683. this.normal.z *= magnitude;
  5684. this.d *= magnitude;
  5685. return this;
  5686. }
  5687. /**
  5688. * Applies a transformation the plane and returns the result
  5689. * @param transformation the transformation matrix to be applied to the plane
  5690. * @returns a new Plane as the result of the transformation of the current Plane by the given matrix.
  5691. */
  5692. public transform(transformation: DeepImmutable<Matrix>): Plane {
  5693. const transposedMatrix = MathTmp.Matrix[0];
  5694. Matrix.TransposeToRef(transformation, transposedMatrix);
  5695. const m = transposedMatrix.m;
  5696. var x = this.normal.x;
  5697. var y = this.normal.y;
  5698. var z = this.normal.z;
  5699. var d = this.d;
  5700. var normalX = x * m[0] + y * m[1] + z * m[2] + d * m[3];
  5701. var normalY = x * m[4] + y * m[5] + z * m[6] + d * m[7];
  5702. var normalZ = x * m[8] + y * m[9] + z * m[10] + d * m[11];
  5703. var finalD = x * m[12] + y * m[13] + z * m[14] + d * m[15];
  5704. return new Plane(normalX, normalY, normalZ, finalD);
  5705. }
  5706. /**
  5707. * Calcualtte the dot product between the point and the plane normal
  5708. * @param point point to calculate the dot product with
  5709. * @returns the dot product (float) of the point coordinates and the plane normal.
  5710. */
  5711. public dotCoordinate(point: DeepImmutable<Vector3>): number {
  5712. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  5713. }
  5714. /**
  5715. * Updates the current Plane from the plane defined by the three given points.
  5716. * @param point1 one of the points used to contruct the plane
  5717. * @param point2 one of the points used to contruct the plane
  5718. * @param point3 one of the points used to contruct the plane
  5719. * @returns the updated Plane.
  5720. */
  5721. public copyFromPoints(point1: DeepImmutable<Vector3>, point2: DeepImmutable<Vector3>, point3: DeepImmutable<Vector3>): Plane {
  5722. var x1 = point2.x - point1.x;
  5723. var y1 = point2.y - point1.y;
  5724. var z1 = point2.z - point1.z;
  5725. var x2 = point3.x - point1.x;
  5726. var y2 = point3.y - point1.y;
  5727. var z2 = point3.z - point1.z;
  5728. var yz = (y1 * z2) - (z1 * y2);
  5729. var xz = (z1 * x2) - (x1 * z2);
  5730. var xy = (x1 * y2) - (y1 * x2);
  5731. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  5732. var invPyth;
  5733. if (pyth !== 0) {
  5734. invPyth = 1.0 / pyth;
  5735. }
  5736. else {
  5737. invPyth = 0.0;
  5738. }
  5739. this.normal.x = yz * invPyth;
  5740. this.normal.y = xz * invPyth;
  5741. this.normal.z = xy * invPyth;
  5742. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  5743. return this;
  5744. }
  5745. /**
  5746. * Checks if the plane is facing a given direction
  5747. * @param direction the direction to check if the plane is facing
  5748. * @param epsilon value the dot product is compared against (returns true if dot <= epsilon)
  5749. * @returns True is the vector "direction" is the same side than the plane normal.
  5750. */
  5751. public isFrontFacingTo(direction: DeepImmutable<Vector3>, epsilon: number): boolean {
  5752. var dot = Vector3.Dot(this.normal, direction);
  5753. return (dot <= epsilon);
  5754. }
  5755. /**
  5756. * Calculates the distance to a point
  5757. * @param point point to calculate distance to
  5758. * @returns the signed distance (float) from the given point to the Plane.
  5759. */
  5760. public signedDistanceTo(point: DeepImmutable<Vector3>): number {
  5761. return Vector3.Dot(point, this.normal) + this.d;
  5762. }
  5763. // Statics
  5764. /**
  5765. * Creates a plane from an array
  5766. * @param array the array to create a plane from
  5767. * @returns a new Plane from the given array.
  5768. */
  5769. static FromArray(array: DeepImmutable<ArrayLike<number>>): Plane {
  5770. return new Plane(array[0], array[1], array[2], array[3]);
  5771. }
  5772. /**
  5773. * Creates a plane from three points
  5774. * @param point1 point used to create the plane
  5775. * @param point2 point used to create the plane
  5776. * @param point3 point used to create the plane
  5777. * @returns a new Plane defined by the three given points.
  5778. */
  5779. static FromPoints(point1: DeepImmutable<Vector3>, point2: DeepImmutable<Vector3>, point3: DeepImmutable<Vector3>): Plane {
  5780. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  5781. result.copyFromPoints(point1, point2, point3);
  5782. return result;
  5783. }
  5784. /**
  5785. * Creates a plane from an origin point and a normal
  5786. * @param origin origin of the plane to be constructed
  5787. * @param normal normal of the plane to be constructed
  5788. * @returns a new Plane the normal vector to this plane at the given origin point.
  5789. * Note : the vector "normal" is updated because normalized.
  5790. */
  5791. static FromPositionAndNormal(origin: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>): Plane {
  5792. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  5793. normal.normalize();
  5794. result.normal = normal;
  5795. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  5796. return result;
  5797. }
  5798. /**
  5799. * Calculates the distance from a plane and a point
  5800. * @param origin origin of the plane to be constructed
  5801. * @param normal normal of the plane to be constructed
  5802. * @param point point to calculate distance to
  5803. * @returns the signed distance between the plane defined by the normal vector at the "origin"" point and the given other point.
  5804. */
  5805. static SignedDistanceToPlaneFromPositionAndNormal(origin: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>, point: DeepImmutable<Vector3>): number {
  5806. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  5807. return Vector3.Dot(point, normal) + d;
  5808. }
  5809. }
  5810. /**
  5811. * Class used to represent a viewport on screen
  5812. */
  5813. export class Viewport {
  5814. /**
  5815. * Creates a Viewport object located at (x, y) and sized (width, height)
  5816. * @param x defines viewport left coordinate
  5817. * @param y defines viewport top coordinate
  5818. * @param width defines the viewport width
  5819. * @param height defines the viewport height
  5820. */
  5821. constructor(
  5822. /** viewport left coordinate */
  5823. public x: number,
  5824. /** viewport top coordinate */
  5825. public y: number,
  5826. /**viewport width */
  5827. public width: number,
  5828. /** viewport height */
  5829. public height: number) {
  5830. }
  5831. /**
  5832. * Creates a new viewport using absolute sizing (from 0-> width, 0-> height instead of 0->1)
  5833. * @param renderWidthOrEngine defines either an engine or the rendering width
  5834. * @param renderHeight defines the rendering height
  5835. * @returns a new Viewport
  5836. */
  5837. public toGlobal(renderWidthOrEngine: number | Engine, renderHeight: number): Viewport {
  5838. if ((<Engine>renderWidthOrEngine).getRenderWidth) {
  5839. var engine = (<Engine>renderWidthOrEngine);
  5840. return this.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  5841. }
  5842. let renderWidth = <number>renderWidthOrEngine;
  5843. return new Viewport(this.x * renderWidth, this.y * renderHeight, this.width * renderWidth, this.height * renderHeight);
  5844. }
  5845. /**
  5846. * Returns a new Viewport copied from the current one
  5847. * @returns a new Viewport
  5848. */
  5849. public clone(): Viewport {
  5850. return new Viewport(this.x, this.y, this.width, this.height);
  5851. }
  5852. }
  5853. /**
  5854. * Reprasents a camera frustum
  5855. */
  5856. export class Frustum {
  5857. /**
  5858. * Gets the planes representing the frustum
  5859. * @param transform matrix to be applied to the returned planes
  5860. * @returns a new array of 6 Frustum planes computed by the given transformation matrix.
  5861. */
  5862. public static GetPlanes(transform: DeepImmutable<Matrix>): Plane[] {
  5863. var frustumPlanes = [];
  5864. for (var index = 0; index < 6; index++) {
  5865. frustumPlanes.push(new Plane(0.0, 0.0, 0.0, 0.0));
  5866. }
  5867. Frustum.GetPlanesToRef(transform, frustumPlanes);
  5868. return frustumPlanes;
  5869. }
  5870. /**
  5871. * Gets the near frustum plane transformed by the transform matrix
  5872. * @param transform transformation matrix to be applied to the resulting frustum plane
  5873. * @param frustumPlane the resuling frustum plane
  5874. */
  5875. public static GetNearPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  5876. const m = transform.m;
  5877. frustumPlane.normal.x = m[3] + m[2];
  5878. frustumPlane.normal.y = m[7] + m[6];
  5879. frustumPlane.normal.z = m[11] + m[10];
  5880. frustumPlane.d = m[15] + m[14];
  5881. frustumPlane.normalize();
  5882. }
  5883. /**
  5884. * Gets the far frustum plane transformed by the transform matrix
  5885. * @param transform transformation matrix to be applied to the resulting frustum plane
  5886. * @param frustumPlane the resuling frustum plane
  5887. */
  5888. public static GetFarPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  5889. const m = transform.m;
  5890. frustumPlane.normal.x = m[3] - m[2];
  5891. frustumPlane.normal.y = m[7] - m[6];
  5892. frustumPlane.normal.z = m[11] - m[10];
  5893. frustumPlane.d = m[15] - m[14];
  5894. frustumPlane.normalize();
  5895. }
  5896. /**
  5897. * Gets the left frustum plane transformed by the transform matrix
  5898. * @param transform transformation matrix to be applied to the resulting frustum plane
  5899. * @param frustumPlane the resuling frustum plane
  5900. */
  5901. public static GetLeftPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  5902. const m = transform.m;
  5903. frustumPlane.normal.x = m[3] + m[0];
  5904. frustumPlane.normal.y = m[7] + m[4];
  5905. frustumPlane.normal.z = m[11] + m[8];
  5906. frustumPlane.d = m[15] + m[12];
  5907. frustumPlane.normalize();
  5908. }
  5909. /**
  5910. * Gets the right frustum plane transformed by the transform matrix
  5911. * @param transform transformation matrix to be applied to the resulting frustum plane
  5912. * @param frustumPlane the resuling frustum plane
  5913. */
  5914. public static GetRightPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  5915. const m = transform.m;
  5916. frustumPlane.normal.x = m[3] - m[0];
  5917. frustumPlane.normal.y = m[7] - m[4];
  5918. frustumPlane.normal.z = m[11] - m[8];
  5919. frustumPlane.d = m[15] - m[12];
  5920. frustumPlane.normalize();
  5921. }
  5922. /**
  5923. * Gets the top frustum plane transformed by the transform matrix
  5924. * @param transform transformation matrix to be applied to the resulting frustum plane
  5925. * @param frustumPlane the resuling frustum plane
  5926. */
  5927. public static GetTopPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  5928. const m = transform.m;
  5929. frustumPlane.normal.x = m[3] - m[1];
  5930. frustumPlane.normal.y = m[7] - m[5];
  5931. frustumPlane.normal.z = m[11] - m[9];
  5932. frustumPlane.d = m[15] - m[13];
  5933. frustumPlane.normalize();
  5934. }
  5935. /**
  5936. * Gets the bottom frustum plane transformed by the transform matrix
  5937. * @param transform transformation matrix to be applied to the resulting frustum plane
  5938. * @param frustumPlane the resuling frustum plane
  5939. */
  5940. public static GetBottomPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  5941. const m = transform.m;
  5942. frustumPlane.normal.x = m[3] + m[1];
  5943. frustumPlane.normal.y = m[7] + m[5];
  5944. frustumPlane.normal.z = m[11] + m[9];
  5945. frustumPlane.d = m[15] + m[13];
  5946. frustumPlane.normalize();
  5947. }
  5948. /**
  5949. * Sets the given array "frustumPlanes" with the 6 Frustum planes computed by the given transformation matrix.
  5950. * @param transform transformation matrix to be applied to the resulting frustum planes
  5951. * @param frustumPlanes the resuling frustum planes
  5952. */
  5953. public static GetPlanesToRef(transform: DeepImmutable<Matrix>, frustumPlanes: Plane[]): void {
  5954. // Near
  5955. Frustum.GetNearPlaneToRef(transform, frustumPlanes[0]);
  5956. // Far
  5957. Frustum.GetFarPlaneToRef(transform, frustumPlanes[1]);
  5958. // Left
  5959. Frustum.GetLeftPlaneToRef(transform, frustumPlanes[2]);
  5960. // Right
  5961. Frustum.GetRightPlaneToRef(transform, frustumPlanes[3]);
  5962. // Top
  5963. Frustum.GetTopPlaneToRef(transform, frustumPlanes[4]);
  5964. // Bottom
  5965. Frustum.GetBottomPlaneToRef(transform, frustumPlanes[5]);
  5966. }
  5967. }
  5968. /** Defines supported spaces */
  5969. export enum Space {
  5970. /** Local (object) space */
  5971. LOCAL = 0,
  5972. /** World space */
  5973. WORLD = 1,
  5974. /** Bone space */
  5975. BONE = 2
  5976. }
  5977. /** Defines the 3 main axes */
  5978. export class Axis {
  5979. /** X axis */
  5980. public static X: Vector3 = new Vector3(1.0, 0.0, 0.0);
  5981. /** Y axis */
  5982. public static Y: Vector3 = new Vector3(0.0, 1.0, 0.0);
  5983. /** Z axis */
  5984. public static Z: Vector3 = new Vector3(0.0, 0.0, 1.0);
  5985. }
  5986. /** Class used to represent a Bezier curve */
  5987. export class BezierCurve {
  5988. /**
  5989. * Returns the cubic Bezier interpolated value (float) at "t" (float) from the given x1, y1, x2, y2 floats
  5990. * @param t defines the time
  5991. * @param x1 defines the left coordinate on X axis
  5992. * @param y1 defines the left coordinate on Y axis
  5993. * @param x2 defines the right coordinate on X axis
  5994. * @param y2 defines the right coordinate on Y axis
  5995. * @returns the interpolated value
  5996. */
  5997. public static Interpolate(t: number, x1: number, y1: number, x2: number, y2: number): number {
  5998. // Extract X (which is equal to time here)
  5999. var f0 = 1 - 3 * x2 + 3 * x1;
  6000. var f1 = 3 * x2 - 6 * x1;
  6001. var f2 = 3 * x1;
  6002. var refinedT = t;
  6003. for (var i = 0; i < 5; i++) {
  6004. var refinedT2 = refinedT * refinedT;
  6005. var refinedT3 = refinedT2 * refinedT;
  6006. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  6007. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  6008. refinedT -= (x - t) * slope;
  6009. refinedT = Math.min(1, Math.max(0, refinedT));
  6010. }
  6011. // Resolve cubic bezier for the given x
  6012. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
  6013. 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
  6014. Math.pow(refinedT, 3);
  6015. }
  6016. }
  6017. /**
  6018. * Defines potential orientation for back face culling
  6019. */
  6020. export enum Orientation {
  6021. /**
  6022. * Clockwise
  6023. */
  6024. CW = 0,
  6025. /** Counter clockwise */
  6026. CCW = 1
  6027. }
  6028. /**
  6029. * Defines angle representation
  6030. */
  6031. export class Angle {
  6032. private _radians: number;
  6033. /**
  6034. * Creates an Angle object of "radians" radians (float).
  6035. */
  6036. constructor(radians: number) {
  6037. this._radians = radians;
  6038. if (this._radians < 0.0) { this._radians += (2.0 * Math.PI); }
  6039. }
  6040. /**
  6041. * Get value in degrees
  6042. * @returns the Angle value in degrees (float)
  6043. */
  6044. public degrees() {
  6045. return this._radians * 180.0 / Math.PI;
  6046. }
  6047. /**
  6048. * Get value in radians
  6049. * @returns the Angle value in radians (float)
  6050. */
  6051. public radians() {
  6052. return this._radians;
  6053. }
  6054. /**
  6055. * Gets a new Angle object valued with the angle value in radians between the two given vectors
  6056. * @param a defines first vector
  6057. * @param b defines second vector
  6058. * @returns a new Angle
  6059. */
  6060. public static BetweenTwoPoints(a: DeepImmutable<Vector2>, b: DeepImmutable<Vector2>): Angle {
  6061. var delta = b.subtract(a);
  6062. var theta = Math.atan2(delta.y, delta.x);
  6063. return new Angle(theta);
  6064. }
  6065. /**
  6066. * Gets a new Angle object from the given float in radians
  6067. * @param radians defines the angle value in radians
  6068. * @returns a new Angle
  6069. */
  6070. public static FromRadians(radians: number): Angle {
  6071. return new Angle(radians);
  6072. }
  6073. /**
  6074. * Gets a new Angle object from the given float in degrees
  6075. * @param degrees defines the angle value in degrees
  6076. * @returns a new Angle
  6077. */
  6078. public static FromDegrees(degrees: number): Angle {
  6079. return new Angle(degrees * Math.PI / 180.0);
  6080. }
  6081. }
  6082. /**
  6083. * This represents an arc in a 2d space.
  6084. */
  6085. export class Arc2 {
  6086. /**
  6087. * Defines the center point of the arc.
  6088. */
  6089. public centerPoint: Vector2;
  6090. /**
  6091. * Defines the radius of the arc.
  6092. */
  6093. public radius: number;
  6094. /**
  6095. * Defines the angle of the arc (from mid point to end point).
  6096. */
  6097. public angle: Angle;
  6098. /**
  6099. * Defines the start angle of the arc (from start point to middle point).
  6100. */
  6101. public startAngle: Angle;
  6102. /**
  6103. * Defines the orientation of the arc (clock wise/counter clock wise).
  6104. */
  6105. public orientation: Orientation;
  6106. /**
  6107. * Creates an Arc object from the three given points : start, middle and end.
  6108. * @param startPoint Defines the start point of the arc
  6109. * @param midPoint Defines the midlle point of the arc
  6110. * @param endPoint Defines the end point of the arc
  6111. */
  6112. constructor(
  6113. /** Defines the start point of the arc */
  6114. public startPoint: Vector2,
  6115. /** Defines the mid point of the arc */
  6116. public midPoint: Vector2,
  6117. /** Defines the end point of the arc */
  6118. public endPoint: Vector2) {
  6119. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  6120. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  6121. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  6122. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  6123. this.centerPoint = new Vector2(
  6124. (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
  6125. ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
  6126. );
  6127. this.radius = this.centerPoint.subtract(this.startPoint).length();
  6128. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  6129. var a1 = this.startAngle.degrees();
  6130. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  6131. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  6132. // angles correction
  6133. if (a2 - a1 > +180.0) { a2 -= 360.0; }
  6134. if (a2 - a1 < -180.0) { a2 += 360.0; }
  6135. if (a3 - a2 > +180.0) { a3 -= 360.0; }
  6136. if (a3 - a2 < -180.0) { a3 += 360.0; }
  6137. this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
  6138. this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
  6139. }
  6140. }
  6141. /**
  6142. * Represents a 2D path made up of multiple 2D points
  6143. */
  6144. export class Path2 {
  6145. private _points = new Array<Vector2>();
  6146. private _length = 0.0;
  6147. /**
  6148. * If the path start and end point are the same
  6149. */
  6150. public closed = false;
  6151. /**
  6152. * Creates a Path2 object from the starting 2D coordinates x and y.
  6153. * @param x the starting points x value
  6154. * @param y the starting points y value
  6155. */
  6156. constructor(x: number, y: number) {
  6157. this._points.push(new Vector2(x, y));
  6158. }
  6159. /**
  6160. * Adds a new segment until the given coordinates (x, y) to the current Path2.
  6161. * @param x the added points x value
  6162. * @param y the added points y value
  6163. * @returns the updated Path2.
  6164. */
  6165. public addLineTo(x: number, y: number): Path2 {
  6166. if (this.closed) {
  6167. return this;
  6168. }
  6169. var newPoint = new Vector2(x, y);
  6170. var previousPoint = this._points[this._points.length - 1];
  6171. this._points.push(newPoint);
  6172. this._length += newPoint.subtract(previousPoint).length();
  6173. return this;
  6174. }
  6175. /**
  6176. * 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.
  6177. * @param midX middle point x value
  6178. * @param midY middle point y value
  6179. * @param endX end point x value
  6180. * @param endY end point y value
  6181. * @param numberOfSegments (default: 36)
  6182. * @returns the updated Path2.
  6183. */
  6184. public addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36): Path2 {
  6185. if (this.closed) {
  6186. return this;
  6187. }
  6188. var startPoint = this._points[this._points.length - 1];
  6189. var midPoint = new Vector2(midX, midY);
  6190. var endPoint = new Vector2(endX, endY);
  6191. var arc = new Arc2(startPoint, midPoint, endPoint);
  6192. var increment = arc.angle.radians() / numberOfSegments;
  6193. if (arc.orientation === Orientation.CW) { increment *= -1; }
  6194. var currentAngle = arc.startAngle.radians() + increment;
  6195. for (var i = 0; i < numberOfSegments; i++) {
  6196. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  6197. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  6198. this.addLineTo(x, y);
  6199. currentAngle += increment;
  6200. }
  6201. return this;
  6202. }
  6203. /**
  6204. * Closes the Path2.
  6205. * @returns the Path2.
  6206. */
  6207. public close(): Path2 {
  6208. this.closed = true;
  6209. return this;
  6210. }
  6211. /**
  6212. * Gets the sum of the distance between each sequential point in the path
  6213. * @returns the Path2 total length (float).
  6214. */
  6215. public length(): number {
  6216. var result = this._length;
  6217. if (!this.closed) {
  6218. var lastPoint = this._points[this._points.length - 1];
  6219. var firstPoint = this._points[0];
  6220. result += (firstPoint.subtract(lastPoint).length());
  6221. }
  6222. return result;
  6223. }
  6224. /**
  6225. * Gets the points which construct the path
  6226. * @returns the Path2 internal array of points.
  6227. */
  6228. public getPoints(): Vector2[] {
  6229. return this._points;
  6230. }
  6231. /**
  6232. * Retreives the point at the distance aways from the starting point
  6233. * @param normalizedLengthPosition the length along the path to retreive the point from
  6234. * @returns a new Vector2 located at a percentage of the Path2 total length on this path.
  6235. */
  6236. public getPointAtLengthPosition(normalizedLengthPosition: number): Vector2 {
  6237. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  6238. return Vector2.Zero();
  6239. }
  6240. var lengthPosition = normalizedLengthPosition * this.length();
  6241. var previousOffset = 0;
  6242. for (var i = 0; i < this._points.length; i++) {
  6243. var j = (i + 1) % this._points.length;
  6244. var a = this._points[i];
  6245. var b = this._points[j];
  6246. var bToA = b.subtract(a);
  6247. var nextOffset = (bToA.length() + previousOffset);
  6248. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  6249. var dir = bToA.normalize();
  6250. var localOffset = lengthPosition - previousOffset;
  6251. return new Vector2(
  6252. a.x + (dir.x * localOffset),
  6253. a.y + (dir.y * localOffset)
  6254. );
  6255. }
  6256. previousOffset = nextOffset;
  6257. }
  6258. return Vector2.Zero();
  6259. }
  6260. /**
  6261. * Creates a new path starting from an x and y position
  6262. * @param x starting x value
  6263. * @param y starting y value
  6264. * @returns a new Path2 starting at the coordinates (x, y).
  6265. */
  6266. public static StartingAt(x: number, y: number): Path2 {
  6267. return new Path2(x, y);
  6268. }
  6269. }
  6270. /**
  6271. * Represents a 3D path made up of multiple 3D points
  6272. */
  6273. export class Path3D {
  6274. private _curve = new Array<Vector3>();
  6275. private _distances = new Array<number>();
  6276. private _tangents = new Array<Vector3>();
  6277. private _normals = new Array<Vector3>();
  6278. private _binormals = new Array<Vector3>();
  6279. private _raw: boolean;
  6280. /**
  6281. * new Path3D(path, normal, raw)
  6282. * Creates a Path3D. A Path3D is a logical math object, so not a mesh.
  6283. * please read the description in the tutorial : https://doc.babylonjs.com/how_to/how_to_use_path3d
  6284. * @param path an array of Vector3, the curve axis of the Path3D
  6285. * @param normal (options) Vector3, the first wanted normal to the curve. Ex (0, 1, 0) for a vertical normal.
  6286. * @param raw (optional, default false) : boolean, if true the returned Path3D isn't normalized. Useful to depict path acceleration or speed.
  6287. */
  6288. constructor(
  6289. /**
  6290. * an array of Vector3, the curve axis of the Path3D
  6291. */
  6292. public path: Vector3[],
  6293. firstNormal: Nullable<Vector3> = null,
  6294. raw?: boolean
  6295. ) {
  6296. for (var p = 0; p < path.length; p++) {
  6297. this._curve[p] = path[p].clone(); // hard copy
  6298. }
  6299. this._raw = raw || false;
  6300. this._compute(firstNormal);
  6301. }
  6302. /**
  6303. * Returns the Path3D array of successive Vector3 designing its curve.
  6304. * @returns the Path3D array of successive Vector3 designing its curve.
  6305. */
  6306. public getCurve(): Vector3[] {
  6307. return this._curve;
  6308. }
  6309. /**
  6310. * Returns an array populated with tangent vectors on each Path3D curve point.
  6311. * @returns an array populated with tangent vectors on each Path3D curve point.
  6312. */
  6313. public getTangents(): Vector3[] {
  6314. return this._tangents;
  6315. }
  6316. /**
  6317. * Returns an array populated with normal vectors on each Path3D curve point.
  6318. * @returns an array populated with normal vectors on each Path3D curve point.
  6319. */
  6320. public getNormals(): Vector3[] {
  6321. return this._normals;
  6322. }
  6323. /**
  6324. * Returns an array populated with binormal vectors on each Path3D curve point.
  6325. * @returns an array populated with binormal vectors on each Path3D curve point.
  6326. */
  6327. public getBinormals(): Vector3[] {
  6328. return this._binormals;
  6329. }
  6330. /**
  6331. * Returns an array populated with distances (float) of the i-th point from the first curve point.
  6332. * @returns an array populated with distances (float) of the i-th point from the first curve point.
  6333. */
  6334. public getDistances(): number[] {
  6335. return this._distances;
  6336. }
  6337. /**
  6338. * Forces the Path3D tangent, normal, binormal and distance recomputation.
  6339. * @param path path which all values are copied into the curves points
  6340. * @param firstNormal which should be projected onto the curve
  6341. * @returns the same object updated.
  6342. */
  6343. public update(path: Vector3[], firstNormal: Nullable<Vector3> = null): Path3D {
  6344. for (var p = 0; p < path.length; p++) {
  6345. this._curve[p].x = path[p].x;
  6346. this._curve[p].y = path[p].y;
  6347. this._curve[p].z = path[p].z;
  6348. }
  6349. this._compute(firstNormal);
  6350. return this;
  6351. }
  6352. // private function compute() : computes tangents, normals and binormals
  6353. private _compute(firstNormal: Nullable<Vector3>): void {
  6354. var l = this._curve.length;
  6355. // first and last tangents
  6356. this._tangents[0] = this._getFirstNonNullVector(0);
  6357. if (!this._raw) {
  6358. this._tangents[0].normalize();
  6359. }
  6360. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  6361. if (!this._raw) {
  6362. this._tangents[l - 1].normalize();
  6363. }
  6364. // normals and binormals at first point : arbitrary vector with _normalVector()
  6365. var tg0 = this._tangents[0];
  6366. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  6367. this._normals[0] = pp0;
  6368. if (!this._raw) {
  6369. this._normals[0].normalize();
  6370. }
  6371. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  6372. if (!this._raw) {
  6373. this._binormals[0].normalize();
  6374. }
  6375. this._distances[0] = 0.0;
  6376. // normals and binormals : next points
  6377. var prev: Vector3; // previous vector (segment)
  6378. var cur: Vector3; // current vector (segment)
  6379. var curTang: Vector3; // current tangent
  6380. // previous normal
  6381. var prevBinor: Vector3; // previous binormal
  6382. for (var i = 1; i < l; i++) {
  6383. // tangents
  6384. prev = this._getLastNonNullVector(i);
  6385. if (i < l - 1) {
  6386. cur = this._getFirstNonNullVector(i);
  6387. this._tangents[i] = prev.add(cur);
  6388. this._tangents[i].normalize();
  6389. }
  6390. this._distances[i] = this._distances[i - 1] + prev.length();
  6391. // normals and binormals
  6392. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  6393. curTang = this._tangents[i];
  6394. prevBinor = this._binormals[i - 1];
  6395. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  6396. if (!this._raw) {
  6397. this._normals[i].normalize();
  6398. }
  6399. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  6400. if (!this._raw) {
  6401. this._binormals[i].normalize();
  6402. }
  6403. }
  6404. }
  6405. // private function getFirstNonNullVector(index)
  6406. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  6407. private _getFirstNonNullVector(index: number): Vector3 {
  6408. var i = 1;
  6409. var nNVector: Vector3 = this._curve[index + i].subtract(this._curve[index]);
  6410. while (nNVector.length() === 0 && index + i + 1 < this._curve.length) {
  6411. i++;
  6412. nNVector = this._curve[index + i].subtract(this._curve[index]);
  6413. }
  6414. return nNVector;
  6415. }
  6416. // private function getLastNonNullVector(index)
  6417. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  6418. private _getLastNonNullVector(index: number): Vector3 {
  6419. var i = 1;
  6420. var nLVector: Vector3 = this._curve[index].subtract(this._curve[index - i]);
  6421. while (nLVector.length() === 0 && index > i + 1) {
  6422. i++;
  6423. nLVector = this._curve[index].subtract(this._curve[index - i]);
  6424. }
  6425. return nLVector;
  6426. }
  6427. // private function normalVector(v0, vt, va) :
  6428. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  6429. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  6430. private _normalVector(v0: Vector3, vt: Vector3, va: Nullable<Vector3>): Vector3 {
  6431. var normal0: Vector3;
  6432. var tgl = vt.length();
  6433. if (tgl === 0.0) {
  6434. tgl = 1.0;
  6435. }
  6436. if (va === undefined || va === null) {
  6437. var point: Vector3;
  6438. if (!Scalar.WithinEpsilon(Math.abs(vt.y) / tgl, 1.0, Epsilon)) { // search for a point in the plane
  6439. point = new Vector3(0.0, -1.0, 0.0);
  6440. }
  6441. else if (!Scalar.WithinEpsilon(Math.abs(vt.x) / tgl, 1.0, Epsilon)) {
  6442. point = new Vector3(1.0, 0.0, 0.0);
  6443. }
  6444. else if (!Scalar.WithinEpsilon(Math.abs(vt.z) / tgl, 1.0, Epsilon)) {
  6445. point = new Vector3(0.0, 0.0, 1.0);
  6446. }
  6447. else {
  6448. point = Vector3.Zero();
  6449. }
  6450. normal0 = Vector3.Cross(vt, point);
  6451. }
  6452. else {
  6453. normal0 = Vector3.Cross(vt, va);
  6454. Vector3.CrossToRef(normal0, vt, normal0);
  6455. }
  6456. normal0.normalize();
  6457. return normal0;
  6458. }
  6459. }
  6460. /**
  6461. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  6462. * A Curve3 is designed from a series of successive Vector3.
  6463. * @see https://doc.babylonjs.com/how_to/how_to_use_curve3
  6464. */
  6465. export class Curve3 {
  6466. private _points: Vector3[];
  6467. private _length: number = 0.0;
  6468. /**
  6469. * Returns a Curve3 object along a Quadratic Bezier curve : https://doc.babylonjs.com/how_to/how_to_use_curve3#quadratic-bezier-curve
  6470. * @param v0 (Vector3) the origin point of the Quadratic Bezier
  6471. * @param v1 (Vector3) the control point
  6472. * @param v2 (Vector3) the end point of the Quadratic Bezier
  6473. * @param nbPoints (integer) the wanted number of points in the curve
  6474. * @returns the created Curve3
  6475. */
  6476. public static CreateQuadraticBezier(v0: DeepImmutable<Vector3>, v1: DeepImmutable<Vector3>, v2: DeepImmutable<Vector3>, nbPoints: number): Curve3 {
  6477. nbPoints = nbPoints > 2 ? nbPoints : 3;
  6478. var bez = new Array<Vector3>();
  6479. var equation = (t: number, val0: number, val1: number, val2: number) => {
  6480. var res = (1.0 - t) * (1.0 - t) * val0 + 2.0 * t * (1.0 - t) * val1 + t * t * val2;
  6481. return res;
  6482. };
  6483. for (var i = 0; i <= nbPoints; i++) {
  6484. 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)));
  6485. }
  6486. return new Curve3(bez);
  6487. }
  6488. /**
  6489. * Returns a Curve3 object along a Cubic Bezier curve : https://doc.babylonjs.com/how_to/how_to_use_curve3#cubic-bezier-curve
  6490. * @param v0 (Vector3) the origin point of the Cubic Bezier
  6491. * @param v1 (Vector3) the first control point
  6492. * @param v2 (Vector3) the second control point
  6493. * @param v3 (Vector3) the end point of the Cubic Bezier
  6494. * @param nbPoints (integer) the wanted number of points in the curve
  6495. * @returns the created Curve3
  6496. */
  6497. public static CreateCubicBezier(v0: DeepImmutable<Vector3>, v1: DeepImmutable<Vector3>, v2: DeepImmutable<Vector3>, v3: DeepImmutable<Vector3>, nbPoints: number): Curve3 {
  6498. nbPoints = nbPoints > 3 ? nbPoints : 4;
  6499. var bez = new Array<Vector3>();
  6500. var equation = (t: number, val0: number, val1: number, val2: number, val3: number) => {
  6501. 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;
  6502. return res;
  6503. };
  6504. for (var i = 0; i <= nbPoints; i++) {
  6505. 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)));
  6506. }
  6507. return new Curve3(bez);
  6508. }
  6509. /**
  6510. * Returns a Curve3 object along a Hermite Spline curve : https://doc.babylonjs.com/how_to/how_to_use_curve3#hermite-spline
  6511. * @param p1 (Vector3) the origin point of the Hermite Spline
  6512. * @param t1 (Vector3) the tangent vector at the origin point
  6513. * @param p2 (Vector3) the end point of the Hermite Spline
  6514. * @param t2 (Vector3) the tangent vector at the end point
  6515. * @param nbPoints (integer) the wanted number of points in the curve
  6516. * @returns the created Curve3
  6517. */
  6518. public static CreateHermiteSpline(p1: DeepImmutable<Vector3>, t1: DeepImmutable<Vector3>, p2: DeepImmutable<Vector3>, t2: DeepImmutable<Vector3>, nbPoints: number): Curve3 {
  6519. var hermite = new Array<Vector3>();
  6520. var step = 1.0 / nbPoints;
  6521. for (var i = 0; i <= nbPoints; i++) {
  6522. hermite.push(Vector3.Hermite(p1, t1, p2, t2, i * step));
  6523. }
  6524. return new Curve3(hermite);
  6525. }
  6526. /**
  6527. * Returns a Curve3 object along a CatmullRom Spline curve :
  6528. * @param points (array of Vector3) the points the spline must pass through. At least, four points required
  6529. * @param nbPoints (integer) the wanted number of points between each curve control points
  6530. * @param closed (boolean) optional with default false, when true forms a closed loop from the points
  6531. * @returns the created Curve3
  6532. */
  6533. public static CreateCatmullRomSpline(points: DeepImmutable<Vector3[]>, nbPoints: number, closed?: boolean): Curve3 {
  6534. var catmullRom = new Array<Vector3>();
  6535. var step = 1.0 / nbPoints;
  6536. var amount = 0.0;
  6537. if (closed) {
  6538. var pointsCount = points.length;
  6539. for (var i = 0; i < pointsCount; i++) {
  6540. amount = 0;
  6541. for (var c = 0; c < nbPoints; c++) {
  6542. catmullRom.push(Vector3.CatmullRom(points[i % pointsCount], points[(i + 1) % pointsCount], points[(i + 2) % pointsCount], points[(i + 3) % pointsCount], amount));
  6543. amount += step;
  6544. }
  6545. }
  6546. catmullRom.push(catmullRom[0]);
  6547. }
  6548. else {
  6549. var totalPoints = new Array<Vector3>();
  6550. totalPoints.push(points[0].clone());
  6551. Array.prototype.push.apply(totalPoints, points);
  6552. totalPoints.push(points[points.length - 1].clone());
  6553. for (var i = 0; i < totalPoints.length - 3; i++) {
  6554. amount = 0;
  6555. for (var c = 0; c < nbPoints; c++) {
  6556. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  6557. amount += step;
  6558. }
  6559. }
  6560. i--;
  6561. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  6562. }
  6563. return new Curve3(catmullRom);
  6564. }
  6565. /**
  6566. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  6567. * A Curve3 is designed from a series of successive Vector3.
  6568. * Tuto : https://doc.babylonjs.com/how_to/how_to_use_curve3#curve3-object
  6569. * @param points points which make up the curve
  6570. */
  6571. constructor(points: Vector3[]) {
  6572. this._points = points;
  6573. this._length = this._computeLength(points);
  6574. }
  6575. /**
  6576. * @returns the Curve3 stored array of successive Vector3
  6577. */
  6578. public getPoints() {
  6579. return this._points;
  6580. }
  6581. /**
  6582. * @returns the computed length (float) of the curve.
  6583. */
  6584. public length() {
  6585. return this._length;
  6586. }
  6587. /**
  6588. * Returns a new instance of Curve3 object : var curve = curveA.continue(curveB);
  6589. * This new Curve3 is built by translating and sticking the curveB at the end of the curveA.
  6590. * curveA and curveB keep unchanged.
  6591. * @param curve the curve to continue from this curve
  6592. * @returns the newly constructed curve
  6593. */
  6594. public continue(curve: DeepImmutable<Curve3>): Curve3 {
  6595. var lastPoint = this._points[this._points.length - 1];
  6596. var continuedPoints = this._points.slice();
  6597. var curvePoints = curve.getPoints();
  6598. for (var i = 1; i < curvePoints.length; i++) {
  6599. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  6600. }
  6601. var continuedCurve = new Curve3(continuedPoints);
  6602. return continuedCurve;
  6603. }
  6604. private _computeLength(path: DeepImmutable<Vector3[]>): number {
  6605. var l = 0;
  6606. for (var i = 1; i < path.length; i++) {
  6607. l += (path[i].subtract(path[i - 1])).length();
  6608. }
  6609. return l;
  6610. }
  6611. }
  6612. // Vertex formats
  6613. /**
  6614. * Contains position and normal vectors for a vertex
  6615. */
  6616. export class PositionNormalVertex {
  6617. /**
  6618. * Creates a PositionNormalVertex
  6619. * @param position the position of the vertex (defaut: 0,0,0)
  6620. * @param normal the normal of the vertex (defaut: 0,1,0)
  6621. */
  6622. constructor(
  6623. /** the position of the vertex (defaut: 0,0,0) */
  6624. public position: Vector3 = Vector3.Zero(),
  6625. /** the normal of the vertex (defaut: 0,1,0) */
  6626. public normal: Vector3 = Vector3.Up()
  6627. ) {
  6628. }
  6629. /**
  6630. * Clones the PositionNormalVertex
  6631. * @returns the cloned PositionNormalVertex
  6632. */
  6633. public clone(): PositionNormalVertex {
  6634. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  6635. }
  6636. }
  6637. /**
  6638. * Contains position, normal and uv vectors for a vertex
  6639. */
  6640. export class PositionNormalTextureVertex {
  6641. /**
  6642. * Creates a PositionNormalTextureVertex
  6643. * @param position the position of the vertex (defaut: 0,0,0)
  6644. * @param normal the normal of the vertex (defaut: 0,1,0)
  6645. * @param uv the uv of the vertex (default: 0,0)
  6646. */
  6647. constructor(
  6648. /** the position of the vertex (defaut: 0,0,0) */
  6649. public position: Vector3 = Vector3.Zero(),
  6650. /** the normal of the vertex (defaut: 0,1,0) */
  6651. public normal: Vector3 = Vector3.Up(),
  6652. /** the uv of the vertex (default: 0,0) */
  6653. public uv: Vector2 = Vector2.Zero()
  6654. ) {
  6655. }
  6656. /**
  6657. * Clones the PositionNormalTextureVertex
  6658. * @returns the cloned PositionNormalTextureVertex
  6659. */
  6660. public clone(): PositionNormalTextureVertex {
  6661. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  6662. }
  6663. }
  6664. // Temporary pre-allocated objects for engine internal use
  6665. // usage in any internal function :
  6666. // var tmp = Tmp.Vector3[0]; <= gets access to the first pre-created Vector3
  6667. // There's a Tmp array per object type : int, float, Vector2, Vector3, Vector4, Quaternion, Matrix
  6668. /**
  6669. * @hidden
  6670. */
  6671. export class Tmp {
  6672. public static Color3: Color3[] = Tools.BuildArray(3, Color3.Black);
  6673. public static Color4: Color4[] = Tools.BuildArray(3, () => new Color4(0, 0, 0, 0));
  6674. public static Vector2: Vector2[] = Tools.BuildArray(3, Vector2.Zero); // 3 temp Vector2 at once should be enough
  6675. public static Vector3: Vector3[] = Tools.BuildArray(13, Vector3.Zero); // 13 temp Vector3 at once should be enough
  6676. public static Vector4: Vector4[] = Tools.BuildArray(3, Vector4.Zero); // 3 temp Vector4 at once should be enough
  6677. public static Quaternion: Quaternion[] = Tools.BuildArray(2, Quaternion.Zero); // 2 temp Quaternion at once should be enough
  6678. public static Matrix: Matrix[] = Tools.BuildArray(8, Matrix.Identity); // 8 temp Matrices at once should be enough
  6679. }
  6680. /**
  6681. * @hidden
  6682. * Same as Tmp but not exported to keep it only for math functions to avoid conflicts
  6683. */
  6684. class MathTmp {
  6685. public static Vector3: Vector3[] = Tools.BuildArray(6, Vector3.Zero);
  6686. public static Matrix: Matrix[] = Tools.BuildArray(2, Matrix.Identity);
  6687. public static Quaternion: Quaternion[] = Tools.BuildArray(3, Quaternion.Zero);
  6688. }
  6689. }