animation.cpp 208 KB

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