animation.cpp 215 KB

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