animation.cpp 213 KB

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