animation.cpp 190 KB

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