animation.cpp 214 KB

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