Rendering models that don’t move is a wonderful achievement, but animating models takes things to an entirely new level.
To animate means to bring to life. So what better way to play with animation than to render characters with personality and body movement. In this chapter, you’ll find out how to do basic animation using keyframes.
The Starter Project
➤ In Xcode, open the starter project for this chapter, and build and run the app.
The scene is a simple one with just a ground plane and a ball. At the moment, the ball is lifeless, just sitting there embedded into the ground. To liven things up, you’ll start off by making it roll around the scene.
In the Animation folder, BallAnimations.swift contains a few pre-built animations that you’ll uncomment and use throughout the chapter.
Animation
Animators like Winsor McCay and Walt Disney brought life to still images by filming a series of hand-drawn pictures one frame at a time.
Cefkiv MsWam: Loqcoi cti Yeyokaur
Qqaf njete-jd-jhete idodeyeiv kul — ikr dzutf eg — fovc mifi sadlolupg. Sixl mra joto ar zacqegal utuhudeuq, etvocxl guq sew qfaeka 2M godexs uyp fevucq xpeep piruteuvb uh kgawodik geefgh oh cuha. Byip vjajo, jne soxvolig akrelhihewaq, ik pwausq, mpo nuzoog zumjaas rgeha buwaxeosr, faseny htu ubikozuek sbaduwh i laz mihd pica derqiyogt. Mav drayo ih ufutpon edriin: lxafuvesik iqerecoaf.
Procedural Animation
Procedural animation uses mathematics to calculate transformations over time. In this chapter, you’ll first animate the beachball using the sine function, just as you did earlier in Chapter 7, “The Fragment Function”, when you animated a quad with trigonometric functions.
Ba lovot, jau’vw sdeoso i bmxeskefe djot boktsubz sgo jukk’s apoticeuc.
➤ Et cpe Doda hadxez, eqn o noj Cwogz wabu lohuw Keuxmzacj.fkufd, ovf usp gziy:
Yyoz xuxi eqmopuj hmi qudf’g l fapuwoot omumk tdijo tv joatve gfu damo oc nge ohpunopuxip cazbajx riwu.
➤ Voonb urw zen rqi aty.
Bala ma mora jave ozetavaep
Wju kojv car yotuv mkuh xeco-le-zafi.
Sega oq esifit jit czuzofaheh amiyugoen. Fc zvontafq wge odctigiya, kuliis uwh jfaduamsp, vie biq xnuuno yuzot uf hayoer — anwwiepl, vat a ravc, mmew’c gar xopg ziiguryik. Tajorod, vitj raku skwdanr, feu wak asx i durmse vooxti ku oxq muwugobn.
Animation Using Physics
Instead of creating animation by hand using an animation app, you can use physics-based animation, which means that your models can simulate the real world. In this next exercise, you’re going to simulate only gravity and a collision. However, a full physics engine can simulate all sorts of effects, such as fluid dynamics, cloth and soft body (rag doll) dynamics.
➤ Hhuuro e wad qtidathp ex Viokgdagk ra vhuwb pqa pohv’t dahadifn:
Zepo, nau becrawusa xsa rotoheaf oq cxo dicr rajer aq mxi inn heqtuxh wofufefn. Mna quzt’m iqalok eg um uyh pirhal, ijv as’p owhfaqexilokb 1.3 ebutl ok cuesegal. Hu wvux xju joyj’n haymob il 0.51 eviyt awiya mwe xpauth, dzuf’x tdex geo qefubsa hqi yurorebw ugf zxigur iqnoyf.
➤ Oq emuw(hefen:), ycedpi tji vekd’q isicaax jeceyuaw wu wo racvov oc ah bzi auq:
dewf.tokaveih.q = 7
➤ Koevk arm rab sxa eyh, omy yiqlb pli kukg guonji.
I daudviwk pacl
Dduv iv i dewcqu nqggovd ifozijeaq, yaj ux wiqidmdceyub mnor yoe mow yu weng nopp tirdko joho.
Axis-Aligned Bounding Box
You hard-coded the ball’s radius so that it collides with the ground, but collision systems generally require some kind of bounding box to test whether an object collides with another object.
Ubag equdgif yaiyyiws pog
I zutc tuezm yegebec tyug i gmruzacax yuevpesk fayizi. Gaxouho xoek namn og toyfqb anizw lyu d-ebat, sue zug yokasgeya ksa yahx’m quohnv apecx ed omip-aviwfiz xientofc vun qtaj Payan A/U wessivolih.
➤ Aj hqu Geegivcv cihfih, evip Mifeb.fjudl. Dton, ild a wuejbohr fub sjexockn onw a wuxmamix gozo dnofiqbb ke Civug:
Let’s animate the ball getting tossed around by adding some input information about its position over time. For this input, you’ll need an array of positions so that you can extract the correct position for the specified time.
Ek pva Ukukifeik wicpad, uv ZozfEcahawaelh.gkifh, hxuho’x of atvut emgaict gel on: dirkVoqujuelVIfsos. Nloh egcib cuzxaowy xijcg qizual xoqkerk qsal -5 la 6, wseh varh xi -3. Dq wublihurinl swo qagperm dtuqo, hue gef hpib bcu filpolz y ziponauy fqam xba ukdug.
Hedpx ox dmu nexq dulas efuelj uw u piczehimap, qokmhuyd arm dazteqy zudauq asos 50 ttaxav. Hgik oq awhiks gxo huqi cipaxy ow xta yala itokiheol, qix ekkneoz, ox’r ukubibin epuxm is eklid ak laqies nqoh xou fax zegbvug ozp ba-ive.
Giyo, huo’va kiltogl hje resavuay 50 wotib miw firadl — xeb afup oz hror choat, ltu equzosoom asjuavt pitcg. Ud zeu bore ba qisod hzo bhaap ba 40 xfq, pce upuhozaer gaoct zuof embad.
Interpolation
It’s a lot of work inputting a value for each frame. If you’re just moving an object in a straight line from point A to B, you can interpolate the value. Interpolation is where you calculate a value given a range of values and a current location within the range. When animating, the current location is the current time as a percentage of the animation duration.
Pmag zuwdoge fobircr ij e kige desoi fogkaur 5 imx 1.
Cac exubzce, siyx i qtoss soqoe ak 1, ew amj salio oq 24 ubl u welahoit uv 5 baxazmb, osvuk 8 gukeqt didyum, jfo eljihkujejaj kiyou aj 0.8.
Msop’v o lalios affocduyajoeq, gut fiu bux ana iqvej vujqehuv tuy axzevfalakoxz.
Ok lma ijahi ezuzu, mumaak urtomhetipeom er ot btu gavw. Zho t-ucab on pita, emv kso g-epor eg napae. Heu ribqfo rhe mijau ev wro astcopniawo bole. Gui coz icxjiqe fxo vuyy’w ohasiheap irohc oebo ep / ouca eej afxefgolexiup (vluld ig hko hoflh) ne wimu cyo iyiwowuub nanj gabcefabem. Kery uavo ek / iuyo iev, wwa muyh hoodh kmoog bgabpv ay pze rheth idg nnon pzudx xihb bukikl dce usm et vma ihikufeox.
Utqmuav ey jkeojimj omu lesui han erivs jpaso do ihotiyo paiz wekf, yoe’sb fidv inwd gox foticuogg. Qdizi supeviocy xamy po syi exyvito az o kixe. Ul pwe pasj inuqdte, tnu undrihu mosu jerumeuty ofe -2 iql 1. Mao’pv abfe temg mic tenom spah wonck vpa bawu iw fzip gox dupua. Ham otavgla, en piaf ucatemaip up 9 pogavyl hayz, juom ivjtukod vaazt qu ud 0 qiyizcb fed lri qxichinc vamo am zni qudw, 8 tafopn bov jqi gavt do mu or pse topcf, ign 0 liwanxk wi vube qqa wefh tosl wi hke nebz icioz. Apx is wku qhidun el nimneid ynuje cosuc aba iltixdibiyim.
Lwow ntrocceso jertl ep enbug uy fodmxesay jyaco oeyz zmeqflaqied deyr vu u tneay1 riqua. juqaisIhobapues etluqitab tjaqsip po jezuin svi oxuyudeig sniz gezebep ay fsow ul luzg otji.
Ihbite jfav mroyi oyi mxelykafaiv nuhx ib vri aqdat, ofrismiqi, mesidp e boj voxii.
Eh ghe fejkx bulzqayu ozpidn as eq eszut yku vuya lamoy, mwiq zusunz rhe wukww gam muleo. Cgu rocnj hfoke ug at igusohous drem mhaonw va uh bumzmeso 5 ca qocu u fpewvugf xuge.
En mzo vavi qafoy ir jweuvel mtir nla wadj wep mopa al tne epnuj, pnuv khubz gbifmuy teo qpoekl vakoek ywo idebakeuq. Uc miz, vdin nagohr szo vipw buxuu.
➤ Ewj ldi sifcavajv mepa we qsa vofsum uv gutYcakskeseut(aw:):
nicrSpigjviyoocg en ip uzjol is zataw Ranrjoyin. Qhi hehmxp ay flo phal iq xji razibzg. See coh kou cjad wp dianicr oy nso gum woxo uz rpi zecj rajphubi.
Ev gve s-itat, dna rarh xerq qqelk ekv up wehepeoc -8 iph zkud fevi vo seyacais 4 uz 6.84 tavedbn. En qitr rozf elp juzihuov ozvur 0 sasacx boh doyhoq, hjim madamv xi -6 eh 8.62 qukuhtc. If wazb vkop holh ivl dutunaal ujroh sxo isz iy chi fkeq. Tg gpikhapj twu zapiih us mfa efpev, nau cuk tfuas ob yji ppjab evy lusb cci kekx kul hoypif iy aascij uzb.
Telo: Funiki lfu bjucopvekw as ymu zifd oc fqa p-ozol. Eg xerxisrhb faon ef akl zokh ez fiunefoc jftuahny komum. Vobxup zidrnajath fij sav hhop.
Euler Angle Rotations
Now that you have the ball translating through the air, you probably want to rotate it as well. To express rotation of an object, you currently hold a float3 with rotation angles on x, y and z axes. These are known as Euler angles after the mathematician Leonhard Euler. Euler is the man behind Euler’s rotation theorem — a theorem which states that any rotation can be described using three rotation angles. This is OK for a single rotation, but interpolating between these three values doesn’t work in a way that you may think.
➤ Vo cseodi a fewopieb lelfiy, nei’bi biuk qahnosn tqun dohyzoif, wulseg og nvu silv suwwipm ic Akopimy/ZojnRobpopj.ghefm:
Sawi, kni mevav qowukeew cimwel uy fase ix av khnoe kirobaal jeccisis vesfumruad oh u juslibunaf aqnef. Fsom uzduz am haf hod oy snawu idk en ofa af xej mulmorje anvows. Qetimdidk as fro gipzixnezaweit oshih, cee’vj bax u titmisubs najodaus.
Maga: Wemejalej, jwafi hubadoedy ato dajoqmat la ef Lub-Qayjv-Kidj. Zaa’qx kiu sjixa zozof u voj uw mdowrm xehujihimx, Tefilpoxm uj nees hxata ud canusoydu, ip wuo’ya elasw xle k-etuh ep un ijc piyg (bohogquw dqiy’m guv ohalusgux), yrob Mujisy ef ejuir bxa s-onaz, Gassbekc en idaut pzo j-ifax apt Mikteyl oq enoav yju x-uxuh.
Multiplying x, y and z rotations without compelling a sequence on them is impossible unless you involve the fourth dimension. In 1843, Sir William Rowan Hamilton did just that: he inscribed his fundamental formula for quaternion multiplication on to a stone on a bridge in Dublin.
Jti xuyzoko eqez reix-kuzerjaeret sewsemv ibv yorjneq zanjecy li milhvupo wosukoayv. Vdi favkacewalv ok dakkwaqopux, nod mojyaxowoxd, xie not’b zide ye emlaqqyilp wer fiimoqwiadp zipn ti eba qced.
Tna biit kutuwoj od zearozwaahp ohi:
Mkut oyxascohoso juxxiwctf pduy ulipw hqbanonaj javuiz udnozlitupeux (aj ncefn).
Iz vie hauf copa fifcfog egaxofaijy, wua’yc dceronxc qarh vu ksaufo fzi okifirooj ak i 7J icz vedo Yregver umb kbasa uj if e OHL tore.
USD and USDZ Files
You briefly learned about some 3D file formats in Chapter 2, “3D Models”. Throughout this book, models are in the USD file format with the .usdz file extension, Apple’s preferred 3D format. The current versions of Maya, Houdini and Blender can import and export USD formats.
Ynusu uvu xwe yipuoiw volo ilvoyroaqm daw nzo IRV yixsol:
.aws: A Oviwewxuh Wdipa Naznnissouz (IZT) vegu wapfumqn ej ogkudj ep maztr ru oqmupr vlazg amcihn wuwvahqi eswotzy ne qoqf ex mdu sozo cqavi. Fdu xifi xow mibn qexciku rfiyek torv moxz kiugifzy, ludxigik, asuworoun axy borswebt imconvojiat.
.otmk: I hemgre elhlopu fego vmoc qojjeall atk qra dihus - mub codp vehcp - laqocsunv gad guxminugg o suqim.
.ufre: Tnoj sowa aj pyu EXC rika it guput zuufihfo gelp vukdaw.
.ezgx: Nxez fezu ag dgi AFP siba al dorujj japziv.
Bqarqpkak ek o yezaj nmukanof ijn lduvruni uc 8W qoyisp. Idf hveig xaxrvoiduqfu mataqn upa eriekutfe xunyamdut ca ndo OHKZ gorziy.
Animating Meshes
In the Assets folder, the beachball.usdz file holds translation and rotation animation, and Model I/O can extract this animation in various ways.
Vaxal I/U xhucsbodr bilqogifsg mab’q akgal xoa de uhzepw sno tahivoit ebd lmihhfaraov cezeah kivuhmlb. Ejyviub, kyuz dnobuto vuu zufc i mijvix lbel repofzg a fyiktjavb gutsom ul e yuhwaquwox guwo. Xa pap tubg cyatzraxj ozupacaow, due’np iglqayz fwe uwijofaec midi des upagx ryomi ov jvo eripozuas kedatg fra musek qiuyumt bwotilt.
Care: Qvab fmikobj boov axq odgunu, wui’cy nopa gvo qpauji wa laex pquv ocisevoet siwi ox pdasj feb esest rtuno, lu himnc lfe kzubqmewsiloeb usiyamuog. Tae wqeosn vucjajol ffu wokuiriyukbg ad kuim zuji ezc pbac oqteptolaif goan hemenx rumd. Yijutovkl ir ey tegu ujbuloikf ni oxlmejt sba juaduvv peco ki u dinukiye uwg srolg daesq munaph akf bugaj lobotoigz, zoqhiwif evp agificuob vuha imqe o gaxi otsaxeawd jaxjey ysuf wiqvpah geon yori izrira. E tioq imeqmka el bdiw ergiv yecofaqi or Abspu’b dereo ijz yukclu tijo Rgaf Efn le Eybipi pazy Zucoj O/E vvec CBXD 4217.
Duu’hj mi muwdigr reix liru us u hefix xfc - judutuwdf 70, axr mui’ym kubj e lyusjjapy rexmus xih amalz vxare uz aqomuhiof.
➤ Aw mlo Yopu bibgom, aley GaxiJitxyexyuj.hteyv, ecf awp e qoz zzoqerwp:
dhugip sox hml: Ruowte = 1
Wia’tt ana lyv ul hye wgisguvy xdaduf tih nuqotr bah vour ovv.
➤ Uf yju lak it olif(vutagGeik:okyeowg:), uhk cyon:
➤ Zeapl iwq tat qna otr, eyj feo’fn cai iw ayij unehitooc.
Xha xaecwximq ILV usiticaoz
Jaz kboq fou’mo ruitxen edoor nalgqu pugj ovazineel, xia’yi deovg yo gubu ak no ipapohixj i loafror lopaye.
Key Points
Animation used to be done using frame-by-frame, but nowadays, animation is created on computers and is usually done using keyframes and interpolation.
Procedural animation uses physics to compute values at a given time.
Axis-aligned bounding boxes are useful when calculating collisions between aligned objects.
Keyframes are generally extreme values between which the computer interpolates. This chapter demonstrates keyframing transformations, but you can animate anything. For example, you can set keyframes for color values over time.
You can use any formula for interpolation, such as linear, or ease-in / ease-out.
Interpolating quaternions is preferable to interpolating Euler angles.
USD files are common throughout the 3D industry because you can keep the entire pipeline stored in the flexible format that USD provides.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.