animation.cpp 208 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294
  1. /**************************************************************************/
  2. /* animation.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "animation.h"
  31. #include "animation.compat.inc"
  32. #include "core/io/marshalls.h"
  33. #include "core/math/geometry_3d.h"
  34. bool Animation::_set(const StringName &p_name, const Variant &p_value) {
  35. String prop_name = p_name;
  36. if (p_name == SNAME("_compression")) {
  37. ERR_FAIL_COND_V(tracks.size() > 0, false); //can only set compression if no tracks exist
  38. Dictionary comp = p_value;
  39. ERR_FAIL_COND_V(!comp.has("fps"), false);
  40. ERR_FAIL_COND_V(!comp.has("bounds"), false);
  41. ERR_FAIL_COND_V(!comp.has("pages"), false);
  42. ERR_FAIL_COND_V(!comp.has("format_version"), false);
  43. uint32_t format_version = comp["format_version"];
  44. ERR_FAIL_COND_V(format_version > Compression::FORMAT_VERSION, false); // version does not match this supported version
  45. compression.fps = comp["fps"];
  46. Array bounds = comp["bounds"];
  47. compression.bounds.resize(bounds.size());
  48. for (int i = 0; i < bounds.size(); i++) {
  49. compression.bounds[i] = bounds[i];
  50. }
  51. Array pages = comp["pages"];
  52. compression.pages.resize(pages.size());
  53. for (int i = 0; i < pages.size(); i++) {
  54. Dictionary page = pages[i];
  55. ERR_FAIL_COND_V(!page.has("data"), false);
  56. ERR_FAIL_COND_V(!page.has("time_offset"), false);
  57. compression.pages[i].data = page["data"];
  58. compression.pages[i].time_offset = page["time_offset"];
  59. }
  60. compression.enabled = true;
  61. return true;
  62. } else if (prop_name.begins_with("tracks/")) {
  63. int track = prop_name.get_slicec('/', 1).to_int();
  64. String what = prop_name.get_slicec('/', 2);
  65. if (tracks.size() == track && what == "type") {
  66. String type = p_value;
  67. if (type == "position_3d") {
  68. add_track(TYPE_POSITION_3D);
  69. } else if (type == "rotation_3d") {
  70. add_track(TYPE_ROTATION_3D);
  71. } else if (type == "scale_3d") {
  72. add_track(TYPE_SCALE_3D);
  73. } else if (type == "blend_shape") {
  74. add_track(TYPE_BLEND_SHAPE);
  75. } else if (type == "value") {
  76. add_track(TYPE_VALUE);
  77. } else if (type == "method") {
  78. add_track(TYPE_METHOD);
  79. } else if (type == "bezier") {
  80. add_track(TYPE_BEZIER);
  81. } else if (type == "audio") {
  82. add_track(TYPE_AUDIO);
  83. } else if (type == "animation") {
  84. add_track(TYPE_ANIMATION);
  85. } else {
  86. return false;
  87. }
  88. return true;
  89. }
  90. ERR_FAIL_INDEX_V(track, tracks.size(), false);
  91. if (what == "path") {
  92. track_set_path(track, p_value);
  93. } else if (what == "compressed_track") {
  94. int index = p_value;
  95. ERR_FAIL_COND_V(!compression.enabled, false);
  96. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)index, compression.bounds.size(), false);
  97. Track *t = tracks[track];
  98. t->interpolation = INTERPOLATION_LINEAR; //only linear supported
  99. switch (t->type) {
  100. case TYPE_POSITION_3D: {
  101. PositionTrack *tt = static_cast<PositionTrack *>(t);
  102. tt->compressed_track = index;
  103. } break;
  104. case TYPE_ROTATION_3D: {
  105. RotationTrack *rt = static_cast<RotationTrack *>(t);
  106. rt->compressed_track = index;
  107. } break;
  108. case TYPE_SCALE_3D: {
  109. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  110. st->compressed_track = index;
  111. } break;
  112. case TYPE_BLEND_SHAPE: {
  113. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  114. bst->compressed_track = index;
  115. } break;
  116. default: {
  117. return false;
  118. }
  119. }
  120. return true;
  121. } else if (what == "use_blend") {
  122. if (track_get_type(track) == TYPE_AUDIO) {
  123. audio_track_set_use_blend(track, p_value);
  124. }
  125. } else if (what == "interp") {
  126. track_set_interpolation_type(track, InterpolationType(p_value.operator int()));
  127. } else if (what == "loop_wrap") {
  128. track_set_interpolation_loop_wrap(track, p_value);
  129. } else if (what == "imported") {
  130. track_set_imported(track, p_value);
  131. } else if (what == "enabled") {
  132. track_set_enabled(track, p_value);
  133. } else if (what == "keys" || what == "key_values") {
  134. if (track_get_type(track) == TYPE_POSITION_3D) {
  135. PositionTrack *tt = static_cast<PositionTrack *>(tracks[track]);
  136. Vector<real_t> values = p_value;
  137. int vcount = values.size();
  138. ERR_FAIL_COND_V(vcount % POSITION_TRACK_SIZE, false);
  139. const real_t *r = values.ptr();
  140. int64_t count = vcount / POSITION_TRACK_SIZE;
  141. tt->positions.resize(count);
  142. TKey<Vector3> *tw = tt->positions.ptrw();
  143. for (int i = 0; i < count; i++) {
  144. TKey<Vector3> &tk = tw[i];
  145. const real_t *ofs = &r[i * POSITION_TRACK_SIZE];
  146. tk.time = ofs[0];
  147. tk.transition = ofs[1];
  148. tk.value.x = ofs[2];
  149. tk.value.y = ofs[3];
  150. tk.value.z = ofs[4];
  151. }
  152. } else if (track_get_type(track) == TYPE_ROTATION_3D) {
  153. RotationTrack *rt = static_cast<RotationTrack *>(tracks[track]);
  154. Vector<real_t> values = p_value;
  155. int vcount = values.size();
  156. ERR_FAIL_COND_V(vcount % ROTATION_TRACK_SIZE, false);
  157. const real_t *r = values.ptr();
  158. int64_t count = vcount / ROTATION_TRACK_SIZE;
  159. rt->rotations.resize(count);
  160. TKey<Quaternion> *rw = rt->rotations.ptrw();
  161. for (int i = 0; i < count; i++) {
  162. TKey<Quaternion> &rk = rw[i];
  163. const real_t *ofs = &r[i * ROTATION_TRACK_SIZE];
  164. rk.time = ofs[0];
  165. rk.transition = ofs[1];
  166. rk.value.x = ofs[2];
  167. rk.value.y = ofs[3];
  168. rk.value.z = ofs[4];
  169. rk.value.w = ofs[5];
  170. }
  171. } else if (track_get_type(track) == TYPE_SCALE_3D) {
  172. ScaleTrack *st = static_cast<ScaleTrack *>(tracks[track]);
  173. Vector<real_t> values = p_value;
  174. int vcount = values.size();
  175. ERR_FAIL_COND_V(vcount % SCALE_TRACK_SIZE, false);
  176. const real_t *r = values.ptr();
  177. int64_t count = vcount / SCALE_TRACK_SIZE;
  178. st->scales.resize(count);
  179. TKey<Vector3> *sw = st->scales.ptrw();
  180. for (int i = 0; i < count; i++) {
  181. TKey<Vector3> &sk = sw[i];
  182. const real_t *ofs = &r[i * SCALE_TRACK_SIZE];
  183. sk.time = ofs[0];
  184. sk.transition = ofs[1];
  185. sk.value.x = ofs[2];
  186. sk.value.y = ofs[3];
  187. sk.value.z = ofs[4];
  188. }
  189. } else if (track_get_type(track) == TYPE_BLEND_SHAPE) {
  190. BlendShapeTrack *st = static_cast<BlendShapeTrack *>(tracks[track]);
  191. Vector<real_t> values = p_value;
  192. int vcount = values.size();
  193. ERR_FAIL_COND_V(vcount % BLEND_SHAPE_TRACK_SIZE, false);
  194. const real_t *r = values.ptr();
  195. int64_t count = vcount / BLEND_SHAPE_TRACK_SIZE;
  196. st->blend_shapes.resize(count);
  197. TKey<float> *sw = st->blend_shapes.ptrw();
  198. for (int i = 0; i < count; i++) {
  199. TKey<float> &sk = sw[i];
  200. const real_t *ofs = &r[i * BLEND_SHAPE_TRACK_SIZE];
  201. sk.time = ofs[0];
  202. sk.transition = ofs[1];
  203. sk.value = ofs[2];
  204. }
  205. } else if (track_get_type(track) == TYPE_VALUE) {
  206. ValueTrack *vt = static_cast<ValueTrack *>(tracks[track]);
  207. Dictionary d = p_value;
  208. ERR_FAIL_COND_V(!d.has("times"), false);
  209. ERR_FAIL_COND_V(!d.has("values"), false);
  210. if (d.has("cont")) {
  211. bool v = d["cont"];
  212. vt->update_mode = v ? UPDATE_CONTINUOUS : UPDATE_DISCRETE;
  213. }
  214. if (d.has("update")) {
  215. int um = d["update"];
  216. if (um < 0) {
  217. um = 0;
  218. } else if (um > 3) {
  219. um = 3;
  220. }
  221. vt->update_mode = UpdateMode(um);
  222. }
  223. capture_included = capture_included || (vt->update_mode == UPDATE_CAPTURE);
  224. Vector<real_t> times = d["times"];
  225. Array values = d["values"];
  226. ERR_FAIL_COND_V(times.size() != values.size(), false);
  227. if (times.size()) {
  228. int valcount = times.size();
  229. const real_t *rt = times.ptr();
  230. vt->values.resize(valcount);
  231. for (int i = 0; i < valcount; i++) {
  232. vt->values.write[i].time = rt[i];
  233. vt->values.write[i].value = values[i];
  234. }
  235. if (d.has("transitions")) {
  236. Vector<real_t> transitions = d["transitions"];
  237. ERR_FAIL_COND_V(transitions.size() != valcount, false);
  238. const real_t *rtr = transitions.ptr();
  239. for (int i = 0; i < valcount; i++) {
  240. vt->values.write[i].transition = rtr[i];
  241. }
  242. }
  243. }
  244. return true;
  245. } else if (track_get_type(track) == TYPE_METHOD) {
  246. while (track_get_key_count(track)) {
  247. track_remove_key(track, 0); //well shouldn't be set anyway
  248. }
  249. Dictionary d = p_value;
  250. ERR_FAIL_COND_V(!d.has("times"), false);
  251. ERR_FAIL_COND_V(!d.has("values"), false);
  252. Vector<real_t> times = d["times"];
  253. Array values = d["values"];
  254. ERR_FAIL_COND_V(times.size() != values.size(), false);
  255. if (times.size()) {
  256. int valcount = times.size();
  257. const real_t *rt = times.ptr();
  258. for (int i = 0; i < valcount; i++) {
  259. track_insert_key(track, rt[i], values[i]);
  260. }
  261. if (d.has("transitions")) {
  262. Vector<real_t> transitions = d["transitions"];
  263. ERR_FAIL_COND_V(transitions.size() != valcount, false);
  264. const real_t *rtr = transitions.ptr();
  265. for (int i = 0; i < valcount; i++) {
  266. track_set_key_transition(track, i, rtr[i]);
  267. }
  268. }
  269. }
  270. } else if (track_get_type(track) == TYPE_BEZIER) {
  271. BezierTrack *bt = static_cast<BezierTrack *>(tracks[track]);
  272. Dictionary d = p_value;
  273. ERR_FAIL_COND_V(!d.has("times"), false);
  274. ERR_FAIL_COND_V(!d.has("points"), false);
  275. Vector<real_t> times = d["times"];
  276. Vector<real_t> values = d["points"];
  277. #ifdef TOOLS_ENABLED
  278. ERR_FAIL_COND_V(!d.has("handle_modes"), false);
  279. Vector<int> handle_modes = d["handle_modes"];
  280. #endif // TOOLS_ENABLED
  281. ERR_FAIL_COND_V(times.size() * 5 != values.size(), false);
  282. if (times.size()) {
  283. int valcount = times.size();
  284. const real_t *rt = times.ptr();
  285. const real_t *rv = values.ptr();
  286. #ifdef TOOLS_ENABLED
  287. const int *rh = handle_modes.ptr();
  288. #endif // TOOLS_ENABLED
  289. bt->values.resize(valcount);
  290. for (int i = 0; i < valcount; i++) {
  291. bt->values.write[i].time = rt[i];
  292. bt->values.write[i].transition = 0; //unused in bezier
  293. bt->values.write[i].value.value = rv[i * 5 + 0];
  294. bt->values.write[i].value.in_handle.x = rv[i * 5 + 1];
  295. bt->values.write[i].value.in_handle.y = rv[i * 5 + 2];
  296. bt->values.write[i].value.out_handle.x = rv[i * 5 + 3];
  297. bt->values.write[i].value.out_handle.y = rv[i * 5 + 4];
  298. #ifdef TOOLS_ENABLED
  299. bt->values.write[i].value.handle_mode = static_cast<HandleMode>(rh[i]);
  300. #endif // TOOLS_ENABLED
  301. }
  302. }
  303. return true;
  304. } else if (track_get_type(track) == TYPE_AUDIO) {
  305. AudioTrack *ad = static_cast<AudioTrack *>(tracks[track]);
  306. Dictionary d = p_value;
  307. ERR_FAIL_COND_V(!d.has("times"), false);
  308. ERR_FAIL_COND_V(!d.has("clips"), false);
  309. Vector<real_t> times = d["times"];
  310. Array clips = d["clips"];
  311. ERR_FAIL_COND_V(clips.size() != times.size(), false);
  312. if (times.size()) {
  313. int valcount = times.size();
  314. const real_t *rt = times.ptr();
  315. ad->values.clear();
  316. for (int i = 0; i < valcount; i++) {
  317. Dictionary d2 = clips[i];
  318. if (!d2.has("start_offset")) {
  319. continue;
  320. }
  321. if (!d2.has("end_offset")) {
  322. continue;
  323. }
  324. if (!d2.has("stream")) {
  325. continue;
  326. }
  327. TKey<AudioKey> ak;
  328. ak.time = rt[i];
  329. ak.value.start_offset = d2["start_offset"];
  330. ak.value.end_offset = d2["end_offset"];
  331. ak.value.stream = d2["stream"];
  332. ad->values.push_back(ak);
  333. }
  334. }
  335. return true;
  336. } else if (track_get_type(track) == TYPE_ANIMATION) {
  337. AnimationTrack *an = static_cast<AnimationTrack *>(tracks[track]);
  338. Dictionary d = p_value;
  339. ERR_FAIL_COND_V(!d.has("times"), false);
  340. ERR_FAIL_COND_V(!d.has("clips"), false);
  341. Vector<real_t> times = d["times"];
  342. Vector<String> clips = d["clips"];
  343. ERR_FAIL_COND_V(clips.size() != times.size(), false);
  344. if (times.size()) {
  345. int valcount = times.size();
  346. const real_t *rt = times.ptr();
  347. const String *rc = clips.ptr();
  348. an->values.resize(valcount);
  349. for (int i = 0; i < valcount; i++) {
  350. TKey<StringName> ak;
  351. ak.time = rt[i];
  352. ak.value = rc[i];
  353. an->values.write[i] = ak;
  354. }
  355. }
  356. return true;
  357. } else {
  358. return false;
  359. }
  360. } else {
  361. return false;
  362. }
  363. #ifndef DISABLE_DEPRECATED
  364. } else if (prop_name == "loop" && p_value.operator bool()) { // Compatibility with Godot 3.x.
  365. loop_mode = Animation::LoopMode::LOOP_LINEAR;
  366. return true;
  367. #endif // DISABLE_DEPRECATED
  368. } else {
  369. return false;
  370. }
  371. return true;
  372. }
  373. bool Animation::_get(const StringName &p_name, Variant &r_ret) const {
  374. String prop_name = p_name;
  375. if (p_name == SNAME("_compression")) {
  376. ERR_FAIL_COND_V(!compression.enabled, false);
  377. Dictionary comp;
  378. comp["fps"] = compression.fps;
  379. Array bounds;
  380. bounds.resize(compression.bounds.size());
  381. for (uint32_t i = 0; i < compression.bounds.size(); i++) {
  382. bounds[i] = compression.bounds[i];
  383. }
  384. comp["bounds"] = bounds;
  385. Array pages;
  386. pages.resize(compression.pages.size());
  387. for (uint32_t i = 0; i < compression.pages.size(); i++) {
  388. Dictionary page;
  389. page["data"] = compression.pages[i].data;
  390. page["time_offset"] = compression.pages[i].time_offset;
  391. pages[i] = page;
  392. }
  393. comp["pages"] = pages;
  394. comp["format_version"] = Compression::FORMAT_VERSION;
  395. r_ret = comp;
  396. return true;
  397. } else if (prop_name == "length") {
  398. r_ret = length;
  399. } else if (prop_name == "loop_mode") {
  400. r_ret = loop_mode;
  401. } else if (prop_name == "step") {
  402. r_ret = step;
  403. } else if (prop_name.begins_with("tracks/")) {
  404. int track = prop_name.get_slicec('/', 1).to_int();
  405. String what = prop_name.get_slicec('/', 2);
  406. ERR_FAIL_INDEX_V(track, tracks.size(), false);
  407. if (what == "type") {
  408. switch (track_get_type(track)) {
  409. case TYPE_POSITION_3D:
  410. r_ret = "position_3d";
  411. break;
  412. case TYPE_ROTATION_3D:
  413. r_ret = "rotation_3d";
  414. break;
  415. case TYPE_SCALE_3D:
  416. r_ret = "scale_3d";
  417. break;
  418. case TYPE_BLEND_SHAPE:
  419. r_ret = "blend_shape";
  420. break;
  421. case TYPE_VALUE:
  422. r_ret = "value";
  423. break;
  424. case TYPE_METHOD:
  425. r_ret = "method";
  426. break;
  427. case TYPE_BEZIER:
  428. r_ret = "bezier";
  429. break;
  430. case TYPE_AUDIO:
  431. r_ret = "audio";
  432. break;
  433. case TYPE_ANIMATION:
  434. r_ret = "animation";
  435. break;
  436. }
  437. return true;
  438. } else if (what == "path") {
  439. r_ret = track_get_path(track);
  440. } else if (what == "compressed_track") {
  441. ERR_FAIL_COND_V(!compression.enabled, false);
  442. Track *t = tracks[track];
  443. switch (t->type) {
  444. case TYPE_POSITION_3D: {
  445. PositionTrack *tt = static_cast<PositionTrack *>(t);
  446. r_ret = tt->compressed_track;
  447. } break;
  448. case TYPE_ROTATION_3D: {
  449. RotationTrack *rt = static_cast<RotationTrack *>(t);
  450. r_ret = rt->compressed_track;
  451. } break;
  452. case TYPE_SCALE_3D: {
  453. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  454. r_ret = st->compressed_track;
  455. } break;
  456. case TYPE_BLEND_SHAPE: {
  457. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  458. r_ret = bst->compressed_track;
  459. } break;
  460. default: {
  461. r_ret = Variant();
  462. ERR_FAIL_V(false);
  463. }
  464. }
  465. return true;
  466. } else if (what == "use_blend") {
  467. if (track_get_type(track) == TYPE_AUDIO) {
  468. r_ret = audio_track_is_use_blend(track);
  469. }
  470. } else if (what == "interp") {
  471. r_ret = track_get_interpolation_type(track);
  472. } else if (what == "loop_wrap") {
  473. r_ret = track_get_interpolation_loop_wrap(track);
  474. } else if (what == "imported") {
  475. r_ret = track_is_imported(track);
  476. } else if (what == "enabled") {
  477. r_ret = track_is_enabled(track);
  478. } else if (what == "keys") {
  479. if (track_get_type(track) == TYPE_POSITION_3D) {
  480. Vector<real_t> keys;
  481. int kk = track_get_key_count(track);
  482. keys.resize(kk * POSITION_TRACK_SIZE);
  483. real_t *w = keys.ptrw();
  484. int idx = 0;
  485. for (int i = 0; i < track_get_key_count(track); i++) {
  486. Vector3 loc;
  487. position_track_get_key(track, i, &loc);
  488. w[idx++] = track_get_key_time(track, i);
  489. w[idx++] = track_get_key_transition(track, i);
  490. w[idx++] = loc.x;
  491. w[idx++] = loc.y;
  492. w[idx++] = loc.z;
  493. }
  494. r_ret = keys;
  495. return true;
  496. } else if (track_get_type(track) == TYPE_ROTATION_3D) {
  497. Vector<real_t> keys;
  498. int kk = track_get_key_count(track);
  499. keys.resize(kk * ROTATION_TRACK_SIZE);
  500. real_t *w = keys.ptrw();
  501. int idx = 0;
  502. for (int i = 0; i < track_get_key_count(track); i++) {
  503. Quaternion rot;
  504. rotation_track_get_key(track, i, &rot);
  505. w[idx++] = track_get_key_time(track, i);
  506. w[idx++] = track_get_key_transition(track, i);
  507. w[idx++] = rot.x;
  508. w[idx++] = rot.y;
  509. w[idx++] = rot.z;
  510. w[idx++] = rot.w;
  511. }
  512. r_ret = keys;
  513. return true;
  514. } else if (track_get_type(track) == TYPE_SCALE_3D) {
  515. Vector<real_t> keys;
  516. int kk = track_get_key_count(track);
  517. keys.resize(kk * SCALE_TRACK_SIZE);
  518. real_t *w = keys.ptrw();
  519. int idx = 0;
  520. for (int i = 0; i < track_get_key_count(track); i++) {
  521. Vector3 scale;
  522. scale_track_get_key(track, i, &scale);
  523. w[idx++] = track_get_key_time(track, i);
  524. w[idx++] = track_get_key_transition(track, i);
  525. w[idx++] = scale.x;
  526. w[idx++] = scale.y;
  527. w[idx++] = scale.z;
  528. }
  529. r_ret = keys;
  530. return true;
  531. } else if (track_get_type(track) == TYPE_BLEND_SHAPE) {
  532. Vector<real_t> keys;
  533. int kk = track_get_key_count(track);
  534. keys.resize(kk * BLEND_SHAPE_TRACK_SIZE);
  535. real_t *w = keys.ptrw();
  536. int idx = 0;
  537. for (int i = 0; i < track_get_key_count(track); i++) {
  538. float bs;
  539. blend_shape_track_get_key(track, i, &bs);
  540. w[idx++] = track_get_key_time(track, i);
  541. w[idx++] = track_get_key_transition(track, i);
  542. w[idx++] = bs;
  543. }
  544. r_ret = keys;
  545. return true;
  546. } else if (track_get_type(track) == TYPE_VALUE) {
  547. const ValueTrack *vt = static_cast<const ValueTrack *>(tracks[track]);
  548. Dictionary d;
  549. Vector<real_t> key_times;
  550. Vector<real_t> key_transitions;
  551. Array key_values;
  552. int kk = vt->values.size();
  553. key_times.resize(kk);
  554. key_transitions.resize(kk);
  555. key_values.resize(kk);
  556. real_t *wti = key_times.ptrw();
  557. real_t *wtr = key_transitions.ptrw();
  558. int idx = 0;
  559. const TKey<Variant> *vls = vt->values.ptr();
  560. for (int i = 0; i < kk; i++) {
  561. wti[idx] = vls[i].time;
  562. wtr[idx] = vls[i].transition;
  563. key_values[idx] = vls[i].value;
  564. idx++;
  565. }
  566. d["times"] = key_times;
  567. d["transitions"] = key_transitions;
  568. d["values"] = key_values;
  569. if (track_get_type(track) == TYPE_VALUE) {
  570. d["update"] = value_track_get_update_mode(track);
  571. }
  572. r_ret = d;
  573. return true;
  574. } else if (track_get_type(track) == TYPE_METHOD) {
  575. Dictionary d;
  576. Vector<real_t> key_times;
  577. Vector<real_t> key_transitions;
  578. Array key_values;
  579. int kk = track_get_key_count(track);
  580. key_times.resize(kk);
  581. key_transitions.resize(kk);
  582. key_values.resize(kk);
  583. real_t *wti = key_times.ptrw();
  584. real_t *wtr = key_transitions.ptrw();
  585. int idx = 0;
  586. for (int i = 0; i < track_get_key_count(track); i++) {
  587. wti[idx] = track_get_key_time(track, i);
  588. wtr[idx] = track_get_key_transition(track, i);
  589. key_values[idx] = track_get_key_value(track, i);
  590. idx++;
  591. }
  592. d["times"] = key_times;
  593. d["transitions"] = key_transitions;
  594. d["values"] = key_values;
  595. if (track_get_type(track) == TYPE_VALUE) {
  596. d["update"] = value_track_get_update_mode(track);
  597. }
  598. r_ret = d;
  599. return true;
  600. } else if (track_get_type(track) == TYPE_BEZIER) {
  601. const BezierTrack *bt = static_cast<const BezierTrack *>(tracks[track]);
  602. Dictionary d;
  603. Vector<real_t> key_times;
  604. Vector<real_t> key_points;
  605. int kk = bt->values.size();
  606. key_times.resize(kk);
  607. key_points.resize(kk * 5);
  608. real_t *wti = key_times.ptrw();
  609. real_t *wpo = key_points.ptrw();
  610. #ifdef TOOLS_ENABLED
  611. Vector<int> handle_modes;
  612. handle_modes.resize(kk);
  613. int *whm = handle_modes.ptrw();
  614. #endif // TOOLS_ENABLED
  615. int idx = 0;
  616. const TKey<BezierKey> *vls = bt->values.ptr();
  617. for (int i = 0; i < kk; i++) {
  618. wti[idx] = vls[i].time;
  619. wpo[idx * 5 + 0] = vls[i].value.value;
  620. wpo[idx * 5 + 1] = vls[i].value.in_handle.x;
  621. wpo[idx * 5 + 2] = vls[i].value.in_handle.y;
  622. wpo[idx * 5 + 3] = vls[i].value.out_handle.x;
  623. wpo[idx * 5 + 4] = vls[i].value.out_handle.y;
  624. #ifdef TOOLS_ENABLED
  625. whm[idx] = static_cast<int>(vls[i].value.handle_mode);
  626. #endif // TOOLS_ENABLED
  627. idx++;
  628. }
  629. d["times"] = key_times;
  630. d["points"] = key_points;
  631. #ifdef TOOLS_ENABLED
  632. d["handle_modes"] = handle_modes;
  633. #endif // TOOLS_ENABLED
  634. r_ret = d;
  635. return true;
  636. } else if (track_get_type(track) == TYPE_AUDIO) {
  637. const AudioTrack *ad = static_cast<const AudioTrack *>(tracks[track]);
  638. Dictionary d;
  639. Vector<real_t> key_times;
  640. Array clips;
  641. int kk = ad->values.size();
  642. key_times.resize(kk);
  643. real_t *wti = key_times.ptrw();
  644. int idx = 0;
  645. const TKey<AudioKey> *vls = ad->values.ptr();
  646. for (int i = 0; i < kk; i++) {
  647. wti[idx] = vls[i].time;
  648. Dictionary clip;
  649. clip["start_offset"] = vls[i].value.start_offset;
  650. clip["end_offset"] = vls[i].value.end_offset;
  651. clip["stream"] = vls[i].value.stream;
  652. clips.push_back(clip);
  653. idx++;
  654. }
  655. d["times"] = key_times;
  656. d["clips"] = clips;
  657. r_ret = d;
  658. return true;
  659. } else if (track_get_type(track) == TYPE_ANIMATION) {
  660. const AnimationTrack *an = static_cast<const AnimationTrack *>(tracks[track]);
  661. Dictionary d;
  662. Vector<real_t> key_times;
  663. Vector<String> clips;
  664. int kk = an->values.size();
  665. key_times.resize(kk);
  666. clips.resize(kk);
  667. real_t *wti = key_times.ptrw();
  668. String *wcl = clips.ptrw();
  669. const TKey<StringName> *vls = an->values.ptr();
  670. for (int i = 0; i < kk; i++) {
  671. wti[i] = vls[i].time;
  672. wcl[i] = vls[i].value;
  673. }
  674. d["times"] = key_times;
  675. d["clips"] = clips;
  676. r_ret = d;
  677. return true;
  678. }
  679. } else {
  680. return false;
  681. }
  682. } else {
  683. return false;
  684. }
  685. return true;
  686. }
  687. void Animation::_get_property_list(List<PropertyInfo> *p_list) const {
  688. if (compression.enabled) {
  689. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "_compression", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  690. }
  691. for (int i = 0; i < tracks.size(); i++) {
  692. p_list->push_back(PropertyInfo(Variant::STRING, "tracks/" + itos(i) + "/type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  693. p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/imported", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  694. p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  695. p_list->push_back(PropertyInfo(Variant::NODE_PATH, "tracks/" + itos(i) + "/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  696. if (track_is_compressed(i)) {
  697. p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/compressed_track", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  698. } else {
  699. p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/interp", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  700. p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/loop_wrap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  701. p_list->push_back(PropertyInfo(Variant::ARRAY, "tracks/" + itos(i) + "/keys", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  702. }
  703. if (track_get_type(i) == TYPE_AUDIO) {
  704. p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/use_blend", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  705. }
  706. }
  707. }
  708. void Animation::reset_state() {
  709. clear();
  710. }
  711. int Animation::add_track(TrackType p_type, int p_at_pos) {
  712. if (p_at_pos < 0 || p_at_pos >= tracks.size()) {
  713. p_at_pos = tracks.size();
  714. }
  715. switch (p_type) {
  716. case TYPE_POSITION_3D: {
  717. PositionTrack *tt = memnew(PositionTrack);
  718. tracks.insert(p_at_pos, tt);
  719. } break;
  720. case TYPE_ROTATION_3D: {
  721. RotationTrack *rt = memnew(RotationTrack);
  722. tracks.insert(p_at_pos, rt);
  723. } break;
  724. case TYPE_SCALE_3D: {
  725. ScaleTrack *st = memnew(ScaleTrack);
  726. tracks.insert(p_at_pos, st);
  727. } break;
  728. case TYPE_BLEND_SHAPE: {
  729. BlendShapeTrack *bst = memnew(BlendShapeTrack);
  730. tracks.insert(p_at_pos, bst);
  731. } break;
  732. case TYPE_VALUE: {
  733. tracks.insert(p_at_pos, memnew(ValueTrack));
  734. } break;
  735. case TYPE_METHOD: {
  736. tracks.insert(p_at_pos, memnew(MethodTrack));
  737. } break;
  738. case TYPE_BEZIER: {
  739. tracks.insert(p_at_pos, memnew(BezierTrack));
  740. } break;
  741. case TYPE_AUDIO: {
  742. tracks.insert(p_at_pos, memnew(AudioTrack));
  743. } break;
  744. case TYPE_ANIMATION: {
  745. tracks.insert(p_at_pos, memnew(AnimationTrack));
  746. } break;
  747. default: {
  748. ERR_PRINT("Unknown track type");
  749. }
  750. }
  751. emit_changed();
  752. return p_at_pos;
  753. }
  754. void Animation::remove_track(int p_track) {
  755. ERR_FAIL_INDEX(p_track, tracks.size());
  756. Track *t = tracks[p_track];
  757. switch (t->type) {
  758. case TYPE_POSITION_3D: {
  759. PositionTrack *tt = static_cast<PositionTrack *>(t);
  760. ERR_FAIL_COND_MSG(tt->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first.");
  761. _clear(tt->positions);
  762. } break;
  763. case TYPE_ROTATION_3D: {
  764. RotationTrack *rt = static_cast<RotationTrack *>(t);
  765. ERR_FAIL_COND_MSG(rt->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first.");
  766. _clear(rt->rotations);
  767. } break;
  768. case TYPE_SCALE_3D: {
  769. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  770. ERR_FAIL_COND_MSG(st->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first.");
  771. _clear(st->scales);
  772. } break;
  773. case TYPE_BLEND_SHAPE: {
  774. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  775. ERR_FAIL_COND_MSG(bst->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first.");
  776. _clear(bst->blend_shapes);
  777. } break;
  778. case TYPE_VALUE: {
  779. ValueTrack *vt = static_cast<ValueTrack *>(t);
  780. _clear(vt->values);
  781. } break;
  782. case TYPE_METHOD: {
  783. MethodTrack *mt = static_cast<MethodTrack *>(t);
  784. _clear(mt->methods);
  785. } break;
  786. case TYPE_BEZIER: {
  787. BezierTrack *bz = static_cast<BezierTrack *>(t);
  788. _clear(bz->values);
  789. } break;
  790. case TYPE_AUDIO: {
  791. AudioTrack *ad = static_cast<AudioTrack *>(t);
  792. _clear(ad->values);
  793. } break;
  794. case TYPE_ANIMATION: {
  795. AnimationTrack *an = static_cast<AnimationTrack *>(t);
  796. _clear(an->values);
  797. } break;
  798. }
  799. memdelete(t);
  800. tracks.remove_at(p_track);
  801. emit_changed();
  802. _check_capture_included();
  803. }
  804. void Animation::set_capture_included(bool p_capture_included) {
  805. capture_included = p_capture_included;
  806. }
  807. bool Animation::is_capture_included() const {
  808. return capture_included;
  809. }
  810. void Animation::_check_capture_included() {
  811. capture_included = false;
  812. for (int i = 0; i < tracks.size(); i++) {
  813. if (tracks[i]->type == TYPE_VALUE) {
  814. ValueTrack *vt = static_cast<ValueTrack *>(tracks[i]);
  815. if (vt->update_mode == UPDATE_CAPTURE) {
  816. capture_included = true;
  817. break;
  818. }
  819. }
  820. }
  821. }
  822. int Animation::get_track_count() const {
  823. return tracks.size();
  824. }
  825. Animation::TrackType Animation::track_get_type(int p_track) const {
  826. ERR_FAIL_INDEX_V(p_track, tracks.size(), TYPE_VALUE);
  827. return tracks[p_track]->type;
  828. }
  829. void Animation::track_set_path(int p_track, const NodePath &p_path) {
  830. ERR_FAIL_INDEX(p_track, tracks.size());
  831. tracks[p_track]->path = p_path;
  832. _track_update_hash(p_track);
  833. emit_changed();
  834. }
  835. NodePath Animation::track_get_path(int p_track) const {
  836. ERR_FAIL_INDEX_V(p_track, tracks.size(), NodePath());
  837. return tracks[p_track]->path;
  838. }
  839. int Animation::find_track(const NodePath &p_path, const TrackType p_type) const {
  840. for (int i = 0; i < tracks.size(); i++) {
  841. if (tracks[i]->path == p_path && tracks[i]->type == p_type) {
  842. return i;
  843. }
  844. };
  845. return -1;
  846. };
  847. Animation::TrackType Animation::get_cache_type(TrackType p_type) {
  848. if (p_type == Animation::TYPE_BEZIER) {
  849. return Animation::TYPE_VALUE;
  850. }
  851. if (p_type == Animation::TYPE_ROTATION_3D || p_type == Animation::TYPE_SCALE_3D) {
  852. return Animation::TYPE_POSITION_3D; // Reference them as position3D tracks, even if they modify rotation or scale.
  853. }
  854. return p_type;
  855. }
  856. void Animation::_track_update_hash(int p_track) {
  857. NodePath track_path = tracks[p_track]->path;
  858. TrackType track_cache_type = get_cache_type(tracks[p_track]->type);
  859. tracks[p_track]->thash = StringName(String(track_path.get_concatenated_names()) + String(track_path.get_concatenated_subnames()) + itos(track_cache_type)).hash();
  860. }
  861. Animation::TypeHash Animation::track_get_type_hash(int p_track) const {
  862. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  863. return tracks[p_track]->thash;
  864. }
  865. void Animation::track_set_interpolation_type(int p_track, InterpolationType p_interp) {
  866. ERR_FAIL_INDEX(p_track, tracks.size());
  867. tracks[p_track]->interpolation = p_interp;
  868. emit_changed();
  869. }
  870. Animation::InterpolationType Animation::track_get_interpolation_type(int p_track) const {
  871. ERR_FAIL_INDEX_V(p_track, tracks.size(), INTERPOLATION_NEAREST);
  872. return tracks[p_track]->interpolation;
  873. }
  874. void Animation::track_set_interpolation_loop_wrap(int p_track, bool p_enable) {
  875. ERR_FAIL_INDEX(p_track, tracks.size());
  876. tracks[p_track]->loop_wrap = p_enable;
  877. emit_changed();
  878. }
  879. bool Animation::track_get_interpolation_loop_wrap(int p_track) const {
  880. ERR_FAIL_INDEX_V(p_track, tracks.size(), INTERPOLATION_NEAREST);
  881. return tracks[p_track]->loop_wrap;
  882. }
  883. template <typename T, typename V>
  884. int Animation::_insert(double p_time, T &p_keys, const V &p_value) {
  885. int idx = p_keys.size();
  886. while (true) {
  887. // Condition for replacement.
  888. if (idx > 0 && Math::is_equal_approx((double)p_keys[idx - 1].time, p_time)) {
  889. float transition = p_keys[idx - 1].transition;
  890. p_keys.write[idx - 1] = p_value;
  891. p_keys.write[idx - 1].transition = transition;
  892. return idx - 1;
  893. // Condition for insert.
  894. } else if (idx == 0 || p_keys[idx - 1].time < p_time) {
  895. p_keys.insert(idx, p_value);
  896. return idx;
  897. }
  898. idx--;
  899. }
  900. return -1;
  901. }
  902. template <typename T>
  903. void Animation::_clear(T &p_keys) {
  904. p_keys.clear();
  905. }
  906. ////
  907. int Animation::position_track_insert_key(int p_track, double p_time, const Vector3 &p_position) {
  908. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  909. Track *t = tracks[p_track];
  910. ERR_FAIL_COND_V(t->type != TYPE_POSITION_3D, -1);
  911. PositionTrack *tt = static_cast<PositionTrack *>(t);
  912. ERR_FAIL_COND_V(tt->compressed_track >= 0, -1);
  913. TKey<Vector3> tkey;
  914. tkey.time = p_time;
  915. tkey.value = p_position;
  916. int ret = _insert(p_time, tt->positions, tkey);
  917. emit_changed();
  918. return ret;
  919. }
  920. Error Animation::position_track_get_key(int p_track, int p_key, Vector3 *r_position) const {
  921. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  922. Track *t = tracks[p_track];
  923. PositionTrack *tt = static_cast<PositionTrack *>(t);
  924. ERR_FAIL_COND_V(t->type != TYPE_POSITION_3D, ERR_INVALID_PARAMETER);
  925. if (tt->compressed_track >= 0) {
  926. Vector3i key;
  927. double time;
  928. bool fetch_success = _fetch_compressed_by_index<3>(tt->compressed_track, p_key, key, time);
  929. if (!fetch_success) {
  930. return ERR_INVALID_PARAMETER;
  931. }
  932. *r_position = _uncompress_pos_scale(tt->compressed_track, key);
  933. return OK;
  934. }
  935. ERR_FAIL_INDEX_V(p_key, tt->positions.size(), ERR_INVALID_PARAMETER);
  936. *r_position = tt->positions[p_key].value;
  937. return OK;
  938. }
  939. Error Animation::try_position_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation, bool p_backward) const {
  940. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  941. Track *t = tracks[p_track];
  942. ERR_FAIL_COND_V(t->type != TYPE_POSITION_3D, ERR_INVALID_PARAMETER);
  943. PositionTrack *tt = static_cast<PositionTrack *>(t);
  944. if (tt->compressed_track >= 0) {
  945. if (_pos_scale_interpolate_compressed(tt->compressed_track, p_time, *r_interpolation)) {
  946. return OK;
  947. } else {
  948. return ERR_UNAVAILABLE;
  949. }
  950. }
  951. bool ok = false;
  952. Vector3 tk = _interpolate(tt->positions, p_time, tt->interpolation, tt->loop_wrap, &ok, p_backward);
  953. if (!ok) {
  954. return ERR_UNAVAILABLE;
  955. }
  956. *r_interpolation = tk;
  957. return OK;
  958. }
  959. Vector3 Animation::position_track_interpolate(int p_track, double p_time, bool p_backward) const {
  960. Vector3 ret = Vector3(0, 0, 0);
  961. ERR_FAIL_INDEX_V(p_track, tracks.size(), ret);
  962. bool err = try_position_track_interpolate(p_track, p_time, &ret, p_backward);
  963. ERR_FAIL_COND_V_MSG(err, ret, "3D Position Track: '" + tracks[p_track]->path + "' is unavailable.");
  964. return ret;
  965. }
  966. ////
  967. int Animation::rotation_track_insert_key(int p_track, double p_time, const Quaternion &p_rotation) {
  968. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  969. Track *t = tracks[p_track];
  970. ERR_FAIL_COND_V(t->type != TYPE_ROTATION_3D, -1);
  971. RotationTrack *rt = static_cast<RotationTrack *>(t);
  972. ERR_FAIL_COND_V(rt->compressed_track >= 0, -1);
  973. TKey<Quaternion> tkey;
  974. tkey.time = p_time;
  975. tkey.value = p_rotation;
  976. int ret = _insert(p_time, rt->rotations, tkey);
  977. emit_changed();
  978. return ret;
  979. }
  980. Error Animation::rotation_track_get_key(int p_track, int p_key, Quaternion *r_rotation) const {
  981. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  982. Track *t = tracks[p_track];
  983. RotationTrack *rt = static_cast<RotationTrack *>(t);
  984. ERR_FAIL_COND_V(t->type != TYPE_ROTATION_3D, ERR_INVALID_PARAMETER);
  985. if (rt->compressed_track >= 0) {
  986. Vector3i key;
  987. double time;
  988. bool fetch_success = _fetch_compressed_by_index<3>(rt->compressed_track, p_key, key, time);
  989. if (!fetch_success) {
  990. return ERR_INVALID_PARAMETER;
  991. }
  992. *r_rotation = _uncompress_quaternion(key);
  993. return OK;
  994. }
  995. ERR_FAIL_INDEX_V(p_key, rt->rotations.size(), ERR_INVALID_PARAMETER);
  996. *r_rotation = rt->rotations[p_key].value;
  997. return OK;
  998. }
  999. Error Animation::try_rotation_track_interpolate(int p_track, double p_time, Quaternion *r_interpolation, bool p_backward) const {
  1000. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  1001. Track *t = tracks[p_track];
  1002. ERR_FAIL_COND_V(t->type != TYPE_ROTATION_3D, ERR_INVALID_PARAMETER);
  1003. RotationTrack *rt = static_cast<RotationTrack *>(t);
  1004. if (rt->compressed_track >= 0) {
  1005. if (_rotation_interpolate_compressed(rt->compressed_track, p_time, *r_interpolation)) {
  1006. return OK;
  1007. } else {
  1008. return ERR_UNAVAILABLE;
  1009. }
  1010. }
  1011. bool ok = false;
  1012. Quaternion tk = _interpolate(rt->rotations, p_time, rt->interpolation, rt->loop_wrap, &ok, p_backward);
  1013. if (!ok) {
  1014. return ERR_UNAVAILABLE;
  1015. }
  1016. *r_interpolation = tk;
  1017. return OK;
  1018. }
  1019. Quaternion Animation::rotation_track_interpolate(int p_track, double p_time, bool p_backward) const {
  1020. Quaternion ret = Quaternion(0, 0, 0, 1);
  1021. ERR_FAIL_INDEX_V(p_track, tracks.size(), ret);
  1022. bool err = try_rotation_track_interpolate(p_track, p_time, &ret, p_backward);
  1023. ERR_FAIL_COND_V_MSG(err, ret, "3D Rotation Track: '" + tracks[p_track]->path + "' is unavailable.");
  1024. return ret;
  1025. }
  1026. ////
  1027. int Animation::scale_track_insert_key(int p_track, double p_time, const Vector3 &p_scale) {
  1028. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1029. Track *t = tracks[p_track];
  1030. ERR_FAIL_COND_V(t->type != TYPE_SCALE_3D, -1);
  1031. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1032. ERR_FAIL_COND_V(st->compressed_track >= 0, -1);
  1033. TKey<Vector3> tkey;
  1034. tkey.time = p_time;
  1035. tkey.value = p_scale;
  1036. int ret = _insert(p_time, st->scales, tkey);
  1037. emit_changed();
  1038. return ret;
  1039. }
  1040. Error Animation::scale_track_get_key(int p_track, int p_key, Vector3 *r_scale) const {
  1041. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  1042. Track *t = tracks[p_track];
  1043. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1044. ERR_FAIL_COND_V(t->type != TYPE_SCALE_3D, ERR_INVALID_PARAMETER);
  1045. if (st->compressed_track >= 0) {
  1046. Vector3i key;
  1047. double time;
  1048. bool fetch_success = _fetch_compressed_by_index<3>(st->compressed_track, p_key, key, time);
  1049. if (!fetch_success) {
  1050. return ERR_INVALID_PARAMETER;
  1051. }
  1052. *r_scale = _uncompress_pos_scale(st->compressed_track, key);
  1053. return OK;
  1054. }
  1055. ERR_FAIL_INDEX_V(p_key, st->scales.size(), ERR_INVALID_PARAMETER);
  1056. *r_scale = st->scales[p_key].value;
  1057. return OK;
  1058. }
  1059. Error Animation::try_scale_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation, bool p_backward) const {
  1060. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  1061. Track *t = tracks[p_track];
  1062. ERR_FAIL_COND_V(t->type != TYPE_SCALE_3D, ERR_INVALID_PARAMETER);
  1063. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1064. if (st->compressed_track >= 0) {
  1065. if (_pos_scale_interpolate_compressed(st->compressed_track, p_time, *r_interpolation)) {
  1066. return OK;
  1067. } else {
  1068. return ERR_UNAVAILABLE;
  1069. }
  1070. }
  1071. bool ok = false;
  1072. Vector3 tk = _interpolate(st->scales, p_time, st->interpolation, st->loop_wrap, &ok, p_backward);
  1073. if (!ok) {
  1074. return ERR_UNAVAILABLE;
  1075. }
  1076. *r_interpolation = tk;
  1077. return OK;
  1078. }
  1079. Vector3 Animation::scale_track_interpolate(int p_track, double p_time, bool p_backward) const {
  1080. Vector3 ret = Vector3(1, 1, 1);
  1081. ERR_FAIL_INDEX_V(p_track, tracks.size(), ret);
  1082. bool err = try_scale_track_interpolate(p_track, p_time, &ret, p_backward);
  1083. ERR_FAIL_COND_V_MSG(err, ret, "3D Scale Track: '" + tracks[p_track]->path + "' is unavailable.");
  1084. return ret;
  1085. }
  1086. ////
  1087. int Animation::blend_shape_track_insert_key(int p_track, double p_time, float p_blend_shape) {
  1088. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1089. Track *t = tracks[p_track];
  1090. ERR_FAIL_COND_V(t->type != TYPE_BLEND_SHAPE, -1);
  1091. BlendShapeTrack *st = static_cast<BlendShapeTrack *>(t);
  1092. ERR_FAIL_COND_V(st->compressed_track >= 0, -1);
  1093. TKey<float> tkey;
  1094. tkey.time = p_time;
  1095. tkey.value = p_blend_shape;
  1096. int ret = _insert(p_time, st->blend_shapes, tkey);
  1097. emit_changed();
  1098. return ret;
  1099. }
  1100. Error Animation::blend_shape_track_get_key(int p_track, int p_key, float *r_blend_shape) const {
  1101. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  1102. Track *t = tracks[p_track];
  1103. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1104. ERR_FAIL_COND_V(t->type != TYPE_BLEND_SHAPE, ERR_INVALID_PARAMETER);
  1105. if (bst->compressed_track >= 0) {
  1106. Vector3i key;
  1107. double time;
  1108. bool fetch_success = _fetch_compressed_by_index<1>(bst->compressed_track, p_key, key, time);
  1109. if (!fetch_success) {
  1110. return ERR_INVALID_PARAMETER;
  1111. }
  1112. *r_blend_shape = _uncompress_blend_shape(key);
  1113. return OK;
  1114. }
  1115. ERR_FAIL_INDEX_V(p_key, bst->blend_shapes.size(), ERR_INVALID_PARAMETER);
  1116. *r_blend_shape = bst->blend_shapes[p_key].value;
  1117. return OK;
  1118. }
  1119. Error Animation::try_blend_shape_track_interpolate(int p_track, double p_time, float *r_interpolation, bool p_backward) const {
  1120. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  1121. Track *t = tracks[p_track];
  1122. ERR_FAIL_COND_V(t->type != TYPE_BLEND_SHAPE, ERR_INVALID_PARAMETER);
  1123. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1124. if (bst->compressed_track >= 0) {
  1125. if (_blend_shape_interpolate_compressed(bst->compressed_track, p_time, *r_interpolation)) {
  1126. return OK;
  1127. } else {
  1128. return ERR_UNAVAILABLE;
  1129. }
  1130. }
  1131. bool ok = false;
  1132. float tk = _interpolate(bst->blend_shapes, p_time, bst->interpolation, bst->loop_wrap, &ok, p_backward);
  1133. if (!ok) {
  1134. return ERR_UNAVAILABLE;
  1135. }
  1136. *r_interpolation = tk;
  1137. return OK;
  1138. }
  1139. float Animation::blend_shape_track_interpolate(int p_track, double p_time, bool p_backward) const {
  1140. float ret = 0;
  1141. ERR_FAIL_INDEX_V(p_track, tracks.size(), ret);
  1142. bool err = try_blend_shape_track_interpolate(p_track, p_time, &ret, p_backward);
  1143. ERR_FAIL_COND_V_MSG(err, ret, "Blend Shape Track: '" + tracks[p_track]->path + "' is unavailable.");
  1144. return ret;
  1145. }
  1146. ////
  1147. void Animation::track_remove_key_at_time(int p_track, double p_time) {
  1148. int idx = track_find_key(p_track, p_time, FIND_MODE_APPROX);
  1149. ERR_FAIL_COND(idx < 0);
  1150. track_remove_key(p_track, idx);
  1151. }
  1152. void Animation::track_remove_key(int p_track, int p_idx) {
  1153. ERR_FAIL_INDEX(p_track, tracks.size());
  1154. Track *t = tracks[p_track];
  1155. switch (t->type) {
  1156. case TYPE_POSITION_3D: {
  1157. PositionTrack *tt = static_cast<PositionTrack *>(t);
  1158. ERR_FAIL_COND(tt->compressed_track >= 0);
  1159. ERR_FAIL_INDEX(p_idx, tt->positions.size());
  1160. tt->positions.remove_at(p_idx);
  1161. } break;
  1162. case TYPE_ROTATION_3D: {
  1163. RotationTrack *rt = static_cast<RotationTrack *>(t);
  1164. ERR_FAIL_COND(rt->compressed_track >= 0);
  1165. ERR_FAIL_INDEX(p_idx, rt->rotations.size());
  1166. rt->rotations.remove_at(p_idx);
  1167. } break;
  1168. case TYPE_SCALE_3D: {
  1169. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1170. ERR_FAIL_COND(st->compressed_track >= 0);
  1171. ERR_FAIL_INDEX(p_idx, st->scales.size());
  1172. st->scales.remove_at(p_idx);
  1173. } break;
  1174. case TYPE_BLEND_SHAPE: {
  1175. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1176. ERR_FAIL_COND(bst->compressed_track >= 0);
  1177. ERR_FAIL_INDEX(p_idx, bst->blend_shapes.size());
  1178. bst->blend_shapes.remove_at(p_idx);
  1179. } break;
  1180. case TYPE_VALUE: {
  1181. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1182. ERR_FAIL_INDEX(p_idx, vt->values.size());
  1183. vt->values.remove_at(p_idx);
  1184. } break;
  1185. case TYPE_METHOD: {
  1186. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1187. ERR_FAIL_INDEX(p_idx, mt->methods.size());
  1188. mt->methods.remove_at(p_idx);
  1189. } break;
  1190. case TYPE_BEZIER: {
  1191. BezierTrack *bz = static_cast<BezierTrack *>(t);
  1192. ERR_FAIL_INDEX(p_idx, bz->values.size());
  1193. bz->values.remove_at(p_idx);
  1194. } break;
  1195. case TYPE_AUDIO: {
  1196. AudioTrack *ad = static_cast<AudioTrack *>(t);
  1197. ERR_FAIL_INDEX(p_idx, ad->values.size());
  1198. ad->values.remove_at(p_idx);
  1199. } break;
  1200. case TYPE_ANIMATION: {
  1201. AnimationTrack *an = static_cast<AnimationTrack *>(t);
  1202. ERR_FAIL_INDEX(p_idx, an->values.size());
  1203. an->values.remove_at(p_idx);
  1204. } break;
  1205. }
  1206. emit_changed();
  1207. }
  1208. int Animation::track_find_key(int p_track, double p_time, FindMode p_find_mode, bool p_limit, bool p_backward) const {
  1209. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1210. Track *t = tracks[p_track];
  1211. switch (t->type) {
  1212. case TYPE_POSITION_3D: {
  1213. PositionTrack *tt = static_cast<PositionTrack *>(t);
  1214. if (tt->compressed_track >= 0) {
  1215. double time;
  1216. double time_next;
  1217. Vector3i key;
  1218. Vector3i key_next;
  1219. uint32_t key_index;
  1220. bool fetch_compressed_success = _fetch_compressed<3>(tt->compressed_track, p_time, key, time, key_next, time_next, &key_index);
  1221. ERR_FAIL_COND_V(!fetch_compressed_success, -1);
  1222. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(time, p_time)) || (p_find_mode == FIND_MODE_EXACT && time != p_time)) {
  1223. return -1;
  1224. }
  1225. return key_index;
  1226. }
  1227. int k = _find(tt->positions, p_time, p_backward, p_limit);
  1228. if (k < 0 || k >= tt->positions.size()) {
  1229. return -1;
  1230. }
  1231. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(tt->positions[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && tt->positions[k].time != p_time)) {
  1232. return -1;
  1233. }
  1234. return k;
  1235. } break;
  1236. case TYPE_ROTATION_3D: {
  1237. RotationTrack *rt = static_cast<RotationTrack *>(t);
  1238. if (rt->compressed_track >= 0) {
  1239. double time;
  1240. double time_next;
  1241. Vector3i key;
  1242. Vector3i key_next;
  1243. uint32_t key_index;
  1244. bool fetch_compressed_success = _fetch_compressed<3>(rt->compressed_track, p_time, key, time, key_next, time_next, &key_index);
  1245. ERR_FAIL_COND_V(!fetch_compressed_success, -1);
  1246. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(time, p_time)) || (p_find_mode == FIND_MODE_EXACT && time != p_time)) {
  1247. return -1;
  1248. }
  1249. return key_index;
  1250. }
  1251. int k = _find(rt->rotations, p_time, p_backward, p_limit);
  1252. if (k < 0 || k >= rt->rotations.size()) {
  1253. return -1;
  1254. }
  1255. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(rt->rotations[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && rt->rotations[k].time != p_time)) {
  1256. return -1;
  1257. }
  1258. return k;
  1259. } break;
  1260. case TYPE_SCALE_3D: {
  1261. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1262. if (st->compressed_track >= 0) {
  1263. double time;
  1264. double time_next;
  1265. Vector3i key;
  1266. Vector3i key_next;
  1267. uint32_t key_index;
  1268. bool fetch_compressed_success = _fetch_compressed<3>(st->compressed_track, p_time, key, time, key_next, time_next, &key_index);
  1269. ERR_FAIL_COND_V(!fetch_compressed_success, -1);
  1270. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(time, p_time)) || (p_find_mode == FIND_MODE_EXACT && time != p_time)) {
  1271. return -1;
  1272. }
  1273. return key_index;
  1274. }
  1275. int k = _find(st->scales, p_time, p_backward, p_limit);
  1276. if (k < 0 || k >= st->scales.size()) {
  1277. return -1;
  1278. }
  1279. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(st->scales[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && st->scales[k].time != p_time)) {
  1280. return -1;
  1281. }
  1282. return k;
  1283. } break;
  1284. case TYPE_BLEND_SHAPE: {
  1285. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1286. if (bst->compressed_track >= 0) {
  1287. double time;
  1288. double time_next;
  1289. Vector3i key;
  1290. Vector3i key_next;
  1291. uint32_t key_index;
  1292. bool fetch_compressed_success = _fetch_compressed<1>(bst->compressed_track, p_time, key, time, key_next, time_next, &key_index);
  1293. ERR_FAIL_COND_V(!fetch_compressed_success, -1);
  1294. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(time, p_time)) || (p_find_mode == FIND_MODE_EXACT && time != p_time)) {
  1295. return -1;
  1296. }
  1297. return key_index;
  1298. }
  1299. int k = _find(bst->blend_shapes, p_time, p_backward, p_limit);
  1300. if (k < 0 || k >= bst->blend_shapes.size()) {
  1301. return -1;
  1302. }
  1303. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(bst->blend_shapes[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && bst->blend_shapes[k].time != p_time)) {
  1304. return -1;
  1305. }
  1306. return k;
  1307. } break;
  1308. case TYPE_VALUE: {
  1309. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1310. int k = _find(vt->values, p_time, p_backward, p_limit);
  1311. if (k < 0 || k >= vt->values.size()) {
  1312. return -1;
  1313. }
  1314. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(vt->values[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && vt->values[k].time != p_time)) {
  1315. return -1;
  1316. }
  1317. return k;
  1318. } break;
  1319. case TYPE_METHOD: {
  1320. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1321. int k = _find(mt->methods, p_time, p_backward, p_limit);
  1322. if (k < 0 || k >= mt->methods.size()) {
  1323. return -1;
  1324. }
  1325. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(mt->methods[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && mt->methods[k].time != p_time)) {
  1326. return -1;
  1327. }
  1328. return k;
  1329. } break;
  1330. case TYPE_BEZIER: {
  1331. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1332. int k = _find(bt->values, p_time, p_backward, p_limit);
  1333. if (k < 0 || k >= bt->values.size()) {
  1334. return -1;
  1335. }
  1336. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(bt->values[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && bt->values[k].time != p_time)) {
  1337. return -1;
  1338. }
  1339. return k;
  1340. } break;
  1341. case TYPE_AUDIO: {
  1342. AudioTrack *at = static_cast<AudioTrack *>(t);
  1343. int k = _find(at->values, p_time, p_backward, p_limit);
  1344. if (k < 0 || k >= at->values.size()) {
  1345. return -1;
  1346. }
  1347. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(at->values[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && at->values[k].time != p_time)) {
  1348. return -1;
  1349. }
  1350. return k;
  1351. } break;
  1352. case TYPE_ANIMATION: {
  1353. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1354. int k = _find(at->values, p_time, p_backward, p_limit);
  1355. if (k < 0 || k >= at->values.size()) {
  1356. return -1;
  1357. }
  1358. if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(at->values[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && at->values[k].time != p_time)) {
  1359. return -1;
  1360. }
  1361. return k;
  1362. } break;
  1363. }
  1364. return -1;
  1365. }
  1366. int Animation::track_insert_key(int p_track, double p_time, const Variant &p_key, real_t p_transition) {
  1367. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1368. Track *t = tracks[p_track];
  1369. int ret = -1;
  1370. switch (t->type) {
  1371. case TYPE_POSITION_3D: {
  1372. ERR_FAIL_COND_V((p_key.get_type() != Variant::VECTOR3) && (p_key.get_type() != Variant::VECTOR3I), -1);
  1373. ret = position_track_insert_key(p_track, p_time, p_key);
  1374. track_set_key_transition(p_track, ret, p_transition);
  1375. } break;
  1376. case TYPE_ROTATION_3D: {
  1377. ERR_FAIL_COND_V((p_key.get_type() != Variant::QUATERNION) && (p_key.get_type() != Variant::BASIS), -1);
  1378. ret = rotation_track_insert_key(p_track, p_time, p_key);
  1379. track_set_key_transition(p_track, ret, p_transition);
  1380. } break;
  1381. case TYPE_SCALE_3D: {
  1382. ERR_FAIL_COND_V((p_key.get_type() != Variant::VECTOR3) && (p_key.get_type() != Variant::VECTOR3I), -1);
  1383. ret = scale_track_insert_key(p_track, p_time, p_key);
  1384. track_set_key_transition(p_track, ret, p_transition);
  1385. } break;
  1386. case TYPE_BLEND_SHAPE: {
  1387. ERR_FAIL_COND_V((p_key.get_type() != Variant::FLOAT) && (p_key.get_type() != Variant::INT), -1);
  1388. ret = blend_shape_track_insert_key(p_track, p_time, p_key);
  1389. track_set_key_transition(p_track, ret, p_transition);
  1390. } break;
  1391. case TYPE_VALUE: {
  1392. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1393. TKey<Variant> k;
  1394. k.time = p_time;
  1395. k.transition = p_transition;
  1396. k.value = p_key;
  1397. ret = _insert(p_time, vt->values, k);
  1398. } break;
  1399. case TYPE_METHOD: {
  1400. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1401. ERR_FAIL_COND_V(p_key.get_type() != Variant::DICTIONARY, -1);
  1402. Dictionary d = p_key;
  1403. ERR_FAIL_COND_V(!d.has("method") || (d["method"].get_type() != Variant::STRING_NAME && d["method"].get_type() != Variant::STRING), -1);
  1404. ERR_FAIL_COND_V(!d.has("args") || !d["args"].is_array(), -1);
  1405. MethodKey k;
  1406. k.time = p_time;
  1407. k.transition = p_transition;
  1408. k.method = d["method"];
  1409. k.params = d["args"];
  1410. ret = _insert(p_time, mt->methods, k);
  1411. } break;
  1412. case TYPE_BEZIER: {
  1413. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1414. Array arr = p_key;
  1415. ERR_FAIL_COND_V(arr.size() != 5, -1);
  1416. TKey<BezierKey> k;
  1417. k.time = p_time;
  1418. k.value.value = arr[0];
  1419. k.value.in_handle.x = arr[1];
  1420. k.value.in_handle.y = arr[2];
  1421. k.value.out_handle.x = arr[3];
  1422. k.value.out_handle.y = arr[4];
  1423. ret = _insert(p_time, bt->values, k);
  1424. Vector<int> key_neighborhood;
  1425. key_neighborhood.push_back(ret);
  1426. if (ret > 0) {
  1427. key_neighborhood.push_back(ret - 1);
  1428. }
  1429. if (ret < track_get_key_count(p_track) - 1) {
  1430. key_neighborhood.push_back(ret + 1);
  1431. }
  1432. } break;
  1433. case TYPE_AUDIO: {
  1434. AudioTrack *at = static_cast<AudioTrack *>(t);
  1435. Dictionary k = p_key;
  1436. ERR_FAIL_COND_V(!k.has("start_offset"), -1);
  1437. ERR_FAIL_COND_V(!k.has("end_offset"), -1);
  1438. ERR_FAIL_COND_V(!k.has("stream"), -1);
  1439. TKey<AudioKey> ak;
  1440. ak.time = p_time;
  1441. ak.value.start_offset = k["start_offset"];
  1442. ak.value.end_offset = k["end_offset"];
  1443. ak.value.stream = k["stream"];
  1444. ret = _insert(p_time, at->values, ak);
  1445. } break;
  1446. case TYPE_ANIMATION: {
  1447. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1448. TKey<StringName> ak;
  1449. ak.time = p_time;
  1450. ak.value = p_key;
  1451. ret = _insert(p_time, at->values, ak);
  1452. } break;
  1453. }
  1454. emit_changed();
  1455. return ret;
  1456. }
  1457. int Animation::track_get_key_count(int p_track) const {
  1458. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1459. Track *t = tracks[p_track];
  1460. switch (t->type) {
  1461. case TYPE_POSITION_3D: {
  1462. PositionTrack *tt = static_cast<PositionTrack *>(t);
  1463. if (tt->compressed_track >= 0) {
  1464. return _get_compressed_key_count(tt->compressed_track);
  1465. }
  1466. return tt->positions.size();
  1467. } break;
  1468. case TYPE_ROTATION_3D: {
  1469. RotationTrack *rt = static_cast<RotationTrack *>(t);
  1470. if (rt->compressed_track >= 0) {
  1471. return _get_compressed_key_count(rt->compressed_track);
  1472. }
  1473. return rt->rotations.size();
  1474. } break;
  1475. case TYPE_SCALE_3D: {
  1476. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1477. if (st->compressed_track >= 0) {
  1478. return _get_compressed_key_count(st->compressed_track);
  1479. }
  1480. return st->scales.size();
  1481. } break;
  1482. case TYPE_BLEND_SHAPE: {
  1483. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1484. if (bst->compressed_track >= 0) {
  1485. return _get_compressed_key_count(bst->compressed_track);
  1486. }
  1487. return bst->blend_shapes.size();
  1488. } break;
  1489. case TYPE_VALUE: {
  1490. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1491. return vt->values.size();
  1492. } break;
  1493. case TYPE_METHOD: {
  1494. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1495. return mt->methods.size();
  1496. } break;
  1497. case TYPE_BEZIER: {
  1498. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1499. return bt->values.size();
  1500. } break;
  1501. case TYPE_AUDIO: {
  1502. AudioTrack *at = static_cast<AudioTrack *>(t);
  1503. return at->values.size();
  1504. } break;
  1505. case TYPE_ANIMATION: {
  1506. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1507. return at->values.size();
  1508. } break;
  1509. }
  1510. ERR_FAIL_V(-1);
  1511. }
  1512. Variant Animation::track_get_key_value(int p_track, int p_key_idx) const {
  1513. ERR_FAIL_INDEX_V(p_track, tracks.size(), Variant());
  1514. Track *t = tracks[p_track];
  1515. switch (t->type) {
  1516. case TYPE_POSITION_3D: {
  1517. Vector3 value;
  1518. position_track_get_key(p_track, p_key_idx, &value);
  1519. return value;
  1520. } break;
  1521. case TYPE_ROTATION_3D: {
  1522. Quaternion value;
  1523. rotation_track_get_key(p_track, p_key_idx, &value);
  1524. return value;
  1525. } break;
  1526. case TYPE_SCALE_3D: {
  1527. Vector3 value;
  1528. scale_track_get_key(p_track, p_key_idx, &value);
  1529. return value;
  1530. } break;
  1531. case TYPE_BLEND_SHAPE: {
  1532. float value;
  1533. blend_shape_track_get_key(p_track, p_key_idx, &value);
  1534. return value;
  1535. } break;
  1536. case TYPE_VALUE: {
  1537. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1538. ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), Variant());
  1539. return vt->values[p_key_idx].value;
  1540. } break;
  1541. case TYPE_METHOD: {
  1542. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1543. ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), Variant());
  1544. Dictionary d;
  1545. d["method"] = mt->methods[p_key_idx].method;
  1546. d["args"] = mt->methods[p_key_idx].params;
  1547. return d;
  1548. } break;
  1549. case TYPE_BEZIER: {
  1550. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1551. ERR_FAIL_INDEX_V(p_key_idx, bt->values.size(), Variant());
  1552. Array arr;
  1553. arr.resize(5);
  1554. arr[0] = bt->values[p_key_idx].value.value;
  1555. arr[1] = bt->values[p_key_idx].value.in_handle.x;
  1556. arr[2] = bt->values[p_key_idx].value.in_handle.y;
  1557. arr[3] = bt->values[p_key_idx].value.out_handle.x;
  1558. arr[4] = bt->values[p_key_idx].value.out_handle.y;
  1559. return arr;
  1560. } break;
  1561. case TYPE_AUDIO: {
  1562. AudioTrack *at = static_cast<AudioTrack *>(t);
  1563. ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant());
  1564. Dictionary k;
  1565. k["start_offset"] = at->values[p_key_idx].value.start_offset;
  1566. k["end_offset"] = at->values[p_key_idx].value.end_offset;
  1567. k["stream"] = at->values[p_key_idx].value.stream;
  1568. return k;
  1569. } break;
  1570. case TYPE_ANIMATION: {
  1571. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1572. ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant());
  1573. return at->values[p_key_idx].value;
  1574. } break;
  1575. }
  1576. ERR_FAIL_V(Variant());
  1577. }
  1578. double Animation::track_get_key_time(int p_track, int p_key_idx) const {
  1579. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1580. Track *t = tracks[p_track];
  1581. switch (t->type) {
  1582. case TYPE_POSITION_3D: {
  1583. PositionTrack *tt = static_cast<PositionTrack *>(t);
  1584. if (tt->compressed_track >= 0) {
  1585. Vector3i value;
  1586. double time;
  1587. bool fetch_compressed_success = _fetch_compressed_by_index<3>(tt->compressed_track, p_key_idx, value, time);
  1588. ERR_FAIL_COND_V(!fetch_compressed_success, false);
  1589. return time;
  1590. }
  1591. ERR_FAIL_INDEX_V(p_key_idx, tt->positions.size(), -1);
  1592. return tt->positions[p_key_idx].time;
  1593. } break;
  1594. case TYPE_ROTATION_3D: {
  1595. RotationTrack *rt = static_cast<RotationTrack *>(t);
  1596. if (rt->compressed_track >= 0) {
  1597. Vector3i value;
  1598. double time;
  1599. bool fetch_compressed_success = _fetch_compressed_by_index<3>(rt->compressed_track, p_key_idx, value, time);
  1600. ERR_FAIL_COND_V(!fetch_compressed_success, false);
  1601. return time;
  1602. }
  1603. ERR_FAIL_INDEX_V(p_key_idx, rt->rotations.size(), -1);
  1604. return rt->rotations[p_key_idx].time;
  1605. } break;
  1606. case TYPE_SCALE_3D: {
  1607. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1608. if (st->compressed_track >= 0) {
  1609. Vector3i value;
  1610. double time;
  1611. bool fetch_compressed_success = _fetch_compressed_by_index<3>(st->compressed_track, p_key_idx, value, time);
  1612. ERR_FAIL_COND_V(!fetch_compressed_success, false);
  1613. return time;
  1614. }
  1615. ERR_FAIL_INDEX_V(p_key_idx, st->scales.size(), -1);
  1616. return st->scales[p_key_idx].time;
  1617. } break;
  1618. case TYPE_BLEND_SHAPE: {
  1619. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1620. if (bst->compressed_track >= 0) {
  1621. Vector3i value;
  1622. double time;
  1623. bool fetch_compressed_success = _fetch_compressed_by_index<1>(bst->compressed_track, p_key_idx, value, time);
  1624. ERR_FAIL_COND_V(!fetch_compressed_success, false);
  1625. return time;
  1626. }
  1627. ERR_FAIL_INDEX_V(p_key_idx, bst->blend_shapes.size(), -1);
  1628. return bst->blend_shapes[p_key_idx].time;
  1629. } break;
  1630. case TYPE_VALUE: {
  1631. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1632. ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
  1633. return vt->values[p_key_idx].time;
  1634. } break;
  1635. case TYPE_METHOD: {
  1636. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1637. ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
  1638. return mt->methods[p_key_idx].time;
  1639. } break;
  1640. case TYPE_BEZIER: {
  1641. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1642. ERR_FAIL_INDEX_V(p_key_idx, bt->values.size(), -1);
  1643. return bt->values[p_key_idx].time;
  1644. } break;
  1645. case TYPE_AUDIO: {
  1646. AudioTrack *at = static_cast<AudioTrack *>(t);
  1647. ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), -1);
  1648. return at->values[p_key_idx].time;
  1649. } break;
  1650. case TYPE_ANIMATION: {
  1651. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1652. ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), -1);
  1653. return at->values[p_key_idx].time;
  1654. } break;
  1655. }
  1656. ERR_FAIL_V(-1);
  1657. }
  1658. void Animation::track_set_key_time(int p_track, int p_key_idx, double p_time) {
  1659. ERR_FAIL_INDEX(p_track, tracks.size());
  1660. Track *t = tracks[p_track];
  1661. switch (t->type) {
  1662. case TYPE_POSITION_3D: {
  1663. PositionTrack *tt = static_cast<PositionTrack *>(t);
  1664. ERR_FAIL_COND(tt->compressed_track >= 0);
  1665. ERR_FAIL_INDEX(p_key_idx, tt->positions.size());
  1666. TKey<Vector3> key = tt->positions[p_key_idx];
  1667. key.time = p_time;
  1668. tt->positions.remove_at(p_key_idx);
  1669. _insert(p_time, tt->positions, key);
  1670. return;
  1671. }
  1672. case TYPE_ROTATION_3D: {
  1673. RotationTrack *tt = static_cast<RotationTrack *>(t);
  1674. ERR_FAIL_COND(tt->compressed_track >= 0);
  1675. ERR_FAIL_INDEX(p_key_idx, tt->rotations.size());
  1676. TKey<Quaternion> key = tt->rotations[p_key_idx];
  1677. key.time = p_time;
  1678. tt->rotations.remove_at(p_key_idx);
  1679. _insert(p_time, tt->rotations, key);
  1680. return;
  1681. }
  1682. case TYPE_SCALE_3D: {
  1683. ScaleTrack *tt = static_cast<ScaleTrack *>(t);
  1684. ERR_FAIL_COND(tt->compressed_track >= 0);
  1685. ERR_FAIL_INDEX(p_key_idx, tt->scales.size());
  1686. TKey<Vector3> key = tt->scales[p_key_idx];
  1687. key.time = p_time;
  1688. tt->scales.remove_at(p_key_idx);
  1689. _insert(p_time, tt->scales, key);
  1690. return;
  1691. }
  1692. case TYPE_BLEND_SHAPE: {
  1693. BlendShapeTrack *tt = static_cast<BlendShapeTrack *>(t);
  1694. ERR_FAIL_COND(tt->compressed_track >= 0);
  1695. ERR_FAIL_INDEX(p_key_idx, tt->blend_shapes.size());
  1696. TKey<float> key = tt->blend_shapes[p_key_idx];
  1697. key.time = p_time;
  1698. tt->blend_shapes.remove_at(p_key_idx);
  1699. _insert(p_time, tt->blend_shapes, key);
  1700. return;
  1701. }
  1702. case TYPE_VALUE: {
  1703. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1704. ERR_FAIL_INDEX(p_key_idx, vt->values.size());
  1705. TKey<Variant> key = vt->values[p_key_idx];
  1706. key.time = p_time;
  1707. vt->values.remove_at(p_key_idx);
  1708. _insert(p_time, vt->values, key);
  1709. return;
  1710. }
  1711. case TYPE_METHOD: {
  1712. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1713. ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
  1714. MethodKey key = mt->methods[p_key_idx];
  1715. key.time = p_time;
  1716. mt->methods.remove_at(p_key_idx);
  1717. _insert(p_time, mt->methods, key);
  1718. return;
  1719. }
  1720. case TYPE_BEZIER: {
  1721. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1722. ERR_FAIL_INDEX(p_key_idx, bt->values.size());
  1723. TKey<BezierKey> key = bt->values[p_key_idx];
  1724. key.time = p_time;
  1725. bt->values.remove_at(p_key_idx);
  1726. _insert(p_time, bt->values, key);
  1727. return;
  1728. }
  1729. case TYPE_AUDIO: {
  1730. AudioTrack *at = static_cast<AudioTrack *>(t);
  1731. ERR_FAIL_INDEX(p_key_idx, at->values.size());
  1732. TKey<AudioKey> key = at->values[p_key_idx];
  1733. key.time = p_time;
  1734. at->values.remove_at(p_key_idx);
  1735. _insert(p_time, at->values, key);
  1736. return;
  1737. }
  1738. case TYPE_ANIMATION: {
  1739. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1740. ERR_FAIL_INDEX(p_key_idx, at->values.size());
  1741. TKey<StringName> key = at->values[p_key_idx];
  1742. key.time = p_time;
  1743. at->values.remove_at(p_key_idx);
  1744. _insert(p_time, at->values, key);
  1745. return;
  1746. }
  1747. }
  1748. ERR_FAIL();
  1749. }
  1750. real_t Animation::track_get_key_transition(int p_track, int p_key_idx) const {
  1751. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1752. Track *t = tracks[p_track];
  1753. switch (t->type) {
  1754. case TYPE_POSITION_3D: {
  1755. PositionTrack *tt = static_cast<PositionTrack *>(t);
  1756. if (tt->compressed_track >= 0) {
  1757. return 1.0;
  1758. }
  1759. ERR_FAIL_INDEX_V(p_key_idx, tt->positions.size(), -1);
  1760. return tt->positions[p_key_idx].transition;
  1761. } break;
  1762. case TYPE_ROTATION_3D: {
  1763. RotationTrack *rt = static_cast<RotationTrack *>(t);
  1764. if (rt->compressed_track >= 0) {
  1765. return 1.0;
  1766. }
  1767. ERR_FAIL_INDEX_V(p_key_idx, rt->rotations.size(), -1);
  1768. return rt->rotations[p_key_idx].transition;
  1769. } break;
  1770. case TYPE_SCALE_3D: {
  1771. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1772. if (st->compressed_track >= 0) {
  1773. return 1.0;
  1774. }
  1775. ERR_FAIL_INDEX_V(p_key_idx, st->scales.size(), -1);
  1776. return st->scales[p_key_idx].transition;
  1777. } break;
  1778. case TYPE_BLEND_SHAPE: {
  1779. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1780. if (bst->compressed_track >= 0) {
  1781. return 1.0;
  1782. }
  1783. ERR_FAIL_INDEX_V(p_key_idx, bst->blend_shapes.size(), -1);
  1784. return bst->blend_shapes[p_key_idx].transition;
  1785. } break;
  1786. case TYPE_VALUE: {
  1787. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1788. ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
  1789. return vt->values[p_key_idx].transition;
  1790. } break;
  1791. case TYPE_METHOD: {
  1792. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1793. ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
  1794. return mt->methods[p_key_idx].transition;
  1795. } break;
  1796. case TYPE_BEZIER: {
  1797. return 1; //bezier does not really use transitions
  1798. } break;
  1799. case TYPE_AUDIO: {
  1800. return 1; //audio does not really use transitions
  1801. } break;
  1802. case TYPE_ANIMATION: {
  1803. return 1; //animation does not really use transitions
  1804. } break;
  1805. }
  1806. ERR_FAIL_V(0);
  1807. }
  1808. bool Animation::track_is_compressed(int p_track) const {
  1809. ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
  1810. Track *t = tracks[p_track];
  1811. switch (t->type) {
  1812. case TYPE_POSITION_3D: {
  1813. PositionTrack *tt = static_cast<PositionTrack *>(t);
  1814. return tt->compressed_track >= 0;
  1815. } break;
  1816. case TYPE_ROTATION_3D: {
  1817. RotationTrack *rt = static_cast<RotationTrack *>(t);
  1818. return rt->compressed_track >= 0;
  1819. } break;
  1820. case TYPE_SCALE_3D: {
  1821. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1822. return st->compressed_track >= 0;
  1823. } break;
  1824. case TYPE_BLEND_SHAPE: {
  1825. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1826. return bst->compressed_track >= 0;
  1827. } break;
  1828. default: {
  1829. return false; // Animation does not really use transitions.
  1830. } break;
  1831. }
  1832. }
  1833. void Animation::track_set_key_value(int p_track, int p_key_idx, const Variant &p_value) {
  1834. ERR_FAIL_INDEX(p_track, tracks.size());
  1835. Track *t = tracks[p_track];
  1836. switch (t->type) {
  1837. case TYPE_POSITION_3D: {
  1838. ERR_FAIL_COND((p_value.get_type() != Variant::VECTOR3) && (p_value.get_type() != Variant::VECTOR3I));
  1839. PositionTrack *tt = static_cast<PositionTrack *>(t);
  1840. ERR_FAIL_COND(tt->compressed_track >= 0);
  1841. ERR_FAIL_INDEX(p_key_idx, tt->positions.size());
  1842. tt->positions.write[p_key_idx].value = p_value;
  1843. } break;
  1844. case TYPE_ROTATION_3D: {
  1845. ERR_FAIL_COND((p_value.get_type() != Variant::QUATERNION) && (p_value.get_type() != Variant::BASIS));
  1846. RotationTrack *rt = static_cast<RotationTrack *>(t);
  1847. ERR_FAIL_COND(rt->compressed_track >= 0);
  1848. ERR_FAIL_INDEX(p_key_idx, rt->rotations.size());
  1849. rt->rotations.write[p_key_idx].value = p_value;
  1850. } break;
  1851. case TYPE_SCALE_3D: {
  1852. ERR_FAIL_COND((p_value.get_type() != Variant::VECTOR3) && (p_value.get_type() != Variant::VECTOR3I));
  1853. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1854. ERR_FAIL_COND(st->compressed_track >= 0);
  1855. ERR_FAIL_INDEX(p_key_idx, st->scales.size());
  1856. st->scales.write[p_key_idx].value = p_value;
  1857. } break;
  1858. case TYPE_BLEND_SHAPE: {
  1859. ERR_FAIL_COND((p_value.get_type() != Variant::FLOAT) && (p_value.get_type() != Variant::INT));
  1860. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1861. ERR_FAIL_COND(bst->compressed_track >= 0);
  1862. ERR_FAIL_INDEX(p_key_idx, bst->blend_shapes.size());
  1863. bst->blend_shapes.write[p_key_idx].value = p_value;
  1864. } break;
  1865. case TYPE_VALUE: {
  1866. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1867. ERR_FAIL_INDEX(p_key_idx, vt->values.size());
  1868. vt->values.write[p_key_idx].value = p_value;
  1869. } break;
  1870. case TYPE_METHOD: {
  1871. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1872. ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
  1873. Dictionary d = p_value;
  1874. if (d.has("method")) {
  1875. mt->methods.write[p_key_idx].method = d["method"];
  1876. }
  1877. if (d.has("args")) {
  1878. mt->methods.write[p_key_idx].params = d["args"];
  1879. }
  1880. } break;
  1881. case TYPE_BEZIER: {
  1882. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1883. ERR_FAIL_INDEX(p_key_idx, bt->values.size());
  1884. Array arr = p_value;
  1885. ERR_FAIL_COND(arr.size() != 5);
  1886. bt->values.write[p_key_idx].value.value = arr[0];
  1887. bt->values.write[p_key_idx].value.in_handle.x = arr[1];
  1888. bt->values.write[p_key_idx].value.in_handle.y = arr[2];
  1889. bt->values.write[p_key_idx].value.out_handle.x = arr[3];
  1890. bt->values.write[p_key_idx].value.out_handle.y = arr[4];
  1891. } break;
  1892. case TYPE_AUDIO: {
  1893. AudioTrack *at = static_cast<AudioTrack *>(t);
  1894. ERR_FAIL_INDEX(p_key_idx, at->values.size());
  1895. Dictionary k = p_value;
  1896. ERR_FAIL_COND(!k.has("start_offset"));
  1897. ERR_FAIL_COND(!k.has("end_offset"));
  1898. ERR_FAIL_COND(!k.has("stream"));
  1899. at->values.write[p_key_idx].value.start_offset = k["start_offset"];
  1900. at->values.write[p_key_idx].value.end_offset = k["end_offset"];
  1901. at->values.write[p_key_idx].value.stream = k["stream"];
  1902. } break;
  1903. case TYPE_ANIMATION: {
  1904. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1905. ERR_FAIL_INDEX(p_key_idx, at->values.size());
  1906. at->values.write[p_key_idx].value = p_value;
  1907. } break;
  1908. }
  1909. emit_changed();
  1910. }
  1911. void Animation::track_set_key_transition(int p_track, int p_key_idx, real_t p_transition) {
  1912. ERR_FAIL_INDEX(p_track, tracks.size());
  1913. Track *t = tracks[p_track];
  1914. switch (t->type) {
  1915. case TYPE_POSITION_3D: {
  1916. PositionTrack *tt = static_cast<PositionTrack *>(t);
  1917. ERR_FAIL_COND(tt->compressed_track >= 0);
  1918. ERR_FAIL_INDEX(p_key_idx, tt->positions.size());
  1919. tt->positions.write[p_key_idx].transition = p_transition;
  1920. } break;
  1921. case TYPE_ROTATION_3D: {
  1922. RotationTrack *rt = static_cast<RotationTrack *>(t);
  1923. ERR_FAIL_COND(rt->compressed_track >= 0);
  1924. ERR_FAIL_INDEX(p_key_idx, rt->rotations.size());
  1925. rt->rotations.write[p_key_idx].transition = p_transition;
  1926. } break;
  1927. case TYPE_SCALE_3D: {
  1928. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  1929. ERR_FAIL_COND(st->compressed_track >= 0);
  1930. ERR_FAIL_INDEX(p_key_idx, st->scales.size());
  1931. st->scales.write[p_key_idx].transition = p_transition;
  1932. } break;
  1933. case TYPE_BLEND_SHAPE: {
  1934. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  1935. ERR_FAIL_COND(bst->compressed_track >= 0);
  1936. ERR_FAIL_INDEX(p_key_idx, bst->blend_shapes.size());
  1937. bst->blend_shapes.write[p_key_idx].transition = p_transition;
  1938. } break;
  1939. case TYPE_VALUE: {
  1940. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1941. ERR_FAIL_INDEX(p_key_idx, vt->values.size());
  1942. vt->values.write[p_key_idx].transition = p_transition;
  1943. } break;
  1944. case TYPE_METHOD: {
  1945. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1946. ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
  1947. mt->methods.write[p_key_idx].transition = p_transition;
  1948. } break;
  1949. case TYPE_BEZIER:
  1950. case TYPE_AUDIO:
  1951. case TYPE_ANIMATION: {
  1952. // they don't use transition
  1953. } break;
  1954. }
  1955. emit_changed();
  1956. }
  1957. template <typename K>
  1958. int Animation::_find(const Vector<K> &p_keys, double p_time, bool p_backward, bool p_limit) const {
  1959. int len = p_keys.size();
  1960. if (len == 0) {
  1961. return -2;
  1962. }
  1963. int low = 0;
  1964. int high = len - 1;
  1965. int middle = 0;
  1966. #ifdef DEBUG_ENABLED
  1967. if (low > high) {
  1968. ERR_PRINT("low > high, this may be a bug.");
  1969. }
  1970. #endif
  1971. const K *keys = &p_keys[0];
  1972. while (low <= high) {
  1973. middle = (low + high) / 2;
  1974. if (Math::is_equal_approx(p_time, (double)keys[middle].time)) { //match
  1975. return middle;
  1976. } else if (p_time < keys[middle].time) {
  1977. high = middle - 1; //search low end of array
  1978. } else {
  1979. low = middle + 1; //search high end of array
  1980. }
  1981. }
  1982. if (!p_backward) {
  1983. if (keys[middle].time > p_time) {
  1984. middle--;
  1985. }
  1986. } else {
  1987. if (keys[middle].time < p_time) {
  1988. middle++;
  1989. }
  1990. }
  1991. if (p_limit) {
  1992. double diff = length - keys[middle].time;
  1993. if ((signbit(keys[middle].time) && !Math::is_zero_approx(keys[middle].time)) || (signbit(diff) && !Math::is_zero_approx(diff))) {
  1994. ERR_PRINT_ONCE_ED("Found the key outside the animation range. Consider using the clean-up option in AnimationTrackEditor to fix it.");
  1995. return -1;
  1996. }
  1997. }
  1998. return middle;
  1999. }
  2000. // Linear interpolation for anytype.
  2001. Vector3 Animation::_interpolate(const Vector3 &p_a, const Vector3 &p_b, real_t p_c) const {
  2002. return p_a.lerp(p_b, p_c);
  2003. }
  2004. Quaternion Animation::_interpolate(const Quaternion &p_a, const Quaternion &p_b, real_t p_c) const {
  2005. return p_a.slerp(p_b, p_c);
  2006. }
  2007. Variant Animation::_interpolate(const Variant &p_a, const Variant &p_b, real_t p_c) const {
  2008. return interpolate_variant(p_a, p_b, p_c);
  2009. }
  2010. real_t Animation::_interpolate(const real_t &p_a, const real_t &p_b, real_t p_c) const {
  2011. return Math::lerp(p_a, p_b, p_c);
  2012. }
  2013. Variant Animation::_interpolate_angle(const Variant &p_a, const Variant &p_b, real_t p_c) const {
  2014. Variant::Type type_a = p_a.get_type();
  2015. Variant::Type type_b = p_b.get_type();
  2016. uint32_t vformat = 1 << type_a;
  2017. vformat |= 1 << type_b;
  2018. if (vformat == ((1 << Variant::INT) | (1 << Variant::FLOAT)) || vformat == (1 << Variant::FLOAT)) {
  2019. real_t a = p_a;
  2020. real_t b = p_b;
  2021. return Math::fposmod((float)Math::lerp_angle(a, b, p_c), (float)Math_TAU);
  2022. }
  2023. return _interpolate(p_a, p_b, p_c);
  2024. }
  2025. // Cubic interpolation for anytype.
  2026. Vector3 Animation::_cubic_interpolate_in_time(const Vector3 &p_pre_a, const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
  2027. return p_a.cubic_interpolate_in_time(p_b, p_pre_a, p_post_b, p_c, p_b_t, p_pre_a_t, p_post_b_t);
  2028. }
  2029. Quaternion Animation::_cubic_interpolate_in_time(const Quaternion &p_pre_a, const Quaternion &p_a, const Quaternion &p_b, const Quaternion &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
  2030. return p_a.spherical_cubic_interpolate_in_time(p_b, p_pre_a, p_post_b, p_c, p_b_t, p_pre_a_t, p_post_b_t);
  2031. }
  2032. Variant Animation::_cubic_interpolate_in_time(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
  2033. return cubic_interpolate_in_time_variant(p_pre_a, p_a, p_b, p_post_b, p_c, p_pre_a_t, p_b_t, p_post_b_t);
  2034. }
  2035. real_t Animation::_cubic_interpolate_in_time(const real_t &p_pre_a, const real_t &p_a, const real_t &p_b, const real_t &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
  2036. return Math::cubic_interpolate_in_time(p_a, p_b, p_pre_a, p_post_b, p_c, p_b_t, p_pre_a_t, p_post_b_t);
  2037. }
  2038. Variant Animation::_cubic_interpolate_angle_in_time(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
  2039. Variant::Type type_a = p_a.get_type();
  2040. Variant::Type type_b = p_b.get_type();
  2041. Variant::Type type_pa = p_pre_a.get_type();
  2042. Variant::Type type_pb = p_post_b.get_type();
  2043. uint32_t vformat = 1 << type_a;
  2044. vformat |= 1 << type_b;
  2045. vformat |= 1 << type_pa;
  2046. vformat |= 1 << type_pb;
  2047. if (vformat == ((1 << Variant::INT) | (1 << Variant::FLOAT)) || vformat == (1 << Variant::FLOAT)) {
  2048. real_t a = p_a;
  2049. real_t b = p_b;
  2050. real_t pa = p_pre_a;
  2051. real_t pb = p_post_b;
  2052. return Math::fposmod((float)Math::cubic_interpolate_angle_in_time(a, b, pa, pb, p_c, p_b_t, p_pre_a_t, p_post_b_t), (float)Math_TAU);
  2053. }
  2054. return _cubic_interpolate_in_time(p_pre_a, p_a, p_b, p_post_b, p_c, p_pre_a_t, p_b_t, p_post_b_t);
  2055. }
  2056. template <typename T>
  2057. T Animation::_interpolate(const Vector<TKey<T>> &p_keys, double p_time, InterpolationType p_interp, bool p_loop_wrap, bool *p_ok, bool p_backward) const {
  2058. int len = _find(p_keys, length) + 1; // try to find last key (there may be more past the end)
  2059. if (len <= 0) {
  2060. // (-1 or -2 returned originally) (plus one above)
  2061. // meaning no keys, or only key time is larger than length
  2062. if (p_ok) {
  2063. *p_ok = false;
  2064. }
  2065. return T();
  2066. } else if (len == 1) { // one key found (0+1), return it
  2067. if (p_ok) {
  2068. *p_ok = true;
  2069. }
  2070. return p_keys[0].value;
  2071. }
  2072. int idx = _find(p_keys, p_time, p_backward);
  2073. ERR_FAIL_COND_V(idx == -2, T());
  2074. int maxi = len - 1;
  2075. bool is_start_edge = p_backward ? idx >= len : idx == -1;
  2076. bool is_end_edge = p_backward ? idx == 0 : idx >= maxi;
  2077. real_t c = 0.0;
  2078. // Prepare for all cases of interpolation.
  2079. real_t delta = 0.0;
  2080. real_t from = 0.0;
  2081. int pre = -1;
  2082. int next = -1;
  2083. int post = -1;
  2084. real_t pre_t = 0.0;
  2085. real_t to_t = 0.0;
  2086. real_t post_t = 0.0;
  2087. bool use_cubic = p_interp == INTERPOLATION_CUBIC || p_interp == INTERPOLATION_CUBIC_ANGLE;
  2088. if (!p_loop_wrap || loop_mode == LOOP_NONE) {
  2089. if (is_start_edge) {
  2090. idx = p_backward ? maxi : 0;
  2091. }
  2092. next = CLAMP(idx + (p_backward ? -1 : 1), 0, maxi);
  2093. if (use_cubic) {
  2094. pre = CLAMP(idx + (p_backward ? 1 : -1), 0, maxi);
  2095. post = CLAMP(idx + (p_backward ? -2 : 2), 0, maxi);
  2096. }
  2097. } else if (loop_mode == LOOP_LINEAR) {
  2098. if (is_start_edge) {
  2099. idx = p_backward ? 0 : maxi;
  2100. }
  2101. next = Math::posmod(idx + (p_backward ? -1 : 1), len);
  2102. if (use_cubic) {
  2103. pre = Math::posmod(idx + (p_backward ? 1 : -1), len);
  2104. post = Math::posmod(idx + (p_backward ? -2 : 2), len);
  2105. }
  2106. if (is_start_edge) {
  2107. if (!p_backward) {
  2108. real_t endtime = (length - p_keys[idx].time);
  2109. if (endtime < 0) { // may be keys past the end
  2110. endtime = 0;
  2111. }
  2112. delta = endtime + p_keys[next].time;
  2113. from = endtime + p_time;
  2114. } else {
  2115. real_t endtime = p_keys[idx].time;
  2116. if (endtime > length) { // may be keys past the end
  2117. endtime = length;
  2118. }
  2119. delta = endtime + length - p_keys[next].time;
  2120. from = endtime + length - p_time;
  2121. }
  2122. } else if (is_end_edge) {
  2123. if (!p_backward) {
  2124. delta = (length - p_keys[idx].time) + p_keys[next].time;
  2125. from = p_time - p_keys[idx].time;
  2126. } else {
  2127. delta = p_keys[idx].time + (length - p_keys[next].time);
  2128. from = (length - p_time) - (length - p_keys[idx].time);
  2129. }
  2130. }
  2131. } else {
  2132. if (is_start_edge) {
  2133. idx = p_backward ? len : -1;
  2134. }
  2135. next = (int)Math::round(Math::pingpong((float)(idx + (p_backward ? -1 : 1)) + 0.5f, (float)len) - 0.5f);
  2136. if (use_cubic) {
  2137. pre = (int)Math::round(Math::pingpong((float)(idx + (p_backward ? 1 : -1)) + 0.5f, (float)len) - 0.5f);
  2138. post = (int)Math::round(Math::pingpong((float)(idx + (p_backward ? -2 : 2)) + 0.5f, (float)len) - 0.5f);
  2139. }
  2140. idx = (int)Math::round(Math::pingpong((float)idx + 0.5f, (float)len) - 0.5f);
  2141. if (is_start_edge) {
  2142. if (!p_backward) {
  2143. real_t endtime = p_keys[idx].time;
  2144. if (endtime < 0) { // may be keys past the end
  2145. endtime = 0;
  2146. }
  2147. delta = endtime + p_keys[next].time;
  2148. from = endtime + p_time;
  2149. } else {
  2150. real_t endtime = length - p_keys[idx].time;
  2151. if (endtime > length) { // may be keys past the end
  2152. endtime = length;
  2153. }
  2154. delta = endtime + length - p_keys[next].time;
  2155. from = endtime + length - p_time;
  2156. }
  2157. } else if (is_end_edge) {
  2158. if (!p_backward) {
  2159. delta = length * 2.0 - p_keys[idx].time - p_keys[next].time;
  2160. from = p_time - p_keys[idx].time;
  2161. } else {
  2162. delta = p_keys[idx].time + p_keys[next].time;
  2163. from = (length - p_time) - (length - p_keys[idx].time);
  2164. }
  2165. }
  2166. }
  2167. if (!is_start_edge && !is_end_edge) {
  2168. if (!p_backward) {
  2169. delta = p_keys[next].time - p_keys[idx].time;
  2170. from = p_time - p_keys[idx].time;
  2171. } else {
  2172. delta = (length - p_keys[next].time) - (length - p_keys[idx].time);
  2173. from = (length - p_time) - (length - p_keys[idx].time);
  2174. }
  2175. }
  2176. if (Math::is_zero_approx(delta)) {
  2177. c = 0;
  2178. } else {
  2179. c = from / delta;
  2180. }
  2181. if (p_ok) {
  2182. *p_ok = true;
  2183. }
  2184. real_t tr = p_keys[idx].transition;
  2185. if (tr == 0) {
  2186. // Don't interpolate if not needed.
  2187. return p_keys[idx].value;
  2188. }
  2189. if (tr != 1.0) {
  2190. c = Math::ease(c, tr);
  2191. }
  2192. switch (p_interp) {
  2193. case INTERPOLATION_NEAREST: {
  2194. return p_keys[idx].value;
  2195. } break;
  2196. case INTERPOLATION_LINEAR: {
  2197. return _interpolate(p_keys[idx].value, p_keys[next].value, c);
  2198. } break;
  2199. case INTERPOLATION_LINEAR_ANGLE: {
  2200. return _interpolate_angle(p_keys[idx].value, p_keys[next].value, c);
  2201. } break;
  2202. case INTERPOLATION_CUBIC:
  2203. case INTERPOLATION_CUBIC_ANGLE: {
  2204. if (!p_loop_wrap || loop_mode == LOOP_NONE) {
  2205. pre_t = p_keys[pre].time - p_keys[idx].time;
  2206. to_t = p_keys[next].time - p_keys[idx].time;
  2207. post_t = p_keys[post].time - p_keys[idx].time;
  2208. } else if (loop_mode == LOOP_LINEAR) {
  2209. pre_t = pre > idx ? -length + p_keys[pre].time - p_keys[idx].time : p_keys[pre].time - p_keys[idx].time;
  2210. to_t = next < idx ? length + p_keys[next].time - p_keys[idx].time : p_keys[next].time - p_keys[idx].time;
  2211. post_t = next < idx || post <= idx ? length + p_keys[post].time - p_keys[idx].time : p_keys[post].time - p_keys[idx].time;
  2212. } else {
  2213. pre_t = p_keys[pre].time - p_keys[idx].time;
  2214. to_t = p_keys[next].time - p_keys[idx].time;
  2215. post_t = p_keys[post].time - p_keys[idx].time;
  2216. if ((pre > idx && idx == next && post < next) || (pre < idx && idx == next && post > next)) {
  2217. pre_t = p_keys[idx].time - p_keys[pre].time;
  2218. } else if (pre == idx) {
  2219. pre_t = idx < next ? -p_keys[idx].time * 2.0 : (length - p_keys[idx].time) * 2.0;
  2220. }
  2221. if (idx == next) {
  2222. to_t = pre < idx ? (length - p_keys[idx].time) * 2.0 : -p_keys[idx].time * 2.0;
  2223. post_t = p_keys[next].time - p_keys[post].time + to_t;
  2224. } else if (next == post) {
  2225. post_t = idx < next ? (length - p_keys[next].time) * 2.0 + to_t : -p_keys[next].time * 2.0 + to_t;
  2226. }
  2227. }
  2228. if (p_interp == INTERPOLATION_CUBIC_ANGLE) {
  2229. return _cubic_interpolate_angle_in_time(
  2230. p_keys[pre].value, p_keys[idx].value, p_keys[next].value, p_keys[post].value, c,
  2231. pre_t, to_t, post_t);
  2232. }
  2233. return _cubic_interpolate_in_time(
  2234. p_keys[pre].value, p_keys[idx].value, p_keys[next].value, p_keys[post].value, c,
  2235. pre_t, to_t, post_t);
  2236. } break;
  2237. default:
  2238. return p_keys[idx].value;
  2239. }
  2240. // do a barrel roll
  2241. }
  2242. Variant Animation::value_track_interpolate(int p_track, double p_time, bool p_backward) const {
  2243. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  2244. Track *t = tracks[p_track];
  2245. ERR_FAIL_COND_V(t->type != TYPE_VALUE, Variant());
  2246. ValueTrack *vt = static_cast<ValueTrack *>(t);
  2247. bool ok = false;
  2248. Variant res = _interpolate(vt->values, p_time, vt->update_mode == UPDATE_DISCRETE ? INTERPOLATION_NEAREST : vt->interpolation, vt->loop_wrap, &ok, p_backward);
  2249. if (ok) {
  2250. return res;
  2251. }
  2252. return Variant();
  2253. }
  2254. void Animation::value_track_set_update_mode(int p_track, UpdateMode p_mode) {
  2255. ERR_FAIL_INDEX(p_track, tracks.size());
  2256. Track *t = tracks[p_track];
  2257. ERR_FAIL_COND(t->type != TYPE_VALUE);
  2258. ERR_FAIL_INDEX((int)p_mode, 3);
  2259. ValueTrack *vt = static_cast<ValueTrack *>(t);
  2260. vt->update_mode = p_mode;
  2261. _check_capture_included();
  2262. emit_changed();
  2263. }
  2264. Animation::UpdateMode Animation::value_track_get_update_mode(int p_track) const {
  2265. ERR_FAIL_INDEX_V(p_track, tracks.size(), UPDATE_CONTINUOUS);
  2266. Track *t = tracks[p_track];
  2267. ERR_FAIL_COND_V(t->type != TYPE_VALUE, UPDATE_CONTINUOUS);
  2268. ValueTrack *vt = static_cast<ValueTrack *>(t);
  2269. return vt->update_mode;
  2270. }
  2271. template <typename T>
  2272. void Animation::_track_get_key_indices_in_range(const Vector<T> &p_array, double from_time, double to_time, List<int> *p_indices, bool p_is_backward) const {
  2273. int len = p_array.size();
  2274. if (len == 0) {
  2275. return;
  2276. }
  2277. int from = 0;
  2278. int to = len - 1;
  2279. if (!p_is_backward) {
  2280. while (p_array[from].time < from_time || Math::is_equal_approx(p_array[from].time, from_time)) {
  2281. from++;
  2282. if (to < from) {
  2283. return;
  2284. }
  2285. }
  2286. while (p_array[to].time > to_time && !Math::is_equal_approx(p_array[to].time, to_time)) {
  2287. to--;
  2288. if (to < from) {
  2289. return;
  2290. }
  2291. }
  2292. } else {
  2293. while (p_array[from].time < from_time && !Math::is_equal_approx(p_array[from].time, from_time)) {
  2294. from++;
  2295. if (to < from) {
  2296. return;
  2297. }
  2298. }
  2299. while (p_array[to].time > to_time || Math::is_equal_approx(p_array[to].time, to_time)) {
  2300. to--;
  2301. if (to < from) {
  2302. return;
  2303. }
  2304. }
  2305. }
  2306. if (from == to) {
  2307. p_indices->push_back(from);
  2308. return;
  2309. }
  2310. if (!p_is_backward) {
  2311. for (int i = from; i <= to; i++) {
  2312. p_indices->push_back(i);
  2313. }
  2314. } else {
  2315. for (int i = to; i >= from; i--) {
  2316. p_indices->push_back(i);
  2317. }
  2318. }
  2319. }
  2320. void Animation::track_get_key_indices_in_range(int p_track, double p_time, double p_delta, List<int> *p_indices, Animation::LoopedFlag p_looped_flag) const {
  2321. ERR_FAIL_INDEX(p_track, tracks.size());
  2322. if (p_delta == 0) {
  2323. return; // Prevent to get key continuously.
  2324. }
  2325. const Track *t = tracks[p_track];
  2326. double from_time = p_time - p_delta;
  2327. double to_time = p_time;
  2328. bool is_backward = false;
  2329. if (from_time > to_time) {
  2330. is_backward = true;
  2331. SWAP(from_time, to_time);
  2332. }
  2333. switch (loop_mode) {
  2334. case LOOP_NONE: {
  2335. if (from_time < 0) {
  2336. from_time = 0;
  2337. }
  2338. if (from_time > length) {
  2339. from_time = length;
  2340. }
  2341. if (to_time < 0) {
  2342. to_time = 0;
  2343. }
  2344. if (to_time > length) {
  2345. to_time = length;
  2346. }
  2347. } break;
  2348. case LOOP_LINEAR: {
  2349. if (from_time > length || from_time < 0) {
  2350. from_time = Math::fposmod(from_time, length);
  2351. }
  2352. if (to_time > length || to_time < 0) {
  2353. to_time = Math::fposmod(to_time, length);
  2354. }
  2355. if (from_time > to_time) {
  2356. // Handle loop by splitting.
  2357. double anim_end = length + CMP_EPSILON;
  2358. double anim_start = -CMP_EPSILON;
  2359. switch (t->type) {
  2360. case TYPE_POSITION_3D: {
  2361. const PositionTrack *tt = static_cast<const PositionTrack *>(t);
  2362. if (tt->compressed_track >= 0) {
  2363. _get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, length, p_indices);
  2364. _get_compressed_key_indices_in_range<3>(tt->compressed_track, 0, to_time, p_indices);
  2365. } else {
  2366. if (!is_backward) {
  2367. _track_get_key_indices_in_range(tt->positions, from_time, anim_end, p_indices, is_backward);
  2368. _track_get_key_indices_in_range(tt->positions, anim_start, to_time, p_indices, is_backward);
  2369. } else {
  2370. _track_get_key_indices_in_range(tt->positions, anim_start, to_time, p_indices, is_backward);
  2371. _track_get_key_indices_in_range(tt->positions, from_time, anim_end, p_indices, is_backward);
  2372. }
  2373. }
  2374. } break;
  2375. case TYPE_ROTATION_3D: {
  2376. const RotationTrack *rt = static_cast<const RotationTrack *>(t);
  2377. if (rt->compressed_track >= 0) {
  2378. _get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, length, p_indices);
  2379. _get_compressed_key_indices_in_range<3>(rt->compressed_track, 0, to_time, p_indices);
  2380. } else {
  2381. if (!is_backward) {
  2382. _track_get_key_indices_in_range(rt->rotations, from_time, anim_end, p_indices, is_backward);
  2383. _track_get_key_indices_in_range(rt->rotations, anim_start, to_time, p_indices, is_backward);
  2384. } else {
  2385. _track_get_key_indices_in_range(rt->rotations, anim_start, to_time, p_indices, is_backward);
  2386. _track_get_key_indices_in_range(rt->rotations, from_time, anim_end, p_indices, is_backward);
  2387. }
  2388. }
  2389. } break;
  2390. case TYPE_SCALE_3D: {
  2391. const ScaleTrack *st = static_cast<const ScaleTrack *>(t);
  2392. if (st->compressed_track >= 0) {
  2393. _get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, length, p_indices);
  2394. _get_compressed_key_indices_in_range<3>(st->compressed_track, 0, to_time, p_indices);
  2395. } else {
  2396. if (!is_backward) {
  2397. _track_get_key_indices_in_range(st->scales, from_time, anim_end, p_indices, is_backward);
  2398. _track_get_key_indices_in_range(st->scales, anim_start, to_time, p_indices, is_backward);
  2399. } else {
  2400. _track_get_key_indices_in_range(st->scales, anim_start, to_time, p_indices, is_backward);
  2401. _track_get_key_indices_in_range(st->scales, from_time, anim_end, p_indices, is_backward);
  2402. }
  2403. }
  2404. } break;
  2405. case TYPE_BLEND_SHAPE: {
  2406. const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t);
  2407. if (bst->compressed_track >= 0) {
  2408. _get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, length, p_indices);
  2409. _get_compressed_key_indices_in_range<1>(bst->compressed_track, 0, to_time, p_indices);
  2410. } else {
  2411. if (!is_backward) {
  2412. _track_get_key_indices_in_range(bst->blend_shapes, from_time, anim_end, p_indices, is_backward);
  2413. _track_get_key_indices_in_range(bst->blend_shapes, anim_start, to_time, p_indices, is_backward);
  2414. } else {
  2415. _track_get_key_indices_in_range(bst->blend_shapes, anim_start, to_time, p_indices, is_backward);
  2416. _track_get_key_indices_in_range(bst->blend_shapes, from_time, anim_end, p_indices, is_backward);
  2417. }
  2418. }
  2419. } break;
  2420. case TYPE_VALUE: {
  2421. const ValueTrack *vt = static_cast<const ValueTrack *>(t);
  2422. if (!is_backward) {
  2423. _track_get_key_indices_in_range(vt->values, from_time, anim_end, p_indices, is_backward);
  2424. _track_get_key_indices_in_range(vt->values, anim_start, to_time, p_indices, is_backward);
  2425. } else {
  2426. _track_get_key_indices_in_range(vt->values, anim_start, to_time, p_indices, is_backward);
  2427. _track_get_key_indices_in_range(vt->values, from_time, anim_end, p_indices, is_backward);
  2428. }
  2429. } break;
  2430. case TYPE_METHOD: {
  2431. const MethodTrack *mt = static_cast<const MethodTrack *>(t);
  2432. if (!is_backward) {
  2433. _track_get_key_indices_in_range(mt->methods, from_time, anim_end, p_indices, is_backward);
  2434. _track_get_key_indices_in_range(mt->methods, anim_start, to_time, p_indices, is_backward);
  2435. } else {
  2436. _track_get_key_indices_in_range(mt->methods, anim_start, to_time, p_indices, is_backward);
  2437. _track_get_key_indices_in_range(mt->methods, from_time, anim_end, p_indices, is_backward);
  2438. }
  2439. } break;
  2440. case TYPE_BEZIER: {
  2441. const BezierTrack *bz = static_cast<const BezierTrack *>(t);
  2442. if (!is_backward) {
  2443. _track_get_key_indices_in_range(bz->values, from_time, anim_end, p_indices, is_backward);
  2444. _track_get_key_indices_in_range(bz->values, anim_start, to_time, p_indices, is_backward);
  2445. } else {
  2446. _track_get_key_indices_in_range(bz->values, anim_start, to_time, p_indices, is_backward);
  2447. _track_get_key_indices_in_range(bz->values, from_time, anim_end, p_indices, is_backward);
  2448. }
  2449. } break;
  2450. case TYPE_AUDIO: {
  2451. const AudioTrack *ad = static_cast<const AudioTrack *>(t);
  2452. if (!is_backward) {
  2453. _track_get_key_indices_in_range(ad->values, from_time, anim_end, p_indices, is_backward);
  2454. _track_get_key_indices_in_range(ad->values, anim_start, to_time, p_indices, is_backward);
  2455. } else {
  2456. _track_get_key_indices_in_range(ad->values, anim_start, to_time, p_indices, is_backward);
  2457. _track_get_key_indices_in_range(ad->values, from_time, anim_end, p_indices, is_backward);
  2458. }
  2459. } break;
  2460. case TYPE_ANIMATION: {
  2461. const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
  2462. if (!is_backward) {
  2463. _track_get_key_indices_in_range(an->values, from_time, anim_end, p_indices, is_backward);
  2464. _track_get_key_indices_in_range(an->values, anim_start, to_time, p_indices, is_backward);
  2465. } else {
  2466. _track_get_key_indices_in_range(an->values, anim_start, to_time, p_indices, is_backward);
  2467. _track_get_key_indices_in_range(an->values, from_time, anim_end, p_indices, is_backward);
  2468. }
  2469. } break;
  2470. }
  2471. return;
  2472. }
  2473. // Not from_time > to_time but most recent of looping...
  2474. if (p_looped_flag != Animation::LOOPED_FLAG_NONE) {
  2475. if (!is_backward && Math::is_equal_approx(from_time, 0)) {
  2476. int edge = track_find_key(p_track, 0, FIND_MODE_EXACT);
  2477. if (edge >= 0) {
  2478. p_indices->push_back(edge);
  2479. }
  2480. } else if (is_backward && Math::is_equal_approx(to_time, length)) {
  2481. int edge = track_find_key(p_track, length, FIND_MODE_EXACT);
  2482. if (edge >= 0) {
  2483. p_indices->push_back(edge);
  2484. }
  2485. }
  2486. }
  2487. } break;
  2488. case LOOP_PINGPONG: {
  2489. if (from_time > length || from_time < 0) {
  2490. from_time = Math::pingpong(from_time, length);
  2491. }
  2492. if (to_time > length || to_time < 0) {
  2493. to_time = Math::pingpong(to_time, length);
  2494. }
  2495. if (p_looped_flag == Animation::LOOPED_FLAG_START) {
  2496. // Handle loop by splitting.
  2497. switch (t->type) {
  2498. case TYPE_POSITION_3D: {
  2499. const PositionTrack *tt = static_cast<const PositionTrack *>(t);
  2500. if (tt->compressed_track >= 0) {
  2501. _get_compressed_key_indices_in_range<3>(tt->compressed_track, 0, from_time, p_indices);
  2502. _get_compressed_key_indices_in_range<3>(tt->compressed_track, 0, to_time, p_indices);
  2503. } else {
  2504. _track_get_key_indices_in_range(tt->positions, 0, from_time, p_indices, true);
  2505. _track_get_key_indices_in_range(tt->positions, 0, to_time, p_indices, false);
  2506. }
  2507. } break;
  2508. case TYPE_ROTATION_3D: {
  2509. const RotationTrack *rt = static_cast<const RotationTrack *>(t);
  2510. if (rt->compressed_track >= 0) {
  2511. _get_compressed_key_indices_in_range<3>(rt->compressed_track, 0, from_time, p_indices);
  2512. _get_compressed_key_indices_in_range<3>(rt->compressed_track, 0, to_time, p_indices);
  2513. } else {
  2514. _track_get_key_indices_in_range(rt->rotations, 0, from_time, p_indices, true);
  2515. _track_get_key_indices_in_range(rt->rotations, 0, to_time, p_indices, false);
  2516. }
  2517. } break;
  2518. case TYPE_SCALE_3D: {
  2519. const ScaleTrack *st = static_cast<const ScaleTrack *>(t);
  2520. if (st->compressed_track >= 0) {
  2521. _get_compressed_key_indices_in_range<3>(st->compressed_track, 0, from_time, p_indices);
  2522. _get_compressed_key_indices_in_range<3>(st->compressed_track, 0, to_time, p_indices);
  2523. } else {
  2524. _track_get_key_indices_in_range(st->scales, 0, from_time, p_indices, true);
  2525. _track_get_key_indices_in_range(st->scales, 0, to_time, p_indices, false);
  2526. }
  2527. } break;
  2528. case TYPE_BLEND_SHAPE: {
  2529. const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t);
  2530. if (bst->compressed_track >= 0) {
  2531. _get_compressed_key_indices_in_range<1>(bst->compressed_track, 0, from_time, p_indices);
  2532. _get_compressed_key_indices_in_range<1>(bst->compressed_track, 0, to_time, p_indices);
  2533. } else {
  2534. _track_get_key_indices_in_range(bst->blend_shapes, 0, from_time, p_indices, true);
  2535. _track_get_key_indices_in_range(bst->blend_shapes, 0, to_time, p_indices, false);
  2536. }
  2537. } break;
  2538. case TYPE_VALUE: {
  2539. const ValueTrack *vt = static_cast<const ValueTrack *>(t);
  2540. _track_get_key_indices_in_range(vt->values, 0, from_time, p_indices, true);
  2541. _track_get_key_indices_in_range(vt->values, 0, to_time, p_indices, false);
  2542. } break;
  2543. case TYPE_METHOD: {
  2544. const MethodTrack *mt = static_cast<const MethodTrack *>(t);
  2545. _track_get_key_indices_in_range(mt->methods, 0, from_time, p_indices, true);
  2546. _track_get_key_indices_in_range(mt->methods, 0, to_time, p_indices, false);
  2547. } break;
  2548. case TYPE_BEZIER: {
  2549. const BezierTrack *bz = static_cast<const BezierTrack *>(t);
  2550. _track_get_key_indices_in_range(bz->values, 0, from_time, p_indices, true);
  2551. _track_get_key_indices_in_range(bz->values, 0, to_time, p_indices, false);
  2552. } break;
  2553. case TYPE_AUDIO: {
  2554. const AudioTrack *ad = static_cast<const AudioTrack *>(t);
  2555. _track_get_key_indices_in_range(ad->values, 0, from_time, p_indices, true);
  2556. _track_get_key_indices_in_range(ad->values, 0, to_time, p_indices, false);
  2557. } break;
  2558. case TYPE_ANIMATION: {
  2559. const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
  2560. _track_get_key_indices_in_range(an->values, 0, from_time, p_indices, true);
  2561. _track_get_key_indices_in_range(an->values, 0, to_time, p_indices, false);
  2562. } break;
  2563. }
  2564. return;
  2565. }
  2566. if (p_looped_flag == Animation::LOOPED_FLAG_END) {
  2567. // Handle loop by splitting.
  2568. switch (t->type) {
  2569. case TYPE_POSITION_3D: {
  2570. const PositionTrack *tt = static_cast<const PositionTrack *>(t);
  2571. if (tt->compressed_track >= 0) {
  2572. _get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, length, p_indices);
  2573. _get_compressed_key_indices_in_range<3>(tt->compressed_track, to_time, length, p_indices);
  2574. } else {
  2575. _track_get_key_indices_in_range(tt->positions, from_time, length, p_indices, false);
  2576. _track_get_key_indices_in_range(tt->positions, to_time, length, p_indices, true);
  2577. }
  2578. } break;
  2579. case TYPE_ROTATION_3D: {
  2580. const RotationTrack *rt = static_cast<const RotationTrack *>(t);
  2581. if (rt->compressed_track >= 0) {
  2582. _get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, length, p_indices);
  2583. _get_compressed_key_indices_in_range<3>(rt->compressed_track, to_time, length, p_indices);
  2584. } else {
  2585. _track_get_key_indices_in_range(rt->rotations, from_time, length, p_indices, false);
  2586. _track_get_key_indices_in_range(rt->rotations, to_time, length, p_indices, true);
  2587. }
  2588. } break;
  2589. case TYPE_SCALE_3D: {
  2590. const ScaleTrack *st = static_cast<const ScaleTrack *>(t);
  2591. if (st->compressed_track >= 0) {
  2592. _get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, length, p_indices);
  2593. _get_compressed_key_indices_in_range<3>(st->compressed_track, to_time, length, p_indices);
  2594. } else {
  2595. _track_get_key_indices_in_range(st->scales, from_time, length, p_indices, false);
  2596. _track_get_key_indices_in_range(st->scales, to_time, length, p_indices, true);
  2597. }
  2598. } break;
  2599. case TYPE_BLEND_SHAPE: {
  2600. const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t);
  2601. if (bst->compressed_track >= 0) {
  2602. _get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, length, p_indices);
  2603. _get_compressed_key_indices_in_range<1>(bst->compressed_track, to_time, length, p_indices);
  2604. } else {
  2605. _track_get_key_indices_in_range(bst->blend_shapes, from_time, length, p_indices, false);
  2606. _track_get_key_indices_in_range(bst->blend_shapes, to_time, length, p_indices, true);
  2607. }
  2608. } break;
  2609. case TYPE_VALUE: {
  2610. const ValueTrack *vt = static_cast<const ValueTrack *>(t);
  2611. _track_get_key_indices_in_range(vt->values, from_time, length, p_indices, false);
  2612. _track_get_key_indices_in_range(vt->values, to_time, length, p_indices, true);
  2613. } break;
  2614. case TYPE_METHOD: {
  2615. const MethodTrack *mt = static_cast<const MethodTrack *>(t);
  2616. _track_get_key_indices_in_range(mt->methods, from_time, length, p_indices, false);
  2617. _track_get_key_indices_in_range(mt->methods, to_time, length, p_indices, true);
  2618. } break;
  2619. case TYPE_BEZIER: {
  2620. const BezierTrack *bz = static_cast<const BezierTrack *>(t);
  2621. _track_get_key_indices_in_range(bz->values, from_time, length, p_indices, false);
  2622. _track_get_key_indices_in_range(bz->values, to_time, length, p_indices, true);
  2623. } break;
  2624. case TYPE_AUDIO: {
  2625. const AudioTrack *ad = static_cast<const AudioTrack *>(t);
  2626. _track_get_key_indices_in_range(ad->values, from_time, length, p_indices, false);
  2627. _track_get_key_indices_in_range(ad->values, to_time, length, p_indices, true);
  2628. } break;
  2629. case TYPE_ANIMATION: {
  2630. const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
  2631. _track_get_key_indices_in_range(an->values, from_time, length, p_indices, false);
  2632. _track_get_key_indices_in_range(an->values, to_time, length, p_indices, true);
  2633. } break;
  2634. }
  2635. return;
  2636. }
  2637. // The edge will be pingponged in the next frame and processed there, so let's ignore it now...
  2638. if (!is_backward && Math::is_equal_approx(to_time, length)) {
  2639. to_time -= CMP_EPSILON;
  2640. } else if (is_backward && Math::is_equal_approx(from_time, 0)) {
  2641. from_time += CMP_EPSILON;
  2642. }
  2643. } break;
  2644. }
  2645. switch (t->type) {
  2646. case TYPE_POSITION_3D: {
  2647. const PositionTrack *tt = static_cast<const PositionTrack *>(t);
  2648. if (tt->compressed_track >= 0) {
  2649. _get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, to_time - from_time, p_indices);
  2650. } else {
  2651. _track_get_key_indices_in_range(tt->positions, from_time, to_time, p_indices, is_backward);
  2652. }
  2653. } break;
  2654. case TYPE_ROTATION_3D: {
  2655. const RotationTrack *rt = static_cast<const RotationTrack *>(t);
  2656. if (rt->compressed_track >= 0) {
  2657. _get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, to_time - from_time, p_indices);
  2658. } else {
  2659. _track_get_key_indices_in_range(rt->rotations, from_time, to_time, p_indices, is_backward);
  2660. }
  2661. } break;
  2662. case TYPE_SCALE_3D: {
  2663. const ScaleTrack *st = static_cast<const ScaleTrack *>(t);
  2664. if (st->compressed_track >= 0) {
  2665. _get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, to_time - from_time, p_indices);
  2666. } else {
  2667. _track_get_key_indices_in_range(st->scales, from_time, to_time, p_indices, is_backward);
  2668. }
  2669. } break;
  2670. case TYPE_BLEND_SHAPE: {
  2671. const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t);
  2672. if (bst->compressed_track >= 0) {
  2673. _get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, to_time - from_time, p_indices);
  2674. } else {
  2675. _track_get_key_indices_in_range(bst->blend_shapes, from_time, to_time, p_indices, is_backward);
  2676. }
  2677. } break;
  2678. case TYPE_VALUE: {
  2679. const ValueTrack *vt = static_cast<const ValueTrack *>(t);
  2680. _track_get_key_indices_in_range(vt->values, from_time, to_time, p_indices, is_backward);
  2681. } break;
  2682. case TYPE_METHOD: {
  2683. const MethodTrack *mt = static_cast<const MethodTrack *>(t);
  2684. _track_get_key_indices_in_range(mt->methods, from_time, to_time, p_indices, is_backward);
  2685. } break;
  2686. case TYPE_BEZIER: {
  2687. const BezierTrack *bz = static_cast<const BezierTrack *>(t);
  2688. _track_get_key_indices_in_range(bz->values, from_time, to_time, p_indices, is_backward);
  2689. } break;
  2690. case TYPE_AUDIO: {
  2691. const AudioTrack *ad = static_cast<const AudioTrack *>(t);
  2692. _track_get_key_indices_in_range(ad->values, from_time, to_time, p_indices, is_backward);
  2693. } break;
  2694. case TYPE_ANIMATION: {
  2695. const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
  2696. _track_get_key_indices_in_range(an->values, from_time, to_time, p_indices, is_backward);
  2697. } break;
  2698. }
  2699. }
  2700. Vector<Variant> Animation::method_track_get_params(int p_track, int p_key_idx) const {
  2701. ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector<Variant>());
  2702. Track *t = tracks[p_track];
  2703. ERR_FAIL_COND_V(t->type != TYPE_METHOD, Vector<Variant>());
  2704. MethodTrack *pm = static_cast<MethodTrack *>(t);
  2705. ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), Vector<Variant>());
  2706. const MethodKey &mk = pm->methods[p_key_idx];
  2707. return mk.params;
  2708. }
  2709. StringName Animation::method_track_get_name(int p_track, int p_key_idx) const {
  2710. ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName());
  2711. Track *t = tracks[p_track];
  2712. ERR_FAIL_COND_V(t->type != TYPE_METHOD, StringName());
  2713. MethodTrack *pm = static_cast<MethodTrack *>(t);
  2714. ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), StringName());
  2715. return pm->methods[p_key_idx].method;
  2716. }
  2717. int Animation::bezier_track_insert_key(int p_track, double p_time, real_t p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle) {
  2718. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  2719. Track *t = tracks[p_track];
  2720. ERR_FAIL_COND_V(t->type != TYPE_BEZIER, -1);
  2721. BezierTrack *bt = static_cast<BezierTrack *>(t);
  2722. TKey<BezierKey> k;
  2723. k.time = p_time;
  2724. k.value.value = p_value;
  2725. k.value.in_handle = p_in_handle;
  2726. if (k.value.in_handle.x > 0) {
  2727. k.value.in_handle.x = 0;
  2728. }
  2729. k.value.out_handle = p_out_handle;
  2730. if (k.value.out_handle.x < 0) {
  2731. k.value.out_handle.x = 0;
  2732. }
  2733. int key = _insert(p_time, bt->values, k);
  2734. emit_changed();
  2735. return key;
  2736. }
  2737. void Animation::bezier_track_set_key_value(int p_track, int p_index, real_t p_value) {
  2738. ERR_FAIL_INDEX(p_track, tracks.size());
  2739. Track *t = tracks[p_track];
  2740. ERR_FAIL_COND(t->type != TYPE_BEZIER);
  2741. BezierTrack *bt = static_cast<BezierTrack *>(t);
  2742. ERR_FAIL_INDEX(p_index, bt->values.size());
  2743. bt->values.write[p_index].value.value = p_value;
  2744. emit_changed();
  2745. }
  2746. void Animation::bezier_track_set_key_in_handle(int p_track, int p_index, const Vector2 &p_handle, real_t p_balanced_value_time_ratio) {
  2747. ERR_FAIL_INDEX(p_track, tracks.size());
  2748. Track *t = tracks[p_track];
  2749. ERR_FAIL_COND(t->type != TYPE_BEZIER);
  2750. BezierTrack *bt = static_cast<BezierTrack *>(t);
  2751. ERR_FAIL_INDEX(p_index, bt->values.size());
  2752. Vector2 in_handle = p_handle;
  2753. if (in_handle.x > 0) {
  2754. in_handle.x = 0;
  2755. }
  2756. bt->values.write[p_index].value.in_handle = in_handle;
  2757. #ifdef TOOLS_ENABLED
  2758. if (bt->values[p_index].value.handle_mode == HANDLE_MODE_LINEAR) {
  2759. bt->values.write[p_index].value.in_handle = Vector2();
  2760. bt->values.write[p_index].value.out_handle = Vector2();
  2761. } else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_BALANCED) {
  2762. Transform2D xform;
  2763. xform.set_scale(Vector2(1.0, 1.0 / p_balanced_value_time_ratio));
  2764. Vector2 vec_out = xform.xform(bt->values[p_index].value.out_handle);
  2765. Vector2 vec_in = xform.xform(in_handle);
  2766. bt->values.write[p_index].value.out_handle = xform.affine_inverse().xform(-vec_in.normalized() * vec_out.length());
  2767. } else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_MIRRORED) {
  2768. bt->values.write[p_index].value.out_handle = -in_handle;
  2769. }
  2770. #endif // TOOLS_ENABLED
  2771. emit_changed();
  2772. }
  2773. void Animation::bezier_track_set_key_out_handle(int p_track, int p_index, const Vector2 &p_handle, real_t p_balanced_value_time_ratio) {
  2774. ERR_FAIL_INDEX(p_track, tracks.size());
  2775. Track *t = tracks[p_track];
  2776. ERR_FAIL_COND(t->type != TYPE_BEZIER);
  2777. BezierTrack *bt = static_cast<BezierTrack *>(t);
  2778. ERR_FAIL_INDEX(p_index, bt->values.size());
  2779. Vector2 out_handle = p_handle;
  2780. if (out_handle.x < 0) {
  2781. out_handle.x = 0;
  2782. }
  2783. bt->values.write[p_index].value.out_handle = out_handle;
  2784. #ifdef TOOLS_ENABLED
  2785. if (bt->values[p_index].value.handle_mode == HANDLE_MODE_LINEAR) {
  2786. bt->values.write[p_index].value.in_handle = Vector2();
  2787. bt->values.write[p_index].value.out_handle = Vector2();
  2788. } else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_BALANCED) {
  2789. Transform2D xform;
  2790. xform.set_scale(Vector2(1.0, 1.0 / p_balanced_value_time_ratio));
  2791. Vector2 vec_in = xform.xform(bt->values[p_index].value.in_handle);
  2792. Vector2 vec_out = xform.xform(out_handle);
  2793. bt->values.write[p_index].value.in_handle = xform.affine_inverse().xform(-vec_out.normalized() * vec_in.length());
  2794. } else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_MIRRORED) {
  2795. bt->values.write[p_index].value.in_handle = -out_handle;
  2796. }
  2797. #endif // TOOLS_ENABLED
  2798. emit_changed();
  2799. }
  2800. real_t Animation::bezier_track_get_key_value(int p_track, int p_index) const {
  2801. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  2802. Track *t = tracks[p_track];
  2803. ERR_FAIL_COND_V(t->type != TYPE_BEZIER, 0);
  2804. BezierTrack *bt = static_cast<BezierTrack *>(t);
  2805. ERR_FAIL_INDEX_V(p_index, bt->values.size(), 0);
  2806. return bt->values[p_index].value.value;
  2807. }
  2808. Vector2 Animation::bezier_track_get_key_in_handle(int p_track, int p_index) const {
  2809. ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector2());
  2810. Track *t = tracks[p_track];
  2811. ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2());
  2812. BezierTrack *bt = static_cast<BezierTrack *>(t);
  2813. ERR_FAIL_INDEX_V(p_index, bt->values.size(), Vector2());
  2814. return bt->values[p_index].value.in_handle;
  2815. }
  2816. Vector2 Animation::bezier_track_get_key_out_handle(int p_track, int p_index) const {
  2817. ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector2());
  2818. Track *t = tracks[p_track];
  2819. ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2());
  2820. BezierTrack *bt = static_cast<BezierTrack *>(t);
  2821. ERR_FAIL_INDEX_V(p_index, bt->values.size(), Vector2());
  2822. return bt->values[p_index].value.out_handle;
  2823. }
  2824. #ifdef TOOLS_ENABLED
  2825. void Animation::bezier_track_set_key_handle_mode(int p_track, int p_index, HandleMode p_mode, HandleSetMode p_set_mode) {
  2826. ERR_FAIL_INDEX(p_track, tracks.size());
  2827. Track *t = tracks[p_track];
  2828. ERR_FAIL_COND(t->type != TYPE_BEZIER);
  2829. BezierTrack *bt = static_cast<BezierTrack *>(t);
  2830. ERR_FAIL_INDEX(p_index, bt->values.size());
  2831. bt->values.write[p_index].value.handle_mode = p_mode;
  2832. switch (p_mode) {
  2833. case HANDLE_MODE_LINEAR: {
  2834. bt->values.write[p_index].value.in_handle = Vector2(0, 0);
  2835. bt->values.write[p_index].value.out_handle = Vector2(0, 0);
  2836. } break;
  2837. case HANDLE_MODE_BALANCED:
  2838. case HANDLE_MODE_MIRRORED: {
  2839. int prev_key = MAX(0, p_index - 1);
  2840. int next_key = MIN(bt->values.size() - 1, p_index + 1);
  2841. if (prev_key == next_key) {
  2842. break; // Exists only one key.
  2843. }
  2844. real_t in_handle_x = 0;
  2845. real_t in_handle_y = 0;
  2846. real_t out_handle_x = 0;
  2847. real_t out_handle_y = 0;
  2848. if (p_mode == HANDLE_MODE_BALANCED) {
  2849. // Note:
  2850. // If p_set_mode == HANDLE_SET_MODE_NONE, I don't know if it should change the Tangent implicitly.
  2851. // At the least, we need to avoid corrupting the handles when loading animation from the resource.
  2852. // However, changes made by the Inspector do not go through the BezierEditor,
  2853. // so if you change from Free to Balanced or Mirrored in Inspector, there is no guarantee that
  2854. // it is Balanced or Mirrored until there is a handle operation.
  2855. if (p_set_mode == HANDLE_SET_MODE_RESET) {
  2856. real_t handle_length = 1.0 / 3.0;
  2857. in_handle_x = (bt->values[prev_key].time - bt->values[p_index].time) * handle_length;
  2858. in_handle_y = 0;
  2859. out_handle_x = (bt->values[next_key].time - bt->values[p_index].time) * handle_length;
  2860. out_handle_y = 0;
  2861. bt->values.write[p_index].value.in_handle = Vector2(in_handle_x, in_handle_y);
  2862. bt->values.write[p_index].value.out_handle = Vector2(out_handle_x, out_handle_y);
  2863. } else if (p_set_mode == HANDLE_SET_MODE_AUTO) {
  2864. real_t handle_length = 1.0 / 6.0;
  2865. real_t tangent = (bt->values[next_key].value.value - bt->values[prev_key].value.value) / (bt->values[next_key].time - bt->values[prev_key].time);
  2866. in_handle_x = (bt->values[prev_key].time - bt->values[p_index].time) * handle_length;
  2867. in_handle_y = in_handle_x * tangent;
  2868. out_handle_x = (bt->values[next_key].time - bt->values[p_index].time) * handle_length;
  2869. out_handle_y = out_handle_x * tangent;
  2870. bt->values.write[p_index].value.in_handle = Vector2(in_handle_x, in_handle_y);
  2871. bt->values.write[p_index].value.out_handle = Vector2(out_handle_x, out_handle_y);
  2872. }
  2873. } else {
  2874. real_t handle_length = 1.0 / 4.0;
  2875. real_t prev_interval = Math::abs(bt->values[p_index].time - bt->values[prev_key].time);
  2876. real_t next_interval = Math::abs(bt->values[p_index].time - bt->values[next_key].time);
  2877. real_t min_time = 0;
  2878. if (Math::is_zero_approx(prev_interval)) {
  2879. min_time = next_interval;
  2880. } else if (Math::is_zero_approx(next_interval)) {
  2881. min_time = prev_interval;
  2882. } else {
  2883. min_time = MIN(prev_interval, next_interval);
  2884. }
  2885. if (p_set_mode == HANDLE_SET_MODE_RESET) {
  2886. in_handle_x = -min_time * handle_length;
  2887. in_handle_y = 0;
  2888. out_handle_x = min_time * handle_length;
  2889. out_handle_y = 0;
  2890. bt->values.write[p_index].value.in_handle = Vector2(in_handle_x, in_handle_y);
  2891. bt->values.write[p_index].value.out_handle = Vector2(out_handle_x, out_handle_y);
  2892. } else if (p_set_mode == HANDLE_SET_MODE_AUTO) {
  2893. real_t tangent = (bt->values[next_key].value.value - bt->values[prev_key].value.value) / min_time;
  2894. in_handle_x = -min_time * handle_length;
  2895. in_handle_y = in_handle_x * tangent;
  2896. out_handle_x = min_time * handle_length;
  2897. out_handle_y = out_handle_x * tangent;
  2898. bt->values.write[p_index].value.in_handle = Vector2(in_handle_x, in_handle_y);
  2899. bt->values.write[p_index].value.out_handle = Vector2(out_handle_x, out_handle_y);
  2900. }
  2901. }
  2902. } break;
  2903. default: {
  2904. } break;
  2905. }
  2906. emit_changed();
  2907. }
  2908. Animation::HandleMode Animation::bezier_track_get_key_handle_mode(int p_track, int p_index) const {
  2909. ERR_FAIL_INDEX_V(p_track, tracks.size(), HANDLE_MODE_FREE);
  2910. Track *t = tracks[p_track];
  2911. ERR_FAIL_COND_V(t->type != TYPE_BEZIER, HANDLE_MODE_FREE);
  2912. BezierTrack *bt = static_cast<BezierTrack *>(t);
  2913. ERR_FAIL_INDEX_V(p_index, bt->values.size(), HANDLE_MODE_FREE);
  2914. return bt->values[p_index].value.handle_mode;
  2915. }
  2916. #endif // TOOLS_ENABLED
  2917. real_t Animation::bezier_track_interpolate(int p_track, double p_time) const {
  2918. //this uses a different interpolation scheme
  2919. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  2920. Track *track = tracks[p_track];
  2921. ERR_FAIL_COND_V(track->type != TYPE_BEZIER, 0);
  2922. BezierTrack *bt = static_cast<BezierTrack *>(track);
  2923. int len = _find(bt->values, length) + 1; // try to find last key (there may be more past the end)
  2924. if (len <= 0) {
  2925. // (-1 or -2 returned originally) (plus one above)
  2926. return 0;
  2927. } else if (len == 1) { // one key found (0+1), return it
  2928. return bt->values[0].value.value;
  2929. }
  2930. int idx = _find(bt->values, p_time);
  2931. ERR_FAIL_COND_V(idx == -2, 0);
  2932. //there really is no looping interpolation on bezier
  2933. if (idx < 0) {
  2934. return bt->values[0].value.value;
  2935. }
  2936. if (idx >= bt->values.size() - 1) {
  2937. return bt->values[bt->values.size() - 1].value.value;
  2938. }
  2939. double t = p_time - bt->values[idx].time;
  2940. int iterations = 10;
  2941. real_t duration = bt->values[idx + 1].time - bt->values[idx].time; // time duration between our two keyframes
  2942. real_t low = 0.0; // 0% of the current animation segment
  2943. real_t high = 1.0; // 100% of the current animation segment
  2944. Vector2 start(0, bt->values[idx].value.value);
  2945. Vector2 start_out = start + bt->values[idx].value.out_handle;
  2946. Vector2 end(duration, bt->values[idx + 1].value.value);
  2947. Vector2 end_in = end + bt->values[idx + 1].value.in_handle;
  2948. //narrow high and low as much as possible
  2949. for (int i = 0; i < iterations; i++) {
  2950. real_t middle = (low + high) / 2;
  2951. Vector2 interp = start.bezier_interpolate(start_out, end_in, end, middle);
  2952. if (interp.x < t) {
  2953. low = middle;
  2954. } else {
  2955. high = middle;
  2956. }
  2957. }
  2958. //interpolate the result:
  2959. Vector2 low_pos = start.bezier_interpolate(start_out, end_in, end, low);
  2960. Vector2 high_pos = start.bezier_interpolate(start_out, end_in, end, high);
  2961. real_t c = (t - low_pos.x) / (high_pos.x - low_pos.x);
  2962. return low_pos.lerp(high_pos, c).y;
  2963. }
  2964. int Animation::audio_track_insert_key(int p_track, double p_time, const Ref<Resource> &p_stream, real_t p_start_offset, real_t p_end_offset) {
  2965. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  2966. Track *t = tracks[p_track];
  2967. ERR_FAIL_COND_V(t->type != TYPE_AUDIO, -1);
  2968. AudioTrack *at = static_cast<AudioTrack *>(t);
  2969. TKey<AudioKey> k;
  2970. k.time = p_time;
  2971. k.value.stream = p_stream;
  2972. k.value.start_offset = p_start_offset;
  2973. if (k.value.start_offset < 0) {
  2974. k.value.start_offset = 0;
  2975. }
  2976. k.value.end_offset = p_end_offset;
  2977. if (k.value.end_offset < 0) {
  2978. k.value.end_offset = 0;
  2979. }
  2980. int key = _insert(p_time, at->values, k);
  2981. emit_changed();
  2982. return key;
  2983. }
  2984. void Animation::audio_track_set_key_stream(int p_track, int p_key, const Ref<Resource> &p_stream) {
  2985. ERR_FAIL_INDEX(p_track, tracks.size());
  2986. Track *t = tracks[p_track];
  2987. ERR_FAIL_COND(t->type != TYPE_AUDIO);
  2988. AudioTrack *at = static_cast<AudioTrack *>(t);
  2989. ERR_FAIL_INDEX(p_key, at->values.size());
  2990. at->values.write[p_key].value.stream = p_stream;
  2991. emit_changed();
  2992. }
  2993. void Animation::audio_track_set_key_start_offset(int p_track, int p_key, real_t p_offset) {
  2994. ERR_FAIL_INDEX(p_track, tracks.size());
  2995. Track *t = tracks[p_track];
  2996. ERR_FAIL_COND(t->type != TYPE_AUDIO);
  2997. AudioTrack *at = static_cast<AudioTrack *>(t);
  2998. ERR_FAIL_INDEX(p_key, at->values.size());
  2999. if (p_offset < 0) {
  3000. p_offset = 0;
  3001. }
  3002. at->values.write[p_key].value.start_offset = p_offset;
  3003. emit_changed();
  3004. }
  3005. void Animation::audio_track_set_key_end_offset(int p_track, int p_key, real_t p_offset) {
  3006. ERR_FAIL_INDEX(p_track, tracks.size());
  3007. Track *t = tracks[p_track];
  3008. ERR_FAIL_COND(t->type != TYPE_AUDIO);
  3009. AudioTrack *at = static_cast<AudioTrack *>(t);
  3010. ERR_FAIL_INDEX(p_key, at->values.size());
  3011. if (p_offset < 0) {
  3012. p_offset = 0;
  3013. }
  3014. at->values.write[p_key].value.end_offset = p_offset;
  3015. emit_changed();
  3016. }
  3017. Ref<Resource> Animation::audio_track_get_key_stream(int p_track, int p_key) const {
  3018. ERR_FAIL_INDEX_V(p_track, tracks.size(), Ref<Resource>());
  3019. const Track *t = tracks[p_track];
  3020. ERR_FAIL_COND_V(t->type != TYPE_AUDIO, Ref<Resource>());
  3021. const AudioTrack *at = static_cast<const AudioTrack *>(t);
  3022. ERR_FAIL_INDEX_V(p_key, at->values.size(), Ref<Resource>());
  3023. return at->values[p_key].value.stream;
  3024. }
  3025. real_t Animation::audio_track_get_key_start_offset(int p_track, int p_key) const {
  3026. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  3027. const Track *t = tracks[p_track];
  3028. ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0);
  3029. const AudioTrack *at = static_cast<const AudioTrack *>(t);
  3030. ERR_FAIL_INDEX_V(p_key, at->values.size(), 0);
  3031. return at->values[p_key].value.start_offset;
  3032. }
  3033. real_t Animation::audio_track_get_key_end_offset(int p_track, int p_key) const {
  3034. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  3035. const Track *t = tracks[p_track];
  3036. ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0);
  3037. const AudioTrack *at = static_cast<const AudioTrack *>(t);
  3038. ERR_FAIL_INDEX_V(p_key, at->values.size(), 0);
  3039. return at->values[p_key].value.end_offset;
  3040. }
  3041. void Animation::audio_track_set_use_blend(int p_track, bool p_enable) {
  3042. ERR_FAIL_INDEX(p_track, tracks.size());
  3043. Track *t = tracks[p_track];
  3044. ERR_FAIL_COND(t->type != TYPE_AUDIO);
  3045. AudioTrack *at = static_cast<AudioTrack *>(t);
  3046. at->use_blend = p_enable;
  3047. emit_changed();
  3048. }
  3049. bool Animation::audio_track_is_use_blend(int p_track) const {
  3050. ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
  3051. Track *t = tracks[p_track];
  3052. ERR_FAIL_COND_V(t->type != TYPE_AUDIO, false);
  3053. AudioTrack *at = static_cast<AudioTrack *>(t);
  3054. return at->use_blend;
  3055. }
  3056. //
  3057. int Animation::animation_track_insert_key(int p_track, double p_time, const StringName &p_animation) {
  3058. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  3059. Track *t = tracks[p_track];
  3060. ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, -1);
  3061. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  3062. TKey<StringName> k;
  3063. k.time = p_time;
  3064. k.value = p_animation;
  3065. int key = _insert(p_time, at->values, k);
  3066. emit_changed();
  3067. return key;
  3068. }
  3069. void Animation::animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation) {
  3070. ERR_FAIL_INDEX(p_track, tracks.size());
  3071. Track *t = tracks[p_track];
  3072. ERR_FAIL_COND(t->type != TYPE_ANIMATION);
  3073. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  3074. ERR_FAIL_INDEX(p_key, at->values.size());
  3075. at->values.write[p_key].value = p_animation;
  3076. emit_changed();
  3077. }
  3078. StringName Animation::animation_track_get_key_animation(int p_track, int p_key) const {
  3079. ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName());
  3080. const Track *t = tracks[p_track];
  3081. ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, StringName());
  3082. const AnimationTrack *at = static_cast<const AnimationTrack *>(t);
  3083. ERR_FAIL_INDEX_V(p_key, at->values.size(), StringName());
  3084. return at->values[p_key].value;
  3085. }
  3086. void Animation::set_length(real_t p_length) {
  3087. if (p_length < ANIM_MIN_LENGTH) {
  3088. p_length = ANIM_MIN_LENGTH;
  3089. }
  3090. length = p_length;
  3091. emit_changed();
  3092. }
  3093. real_t Animation::get_length() const {
  3094. return length;
  3095. }
  3096. void Animation::set_loop_mode(Animation::LoopMode p_loop_mode) {
  3097. loop_mode = p_loop_mode;
  3098. emit_changed();
  3099. }
  3100. Animation::LoopMode Animation::get_loop_mode() const {
  3101. return loop_mode;
  3102. }
  3103. void Animation::track_set_imported(int p_track, bool p_imported) {
  3104. ERR_FAIL_INDEX(p_track, tracks.size());
  3105. tracks[p_track]->imported = p_imported;
  3106. }
  3107. bool Animation::track_is_imported(int p_track) const {
  3108. ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
  3109. return tracks[p_track]->imported;
  3110. }
  3111. void Animation::track_set_enabled(int p_track, bool p_enabled) {
  3112. ERR_FAIL_INDEX(p_track, tracks.size());
  3113. tracks[p_track]->enabled = p_enabled;
  3114. emit_changed();
  3115. }
  3116. bool Animation::track_is_enabled(int p_track) const {
  3117. ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
  3118. return tracks[p_track]->enabled;
  3119. }
  3120. void Animation::track_move_up(int p_track) {
  3121. if (p_track >= 0 && p_track < (tracks.size() - 1)) {
  3122. SWAP(tracks.write[p_track], tracks.write[p_track + 1]);
  3123. }
  3124. emit_changed();
  3125. }
  3126. void Animation::track_move_down(int p_track) {
  3127. if (p_track > 0 && p_track < tracks.size()) {
  3128. SWAP(tracks.write[p_track], tracks.write[p_track - 1]);
  3129. }
  3130. emit_changed();
  3131. }
  3132. void Animation::track_move_to(int p_track, int p_to_index) {
  3133. ERR_FAIL_INDEX(p_track, tracks.size());
  3134. ERR_FAIL_INDEX(p_to_index, tracks.size() + 1);
  3135. if (p_track == p_to_index || p_track == p_to_index - 1) {
  3136. return;
  3137. }
  3138. Track *track = tracks.get(p_track);
  3139. tracks.remove_at(p_track);
  3140. // Take into account that the position of the tracks that come after the one removed will change.
  3141. tracks.insert(p_to_index > p_track ? p_to_index - 1 : p_to_index, track);
  3142. emit_changed();
  3143. }
  3144. void Animation::track_swap(int p_track, int p_with_track) {
  3145. ERR_FAIL_INDEX(p_track, tracks.size());
  3146. ERR_FAIL_INDEX(p_with_track, tracks.size());
  3147. if (p_track == p_with_track) {
  3148. return;
  3149. }
  3150. SWAP(tracks.write[p_track], tracks.write[p_with_track]);
  3151. emit_changed();
  3152. }
  3153. void Animation::set_step(real_t p_step) {
  3154. step = p_step;
  3155. emit_changed();
  3156. }
  3157. real_t Animation::get_step() const {
  3158. return step;
  3159. }
  3160. void Animation::copy_track(int p_track, Ref<Animation> p_to_animation) {
  3161. ERR_FAIL_COND(p_to_animation.is_null());
  3162. ERR_FAIL_INDEX(p_track, get_track_count());
  3163. int dst_track = p_to_animation->get_track_count();
  3164. p_to_animation->add_track(track_get_type(p_track));
  3165. p_to_animation->track_set_path(dst_track, track_get_path(p_track));
  3166. p_to_animation->track_set_imported(dst_track, track_is_imported(p_track));
  3167. p_to_animation->track_set_enabled(dst_track, track_is_enabled(p_track));
  3168. p_to_animation->track_set_interpolation_type(dst_track, track_get_interpolation_type(p_track));
  3169. p_to_animation->track_set_interpolation_loop_wrap(dst_track, track_get_interpolation_loop_wrap(p_track));
  3170. if (track_get_type(p_track) == TYPE_VALUE) {
  3171. p_to_animation->value_track_set_update_mode(dst_track, value_track_get_update_mode(p_track));
  3172. }
  3173. for (int i = 0; i < track_get_key_count(p_track); i++) {
  3174. p_to_animation->track_insert_key(dst_track, track_get_key_time(p_track, i), track_get_key_value(p_track, i), track_get_key_transition(p_track, i));
  3175. }
  3176. }
  3177. void Animation::_bind_methods() {
  3178. ClassDB::bind_method(D_METHOD("add_track", "type", "at_position"), &Animation::add_track, DEFVAL(-1));
  3179. ClassDB::bind_method(D_METHOD("remove_track", "track_idx"), &Animation::remove_track);
  3180. ClassDB::bind_method(D_METHOD("get_track_count"), &Animation::get_track_count);
  3181. ClassDB::bind_method(D_METHOD("track_get_type", "track_idx"), &Animation::track_get_type);
  3182. ClassDB::bind_method(D_METHOD("track_get_path", "track_idx"), &Animation::track_get_path);
  3183. ClassDB::bind_method(D_METHOD("track_set_path", "track_idx", "path"), &Animation::track_set_path);
  3184. ClassDB::bind_method(D_METHOD("find_track", "path", "type"), &Animation::find_track);
  3185. ClassDB::bind_method(D_METHOD("track_move_up", "track_idx"), &Animation::track_move_up);
  3186. ClassDB::bind_method(D_METHOD("track_move_down", "track_idx"), &Animation::track_move_down);
  3187. ClassDB::bind_method(D_METHOD("track_move_to", "track_idx", "to_idx"), &Animation::track_move_to);
  3188. ClassDB::bind_method(D_METHOD("track_swap", "track_idx", "with_idx"), &Animation::track_swap);
  3189. ClassDB::bind_method(D_METHOD("track_set_imported", "track_idx", "imported"), &Animation::track_set_imported);
  3190. ClassDB::bind_method(D_METHOD("track_is_imported", "track_idx"), &Animation::track_is_imported);
  3191. ClassDB::bind_method(D_METHOD("track_set_enabled", "track_idx", "enabled"), &Animation::track_set_enabled);
  3192. ClassDB::bind_method(D_METHOD("track_is_enabled", "track_idx"), &Animation::track_is_enabled);
  3193. ClassDB::bind_method(D_METHOD("position_track_insert_key", "track_idx", "time", "position"), &Animation::position_track_insert_key);
  3194. ClassDB::bind_method(D_METHOD("rotation_track_insert_key", "track_idx", "time", "rotation"), &Animation::rotation_track_insert_key);
  3195. ClassDB::bind_method(D_METHOD("scale_track_insert_key", "track_idx", "time", "scale"), &Animation::scale_track_insert_key);
  3196. ClassDB::bind_method(D_METHOD("blend_shape_track_insert_key", "track_idx", "time", "amount"), &Animation::blend_shape_track_insert_key);
  3197. ClassDB::bind_method(D_METHOD("position_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::position_track_interpolate, DEFVAL(false));
  3198. ClassDB::bind_method(D_METHOD("rotation_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::rotation_track_interpolate, DEFVAL(false));
  3199. ClassDB::bind_method(D_METHOD("scale_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::scale_track_interpolate, DEFVAL(false));
  3200. ClassDB::bind_method(D_METHOD("blend_shape_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::blend_shape_track_interpolate, DEFVAL(false));
  3201. ClassDB::bind_method(D_METHOD("track_insert_key", "track_idx", "time", "key", "transition"), &Animation::track_insert_key, DEFVAL(1));
  3202. ClassDB::bind_method(D_METHOD("track_remove_key", "track_idx", "key_idx"), &Animation::track_remove_key);
  3203. ClassDB::bind_method(D_METHOD("track_remove_key_at_time", "track_idx", "time"), &Animation::track_remove_key_at_time);
  3204. ClassDB::bind_method(D_METHOD("track_set_key_value", "track_idx", "key", "value"), &Animation::track_set_key_value);
  3205. ClassDB::bind_method(D_METHOD("track_set_key_transition", "track_idx", "key_idx", "transition"), &Animation::track_set_key_transition);
  3206. ClassDB::bind_method(D_METHOD("track_set_key_time", "track_idx", "key_idx", "time"), &Animation::track_set_key_time);
  3207. ClassDB::bind_method(D_METHOD("track_get_key_transition", "track_idx", "key_idx"), &Animation::track_get_key_transition);
  3208. ClassDB::bind_method(D_METHOD("track_get_key_count", "track_idx"), &Animation::track_get_key_count);
  3209. ClassDB::bind_method(D_METHOD("track_get_key_value", "track_idx", "key_idx"), &Animation::track_get_key_value);
  3210. ClassDB::bind_method(D_METHOD("track_get_key_time", "track_idx", "key_idx"), &Animation::track_get_key_time);
  3211. ClassDB::bind_method(D_METHOD("track_find_key", "track_idx", "time", "find_mode", "limit", "backward"), &Animation::track_find_key, DEFVAL(FIND_MODE_NEAREST), DEFVAL(false), DEFVAL(false));
  3212. ClassDB::bind_method(D_METHOD("track_set_interpolation_type", "track_idx", "interpolation"), &Animation::track_set_interpolation_type);
  3213. ClassDB::bind_method(D_METHOD("track_get_interpolation_type", "track_idx"), &Animation::track_get_interpolation_type);
  3214. ClassDB::bind_method(D_METHOD("track_set_interpolation_loop_wrap", "track_idx", "interpolation"), &Animation::track_set_interpolation_loop_wrap);
  3215. ClassDB::bind_method(D_METHOD("track_get_interpolation_loop_wrap", "track_idx"), &Animation::track_get_interpolation_loop_wrap);
  3216. ClassDB::bind_method(D_METHOD("track_is_compressed", "track_idx"), &Animation::track_is_compressed);
  3217. ClassDB::bind_method(D_METHOD("value_track_set_update_mode", "track_idx", "mode"), &Animation::value_track_set_update_mode);
  3218. ClassDB::bind_method(D_METHOD("value_track_get_update_mode", "track_idx"), &Animation::value_track_get_update_mode);
  3219. ClassDB::bind_method(D_METHOD("value_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::value_track_interpolate, DEFVAL(false));
  3220. ClassDB::bind_method(D_METHOD("method_track_get_name", "track_idx", "key_idx"), &Animation::method_track_get_name);
  3221. ClassDB::bind_method(D_METHOD("method_track_get_params", "track_idx", "key_idx"), &Animation::method_track_get_params);
  3222. ClassDB::bind_method(D_METHOD("bezier_track_insert_key", "track_idx", "time", "value", "in_handle", "out_handle"), &Animation::bezier_track_insert_key, DEFVAL(Vector2()), DEFVAL(Vector2()));
  3223. ClassDB::bind_method(D_METHOD("bezier_track_set_key_value", "track_idx", "key_idx", "value"), &Animation::bezier_track_set_key_value);
  3224. ClassDB::bind_method(D_METHOD("bezier_track_set_key_in_handle", "track_idx", "key_idx", "in_handle", "balanced_value_time_ratio"), &Animation::bezier_track_set_key_in_handle, DEFVAL(1.0));
  3225. ClassDB::bind_method(D_METHOD("bezier_track_set_key_out_handle", "track_idx", "key_idx", "out_handle", "balanced_value_time_ratio"), &Animation::bezier_track_set_key_out_handle, DEFVAL(1.0));
  3226. ClassDB::bind_method(D_METHOD("bezier_track_get_key_value", "track_idx", "key_idx"), &Animation::bezier_track_get_key_value);
  3227. ClassDB::bind_method(D_METHOD("bezier_track_get_key_in_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_in_handle);
  3228. ClassDB::bind_method(D_METHOD("bezier_track_get_key_out_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_out_handle);
  3229. ClassDB::bind_method(D_METHOD("bezier_track_interpolate", "track_idx", "time"), &Animation::bezier_track_interpolate);
  3230. ClassDB::bind_method(D_METHOD("audio_track_insert_key", "track_idx", "time", "stream", "start_offset", "end_offset"), &Animation::audio_track_insert_key, DEFVAL(0), DEFVAL(0));
  3231. ClassDB::bind_method(D_METHOD("audio_track_set_key_stream", "track_idx", "key_idx", "stream"), &Animation::audio_track_set_key_stream);
  3232. ClassDB::bind_method(D_METHOD("audio_track_set_key_start_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_start_offset);
  3233. ClassDB::bind_method(D_METHOD("audio_track_set_key_end_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_end_offset);
  3234. ClassDB::bind_method(D_METHOD("audio_track_get_key_stream", "track_idx", "key_idx"), &Animation::audio_track_get_key_stream);
  3235. ClassDB::bind_method(D_METHOD("audio_track_get_key_start_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_start_offset);
  3236. ClassDB::bind_method(D_METHOD("audio_track_get_key_end_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_end_offset);
  3237. ClassDB::bind_method(D_METHOD("audio_track_set_use_blend", "track_idx", "enable"), &Animation::audio_track_set_use_blend);
  3238. ClassDB::bind_method(D_METHOD("audio_track_is_use_blend", "track_idx"), &Animation::audio_track_is_use_blend);
  3239. ClassDB::bind_method(D_METHOD("animation_track_insert_key", "track_idx", "time", "animation"), &Animation::animation_track_insert_key);
  3240. ClassDB::bind_method(D_METHOD("animation_track_set_key_animation", "track_idx", "key_idx", "animation"), &Animation::animation_track_set_key_animation);
  3241. ClassDB::bind_method(D_METHOD("animation_track_get_key_animation", "track_idx", "key_idx"), &Animation::animation_track_get_key_animation);
  3242. ClassDB::bind_method(D_METHOD("set_length", "time_sec"), &Animation::set_length);
  3243. ClassDB::bind_method(D_METHOD("get_length"), &Animation::get_length);
  3244. ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &Animation::set_loop_mode);
  3245. ClassDB::bind_method(D_METHOD("get_loop_mode"), &Animation::get_loop_mode);
  3246. ClassDB::bind_method(D_METHOD("set_step", "size_sec"), &Animation::set_step);
  3247. ClassDB::bind_method(D_METHOD("get_step"), &Animation::get_step);
  3248. ClassDB::bind_method(D_METHOD("clear"), &Animation::clear);
  3249. ClassDB::bind_method(D_METHOD("copy_track", "track_idx", "to_animation"), &Animation::copy_track);
  3250. ClassDB::bind_method(D_METHOD("compress", "page_size", "fps", "split_tolerance"), &Animation::compress, DEFVAL(8192), DEFVAL(120), DEFVAL(4.0));
  3251. ClassDB::bind_method(D_METHOD("_set_capture_included", "capture_included"), &Animation::set_capture_included);
  3252. ClassDB::bind_method(D_METHOD("is_capture_included"), &Animation::is_capture_included);
  3253. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "0.001,99999,0.001,suffix:s"), "set_length", "get_length");
  3254. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Ping-Pong"), "set_loop_mode", "get_loop_mode");
  3255. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step", PROPERTY_HINT_RANGE, "0,4096,0.001,suffix:s"), "set_step", "get_step");
  3256. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "capture_included", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY | PROPERTY_USAGE_NO_EDITOR), "_set_capture_included", "is_capture_included");
  3257. BIND_ENUM_CONSTANT(TYPE_VALUE);
  3258. BIND_ENUM_CONSTANT(TYPE_POSITION_3D);
  3259. BIND_ENUM_CONSTANT(TYPE_ROTATION_3D);
  3260. BIND_ENUM_CONSTANT(TYPE_SCALE_3D);
  3261. BIND_ENUM_CONSTANT(TYPE_BLEND_SHAPE);
  3262. BIND_ENUM_CONSTANT(TYPE_METHOD);
  3263. BIND_ENUM_CONSTANT(TYPE_BEZIER);
  3264. BIND_ENUM_CONSTANT(TYPE_AUDIO);
  3265. BIND_ENUM_CONSTANT(TYPE_ANIMATION);
  3266. BIND_ENUM_CONSTANT(INTERPOLATION_NEAREST);
  3267. BIND_ENUM_CONSTANT(INTERPOLATION_LINEAR);
  3268. BIND_ENUM_CONSTANT(INTERPOLATION_CUBIC);
  3269. BIND_ENUM_CONSTANT(INTERPOLATION_LINEAR_ANGLE);
  3270. BIND_ENUM_CONSTANT(INTERPOLATION_CUBIC_ANGLE);
  3271. BIND_ENUM_CONSTANT(UPDATE_CONTINUOUS);
  3272. BIND_ENUM_CONSTANT(UPDATE_DISCRETE);
  3273. BIND_ENUM_CONSTANT(UPDATE_CAPTURE);
  3274. BIND_ENUM_CONSTANT(LOOP_NONE);
  3275. BIND_ENUM_CONSTANT(LOOP_LINEAR);
  3276. BIND_ENUM_CONSTANT(LOOP_PINGPONG);
  3277. BIND_ENUM_CONSTANT(LOOPED_FLAG_NONE);
  3278. BIND_ENUM_CONSTANT(LOOPED_FLAG_END);
  3279. BIND_ENUM_CONSTANT(LOOPED_FLAG_START);
  3280. BIND_ENUM_CONSTANT(FIND_MODE_NEAREST);
  3281. BIND_ENUM_CONSTANT(FIND_MODE_APPROX);
  3282. BIND_ENUM_CONSTANT(FIND_MODE_EXACT);
  3283. }
  3284. void Animation::clear() {
  3285. for (int i = 0; i < tracks.size(); i++) {
  3286. memdelete(tracks[i]);
  3287. }
  3288. tracks.clear();
  3289. loop_mode = LOOP_NONE;
  3290. length = 1;
  3291. compression.enabled = false;
  3292. compression.bounds.clear();
  3293. compression.pages.clear();
  3294. compression.fps = 120;
  3295. emit_changed();
  3296. }
  3297. bool Animation::_float_track_optimize_key(const TKey<float> t0, const TKey<float> t1, const TKey<float> t2, real_t p_allowed_velocity_err, real_t p_allowed_precision_error) {
  3298. // Remove overlapping keys.
  3299. if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) {
  3300. return true;
  3301. }
  3302. if (abs(t0.value - t1.value) < p_allowed_precision_error && abs(t1.value - t2.value) < p_allowed_precision_error) {
  3303. return true;
  3304. }
  3305. // Calc velocities.
  3306. double v0 = (t1.value - t0.value) / (t1.time - t0.time);
  3307. double v1 = (t2.value - t1.value) / (t2.time - t1.time);
  3308. // Avoid zero div but check equality.
  3309. if (abs(v0 - v1) < p_allowed_precision_error) {
  3310. return true;
  3311. } else if (abs(v0) < p_allowed_precision_error || abs(v1) < p_allowed_precision_error) {
  3312. return false;
  3313. }
  3314. if (!signbit(v0 * v1)) {
  3315. v0 = abs(v0);
  3316. v1 = abs(v1);
  3317. double ratio = v0 < v1 ? v0 / v1 : v1 / v0;
  3318. if (ratio >= 1.0 - p_allowed_velocity_err) {
  3319. return true;
  3320. }
  3321. }
  3322. return false;
  3323. }
  3324. bool Animation::_vector2_track_optimize_key(const TKey<Vector2> t0, const TKey<Vector2> t1, const TKey<Vector2> t2, real_t p_allowed_velocity_err, real_t p_allowed_angular_error, real_t p_allowed_precision_error) {
  3325. // Remove overlapping keys.
  3326. if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) {
  3327. return true;
  3328. }
  3329. if ((t0.value - t1.value).length() < p_allowed_precision_error && (t1.value - t2.value).length() < p_allowed_precision_error) {
  3330. return true;
  3331. }
  3332. // Calc velocities.
  3333. Vector2 vc0 = (t1.value - t0.value) / (t1.time - t0.time);
  3334. Vector2 vc1 = (t2.value - t1.value) / (t2.time - t1.time);
  3335. double v0 = vc0.length();
  3336. double v1 = vc1.length();
  3337. // Avoid zero div but check equality.
  3338. if (abs(v0 - v1) < p_allowed_precision_error) {
  3339. return true;
  3340. } else if (abs(v0) < p_allowed_precision_error || abs(v1) < p_allowed_precision_error) {
  3341. return false;
  3342. }
  3343. // Check axis.
  3344. if (vc0.normalized().dot(vc1.normalized()) >= 1.0 - p_allowed_angular_error * 2.0) {
  3345. v0 = abs(v0);
  3346. v1 = abs(v1);
  3347. double ratio = v0 < v1 ? v0 / v1 : v1 / v0;
  3348. if (ratio >= 1.0 - p_allowed_velocity_err) {
  3349. return true;
  3350. }
  3351. }
  3352. return false;
  3353. }
  3354. bool Animation::_vector3_track_optimize_key(const TKey<Vector3> t0, const TKey<Vector3> t1, const TKey<Vector3> t2, real_t p_allowed_velocity_err, real_t p_allowed_angular_error, real_t p_allowed_precision_error) {
  3355. // Remove overlapping keys.
  3356. if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) {
  3357. return true;
  3358. }
  3359. if ((t0.value - t1.value).length() < p_allowed_precision_error && (t1.value - t2.value).length() < p_allowed_precision_error) {
  3360. return true;
  3361. }
  3362. // Calc velocities.
  3363. Vector3 vc0 = (t1.value - t0.value) / (t1.time - t0.time);
  3364. Vector3 vc1 = (t2.value - t1.value) / (t2.time - t1.time);
  3365. double v0 = vc0.length();
  3366. double v1 = vc1.length();
  3367. // Avoid zero div but check equality.
  3368. if (abs(v0 - v1) < p_allowed_precision_error) {
  3369. return true;
  3370. } else if (abs(v0) < p_allowed_precision_error || abs(v1) < p_allowed_precision_error) {
  3371. return false;
  3372. }
  3373. // Check axis.
  3374. if (vc0.normalized().dot(vc1.normalized()) >= 1.0 - p_allowed_angular_error * 2.0) {
  3375. v0 = abs(v0);
  3376. v1 = abs(v1);
  3377. double ratio = v0 < v1 ? v0 / v1 : v1 / v0;
  3378. if (ratio >= 1.0 - p_allowed_velocity_err) {
  3379. return true;
  3380. }
  3381. }
  3382. return false;
  3383. }
  3384. bool Animation::_quaternion_track_optimize_key(const TKey<Quaternion> t0, const TKey<Quaternion> t1, const TKey<Quaternion> t2, real_t p_allowed_velocity_err, real_t p_allowed_angular_error, real_t p_allowed_precision_error) {
  3385. // Remove overlapping keys.
  3386. if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) {
  3387. return true;
  3388. }
  3389. if ((t0.value - t1.value).length() < p_allowed_precision_error && (t1.value - t2.value).length() < p_allowed_precision_error) {
  3390. return true;
  3391. }
  3392. // Check axis.
  3393. Quaternion q0 = t0.value * t1.value * t0.value.inverse();
  3394. Quaternion q1 = t1.value * t2.value * t1.value.inverse();
  3395. if (q0.get_axis().dot(q1.get_axis()) >= 1.0 - p_allowed_angular_error * 2.0) {
  3396. double a0 = Math::acos(t0.value.dot(t1.value));
  3397. double a1 = Math::acos(t1.value.dot(t2.value));
  3398. if (a0 + a1 >= Math_PI / 2) {
  3399. return false; // Rotation is more than 180 deg, keep key.
  3400. }
  3401. // Calc velocities.
  3402. double v0 = a0 / (t1.time - t0.time);
  3403. double v1 = a1 / (t2.time - t1.time);
  3404. // Avoid zero div but check equality.
  3405. if (abs(v0 - v1) < p_allowed_precision_error) {
  3406. return true;
  3407. } else if (abs(v0) < p_allowed_precision_error || abs(v1) < p_allowed_precision_error) {
  3408. return false;
  3409. }
  3410. double ratio = v0 < v1 ? v0 / v1 : v1 / v0;
  3411. if (ratio >= 1.0 - p_allowed_velocity_err) {
  3412. return true;
  3413. }
  3414. }
  3415. return false;
  3416. }
  3417. void Animation::_position_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error) {
  3418. ERR_FAIL_INDEX(p_idx, tracks.size());
  3419. ERR_FAIL_COND(tracks[p_idx]->type != TYPE_POSITION_3D);
  3420. PositionTrack *tt = static_cast<PositionTrack *>(tracks[p_idx]);
  3421. int i = 0;
  3422. while (i < tt->positions.size() - 2) {
  3423. TKey<Vector3> t0 = tt->positions[i];
  3424. TKey<Vector3> t1 = tt->positions[i + 1];
  3425. TKey<Vector3> t2 = tt->positions[i + 2];
  3426. bool erase = _vector3_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error);
  3427. if (erase) {
  3428. tt->positions.remove_at(i + 1);
  3429. } else {
  3430. i++;
  3431. }
  3432. }
  3433. if (tt->positions.size() == 2) {
  3434. if ((tt->positions[0].value - tt->positions[1].value).length() < p_allowed_precision_error) {
  3435. tt->positions.remove_at(1);
  3436. }
  3437. }
  3438. }
  3439. void Animation::_rotation_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error) {
  3440. ERR_FAIL_INDEX(p_idx, tracks.size());
  3441. ERR_FAIL_COND(tracks[p_idx]->type != TYPE_ROTATION_3D);
  3442. RotationTrack *rt = static_cast<RotationTrack *>(tracks[p_idx]);
  3443. int i = 0;
  3444. while (i < rt->rotations.size() - 2) {
  3445. TKey<Quaternion> t0 = rt->rotations[i];
  3446. TKey<Quaternion> t1 = rt->rotations[i + 1];
  3447. TKey<Quaternion> t2 = rt->rotations[i + 2];
  3448. bool erase = _quaternion_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error);
  3449. if (erase) {
  3450. rt->rotations.remove_at(i + 1);
  3451. } else {
  3452. i++;
  3453. }
  3454. }
  3455. if (rt->rotations.size() == 2) {
  3456. if ((rt->rotations[0].value - rt->rotations[1].value).length() < p_allowed_precision_error) {
  3457. rt->rotations.remove_at(1);
  3458. }
  3459. }
  3460. }
  3461. void Animation::_scale_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error) {
  3462. ERR_FAIL_INDEX(p_idx, tracks.size());
  3463. ERR_FAIL_COND(tracks[p_idx]->type != TYPE_SCALE_3D);
  3464. ScaleTrack *st = static_cast<ScaleTrack *>(tracks[p_idx]);
  3465. int i = 0;
  3466. while (i < st->scales.size() - 2) {
  3467. TKey<Vector3> t0 = st->scales[i];
  3468. TKey<Vector3> t1 = st->scales[i + 1];
  3469. TKey<Vector3> t2 = st->scales[i + 2];
  3470. bool erase = _vector3_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error);
  3471. if (erase) {
  3472. st->scales.remove_at(i + 1);
  3473. } else {
  3474. i++;
  3475. }
  3476. }
  3477. if (st->scales.size() == 2) {
  3478. if ((st->scales[0].value - st->scales[1].value).length() < p_allowed_precision_error) {
  3479. st->scales.remove_at(1);
  3480. }
  3481. }
  3482. }
  3483. void Animation::_blend_shape_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_precision_error) {
  3484. ERR_FAIL_INDEX(p_idx, tracks.size());
  3485. ERR_FAIL_COND(tracks[p_idx]->type != TYPE_BLEND_SHAPE);
  3486. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(tracks[p_idx]);
  3487. int i = 0;
  3488. while (i < bst->blend_shapes.size() - 2) {
  3489. TKey<float> t0 = bst->blend_shapes[i];
  3490. TKey<float> t1 = bst->blend_shapes[i + 1];
  3491. TKey<float> t2 = bst->blend_shapes[i + 2];
  3492. bool erase = _float_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_precision_error);
  3493. if (erase) {
  3494. bst->blend_shapes.remove_at(i + 1);
  3495. } else {
  3496. i++;
  3497. }
  3498. }
  3499. if (bst->blend_shapes.size() == 2) {
  3500. if (abs(bst->blend_shapes[0].value - bst->blend_shapes[1].value) < p_allowed_precision_error) {
  3501. bst->blend_shapes.remove_at(1);
  3502. }
  3503. }
  3504. }
  3505. void Animation::_value_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error) {
  3506. ERR_FAIL_INDEX(p_idx, tracks.size());
  3507. ERR_FAIL_COND(tracks[p_idx]->type != TYPE_VALUE);
  3508. ValueTrack *vt = static_cast<ValueTrack *>(tracks[p_idx]);
  3509. if (vt->values.size() == 0) {
  3510. return;
  3511. }
  3512. Variant::Type type = vt->values[0].value.get_type();
  3513. // Special case for angle interpolation.
  3514. bool is_using_angle = vt->interpolation == Animation::INTERPOLATION_LINEAR_ANGLE || vt->interpolation == Animation::INTERPOLATION_CUBIC_ANGLE;
  3515. int i = 0;
  3516. while (i < vt->values.size() - 2) {
  3517. bool erase = false;
  3518. switch (type) {
  3519. case Variant::FLOAT: {
  3520. TKey<float> t0;
  3521. TKey<float> t1;
  3522. TKey<float> t2;
  3523. t0.time = vt->values[i].time;
  3524. t1.time = vt->values[i + 1].time;
  3525. t2.time = vt->values[i + 2].time;
  3526. t0.value = vt->values[i].value;
  3527. t1.value = vt->values[i + 1].value;
  3528. t2.value = vt->values[i + 2].value;
  3529. if (is_using_angle) {
  3530. float diff1 = fmod(t1.value - t0.value, Math_TAU);
  3531. t1.value = t0.value + fmod(2.0 * diff1, Math_TAU) - diff1;
  3532. float diff2 = fmod(t2.value - t1.value, Math_TAU);
  3533. t2.value = t1.value + fmod(2.0 * diff2, Math_TAU) - diff2;
  3534. if (abs(abs(diff1) + abs(diff2)) >= Math_PI) {
  3535. break; // Rotation is more than 180 deg, keep key.
  3536. }
  3537. }
  3538. erase = _float_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_precision_error);
  3539. } break;
  3540. case Variant::VECTOR2: {
  3541. TKey<Vector2> t0;
  3542. TKey<Vector2> t1;
  3543. TKey<Vector2> t2;
  3544. t0.time = vt->values[i].time;
  3545. t1.time = vt->values[i + 1].time;
  3546. t2.time = vt->values[i + 2].time;
  3547. t0.value = vt->values[i].value;
  3548. t1.value = vt->values[i + 1].value;
  3549. t2.value = vt->values[i + 2].value;
  3550. erase = _vector2_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error);
  3551. } break;
  3552. case Variant::VECTOR3: {
  3553. TKey<Vector3> t0;
  3554. TKey<Vector3> t1;
  3555. TKey<Vector3> t2;
  3556. t0.time = vt->values[i].time;
  3557. t1.time = vt->values[i + 1].time;
  3558. t2.time = vt->values[i + 2].time;
  3559. t0.value = vt->values[i].value;
  3560. t1.value = vt->values[i + 1].value;
  3561. t2.value = vt->values[i + 2].value;
  3562. erase = _vector3_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error);
  3563. } break;
  3564. case Variant::QUATERNION: {
  3565. TKey<Quaternion> t0;
  3566. TKey<Quaternion> t1;
  3567. TKey<Quaternion> t2;
  3568. t0.time = vt->values[i].time;
  3569. t1.time = vt->values[i + 1].time;
  3570. t2.time = vt->values[i + 2].time;
  3571. t0.value = vt->values[i].value;
  3572. t1.value = vt->values[i + 1].value;
  3573. t2.value = vt->values[i + 2].value;
  3574. erase = _quaternion_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error);
  3575. } break;
  3576. default: {
  3577. } break;
  3578. }
  3579. if (erase) {
  3580. vt->values.remove_at(i + 1);
  3581. } else {
  3582. i++;
  3583. }
  3584. }
  3585. if (vt->values.size() == 2) {
  3586. bool single_key = false;
  3587. switch (type) {
  3588. case Variant::FLOAT: {
  3589. float val_0 = vt->values[0].value;
  3590. float val_1 = vt->values[1].value;
  3591. if (is_using_angle) {
  3592. float diff1 = fmod(val_1 - val_0, Math_TAU);
  3593. val_1 = val_0 + fmod(2.0 * diff1, Math_TAU) - diff1;
  3594. }
  3595. single_key = abs(val_0 - val_1) < p_allowed_precision_error;
  3596. } break;
  3597. case Variant::VECTOR2: {
  3598. Vector2 val_0 = vt->values[0].value;
  3599. Vector2 val_1 = vt->values[1].value;
  3600. single_key = (val_0 - val_1).length() < p_allowed_precision_error;
  3601. } break;
  3602. case Variant::VECTOR3: {
  3603. Vector3 val_0 = vt->values[0].value;
  3604. Vector3 val_1 = vt->values[1].value;
  3605. single_key = (val_0 - val_1).length() < p_allowed_precision_error;
  3606. } break;
  3607. case Variant::QUATERNION: {
  3608. Quaternion val_0 = vt->values[0].value;
  3609. Quaternion val_1 = vt->values[1].value;
  3610. single_key = (val_0 - val_1).length() < p_allowed_precision_error;
  3611. } break;
  3612. default: {
  3613. } break;
  3614. }
  3615. if (single_key) {
  3616. vt->values.remove_at(1);
  3617. }
  3618. }
  3619. }
  3620. void Animation::optimize(real_t p_allowed_velocity_err, real_t p_allowed_angular_err, int p_precision) {
  3621. real_t precision = Math::pow(0.1, p_precision);
  3622. for (int i = 0; i < tracks.size(); i++) {
  3623. if (track_is_compressed(i)) {
  3624. continue; //not possible to optimize compressed track
  3625. }
  3626. if (tracks[i]->type == TYPE_POSITION_3D) {
  3627. _position_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision);
  3628. } else if (tracks[i]->type == TYPE_ROTATION_3D) {
  3629. _rotation_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision);
  3630. } else if (tracks[i]->type == TYPE_SCALE_3D) {
  3631. _scale_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision);
  3632. } else if (tracks[i]->type == TYPE_BLEND_SHAPE) {
  3633. _blend_shape_track_optimize(i, p_allowed_velocity_err, precision);
  3634. } else if (tracks[i]->type == TYPE_VALUE) {
  3635. _value_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision);
  3636. }
  3637. }
  3638. }
  3639. #define print_animc(m_str)
  3640. //#define print_animc(m_str) print_line(m_str);
  3641. struct AnimationCompressionDataState {
  3642. enum {
  3643. MIN_OPTIMIZE_PACKETS = 5,
  3644. MAX_PACKETS = 16
  3645. };
  3646. uint32_t components = 3;
  3647. LocalVector<uint8_t> data; // Committed packets.
  3648. struct PacketData {
  3649. int32_t data[3] = { 0, 0, 0 };
  3650. uint32_t frame = 0;
  3651. };
  3652. float split_tolerance = 1.5;
  3653. LocalVector<PacketData> temp_packets;
  3654. //used for rollback if the new frame does not fit
  3655. int32_t validated_packet_count = -1;
  3656. static int32_t _compute_delta16_signed(int32_t p_from, int32_t p_to) {
  3657. int32_t delta = p_to - p_from;
  3658. if (delta > 32767) {
  3659. return delta - 65536; // use wrap around
  3660. } else if (delta < -32768) {
  3661. return 65536 + delta; // use wrap around
  3662. }
  3663. return delta;
  3664. }
  3665. static uint32_t _compute_shift_bits_signed(int32_t p_delta) {
  3666. if (p_delta == 0) {
  3667. return 0;
  3668. } else if (p_delta < 0) {
  3669. p_delta = ABS(p_delta) - 1;
  3670. if (p_delta == 0) {
  3671. return 1;
  3672. }
  3673. }
  3674. return nearest_shift(p_delta);
  3675. }
  3676. void _compute_max_shifts(uint32_t p_from, uint32_t p_to, uint32_t *max_shifts, uint32_t &max_frame_delta_shift) const {
  3677. for (uint32_t j = 0; j < components; j++) {
  3678. max_shifts[j] = 0;
  3679. }
  3680. max_frame_delta_shift = 0;
  3681. for (uint32_t i = p_from + 1; i <= p_to; i++) {
  3682. int32_t frame_delta = temp_packets[i].frame - temp_packets[i - 1].frame;
  3683. max_frame_delta_shift = MAX(max_frame_delta_shift, nearest_shift(frame_delta));
  3684. for (uint32_t j = 0; j < components; j++) {
  3685. int32_t diff = _compute_delta16_signed(temp_packets[i - 1].data[j], temp_packets[i].data[j]);
  3686. uint32_t shift = _compute_shift_bits_signed(diff);
  3687. max_shifts[j] = MAX(shift, max_shifts[j]);
  3688. }
  3689. }
  3690. }
  3691. bool insert_key(uint32_t p_frame, const Vector3i &p_key) {
  3692. if (temp_packets.size() == MAX_PACKETS) {
  3693. commit_temp_packets();
  3694. }
  3695. PacketData packet;
  3696. packet.frame = p_frame;
  3697. for (int i = 0; i < 3; i++) {
  3698. ERR_FAIL_COND_V(p_key[i] > 65535, false); // Safety checks.
  3699. packet.data[i] = p_key[i];
  3700. }
  3701. temp_packets.push_back(packet);
  3702. if (temp_packets.size() >= MIN_OPTIMIZE_PACKETS) {
  3703. uint32_t max_shifts[3] = { 0, 0, 0 }; // Base sizes, 16 bit
  3704. uint32_t max_frame_delta_shift = 0;
  3705. // Compute the average shift before the packet was added
  3706. _compute_max_shifts(0, temp_packets.size() - 2, max_shifts, max_frame_delta_shift);
  3707. float prev_packet_size_avg = 0;
  3708. prev_packet_size_avg = float(1 << max_frame_delta_shift);
  3709. for (uint32_t i = 0; i < components; i++) {
  3710. prev_packet_size_avg += float(1 << max_shifts[i]);
  3711. }
  3712. prev_packet_size_avg /= float(1 + components);
  3713. _compute_max_shifts(temp_packets.size() - 2, temp_packets.size() - 1, max_shifts, max_frame_delta_shift);
  3714. float new_packet_size_avg = 0;
  3715. new_packet_size_avg = float(1 << max_frame_delta_shift);
  3716. for (uint32_t i = 0; i < components; i++) {
  3717. new_packet_size_avg += float(1 << max_shifts[i]);
  3718. }
  3719. new_packet_size_avg /= float(1 + components);
  3720. print_animc("packet count: " + rtos(temp_packets.size() - 1) + " size avg " + rtos(prev_packet_size_avg) + " new avg " + rtos(new_packet_size_avg));
  3721. float ratio = (prev_packet_size_avg < new_packet_size_avg) ? (new_packet_size_avg / prev_packet_size_avg) : (prev_packet_size_avg / new_packet_size_avg);
  3722. if (ratio > split_tolerance) {
  3723. print_animc("split!");
  3724. temp_packets.resize(temp_packets.size() - 1);
  3725. commit_temp_packets();
  3726. temp_packets.push_back(packet);
  3727. }
  3728. }
  3729. return temp_packets.size() == 1; // First key
  3730. }
  3731. uint32_t get_temp_packet_size() const {
  3732. if (temp_packets.size() == 0) {
  3733. return 0;
  3734. } else if (temp_packets.size() == 1) {
  3735. return components == 1 ? 4 : 8; // 1 component packet is 16 bits and 16 bits unused. 3 component packets is 48 bits and 16 bits unused
  3736. }
  3737. uint32_t max_shifts[3] = { 0, 0, 0 }; //base sizes, 16 bit
  3738. uint32_t max_frame_delta_shift = 0;
  3739. _compute_max_shifts(0, temp_packets.size() - 1, max_shifts, max_frame_delta_shift);
  3740. uint32_t size_bits = 16; //base value (all 4 bits of shift sizes for x,y,z,time)
  3741. size_bits += max_frame_delta_shift * (temp_packets.size() - 1); //times
  3742. for (uint32_t j = 0; j < components; j++) {
  3743. size_bits += 16; //base value
  3744. uint32_t shift = max_shifts[j];
  3745. if (shift > 0) {
  3746. shift += 1; //if not zero, add sign bit
  3747. }
  3748. size_bits += shift * (temp_packets.size() - 1);
  3749. }
  3750. if (size_bits % 8 != 0) { //wrap to 8 bits
  3751. size_bits += 8 - (size_bits % 8);
  3752. }
  3753. uint32_t size_bytes = size_bits / 8; //wrap to words
  3754. if (size_bytes % 4 != 0) {
  3755. size_bytes += 4 - (size_bytes % 4);
  3756. }
  3757. return size_bytes;
  3758. }
  3759. static void _push_bits(LocalVector<uint8_t> &data, uint32_t &r_buffer, uint32_t &r_bits_used, uint32_t p_value, uint32_t p_bits) {
  3760. r_buffer |= p_value << r_bits_used;
  3761. r_bits_used += p_bits;
  3762. while (r_bits_used >= 8) {
  3763. uint8_t byte = r_buffer & 0xFF;
  3764. data.push_back(byte);
  3765. r_buffer >>= 8;
  3766. r_bits_used -= 8;
  3767. }
  3768. }
  3769. void commit_temp_packets() {
  3770. if (temp_packets.size() == 0) {
  3771. return; // Nothing to do.
  3772. }
  3773. //#define DEBUG_PACKET_PUSH
  3774. #ifdef DEBUG_PACKET_PUSH
  3775. #ifndef _MSC_VER
  3776. #warning Debugging packet push, disable this code in production to gain a bit more import performance.
  3777. #endif
  3778. uint32_t debug_packet_push = get_temp_packet_size();
  3779. uint32_t debug_data_size = data.size();
  3780. #endif
  3781. // Store header
  3782. uint8_t header[8];
  3783. uint32_t header_bytes = 0;
  3784. for (uint32_t i = 0; i < components; i++) {
  3785. encode_uint16(temp_packets[0].data[i], &header[header_bytes]);
  3786. header_bytes += 2;
  3787. }
  3788. uint32_t max_shifts[3] = { 0, 0, 0 }; //base sizes, 16 bit
  3789. uint32_t max_frame_delta_shift = 0;
  3790. if (temp_packets.size() > 1) {
  3791. _compute_max_shifts(0, temp_packets.size() - 1, max_shifts, max_frame_delta_shift);
  3792. uint16_t shift_header = (max_frame_delta_shift - 1) << 12;
  3793. for (uint32_t i = 0; i < components; i++) {
  3794. shift_header |= max_shifts[i] << (4 * i);
  3795. }
  3796. encode_uint16(shift_header, &header[header_bytes]);
  3797. header_bytes += 2;
  3798. }
  3799. while (header_bytes < 8 && header_bytes % 4 != 0) { // First cond needed to silence wrong GCC warning.
  3800. header[header_bytes++] = 0;
  3801. }
  3802. for (uint32_t i = 0; i < header_bytes; i++) {
  3803. data.push_back(header[i]);
  3804. }
  3805. if (temp_packets.size() == 1) {
  3806. temp_packets.clear();
  3807. validated_packet_count = 0;
  3808. return; //only header stored, nothing else to do
  3809. }
  3810. uint32_t bit_buffer = 0;
  3811. uint32_t bits_used = 0;
  3812. for (uint32_t i = 1; i < temp_packets.size(); i++) {
  3813. uint32_t frame_delta = temp_packets[i].frame - temp_packets[i - 1].frame;
  3814. _push_bits(data, bit_buffer, bits_used, frame_delta, max_frame_delta_shift);
  3815. for (uint32_t j = 0; j < components; j++) {
  3816. if (max_shifts[j] == 0) {
  3817. continue; // Zero delta, do not store
  3818. }
  3819. int32_t delta = _compute_delta16_signed(temp_packets[i - 1].data[j], temp_packets[i].data[j]);
  3820. ERR_FAIL_COND(delta < -32768 || delta > 32767); // Safety check.
  3821. uint16_t deltau;
  3822. if (delta < 0) {
  3823. deltau = (ABS(delta) - 1) | (1 << max_shifts[j]);
  3824. } else {
  3825. deltau = delta;
  3826. }
  3827. _push_bits(data, bit_buffer, bits_used, deltau, max_shifts[j] + 1); // Include sign bit
  3828. }
  3829. }
  3830. if (bits_used != 0) {
  3831. ERR_FAIL_COND(bit_buffer > 0xFF); // Safety check.
  3832. data.push_back(bit_buffer);
  3833. }
  3834. while (data.size() % 4 != 0) {
  3835. data.push_back(0); //pad to align with 4
  3836. }
  3837. temp_packets.clear();
  3838. validated_packet_count = 0;
  3839. #ifdef DEBUG_PACKET_PUSH
  3840. ERR_FAIL_COND((data.size() - debug_data_size) != debug_packet_push);
  3841. #endif
  3842. }
  3843. };
  3844. struct AnimationCompressionTimeState {
  3845. struct Packet {
  3846. uint32_t frame;
  3847. uint32_t offset;
  3848. uint32_t count;
  3849. };
  3850. LocalVector<Packet> packets;
  3851. //used for rollback
  3852. int32_t key_index = 0;
  3853. int32_t validated_packet_count = 0;
  3854. int32_t validated_key_index = -1;
  3855. bool needs_start_frame = false;
  3856. };
  3857. Vector3i Animation::_compress_key(uint32_t p_track, const AABB &p_bounds, int32_t p_key, float p_time) {
  3858. Vector3i values;
  3859. TrackType tt = track_get_type(p_track);
  3860. switch (tt) {
  3861. case TYPE_POSITION_3D: {
  3862. Vector3 pos;
  3863. if (p_key >= 0) {
  3864. position_track_get_key(p_track, p_key, &pos);
  3865. } else {
  3866. try_position_track_interpolate(p_track, p_time, &pos);
  3867. }
  3868. pos = (pos - p_bounds.position) / p_bounds.size;
  3869. for (int j = 0; j < 3; j++) {
  3870. values[j] = CLAMP(int32_t(pos[j] * 65535.0), 0, 65535);
  3871. }
  3872. } break;
  3873. case TYPE_ROTATION_3D: {
  3874. Quaternion rot;
  3875. if (p_key >= 0) {
  3876. rotation_track_get_key(p_track, p_key, &rot);
  3877. } else {
  3878. try_rotation_track_interpolate(p_track, p_time, &rot);
  3879. }
  3880. Vector3 axis = rot.get_axis();
  3881. float angle = rot.get_angle();
  3882. angle = Math::fposmod(double(angle), double(Math_PI * 2.0));
  3883. Vector2 oct = axis.octahedron_encode();
  3884. Vector3 rot_norm(oct.x, oct.y, angle / (Math_PI * 2.0)); // high resolution rotation in 0-1 angle.
  3885. for (int j = 0; j < 3; j++) {
  3886. values[j] = CLAMP(int32_t(rot_norm[j] * 65535.0), 0, 65535);
  3887. }
  3888. } break;
  3889. case TYPE_SCALE_3D: {
  3890. Vector3 scale;
  3891. if (p_key >= 0) {
  3892. scale_track_get_key(p_track, p_key, &scale);
  3893. } else {
  3894. try_scale_track_interpolate(p_track, p_time, &scale);
  3895. }
  3896. scale = (scale - p_bounds.position) / p_bounds.size;
  3897. for (int j = 0; j < 3; j++) {
  3898. values[j] = CLAMP(int32_t(scale[j] * 65535.0), 0, 65535);
  3899. }
  3900. } break;
  3901. case TYPE_BLEND_SHAPE: {
  3902. float blend;
  3903. if (p_key >= 0) {
  3904. blend_shape_track_get_key(p_track, p_key, &blend);
  3905. } else {
  3906. try_blend_shape_track_interpolate(p_track, p_time, &blend);
  3907. }
  3908. blend = (blend / float(Compression::BLEND_SHAPE_RANGE)) * 0.5 + 0.5;
  3909. values[0] = CLAMP(int32_t(blend * 65535.0), 0, 65535);
  3910. } break;
  3911. default: {
  3912. ERR_FAIL_V(Vector3i()); // Safety check.
  3913. } break;
  3914. }
  3915. return values;
  3916. }
  3917. struct AnimationCompressionBufferBitsRead {
  3918. uint32_t buffer = 0;
  3919. uint32_t used = 0;
  3920. const uint8_t *src_data = nullptr;
  3921. _FORCE_INLINE_ uint32_t read(uint32_t p_bits) {
  3922. uint32_t output = 0;
  3923. uint32_t written = 0;
  3924. while (p_bits > 0) {
  3925. if (used == 0) {
  3926. used = 8;
  3927. buffer = *src_data;
  3928. src_data++;
  3929. }
  3930. uint32_t to_write = MIN(used, p_bits);
  3931. output |= (buffer & ((1 << to_write) - 1)) << written;
  3932. buffer >>= to_write;
  3933. used -= to_write;
  3934. p_bits -= to_write;
  3935. written += to_write;
  3936. }
  3937. return output;
  3938. }
  3939. };
  3940. void Animation::compress(uint32_t p_page_size, uint32_t p_fps, float p_split_tolerance) {
  3941. ERR_FAIL_COND_MSG(compression.enabled, "This animation is already compressed");
  3942. p_split_tolerance = CLAMP(p_split_tolerance, 1.1, 8.0);
  3943. compression.pages.clear();
  3944. uint32_t base_page_size = 0; // Before compressing pages, compute how large the "end page" datablock is.
  3945. LocalVector<uint32_t> tracks_to_compress;
  3946. LocalVector<AABB> track_bounds;
  3947. const uint32_t time_packet_size = 4;
  3948. const uint32_t track_header_size = 4 + 4 + 4; // pointer to time (4 bytes), amount of time keys (4 bytes) pointer to track data (4 bytes)
  3949. for (int i = 0; i < get_track_count(); i++) {
  3950. TrackType type = track_get_type(i);
  3951. if (type != TYPE_POSITION_3D && type != TYPE_ROTATION_3D && type != TYPE_SCALE_3D && type != TYPE_BLEND_SHAPE) {
  3952. continue;
  3953. }
  3954. if (track_get_key_count(i) == 0) {
  3955. continue; //do not compress, no keys
  3956. }
  3957. base_page_size += track_header_size; //pointer to beginning of each track timeline and amount of time keys
  3958. base_page_size += time_packet_size; //for end of track time marker
  3959. base_page_size += (type == TYPE_BLEND_SHAPE) ? 4 : 8; // at least the end of track packet (at much 8 bytes). This could be less, but have to be pessimistic.
  3960. tracks_to_compress.push_back(i);
  3961. AABB bounds;
  3962. if (type == TYPE_POSITION_3D) {
  3963. AABB aabb;
  3964. int kcount = track_get_key_count(i);
  3965. for (int j = 0; j < kcount; j++) {
  3966. Vector3 pos;
  3967. position_track_get_key(i, j, &pos);
  3968. if (j == 0) {
  3969. aabb.position = pos;
  3970. } else {
  3971. aabb.expand_to(pos);
  3972. }
  3973. }
  3974. for (int j = 0; j < 3; j++) {
  3975. // Can't have zero.
  3976. if (aabb.size[j] < CMP_EPSILON) {
  3977. aabb.size[j] = CMP_EPSILON;
  3978. }
  3979. }
  3980. bounds = aabb;
  3981. }
  3982. if (type == TYPE_SCALE_3D) {
  3983. AABB aabb;
  3984. int kcount = track_get_key_count(i);
  3985. for (int j = 0; j < kcount; j++) {
  3986. Vector3 scale;
  3987. scale_track_get_key(i, j, &scale);
  3988. if (j == 0) {
  3989. aabb.position = scale;
  3990. } else {
  3991. aabb.expand_to(scale);
  3992. }
  3993. }
  3994. for (int j = 0; j < 3; j++) {
  3995. // Can't have zero.
  3996. if (aabb.size[j] < CMP_EPSILON) {
  3997. aabb.size[j] = CMP_EPSILON;
  3998. }
  3999. }
  4000. bounds = aabb;
  4001. }
  4002. track_bounds.push_back(bounds);
  4003. }
  4004. if (tracks_to_compress.size() == 0) {
  4005. return; //nothing to compress
  4006. }
  4007. print_animc("Anim Compression:");
  4008. print_animc("-----------------");
  4009. print_animc("Tracks to compress: " + itos(tracks_to_compress.size()));
  4010. uint32_t current_frame = 0;
  4011. uint32_t base_page_frame = 0;
  4012. double frame_len = 1.0 / double(p_fps);
  4013. const uint32_t max_frames_per_page = 65536;
  4014. print_animc("Frame Len: " + rtos(frame_len));
  4015. LocalVector<AnimationCompressionDataState> data_tracks;
  4016. LocalVector<AnimationCompressionTimeState> time_tracks;
  4017. data_tracks.resize(tracks_to_compress.size());
  4018. time_tracks.resize(tracks_to_compress.size());
  4019. uint32_t needed_min_page_size = base_page_size;
  4020. for (uint32_t i = 0; i < data_tracks.size(); i++) {
  4021. data_tracks[i].split_tolerance = p_split_tolerance;
  4022. if (track_get_type(tracks_to_compress[i]) == TYPE_BLEND_SHAPE) {
  4023. data_tracks[i].components = 1;
  4024. } else {
  4025. data_tracks[i].components = 3;
  4026. }
  4027. needed_min_page_size += data_tracks[i].data.size() + data_tracks[i].get_temp_packet_size();
  4028. }
  4029. for (uint32_t i = 0; i < time_tracks.size(); i++) {
  4030. needed_min_page_size += time_tracks[i].packets.size() * 4; // time packet is 32 bits
  4031. }
  4032. ERR_FAIL_COND_MSG(p_page_size < needed_min_page_size, "Cannot compress with the given page size");
  4033. while (true) {
  4034. // Begin by finding the keyframe in all tracks with the time closest to the current time
  4035. const uint32_t FRAME_MAX = 0xFFFFFFFF;
  4036. const int32_t NO_TRACK_FOUND = -1;
  4037. uint32_t best_frame = FRAME_MAX;
  4038. uint32_t best_invalid_frame = FRAME_MAX;
  4039. int32_t best_frame_track = NO_TRACK_FOUND; // Default is -1, which means all keyframes for this page are exhausted.
  4040. bool start_frame = false;
  4041. for (uint32_t i = 0; i < tracks_to_compress.size(); i++) {
  4042. uint32_t uncomp_track = tracks_to_compress[i];
  4043. if (time_tracks[i].key_index == track_get_key_count(uncomp_track)) {
  4044. if (time_tracks[i].needs_start_frame) {
  4045. start_frame = true;
  4046. best_frame = base_page_frame;
  4047. best_frame_track = i;
  4048. time_tracks[i].needs_start_frame = false;
  4049. break;
  4050. } else {
  4051. continue; // This track is exhausted (all keys were added already), don't consider.
  4052. }
  4053. }
  4054. uint32_t key_frame = double(track_get_key_time(uncomp_track, time_tracks[i].key_index)) / frame_len;
  4055. if (time_tracks[i].needs_start_frame && key_frame > base_page_frame) {
  4056. start_frame = true;
  4057. best_frame = base_page_frame;
  4058. best_frame_track = i;
  4059. time_tracks[i].needs_start_frame = false;
  4060. break;
  4061. }
  4062. ERR_FAIL_COND(key_frame < base_page_frame); // Safety check, should never happen.
  4063. if (key_frame - base_page_frame >= max_frames_per_page) {
  4064. // Invalid because beyond the max frames allowed per page
  4065. best_invalid_frame = MIN(best_invalid_frame, key_frame);
  4066. } else if (key_frame < best_frame) {
  4067. best_frame = key_frame;
  4068. best_frame_track = i;
  4069. }
  4070. }
  4071. print_animc("*KEY*: Current Frame: " + itos(current_frame) + " Best Frame: " + rtos(best_frame) + " Best Track: " + itos(best_frame_track) + " Start: " + String(start_frame ? "true" : "false"));
  4072. if (!start_frame && best_frame > current_frame) {
  4073. // Any case where the current frame advanced, either because nothing was found or because something was found greater than the current one.
  4074. print_animc("\tAdvance Condition.");
  4075. bool rollback = false;
  4076. // The frame has advanced, time to validate the previous frame
  4077. uint32_t current_page_size = base_page_size;
  4078. for (const AnimationCompressionDataState &state : data_tracks) {
  4079. uint32_t track_size = state.data.size(); // track size
  4080. track_size += state.get_temp_packet_size(); // Add the temporary data
  4081. if (track_size > Compression::MAX_DATA_TRACK_SIZE) {
  4082. rollback = true; //track to large, time track can't point to keys any longer, because key offset is 12 bits
  4083. break;
  4084. }
  4085. current_page_size += track_size;
  4086. }
  4087. for (const AnimationCompressionTimeState &state : time_tracks) {
  4088. current_page_size += state.packets.size() * 4; // time packet is 32 bits
  4089. }
  4090. if (!rollback && current_page_size > p_page_size) {
  4091. rollback = true;
  4092. }
  4093. print_animc("\tCurrent Page Size: " + itos(current_page_size) + "/" + itos(p_page_size) + " Rollback? " + String(rollback ? "YES!" : "no"));
  4094. if (rollback) {
  4095. // Not valid any longer, so rollback and commit page
  4096. for (AnimationCompressionDataState &state : data_tracks) {
  4097. state.temp_packets.resize(state.validated_packet_count);
  4098. }
  4099. for (AnimationCompressionTimeState &state : time_tracks) {
  4100. state.key_index = state.validated_key_index; //rollback key
  4101. state.packets.resize(state.validated_packet_count);
  4102. }
  4103. } else {
  4104. // All valid, so save rollback information
  4105. for (AnimationCompressionDataState &state : data_tracks) {
  4106. state.validated_packet_count = state.temp_packets.size();
  4107. }
  4108. for (AnimationCompressionTimeState &state : time_tracks) {
  4109. state.validated_key_index = state.key_index;
  4110. state.validated_packet_count = state.packets.size();
  4111. }
  4112. // Accept this frame as the frame being processed (as long as it exists)
  4113. if (best_frame != FRAME_MAX) {
  4114. current_frame = best_frame;
  4115. print_animc("\tValidated, New Current Frame: " + itos(current_frame));
  4116. }
  4117. }
  4118. if (rollback || best_frame == FRAME_MAX) {
  4119. // Commit the page if had to rollback or if no track was found
  4120. print_animc("\tCommiting page...");
  4121. // The end frame for the page depends entirely on whether its valid or
  4122. // no more keys were found.
  4123. // If not valid, then the end frame is the current frame (as this means the current frame is being rolled back
  4124. // If valid, then the end frame is the next invalid one (in case more frames exist), or the current frame in case no more frames exist.
  4125. uint32_t page_end_frame = (rollback || best_frame == FRAME_MAX) ? current_frame : best_invalid_frame;
  4126. print_animc("\tEnd Frame: " + itos(page_end_frame) + ", " + rtos(page_end_frame * frame_len) + "s");
  4127. // Add finalizer frames and commit pending tracks
  4128. uint32_t finalizer_local_frame = page_end_frame - base_page_frame;
  4129. uint32_t total_page_size = 0;
  4130. for (uint32_t i = 0; i < data_tracks.size(); i++) {
  4131. if (data_tracks[i].temp_packets.size() == 0 || (data_tracks[i].temp_packets[data_tracks[i].temp_packets.size() - 1].frame) < finalizer_local_frame) {
  4132. // Add finalizer frame if it makes sense
  4133. Vector3i values = _compress_key(tracks_to_compress[i], track_bounds[i], -1, page_end_frame * frame_len);
  4134. bool first_key = data_tracks[i].insert_key(finalizer_local_frame, values);
  4135. if (first_key) {
  4136. AnimationCompressionTimeState::Packet p;
  4137. p.count = 1;
  4138. p.frame = finalizer_local_frame;
  4139. p.offset = data_tracks[i].data.size();
  4140. time_tracks[i].packets.push_back(p);
  4141. } else {
  4142. ERR_FAIL_COND(time_tracks[i].packets.is_empty());
  4143. time_tracks[i].packets[time_tracks[i].packets.size() - 1].count++;
  4144. }
  4145. }
  4146. data_tracks[i].commit_temp_packets();
  4147. total_page_size += data_tracks[i].data.size();
  4148. total_page_size += time_tracks[i].packets.size() * 4;
  4149. total_page_size += track_header_size;
  4150. print_animc("\tTrack " + itos(i) + " time packets: " + itos(time_tracks[i].packets.size()) + " Packet data: " + itos(data_tracks[i].data.size()));
  4151. }
  4152. print_animc("\tTotal page Size: " + itos(total_page_size) + "/" + itos(p_page_size));
  4153. // Create Page
  4154. Vector<uint8_t> page_data;
  4155. page_data.resize(total_page_size);
  4156. {
  4157. uint8_t *page_ptr = page_data.ptrw();
  4158. uint32_t base_offset = data_tracks.size() * track_header_size;
  4159. for (uint32_t i = 0; i < data_tracks.size(); i++) {
  4160. encode_uint32(base_offset, page_ptr + (track_header_size * i + 0));
  4161. uint16_t *key_time_ptr = (uint16_t *)(page_ptr + base_offset);
  4162. for (uint32_t j = 0; j < time_tracks[i].packets.size(); j++) {
  4163. key_time_ptr[j * 2 + 0] = uint16_t(time_tracks[i].packets[j].frame);
  4164. uint16_t ptr = time_tracks[i].packets[j].offset / 4;
  4165. ptr |= (time_tracks[i].packets[j].count - 1) << 12;
  4166. key_time_ptr[j * 2 + 1] = ptr;
  4167. base_offset += 4;
  4168. }
  4169. encode_uint32(time_tracks[i].packets.size(), page_ptr + (track_header_size * i + 4));
  4170. encode_uint32(base_offset, page_ptr + (track_header_size * i + 8));
  4171. memcpy(page_ptr + base_offset, data_tracks[i].data.ptr(), data_tracks[i].data.size());
  4172. base_offset += data_tracks[i].data.size();
  4173. //reset track
  4174. data_tracks[i].data.clear();
  4175. data_tracks[i].temp_packets.clear();
  4176. data_tracks[i].validated_packet_count = -1;
  4177. time_tracks[i].needs_start_frame = true; //Not required the first time, but from now on it is.
  4178. time_tracks[i].packets.clear();
  4179. time_tracks[i].validated_key_index = -1;
  4180. time_tracks[i].validated_packet_count = 0;
  4181. }
  4182. }
  4183. Compression::Page page;
  4184. page.data = page_data;
  4185. page.time_offset = base_page_frame * frame_len;
  4186. compression.pages.push_back(page);
  4187. if (!rollback && best_invalid_frame == FRAME_MAX) {
  4188. break; // No more pages to add.
  4189. }
  4190. current_frame = page_end_frame;
  4191. base_page_frame = page_end_frame;
  4192. continue; // Start over
  4193. }
  4194. }
  4195. // A key was found for the current frame and all is ok
  4196. uint32_t comp_track = best_frame_track;
  4197. Vector3i values;
  4198. if (start_frame) {
  4199. // Interpolate
  4200. values = _compress_key(tracks_to_compress[comp_track], track_bounds[comp_track], -1, base_page_frame * frame_len);
  4201. } else {
  4202. uint32_t key = time_tracks[comp_track].key_index;
  4203. values = _compress_key(tracks_to_compress[comp_track], track_bounds[comp_track], key);
  4204. time_tracks[comp_track].key_index++; //goto next key (but could be rolled back if beyond page size).
  4205. }
  4206. bool first_key = data_tracks[comp_track].insert_key(best_frame - base_page_frame, values);
  4207. if (first_key) {
  4208. AnimationCompressionTimeState::Packet p;
  4209. p.count = 1;
  4210. p.frame = best_frame - base_page_frame;
  4211. p.offset = data_tracks[comp_track].data.size();
  4212. time_tracks[comp_track].packets.push_back(p);
  4213. } else {
  4214. ERR_CONTINUE(time_tracks[comp_track].packets.is_empty());
  4215. time_tracks[comp_track].packets[time_tracks[comp_track].packets.size() - 1].count++;
  4216. }
  4217. }
  4218. compression.bounds = track_bounds;
  4219. compression.fps = p_fps;
  4220. compression.enabled = true;
  4221. for (uint32_t i = 0; i < tracks_to_compress.size(); i++) {
  4222. Track *t = tracks[tracks_to_compress[i]];
  4223. t->interpolation = INTERPOLATION_LINEAR; //only linear supported
  4224. switch (t->type) {
  4225. case TYPE_POSITION_3D: {
  4226. PositionTrack *tt = static_cast<PositionTrack *>(t);
  4227. tt->positions.clear();
  4228. tt->compressed_track = i;
  4229. } break;
  4230. case TYPE_ROTATION_3D: {
  4231. RotationTrack *rt = static_cast<RotationTrack *>(t);
  4232. rt->rotations.clear();
  4233. rt->compressed_track = i;
  4234. } break;
  4235. case TYPE_SCALE_3D: {
  4236. ScaleTrack *st = static_cast<ScaleTrack *>(t);
  4237. st->scales.clear();
  4238. st->compressed_track = i;
  4239. print_line("Scale Bounds " + itos(i) + ": " + track_bounds[i]);
  4240. } break;
  4241. case TYPE_BLEND_SHAPE: {
  4242. BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
  4243. bst->blend_shapes.clear();
  4244. bst->compressed_track = i;
  4245. } break;
  4246. default: {
  4247. }
  4248. }
  4249. }
  4250. #if 1
  4251. uint32_t orig_size = 0;
  4252. for (int i = 0; i < get_track_count(); i++) {
  4253. switch (track_get_type(i)) {
  4254. case TYPE_SCALE_3D:
  4255. case TYPE_POSITION_3D: {
  4256. orig_size += sizeof(TKey<Vector3>) * track_get_key_count(i);
  4257. } break;
  4258. case TYPE_ROTATION_3D: {
  4259. orig_size += sizeof(TKey<Quaternion>) * track_get_key_count(i);
  4260. } break;
  4261. case TYPE_BLEND_SHAPE: {
  4262. orig_size += sizeof(TKey<float>) * track_get_key_count(i);
  4263. } break;
  4264. default: {
  4265. }
  4266. }
  4267. }
  4268. uint32_t new_size = 0;
  4269. for (const Compression::Page &page : compression.pages) {
  4270. new_size += page.data.size();
  4271. }
  4272. print_line("Original size: " + itos(orig_size) + " - Compressed size: " + itos(new_size) + " " + String::num(float(new_size) / float(orig_size) * 100, 2) + "% pages: " + itos(compression.pages.size()));
  4273. #endif
  4274. }
  4275. bool Animation::_rotation_interpolate_compressed(uint32_t p_compressed_track, double p_time, Quaternion &r_ret) const {
  4276. Vector3i current;
  4277. Vector3i next;
  4278. double time_current;
  4279. double time_next;
  4280. if (!_fetch_compressed<3>(p_compressed_track, p_time, current, time_current, next, time_next)) {
  4281. return false; //some sort of problem
  4282. }
  4283. if (time_current >= p_time || time_current == time_next) {
  4284. r_ret = _uncompress_quaternion(current);
  4285. } else if (p_time >= time_next) {
  4286. r_ret = _uncompress_quaternion(next);
  4287. } else {
  4288. double c = (p_time - time_current) / (time_next - time_current);
  4289. Quaternion from = _uncompress_quaternion(current);
  4290. Quaternion to = _uncompress_quaternion(next);
  4291. r_ret = from.slerp(to, c);
  4292. }
  4293. return true;
  4294. }
  4295. bool Animation::_pos_scale_interpolate_compressed(uint32_t p_compressed_track, double p_time, Vector3 &r_ret) const {
  4296. Vector3i current;
  4297. Vector3i next;
  4298. double time_current;
  4299. double time_next;
  4300. if (!_fetch_compressed<3>(p_compressed_track, p_time, current, time_current, next, time_next)) {
  4301. return false; //some sort of problem
  4302. }
  4303. if (time_current >= p_time || time_current == time_next) {
  4304. r_ret = _uncompress_pos_scale(p_compressed_track, current);
  4305. } else if (p_time >= time_next) {
  4306. r_ret = _uncompress_pos_scale(p_compressed_track, next);
  4307. } else {
  4308. double c = (p_time - time_current) / (time_next - time_current);
  4309. Vector3 from = _uncompress_pos_scale(p_compressed_track, current);
  4310. Vector3 to = _uncompress_pos_scale(p_compressed_track, next);
  4311. r_ret = from.lerp(to, c);
  4312. }
  4313. return true;
  4314. }
  4315. bool Animation::_blend_shape_interpolate_compressed(uint32_t p_compressed_track, double p_time, float &r_ret) const {
  4316. Vector3i current;
  4317. Vector3i next;
  4318. double time_current;
  4319. double time_next;
  4320. if (!_fetch_compressed<1>(p_compressed_track, p_time, current, time_current, next, time_next)) {
  4321. return false; //some sort of problem
  4322. }
  4323. if (time_current >= p_time || time_current == time_next) {
  4324. r_ret = _uncompress_blend_shape(current);
  4325. } else if (p_time >= time_next) {
  4326. r_ret = _uncompress_blend_shape(next);
  4327. } else {
  4328. float c = (p_time - time_current) / (time_next - time_current);
  4329. float from = _uncompress_blend_shape(current);
  4330. float to = _uncompress_blend_shape(next);
  4331. r_ret = Math::lerp(from, to, c);
  4332. }
  4333. return true;
  4334. }
  4335. template <uint32_t COMPONENTS>
  4336. bool Animation::_fetch_compressed(uint32_t p_compressed_track, double p_time, Vector3i &r_current_value, double &r_current_time, Vector3i &r_next_value, double &r_next_time, uint32_t *key_index) const {
  4337. ERR_FAIL_COND_V(!compression.enabled, false);
  4338. ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), false);
  4339. p_time = CLAMP(p_time, 0, length);
  4340. if (key_index) {
  4341. *key_index = 0;
  4342. }
  4343. double frame_to_sec = 1.0 / double(compression.fps);
  4344. int32_t page_index = -1;
  4345. for (uint32_t i = 0; i < compression.pages.size(); i++) {
  4346. if (compression.pages[i].time_offset > p_time) {
  4347. break;
  4348. }
  4349. page_index = i;
  4350. }
  4351. ERR_FAIL_COND_V(page_index == -1, false); //should not happen
  4352. double page_base_time = compression.pages[page_index].time_offset;
  4353. const uint8_t *page_data = compression.pages[page_index].data.ptr();
  4354. // Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported.
  4355. const uint32_t *indices = (const uint32_t *)page_data;
  4356. const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]];
  4357. uint32_t time_key_count = indices[p_compressed_track * 3 + 1];
  4358. int32_t packet_idx = 0;
  4359. double packet_time = double(time_keys[0]) * frame_to_sec + page_base_time;
  4360. uint32_t base_frame = time_keys[0];
  4361. for (uint32_t i = 1; i < time_key_count; i++) {
  4362. uint32_t f = time_keys[i * 2 + 0];
  4363. double frame_time = double(f) * frame_to_sec + page_base_time;
  4364. if (frame_time > p_time) {
  4365. break;
  4366. }
  4367. if (key_index) {
  4368. (*key_index) += (time_keys[(i - 1) * 2 + 1] >> 12) + 1;
  4369. }
  4370. packet_idx = i;
  4371. packet_time = frame_time;
  4372. base_frame = f;
  4373. }
  4374. const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]];
  4375. uint16_t time_key_data = time_keys[packet_idx * 2 + 1];
  4376. uint32_t data_offset = (time_key_data & 0xFFF) * 4; // lower 12 bits
  4377. uint32_t data_count = (time_key_data >> 12) + 1;
  4378. const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset);
  4379. uint16_t decode[COMPONENTS];
  4380. uint16_t decode_next[COMPONENTS];
  4381. for (uint32_t i = 0; i < COMPONENTS; i++) {
  4382. decode[i] = data_key[i];
  4383. decode_next[i] = data_key[i];
  4384. }
  4385. double next_time = packet_time;
  4386. if (p_time > packet_time) { // If its equal or less, then don't bother
  4387. if (data_count > 1) {
  4388. //decode forward
  4389. uint32_t bit_width[COMPONENTS];
  4390. for (uint32_t i = 0; i < COMPONENTS; i++) {
  4391. bit_width[i] = (data_key[COMPONENTS] >> (i * 4)) & 0xF;
  4392. }
  4393. uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1;
  4394. AnimationCompressionBufferBitsRead buffer;
  4395. buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1];
  4396. for (uint32_t i = 1; i < data_count; i++) {
  4397. uint32_t frame_delta = buffer.read(frame_bit_width);
  4398. base_frame += frame_delta;
  4399. for (uint32_t j = 0; j < COMPONENTS; j++) {
  4400. if (bit_width[j] == 0) {
  4401. continue; // do none
  4402. }
  4403. uint32_t valueu = buffer.read(bit_width[j] + 1);
  4404. bool sign = valueu & (1 << bit_width[j]);
  4405. int16_t value = valueu & ((1 << bit_width[j]) - 1);
  4406. if (sign) {
  4407. value = -value - 1;
  4408. }
  4409. decode_next[j] += value;
  4410. }
  4411. next_time = double(base_frame) * frame_to_sec + page_base_time;
  4412. if (p_time < next_time) {
  4413. break;
  4414. }
  4415. packet_time = next_time;
  4416. for (uint32_t j = 0; j < COMPONENTS; j++) {
  4417. decode[j] = decode_next[j];
  4418. }
  4419. if (key_index) {
  4420. (*key_index)++;
  4421. }
  4422. }
  4423. }
  4424. if (p_time > next_time) { // > instead of >= because if its equal, then it will be properly interpolated anyway
  4425. // So, the last frame found still has a time that is less than the required frame,
  4426. // will have to interpolate with the first frame of the next timekey.
  4427. if ((uint32_t)packet_idx < time_key_count - 1) { // Safety check but should not matter much, otherwise current next packet is last packet.
  4428. uint16_t time_key_data_next = time_keys[(packet_idx + 1) * 2 + 1];
  4429. uint32_t data_offset_next = (time_key_data_next & 0xFFF) * 4; // Lower 12 bits
  4430. const uint16_t *data_key_next = (const uint16_t *)(data_keys_base + data_offset_next);
  4431. base_frame = time_keys[(packet_idx + 1) * 2 + 0];
  4432. next_time = double(base_frame) * frame_to_sec + page_base_time;
  4433. for (uint32_t i = 0; i < COMPONENTS; i++) {
  4434. decode_next[i] = data_key_next[i];
  4435. }
  4436. }
  4437. }
  4438. }
  4439. r_current_time = packet_time;
  4440. r_next_time = next_time;
  4441. for (uint32_t i = 0; i < COMPONENTS; i++) {
  4442. r_current_value[i] = decode[i];
  4443. r_next_value[i] = decode_next[i];
  4444. }
  4445. return true;
  4446. }
  4447. template <uint32_t COMPONENTS>
  4448. void Animation::_get_compressed_key_indices_in_range(uint32_t p_compressed_track, double p_time, double p_delta, List<int> *r_indices) const {
  4449. ERR_FAIL_COND(!compression.enabled);
  4450. ERR_FAIL_UNSIGNED_INDEX(p_compressed_track, compression.bounds.size());
  4451. double frame_to_sec = 1.0 / double(compression.fps);
  4452. uint32_t key_index = 0;
  4453. for (uint32_t p = 0; p < compression.pages.size(); p++) {
  4454. if (compression.pages[p].time_offset >= p_time + p_delta) {
  4455. // Page beyond range
  4456. return;
  4457. }
  4458. // Page within range
  4459. uint32_t page_index = p;
  4460. double page_base_time = compression.pages[page_index].time_offset;
  4461. const uint8_t *page_data = compression.pages[page_index].data.ptr();
  4462. // Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported.
  4463. const uint32_t *indices = (const uint32_t *)page_data;
  4464. const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]];
  4465. uint32_t time_key_count = indices[p_compressed_track * 3 + 1];
  4466. for (uint32_t i = 0; i < time_key_count; i++) {
  4467. uint32_t f = time_keys[i * 2 + 0];
  4468. double frame_time = f * frame_to_sec + page_base_time;
  4469. if (frame_time >= p_time + p_delta) {
  4470. return;
  4471. } else if (frame_time >= p_time) {
  4472. r_indices->push_back(key_index);
  4473. }
  4474. key_index++;
  4475. const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]];
  4476. uint16_t time_key_data = time_keys[i * 2 + 1];
  4477. uint32_t data_offset = (time_key_data & 0xFFF) * 4; // lower 12 bits
  4478. uint32_t data_count = (time_key_data >> 12) + 1;
  4479. const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset);
  4480. if (data_count > 1) {
  4481. //decode forward
  4482. uint32_t bit_width[COMPONENTS];
  4483. for (uint32_t j = 0; j < COMPONENTS; j++) {
  4484. bit_width[j] = (data_key[COMPONENTS] >> (j * 4)) & 0xF;
  4485. }
  4486. uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1;
  4487. AnimationCompressionBufferBitsRead buffer;
  4488. buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1];
  4489. for (uint32_t j = 1; j < data_count; j++) {
  4490. uint32_t frame_delta = buffer.read(frame_bit_width);
  4491. f += frame_delta;
  4492. frame_time = f * frame_to_sec + page_base_time;
  4493. if (frame_time >= p_time + p_delta) {
  4494. return;
  4495. } else if (frame_time >= p_time) {
  4496. r_indices->push_back(key_index);
  4497. }
  4498. for (uint32_t k = 0; k < COMPONENTS; k++) {
  4499. if (bit_width[k] == 0) {
  4500. continue; // do none
  4501. }
  4502. buffer.read(bit_width[k] + 1); // skip
  4503. }
  4504. key_index++;
  4505. }
  4506. }
  4507. }
  4508. }
  4509. }
  4510. int Animation::_get_compressed_key_count(uint32_t p_compressed_track) const {
  4511. ERR_FAIL_COND_V(!compression.enabled, -1);
  4512. ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), -1);
  4513. int key_count = 0;
  4514. for (const Compression::Page &page : compression.pages) {
  4515. const uint8_t *page_data = page.data.ptr();
  4516. // Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported.
  4517. const uint32_t *indices = (const uint32_t *)page_data;
  4518. const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]];
  4519. uint32_t time_key_count = indices[p_compressed_track * 3 + 1];
  4520. for (uint32_t j = 0; j < time_key_count; j++) {
  4521. key_count += (time_keys[j * 2 + 1] >> 12) + 1;
  4522. }
  4523. }
  4524. return key_count;
  4525. }
  4526. Quaternion Animation::_uncompress_quaternion(const Vector3i &p_value) const {
  4527. Vector3 axis = Vector3::octahedron_decode(Vector2(float(p_value.x) / 65535.0, float(p_value.y) / 65535.0));
  4528. float angle = (float(p_value.z) / 65535.0) * 2.0 * Math_PI;
  4529. return Quaternion(axis, angle);
  4530. }
  4531. Vector3 Animation::_uncompress_pos_scale(uint32_t p_compressed_track, const Vector3i &p_value) const {
  4532. Vector3 pos_norm(float(p_value.x) / 65535.0, float(p_value.y) / 65535.0, float(p_value.z) / 65535.0);
  4533. return compression.bounds[p_compressed_track].position + pos_norm * compression.bounds[p_compressed_track].size;
  4534. }
  4535. float Animation::_uncompress_blend_shape(const Vector3i &p_value) const {
  4536. float bsn = float(p_value.x) / 65535.0;
  4537. return (bsn * 2.0 - 1.0) * float(Compression::BLEND_SHAPE_RANGE);
  4538. }
  4539. template <uint32_t COMPONENTS>
  4540. bool Animation::_fetch_compressed_by_index(uint32_t p_compressed_track, int p_index, Vector3i &r_value, double &r_time) const {
  4541. ERR_FAIL_COND_V(!compression.enabled, false);
  4542. ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), false);
  4543. for (const Compression::Page &page : compression.pages) {
  4544. const uint8_t *page_data = page.data.ptr();
  4545. // Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported.
  4546. const uint32_t *indices = (const uint32_t *)page_data;
  4547. const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]];
  4548. uint32_t time_key_count = indices[p_compressed_track * 3 + 1];
  4549. const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]];
  4550. for (uint32_t j = 0; j < time_key_count; j++) {
  4551. uint32_t subkeys = (time_keys[j * 2 + 1] >> 12) + 1;
  4552. if ((uint32_t)p_index < subkeys) {
  4553. uint16_t data_offset = (time_keys[j * 2 + 1] & 0xFFF) * 4;
  4554. const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset);
  4555. uint16_t frame = time_keys[j * 2 + 0];
  4556. uint16_t decode[COMPONENTS];
  4557. for (uint32_t k = 0; k < COMPONENTS; k++) {
  4558. decode[k] = data_key[k];
  4559. }
  4560. if (p_index > 0) {
  4561. uint32_t bit_width[COMPONENTS];
  4562. for (uint32_t k = 0; k < COMPONENTS; k++) {
  4563. bit_width[k] = (data_key[COMPONENTS] >> (k * 4)) & 0xF;
  4564. }
  4565. uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1;
  4566. AnimationCompressionBufferBitsRead buffer;
  4567. buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1];
  4568. for (int k = 0; k < p_index; k++) {
  4569. uint32_t frame_delta = buffer.read(frame_bit_width);
  4570. frame += frame_delta;
  4571. for (uint32_t l = 0; l < COMPONENTS; l++) {
  4572. if (bit_width[l] == 0) {
  4573. continue; // do none
  4574. }
  4575. uint32_t valueu = buffer.read(bit_width[l] + 1);
  4576. bool sign = valueu & (1 << bit_width[l]);
  4577. int16_t value = valueu & ((1 << bit_width[l]) - 1);
  4578. if (sign) {
  4579. value = -value - 1;
  4580. }
  4581. decode[l] += value;
  4582. }
  4583. }
  4584. }
  4585. r_time = page.time_offset + double(frame) / double(compression.fps);
  4586. for (uint32_t l = 0; l < COMPONENTS; l++) {
  4587. r_value[l] = decode[l];
  4588. }
  4589. return true;
  4590. } else {
  4591. p_index -= subkeys;
  4592. }
  4593. }
  4594. }
  4595. return false;
  4596. }
  4597. // Helper math functions for Variant.
  4598. bool Animation::is_variant_interpolatable(const Variant p_value) {
  4599. Variant::Type type = p_value.get_type();
  4600. return (type >= Variant::BOOL && type <= Variant::STRING_NAME) || type == Variant::ARRAY || type >= Variant::PACKED_INT32_ARRAY; // PackedByteArray is unsigned, so it would be better to ignore since blending uses float.
  4601. }
  4602. Variant Animation::cast_to_blendwise(const Variant p_value) {
  4603. switch (p_value.get_type()) {
  4604. case Variant::BOOL:
  4605. case Variant::INT: {
  4606. return p_value.operator double();
  4607. } break;
  4608. case Variant::STRING:
  4609. case Variant::STRING_NAME: {
  4610. return string_to_array(p_value);
  4611. } break;
  4612. case Variant::RECT2I: {
  4613. return p_value.operator Rect2();
  4614. } break;
  4615. case Variant::VECTOR2I: {
  4616. return p_value.operator Vector2();
  4617. } break;
  4618. case Variant::VECTOR3I: {
  4619. return p_value.operator Vector3();
  4620. } break;
  4621. case Variant::VECTOR4I: {
  4622. return p_value.operator Vector4();
  4623. } break;
  4624. case Variant::PACKED_INT32_ARRAY: {
  4625. return p_value.operator PackedFloat32Array();
  4626. } break;
  4627. case Variant::PACKED_INT64_ARRAY: {
  4628. return p_value.operator PackedFloat64Array();
  4629. } break;
  4630. default: {
  4631. } break;
  4632. }
  4633. return p_value;
  4634. }
  4635. Variant Animation::cast_from_blendwise(const Variant p_value, const Variant::Type p_type) {
  4636. switch (p_type) {
  4637. case Variant::BOOL: {
  4638. return p_value.operator real_t() >= 0.5;
  4639. } break;
  4640. case Variant::INT: {
  4641. return (int64_t)Math::round(p_value.operator double());
  4642. } break;
  4643. case Variant::STRING: {
  4644. return array_to_string(p_value);
  4645. } break;
  4646. case Variant::STRING_NAME: {
  4647. return StringName(array_to_string(p_value));
  4648. } break;
  4649. case Variant::RECT2I: {
  4650. return Rect2i(p_value.operator Rect2().round());
  4651. } break;
  4652. case Variant::VECTOR2I: {
  4653. return Vector2i(p_value.operator Vector2().round());
  4654. } break;
  4655. case Variant::VECTOR3I: {
  4656. return Vector3i(p_value.operator Vector3().round());
  4657. } break;
  4658. case Variant::VECTOR4I: {
  4659. return Vector4i(p_value.operator Vector4().round());
  4660. } break;
  4661. case Variant::PACKED_INT32_ARRAY: {
  4662. PackedFloat32Array old_val = p_value.operator PackedFloat32Array();
  4663. PackedInt32Array new_val;
  4664. new_val.resize(old_val.size());
  4665. int *new_val_w = new_val.ptrw();
  4666. for (int i = 0; i < old_val.size(); i++) {
  4667. new_val_w[i] = (int32_t)Math::round(old_val[i]);
  4668. }
  4669. return new_val;
  4670. } break;
  4671. case Variant::PACKED_INT64_ARRAY: {
  4672. PackedFloat64Array old_val = p_value.operator PackedFloat64Array();
  4673. PackedInt64Array new_val;
  4674. for (int i = 0; i < old_val.size(); i++) {
  4675. new_val.push_back((int64_t)Math::round(old_val[i]));
  4676. }
  4677. return new_val;
  4678. } break;
  4679. default: {
  4680. } break;
  4681. }
  4682. return p_value;
  4683. }
  4684. Variant Animation::string_to_array(const Variant p_value) {
  4685. if (!p_value.is_string()) {
  4686. return p_value;
  4687. };
  4688. const String &str = p_value.operator String();
  4689. PackedFloat32Array arr;
  4690. for (int i = 0; i < str.length(); i++) {
  4691. arr.push_back((float)str[i]);
  4692. }
  4693. return arr;
  4694. }
  4695. Variant Animation::array_to_string(const Variant p_value) {
  4696. if (!p_value.is_array()) {
  4697. return p_value;
  4698. };
  4699. const PackedFloat32Array &arr = p_value.operator PackedFloat32Array();
  4700. String str;
  4701. for (int i = 0; i < arr.size(); i++) {
  4702. char32_t c = (char32_t)Math::round(arr[i]);
  4703. if (c == 0 || (c & 0xfffff800) == 0xd800 || c > 0x10ffff) {
  4704. c = ' ';
  4705. }
  4706. str += c;
  4707. }
  4708. return str;
  4709. }
  4710. Variant Animation::add_variant(const Variant &a, const Variant &b) {
  4711. if (a.get_type() != b.get_type()) {
  4712. if (a.is_num() && b.is_num()) {
  4713. return add_variant(cast_to_blendwise(a), cast_to_blendwise(b));
  4714. } else if (!a.is_array()) {
  4715. return a;
  4716. }
  4717. }
  4718. switch (a.get_type()) {
  4719. case Variant::NIL: {
  4720. return Variant();
  4721. } break;
  4722. case Variant::FLOAT: {
  4723. return (a.operator double()) + (b.operator double());
  4724. } break;
  4725. case Variant::RECT2: {
  4726. const Rect2 ra = a.operator Rect2();
  4727. const Rect2 rb = b.operator Rect2();
  4728. return Rect2(ra.position + rb.position, ra.size + rb.size);
  4729. } break;
  4730. case Variant::PLANE: {
  4731. const Plane pa = a.operator Plane();
  4732. const Plane pb = b.operator Plane();
  4733. return Plane(pa.normal + pb.normal, pa.d + pb.d);
  4734. } break;
  4735. case Variant::AABB: {
  4736. const ::AABB aa = a.operator ::AABB();
  4737. const ::AABB ab = b.operator ::AABB();
  4738. return ::AABB(aa.position + ab.position, aa.size + ab.size);
  4739. } break;
  4740. case Variant::BASIS: {
  4741. return (a.operator Basis()) * (b.operator Basis());
  4742. } break;
  4743. case Variant::QUATERNION: {
  4744. return (a.operator Quaternion()) * (b.operator Quaternion());
  4745. } break;
  4746. case Variant::TRANSFORM2D: {
  4747. return (a.operator Transform2D()) * (b.operator Transform2D());
  4748. } break;
  4749. case Variant::TRANSFORM3D: {
  4750. return (a.operator Transform3D()) * (b.operator Transform3D());
  4751. } break;
  4752. case Variant::INT:
  4753. case Variant::RECT2I:
  4754. case Variant::VECTOR2I:
  4755. case Variant::VECTOR3I:
  4756. case Variant::VECTOR4I:
  4757. case Variant::PACKED_INT32_ARRAY:
  4758. case Variant::PACKED_INT64_ARRAY: {
  4759. // Fallback the interpolatable value which needs casting.
  4760. return cast_from_blendwise(add_variant(cast_to_blendwise(a), cast_to_blendwise(b)), a.get_type());
  4761. } break;
  4762. case Variant::BOOL:
  4763. case Variant::STRING:
  4764. case Variant::STRING_NAME: {
  4765. // Specialized for Tween.
  4766. return b;
  4767. } break;
  4768. case Variant::PACKED_BYTE_ARRAY: {
  4769. // Skip.
  4770. } break;
  4771. default: {
  4772. if (a.is_array()) {
  4773. const Array arr_a = a.operator Array();
  4774. const Array arr_b = b.operator Array();
  4775. int min_size = arr_a.size();
  4776. int max_size = arr_b.size();
  4777. bool is_a_larger = inform_variant_array(min_size, max_size);
  4778. Array result;
  4779. result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
  4780. result.resize(min_size);
  4781. int i = 0;
  4782. for (; i < min_size; i++) {
  4783. result[i] = add_variant(arr_a[i], arr_b[i]);
  4784. }
  4785. if (min_size != max_size) {
  4786. // Process with last element of the lesser array.
  4787. // This is pretty funny and bizarre, but artists like to use it for polygon animation.
  4788. Variant lesser_last;
  4789. result.resize(max_size);
  4790. if (is_a_larger) {
  4791. if (i > 0) {
  4792. lesser_last = arr_b[i - 1];
  4793. } else {
  4794. Variant vz = arr_a[i];
  4795. vz.zero();
  4796. lesser_last = vz;
  4797. }
  4798. for (; i < max_size; i++) {
  4799. result[i] = add_variant(arr_a[i], lesser_last);
  4800. }
  4801. } else {
  4802. if (i > 0) {
  4803. lesser_last = arr_a[i - 1];
  4804. } else {
  4805. Variant vz = arr_b[i];
  4806. vz.zero();
  4807. lesser_last = vz;
  4808. }
  4809. for (; i < max_size; i++) {
  4810. result[i] = add_variant(lesser_last, arr_b[i]);
  4811. }
  4812. }
  4813. }
  4814. return result;
  4815. }
  4816. } break;
  4817. }
  4818. return Variant::evaluate(Variant::OP_ADD, a, b);
  4819. }
  4820. Variant Animation::subtract_variant(const Variant &a, const Variant &b) {
  4821. if (a.get_type() != b.get_type()) {
  4822. if (a.is_num() && b.is_num()) {
  4823. return subtract_variant(cast_to_blendwise(a), cast_to_blendwise(b));
  4824. } else if (!a.is_array()) {
  4825. return a;
  4826. }
  4827. }
  4828. switch (a.get_type()) {
  4829. case Variant::NIL: {
  4830. return Variant();
  4831. } break;
  4832. case Variant::FLOAT: {
  4833. return (a.operator double()) - (b.operator double());
  4834. } break;
  4835. case Variant::RECT2: {
  4836. const Rect2 ra = a.operator Rect2();
  4837. const Rect2 rb = b.operator Rect2();
  4838. return Rect2(ra.position - rb.position, ra.size - rb.size);
  4839. } break;
  4840. case Variant::PLANE: {
  4841. const Plane pa = a.operator Plane();
  4842. const Plane pb = b.operator Plane();
  4843. return Plane(pa.normal - pb.normal, pa.d - pb.d);
  4844. } break;
  4845. case Variant::AABB: {
  4846. const ::AABB aa = a.operator ::AABB();
  4847. const ::AABB ab = b.operator ::AABB();
  4848. return ::AABB(aa.position - ab.position, aa.size - ab.size);
  4849. } break;
  4850. case Variant::BASIS: {
  4851. return (b.operator Basis()).inverse() * (a.operator Basis());
  4852. } break;
  4853. case Variant::QUATERNION: {
  4854. return (b.operator Quaternion()).inverse() * (a.operator Quaternion());
  4855. } break;
  4856. case Variant::TRANSFORM2D: {
  4857. return (b.operator Transform2D()).affine_inverse() * (a.operator Transform2D());
  4858. } break;
  4859. case Variant::TRANSFORM3D: {
  4860. return (b.operator Transform3D()).affine_inverse() * (a.operator Transform3D());
  4861. } break;
  4862. case Variant::INT:
  4863. case Variant::RECT2I:
  4864. case Variant::VECTOR2I:
  4865. case Variant::VECTOR3I:
  4866. case Variant::VECTOR4I:
  4867. case Variant::PACKED_INT32_ARRAY:
  4868. case Variant::PACKED_INT64_ARRAY: {
  4869. // Fallback the interpolatable value which needs casting.
  4870. return cast_from_blendwise(subtract_variant(cast_to_blendwise(a), cast_to_blendwise(b)), a.get_type());
  4871. } break;
  4872. case Variant::BOOL:
  4873. case Variant::STRING:
  4874. case Variant::STRING_NAME: {
  4875. // Specialized for Tween.
  4876. return a;
  4877. } break;
  4878. case Variant::PACKED_BYTE_ARRAY: {
  4879. // Skip.
  4880. } break;
  4881. default: {
  4882. if (a.is_array()) {
  4883. const Array arr_a = a.operator Array();
  4884. const Array arr_b = b.operator Array();
  4885. int min_size = arr_a.size();
  4886. int max_size = arr_b.size();
  4887. bool is_a_larger = inform_variant_array(min_size, max_size);
  4888. Array result;
  4889. result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
  4890. result.resize(min_size);
  4891. int i = 0;
  4892. for (; i < min_size; i++) {
  4893. result[i] = subtract_variant(arr_a[i], arr_b[i]);
  4894. }
  4895. if (min_size != max_size) {
  4896. // Process with last element of the lesser array.
  4897. // This is pretty funny and bizarre, but artists like to use it for polygon animation.
  4898. Variant lesser_last;
  4899. result.resize(max_size);
  4900. if (is_a_larger) {
  4901. if (i > 0) {
  4902. lesser_last = arr_b[i - 1];
  4903. } else {
  4904. Variant vz = arr_a[i];
  4905. vz.zero();
  4906. lesser_last = vz;
  4907. }
  4908. for (; i < max_size; i++) {
  4909. result[i] = subtract_variant(arr_a[i], lesser_last);
  4910. }
  4911. } else {
  4912. if (i > 0) {
  4913. lesser_last = arr_a[i - 1];
  4914. } else {
  4915. Variant vz = arr_b[i];
  4916. vz.zero();
  4917. lesser_last = vz;
  4918. }
  4919. for (; i < max_size; i++) {
  4920. result[i] = subtract_variant(lesser_last, arr_b[i]);
  4921. }
  4922. }
  4923. }
  4924. return result;
  4925. }
  4926. } break;
  4927. }
  4928. return Variant::evaluate(Variant::OP_SUBTRACT, a, b);
  4929. }
  4930. Variant Animation::blend_variant(const Variant &a, const Variant &b, float c) {
  4931. if (a.get_type() != b.get_type()) {
  4932. if (a.is_num() && b.is_num()) {
  4933. return blend_variant(cast_to_blendwise(a), cast_to_blendwise(b), c);
  4934. } else if (!a.is_array()) {
  4935. return a;
  4936. }
  4937. }
  4938. switch (a.get_type()) {
  4939. case Variant::NIL: {
  4940. return Variant();
  4941. } break;
  4942. case Variant::FLOAT: {
  4943. return (a.operator double()) + (b.operator double()) * c;
  4944. } break;
  4945. case Variant::VECTOR2: {
  4946. return (a.operator Vector2()) + (b.operator Vector2()) * c;
  4947. } break;
  4948. case Variant::RECT2: {
  4949. const Rect2 ra = a.operator Rect2();
  4950. const Rect2 rb = b.operator Rect2();
  4951. return Rect2(ra.position + rb.position * c, ra.size + rb.size * c);
  4952. } break;
  4953. case Variant::VECTOR3: {
  4954. return (a.operator Vector3()) + (b.operator Vector3()) * c;
  4955. } break;
  4956. case Variant::VECTOR4: {
  4957. return (a.operator Vector4()) + (b.operator Vector4()) * c;
  4958. } break;
  4959. case Variant::PLANE: {
  4960. const Plane pa = a.operator Plane();
  4961. const Plane pb = b.operator Plane();
  4962. return Plane(pa.normal + pb.normal * c, pa.d + pb.d * c);
  4963. } break;
  4964. case Variant::COLOR: {
  4965. return (a.operator Color()) + (b.operator Color()) * c;
  4966. } break;
  4967. case Variant::AABB: {
  4968. const ::AABB aa = a.operator ::AABB();
  4969. const ::AABB ab = b.operator ::AABB();
  4970. return ::AABB(aa.position + ab.position * c, aa.size + ab.size * c);
  4971. } break;
  4972. case Variant::BASIS: {
  4973. return (a.operator Basis()) + (b.operator Basis()) * c;
  4974. } break;
  4975. case Variant::QUATERNION: {
  4976. return (a.operator Quaternion()) * Quaternion().slerp((b.operator Quaternion()), c);
  4977. } break;
  4978. case Variant::TRANSFORM2D: {
  4979. return (a.operator Transform2D()) * Transform2D().interpolate_with((b.operator Transform2D()), c);
  4980. } break;
  4981. case Variant::TRANSFORM3D: {
  4982. return (a.operator Transform3D()) * Transform3D().interpolate_with((b.operator Transform3D()), c);
  4983. } break;
  4984. case Variant::BOOL:
  4985. case Variant::INT:
  4986. case Variant::RECT2I:
  4987. case Variant::VECTOR2I:
  4988. case Variant::VECTOR3I:
  4989. case Variant::VECTOR4I:
  4990. case Variant::PACKED_INT32_ARRAY:
  4991. case Variant::PACKED_INT64_ARRAY: {
  4992. // Fallback the interpolatable value which needs casting.
  4993. return cast_from_blendwise(blend_variant(cast_to_blendwise(a), cast_to_blendwise(b), c), a.get_type());
  4994. } break;
  4995. case Variant::STRING:
  4996. case Variant::STRING_NAME: {
  4997. Array arr_a = cast_to_blendwise(a);
  4998. Array arr_b = cast_to_blendwise(b);
  4999. int min_size = arr_a.size();
  5000. int max_size = arr_b.size();
  5001. bool is_a_larger = inform_variant_array(min_size, max_size);
  5002. int mid_size = interpolate_variant(arr_a.size(), arr_b.size(), c);
  5003. if (is_a_larger) {
  5004. arr_a.resize(mid_size);
  5005. } else {
  5006. arr_b.resize(mid_size);
  5007. }
  5008. return cast_from_blendwise(blend_variant(arr_a, arr_b, c), a.get_type());
  5009. } break;
  5010. case Variant::PACKED_BYTE_ARRAY: {
  5011. // Skip.
  5012. } break;
  5013. default: {
  5014. if (a.is_array()) {
  5015. const Array arr_a = a.operator Array();
  5016. const Array arr_b = b.operator Array();
  5017. int min_size = arr_a.size();
  5018. int max_size = arr_b.size();
  5019. bool is_a_larger = inform_variant_array(min_size, max_size);
  5020. Array result;
  5021. result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
  5022. result.resize(min_size);
  5023. int i = 0;
  5024. for (; i < min_size; i++) {
  5025. result[i] = blend_variant(arr_a[i], arr_b[i], c);
  5026. }
  5027. if (min_size != max_size) {
  5028. // Process with last element of the lesser array.
  5029. // This is pretty funny and bizarre, but artists like to use it for polygon animation.
  5030. Variant lesser_last;
  5031. if (is_a_larger && !Math::is_equal_approx(c, 1.0f)) {
  5032. result.resize(max_size);
  5033. if (i > 0) {
  5034. lesser_last = arr_b[i - 1];
  5035. } else {
  5036. Variant vz = arr_a[i];
  5037. vz.zero();
  5038. lesser_last = vz;
  5039. }
  5040. for (; i < max_size; i++) {
  5041. result[i] = blend_variant(arr_a[i], lesser_last, c);
  5042. }
  5043. } else if (!is_a_larger && !Math::is_zero_approx(c)) {
  5044. result.resize(max_size);
  5045. if (i > 0) {
  5046. lesser_last = arr_a[i - 1];
  5047. } else {
  5048. Variant vz = arr_b[i];
  5049. vz.zero();
  5050. lesser_last = vz;
  5051. }
  5052. for (; i < max_size; i++) {
  5053. result[i] = blend_variant(lesser_last, arr_b[i], c);
  5054. }
  5055. }
  5056. }
  5057. return result;
  5058. }
  5059. } break;
  5060. }
  5061. return c < 0.5 ? a : b;
  5062. }
  5063. Variant Animation::interpolate_variant(const Variant &a, const Variant &b, float c, bool p_snap_array_element) {
  5064. if (a.get_type() != b.get_type()) {
  5065. if (a.is_num() && b.is_num()) {
  5066. return interpolate_variant(cast_to_blendwise(a), cast_to_blendwise(b), c);
  5067. } else if (!a.is_array()) {
  5068. return a;
  5069. }
  5070. }
  5071. switch (a.get_type()) {
  5072. case Variant::NIL: {
  5073. return Variant();
  5074. } break;
  5075. case Variant::FLOAT: {
  5076. return Math::lerp(a.operator double(), b.operator double(), (double)c);
  5077. } break;
  5078. case Variant::VECTOR2: {
  5079. return (a.operator Vector2()).lerp(b.operator Vector2(), c);
  5080. } break;
  5081. case Variant::RECT2: {
  5082. const Rect2 ra = a.operator Rect2();
  5083. const Rect2 rb = b.operator Rect2();
  5084. return Rect2(ra.position.lerp(rb.position, c), ra.size.lerp(rb.size, c));
  5085. } break;
  5086. case Variant::VECTOR3: {
  5087. return (a.operator Vector3()).lerp(b.operator Vector3(), c);
  5088. } break;
  5089. case Variant::VECTOR4: {
  5090. return (a.operator Vector4()).lerp(b.operator Vector4(), c);
  5091. } break;
  5092. case Variant::PLANE: {
  5093. const Plane pa = a.operator Plane();
  5094. const Plane pb = b.operator Plane();
  5095. return Plane(pa.normal.lerp(pb.normal, c), Math::lerp((double)pa.d, (double)pb.d, (double)c));
  5096. } break;
  5097. case Variant::COLOR: {
  5098. return (a.operator Color()).lerp(b.operator Color(), c);
  5099. } break;
  5100. case Variant::AABB: {
  5101. const ::AABB aa = a.operator ::AABB();
  5102. const ::AABB ab = b.operator ::AABB();
  5103. return ::AABB(aa.position.lerp(ab.position, c), aa.size.lerp(ab.size, c));
  5104. } break;
  5105. case Variant::BASIS: {
  5106. return (a.operator Basis()).lerp(b.operator Basis(), c);
  5107. } break;
  5108. case Variant::QUATERNION: {
  5109. return (a.operator Quaternion()).slerp(b.operator Quaternion(), c);
  5110. } break;
  5111. case Variant::TRANSFORM2D: {
  5112. return (a.operator Transform2D()).interpolate_with(b.operator Transform2D(), c);
  5113. } break;
  5114. case Variant::TRANSFORM3D: {
  5115. return (a.operator Transform3D()).interpolate_with(b.operator Transform3D(), c);
  5116. } break;
  5117. case Variant::BOOL:
  5118. case Variant::INT:
  5119. case Variant::RECT2I:
  5120. case Variant::VECTOR2I:
  5121. case Variant::VECTOR3I:
  5122. case Variant::VECTOR4I:
  5123. case Variant::PACKED_INT32_ARRAY:
  5124. case Variant::PACKED_INT64_ARRAY: {
  5125. // Fallback the interpolatable value which needs casting.
  5126. return cast_from_blendwise(interpolate_variant(cast_to_blendwise(a), cast_to_blendwise(b), c), a.get_type());
  5127. } break;
  5128. case Variant::STRING:
  5129. case Variant::STRING_NAME: {
  5130. Array arr_a = cast_to_blendwise(a);
  5131. Array arr_b = cast_to_blendwise(b);
  5132. int min_size = arr_a.size();
  5133. int max_size = arr_b.size();
  5134. bool is_a_larger = inform_variant_array(min_size, max_size);
  5135. int mid_size = interpolate_variant(arr_a.size(), arr_b.size(), c);
  5136. if (is_a_larger) {
  5137. arr_a.resize(mid_size);
  5138. } else {
  5139. arr_b.resize(mid_size);
  5140. }
  5141. return cast_from_blendwise(interpolate_variant(arr_a, arr_b, c, true), a.get_type());
  5142. } break;
  5143. case Variant::PACKED_BYTE_ARRAY: {
  5144. // Skip.
  5145. } break;
  5146. default: {
  5147. if (a.is_array()) {
  5148. const Array arr_a = a.operator Array();
  5149. const Array arr_b = b.operator Array();
  5150. int min_size = arr_a.size();
  5151. int max_size = arr_b.size();
  5152. bool is_a_larger = inform_variant_array(min_size, max_size);
  5153. Array result;
  5154. result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
  5155. result.resize(min_size);
  5156. int i = 0;
  5157. for (; i < min_size; i++) {
  5158. result[i] = interpolate_variant(arr_a[i], arr_b[i], c);
  5159. }
  5160. if (min_size != max_size) {
  5161. // Process with last element of the lesser array.
  5162. // This is pretty funny and bizarre, but artists like to use it for polygon animation.
  5163. Variant lesser_last;
  5164. if (is_a_larger && !Math::is_equal_approx(c, 1.0f)) {
  5165. result.resize(max_size);
  5166. if (p_snap_array_element) {
  5167. c = 0;
  5168. }
  5169. if (i > 0) {
  5170. lesser_last = arr_b[i - 1];
  5171. } else {
  5172. Variant vz = arr_a[i];
  5173. vz.zero();
  5174. lesser_last = vz;
  5175. }
  5176. for (; i < max_size; i++) {
  5177. result[i] = interpolate_variant(arr_a[i], lesser_last, c);
  5178. }
  5179. } else if (!is_a_larger && !Math::is_zero_approx(c)) {
  5180. result.resize(max_size);
  5181. if (p_snap_array_element) {
  5182. c = 1;
  5183. }
  5184. if (i > 0) {
  5185. lesser_last = arr_a[i - 1];
  5186. } else {
  5187. Variant vz = arr_b[i];
  5188. vz.zero();
  5189. lesser_last = vz;
  5190. }
  5191. for (; i < max_size; i++) {
  5192. result[i] = interpolate_variant(lesser_last, arr_b[i], c);
  5193. }
  5194. }
  5195. }
  5196. return result;
  5197. }
  5198. } break;
  5199. }
  5200. return c < 0.5 ? a : b;
  5201. }
  5202. Variant Animation::cubic_interpolate_in_time_variant(const Variant &pre_a, const Variant &a, const Variant &b, const Variant &post_b, float c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t, bool p_snap_array_element) {
  5203. if (pre_a.get_type() != a.get_type() || pre_a.get_type() != b.get_type() || pre_a.get_type() != post_b.get_type()) {
  5204. if (pre_a.is_num() && a.is_num() && b.is_num() && post_b.is_num()) {
  5205. return cubic_interpolate_in_time_variant(cast_to_blendwise(pre_a), cast_to_blendwise(a), cast_to_blendwise(b), cast_to_blendwise(post_b), c, p_pre_a_t, p_b_t, p_post_b_t, p_snap_array_element);
  5206. } else if (!a.is_array()) {
  5207. return a;
  5208. }
  5209. }
  5210. switch (a.get_type()) {
  5211. case Variant::NIL: {
  5212. return Variant();
  5213. } break;
  5214. case Variant::FLOAT: {
  5215. return Math::cubic_interpolate_in_time(a.operator double(), b.operator double(), pre_a.operator double(), post_b.operator double(), (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t);
  5216. } break;
  5217. case Variant::VECTOR2: {
  5218. return (a.operator Vector2()).cubic_interpolate_in_time(b.operator Vector2(), pre_a.operator Vector2(), post_b.operator Vector2(), c, p_b_t, p_pre_a_t, p_post_b_t);
  5219. } break;
  5220. case Variant::RECT2: {
  5221. const Rect2 rpa = pre_a.operator Rect2();
  5222. const Rect2 ra = a.operator Rect2();
  5223. const Rect2 rb = b.operator Rect2();
  5224. const Rect2 rpb = post_b.operator Rect2();
  5225. return Rect2(
  5226. ra.position.cubic_interpolate_in_time(rb.position, rpa.position, rpb.position, c, p_b_t, p_pre_a_t, p_post_b_t),
  5227. ra.size.cubic_interpolate_in_time(rb.size, rpa.size, rpb.size, c, p_b_t, p_pre_a_t, p_post_b_t));
  5228. } break;
  5229. case Variant::VECTOR3: {
  5230. return (a.operator Vector3()).cubic_interpolate_in_time(b.operator Vector3(), pre_a.operator Vector3(), post_b.operator Vector3(), c, p_b_t, p_pre_a_t, p_post_b_t);
  5231. } break;
  5232. case Variant::VECTOR4: {
  5233. return (a.operator Vector4()).cubic_interpolate_in_time(b.operator Vector4(), pre_a.operator Vector4(), post_b.operator Vector4(), c, p_b_t, p_pre_a_t, p_post_b_t);
  5234. } break;
  5235. case Variant::PLANE: {
  5236. const Plane ppa = pre_a.operator Plane();
  5237. const Plane pa = a.operator Plane();
  5238. const Plane pb = b.operator Plane();
  5239. const Plane ppb = post_b.operator Plane();
  5240. return Plane(
  5241. pa.normal.cubic_interpolate_in_time(pb.normal, ppa.normal, ppb.normal, c, p_b_t, p_pre_a_t, p_post_b_t),
  5242. Math::cubic_interpolate_in_time((double)pa.d, (double)pb.d, (double)ppa.d, (double)ppb.d, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t));
  5243. } break;
  5244. case Variant::COLOR: {
  5245. const Color cpa = pre_a.operator Color();
  5246. const Color ca = a.operator Color();
  5247. const Color cb = b.operator Color();
  5248. const Color cpb = post_b.operator Color();
  5249. return Color(
  5250. Math::cubic_interpolate_in_time((double)ca.r, (double)cb.r, (double)cpa.r, (double)cpb.r, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t),
  5251. Math::cubic_interpolate_in_time((double)ca.g, (double)cb.g, (double)cpa.g, (double)cpb.g, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t),
  5252. Math::cubic_interpolate_in_time((double)ca.b, (double)cb.b, (double)cpa.b, (double)cpb.b, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t),
  5253. Math::cubic_interpolate_in_time((double)ca.a, (double)cb.a, (double)cpa.a, (double)cpb.a, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t));
  5254. } break;
  5255. case Variant::AABB: {
  5256. const ::AABB apa = pre_a.operator ::AABB();
  5257. const ::AABB aa = a.operator ::AABB();
  5258. const ::AABB ab = b.operator ::AABB();
  5259. const ::AABB apb = post_b.operator ::AABB();
  5260. return AABB(
  5261. aa.position.cubic_interpolate_in_time(ab.position, apa.position, apb.position, c, p_b_t, p_pre_a_t, p_post_b_t),
  5262. aa.size.cubic_interpolate_in_time(ab.size, apa.size, apb.size, c, p_b_t, p_pre_a_t, p_post_b_t));
  5263. } break;
  5264. case Variant::BASIS: {
  5265. const Basis bpa = pre_a.operator Basis();
  5266. const Basis ba = a.operator Basis();
  5267. const Basis bb = b.operator Basis();
  5268. const Basis bpb = post_b.operator Basis();
  5269. return Basis(
  5270. ba.rows[0].cubic_interpolate_in_time(bb.rows[0], bpa.rows[0], bpb.rows[0], c, p_b_t, p_pre_a_t, p_post_b_t),
  5271. ba.rows[1].cubic_interpolate_in_time(bb.rows[1], bpa.rows[1], bpb.rows[1], c, p_b_t, p_pre_a_t, p_post_b_t),
  5272. ba.rows[2].cubic_interpolate_in_time(bb.rows[2], bpa.rows[2], bpb.rows[2], c, p_b_t, p_pre_a_t, p_post_b_t));
  5273. } break;
  5274. case Variant::QUATERNION: {
  5275. return (a.operator Quaternion()).spherical_cubic_interpolate_in_time(b.operator Quaternion(), pre_a.operator Quaternion(), post_b.operator Quaternion(), c, p_b_t, p_pre_a_t, p_post_b_t);
  5276. } break;
  5277. case Variant::TRANSFORM2D: {
  5278. const Transform2D tpa = pre_a.operator Transform2D();
  5279. const Transform2D ta = a.operator Transform2D();
  5280. const Transform2D tb = b.operator Transform2D();
  5281. const Transform2D tpb = post_b.operator Transform2D();
  5282. // TODO: May cause unintended skew, we needs spherical_cubic_interpolate_in_time() for angle and Transform2D::cubic_interpolate_with().
  5283. return Transform2D(
  5284. ta[0].cubic_interpolate_in_time(tb[0], tpa[0], tpb[0], c, p_b_t, p_pre_a_t, p_post_b_t),
  5285. ta[1].cubic_interpolate_in_time(tb[1], tpa[1], tpb[1], c, p_b_t, p_pre_a_t, p_post_b_t),
  5286. ta[2].cubic_interpolate_in_time(tb[2], tpa[2], tpb[2], c, p_b_t, p_pre_a_t, p_post_b_t));
  5287. } break;
  5288. case Variant::TRANSFORM3D: {
  5289. const Transform3D tpa = pre_a.operator Transform3D();
  5290. const Transform3D ta = a.operator Transform3D();
  5291. const Transform3D tb = b.operator Transform3D();
  5292. const Transform3D tpb = post_b.operator Transform3D();
  5293. // TODO: May cause unintended skew, we needs Transform3D::cubic_interpolate_with().
  5294. return Transform3D(
  5295. ta.basis.rows[0].cubic_interpolate_in_time(tb.basis.rows[0], tpa.basis.rows[0], tpb.basis.rows[0], c, p_b_t, p_pre_a_t, p_post_b_t),
  5296. ta.basis.rows[1].cubic_interpolate_in_time(tb.basis.rows[1], tpa.basis.rows[1], tpb.basis.rows[1], c, p_b_t, p_pre_a_t, p_post_b_t),
  5297. ta.basis.rows[2].cubic_interpolate_in_time(tb.basis.rows[2], tpa.basis.rows[2], tpb.basis.rows[2], c, p_b_t, p_pre_a_t, p_post_b_t),
  5298. ta.origin.cubic_interpolate_in_time(tb.origin, tpa.origin, tpb.origin, c, p_b_t, p_pre_a_t, p_post_b_t));
  5299. } break;
  5300. case Variant::BOOL:
  5301. case Variant::INT:
  5302. case Variant::RECT2I:
  5303. case Variant::VECTOR2I:
  5304. case Variant::VECTOR3I:
  5305. case Variant::VECTOR4I:
  5306. case Variant::PACKED_INT32_ARRAY:
  5307. case Variant::PACKED_INT64_ARRAY: {
  5308. // Fallback the interpolatable value which needs casting.
  5309. return cast_from_blendwise(cubic_interpolate_in_time_variant(cast_to_blendwise(pre_a), cast_to_blendwise(a), cast_to_blendwise(b), cast_to_blendwise(post_b), c, p_pre_a_t, p_b_t, p_post_b_t, p_snap_array_element), a.get_type());
  5310. } break;
  5311. case Variant::STRING:
  5312. case Variant::STRING_NAME: {
  5313. // TODO:
  5314. // String interpolation works on both the character array size and the character code, to apply cubic interpolation neatly,
  5315. // we need to figure out how to interpolate well in cases where there are fewer than 4 keys. So, for now, fallback to linear interpolation.
  5316. return interpolate_variant(a, b, c);
  5317. } break;
  5318. case Variant::PACKED_BYTE_ARRAY: {
  5319. // Skip.
  5320. } break;
  5321. default: {
  5322. if (a.is_array()) {
  5323. const Array arr_pa = pre_a.operator Array();
  5324. const Array arr_a = a.operator Array();
  5325. const Array arr_b = b.operator Array();
  5326. const Array arr_pb = post_b.operator Array();
  5327. int min_size = arr_a.size();
  5328. int max_size = arr_b.size();
  5329. bool is_a_larger = inform_variant_array(min_size, max_size);
  5330. Array result;
  5331. result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
  5332. result.resize(min_size);
  5333. if (min_size == 0 && max_size == 0) {
  5334. return result;
  5335. }
  5336. Variant vz;
  5337. if (is_a_larger) {
  5338. vz = arr_a[0];
  5339. } else {
  5340. vz = arr_b[0];
  5341. }
  5342. vz.zero();
  5343. Variant pre_last = arr_pa.size() ? arr_pa[arr_pa.size() - 1] : vz;
  5344. Variant post_last = arr_pb.size() ? arr_pb[arr_pb.size() - 1] : vz;
  5345. int i = 0;
  5346. for (; i < min_size; i++) {
  5347. result[i] = cubic_interpolate_in_time_variant(i >= arr_pa.size() ? pre_last : arr_pa[i], arr_a[i], arr_b[i], i >= arr_pb.size() ? post_last : arr_pb[i], c, p_pre_a_t, p_b_t, p_post_b_t);
  5348. }
  5349. if (min_size != max_size) {
  5350. // Process with last element of the lesser array.
  5351. // This is pretty funny and bizarre, but artists like to use it for polygon animation.
  5352. Variant lesser_last = vz;
  5353. if (is_a_larger && !Math::is_equal_approx(c, 1.0f)) {
  5354. result.resize(max_size);
  5355. if (p_snap_array_element) {
  5356. c = 0;
  5357. }
  5358. if (i > 0) {
  5359. lesser_last = arr_b[i - 1];
  5360. }
  5361. for (; i < max_size; i++) {
  5362. result[i] = cubic_interpolate_in_time_variant(i >= arr_pa.size() ? pre_last : arr_pa[i], arr_a[i], lesser_last, i >= arr_pb.size() ? post_last : arr_pb[i], c, p_pre_a_t, p_b_t, p_post_b_t);
  5363. }
  5364. } else if (!is_a_larger && !Math::is_zero_approx(c)) {
  5365. result.resize(max_size);
  5366. if (p_snap_array_element) {
  5367. c = 1;
  5368. }
  5369. if (i > 0) {
  5370. lesser_last = arr_a[i - 1];
  5371. }
  5372. for (; i < max_size; i++) {
  5373. result[i] = cubic_interpolate_in_time_variant(i >= arr_pa.size() ? pre_last : arr_pa[i], lesser_last, arr_b[i], i >= arr_pb.size() ? post_last : arr_pb[i], c, p_pre_a_t, p_b_t, p_post_b_t);
  5374. }
  5375. }
  5376. }
  5377. return result;
  5378. }
  5379. } break;
  5380. }
  5381. return c < 0.5 ? a : b;
  5382. }
  5383. bool Animation::inform_variant_array(int &r_min, int &r_max) {
  5384. if (r_min <= r_max) {
  5385. return false;
  5386. }
  5387. SWAP(r_min, r_max);
  5388. return true;
  5389. }
  5390. Animation::Animation() {
  5391. }
  5392. Animation::~Animation() {
  5393. for (int i = 0; i < tracks.size(); i++) {
  5394. memdelete(tracks[i]);
  5395. }
  5396. }