2
0

animation.cpp 189 KB

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