animation.cpp 208 KB

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