babylon.math.ts 288 KB

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