animation.cpp 188 KB

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