animation.cpp 213 KB

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