animation.cpp 188 KB

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