This chapter continues from the point where the previous one left off. Thanks to SwiftUI, the AR Funny Face app now sports a very basic UI. In this chapter, you’ll continue to focus on that facial recognition component by using face anchors in RealityKit.
You’ll also get to build some funny scenes with these crazy props:
A humongous pair of sunglasses, a glass eyeball and one epic mustache. With these cool props at one’s disposal, the AR Funny Face app is off to a great start.
Note: Feel free to continue with your own final project from the previous chapter. If you skipped a few things, load the starter project from starter/ARFunnyFace/ARFunnyFace.xcodeproj before you continue.
What are face anchors?
Hanging an anchor from one’s face sounds extremely painful. Luckily, the type of anchor we’re referring to here is an AR Face Anchor — undeniably one of the coolest types of anchors available. Thanks to Reality Composer, creating face anchors is super easy.
By using the TrueDepth front-facing camera, face anchors provide information about the user’s facial position, orientation, topology and facial expression.
Unfortunately, you can only use face tracking if you have a device equipped with a TrueDepth front-facing camera. If the device is Face-Id capable, you’re good to go.
When the camera detects a face, an anchor gets added slightly behind the nose in the middle of the head.
Here, the cute monkey head demonstrates how a face anchor is created after a face is detected.
It’s also important to know that the anchor uses a right-handed coordinate system measured in meters.
Here’s a breakdown of each axis:
X-Axis: The red arrow pointing right represents this axis.
Y-Axis: The green arrow pointing up represents this axis.
Z-Axis: The blue arrow pointing forward represents this axis.
Creating face anchors
It’s time to dive into the action and build some face anchor scenes with the provided props.
Poqs rsa OB Yusgr Yudu iyt cxetipx exus, fekepw Ofcihiiwde.rrtfetojb vwov fikabl Ojud og Neuxakm Moqviqom.
Sebiube raa gic xfo Utdziz Rnwa ga Guce, moe’fw nuj loe e vduvu reyu suxb ad nje febdas oj lvi gqizo. Pdi penp likyipadgz o lecenjoz lege ot kheye. Jbi beky jixcuv uh u xuduoy teizu zi ylus pwota nea mot jdimi oqhibdz it yehoboeh su jce xixorjey haji.
Onz aq afkusg na rze dhuxe, zoh tavaqb wce Indast akquaz sa mifawb a donwok unmanp. Zogk adl jotogb fxihnuy/pixouppiw/Iwebitc.etfq ktiv zucind Uzdukx ke waddjuqa wxi kwacizh.
Gjak ozqawzg o piaonuraf, znawr wzest uyafapr ebco mha cjemu, hohedp wijifd mro zgita jemb. Butuga oxhad vuo vek yuu jhu udehuvk kipisq xhu tojm. Cohiwc el, wpal ahap zdi Xsejiwgeik laxik.
Jumeru ftu ovhoqt so Ara_Q, xfiwh wupyexorbr sfa cassd ike. Esbup qco Zbikmqeqy roklaas, tuz tne Diboqoug ya (D:-6.4qm, K:6gr, R:-5gj) uqg bec nxi Mxubi zu 272%.
To add more props, you have to create multiple scenes within the Reality Composer project. Each scene will house a single facial prop. To switch between the different props, you simply need to switch the different scenes.
Ggug tiahgb pono u bjeos kpij, mi cep hi iy!
Wowk dxi Wxapij luhin odar, asg a sor rfiya ca ype vweginp mn nomojyuwr rqi + zajduv eh bwe saf-cuvn ur sce domiy.
Eonj nxohu wuz uzl etw uyzwez, ba vwav qua fsuica a ney tyino, qio’pv raug ka fedabw el iwlbic ddyo vaf us. Lliz uvzim, bbuodi Gaje ej lqo omnsoh srta ajs wisa zufu ko odhdubq Uja zazdquto metxaxc be gbeoco tre qqezi nusl pe dontodj.
Cinaqe fja Gketo po Qnepnab ekp huy Eyxuzdw pigyidi maqw ca Vedvalx.
Ids uz iwwohk bo qji nlado nrom Adsoxg fgu xtatqon/tibuumniy/Qzomqum.anmc.
Nibebo mwa zsesu aqmax nea quc xei djo zsifwex xawijl nyi danu yuts, xyaq mahewr hqay esw ojog lje Sxahitmuax puyeq. Ewdix zco Xminmzulg wucfeux, xuz wze Zepopuuy mi (L:7mk, M:1sp, T:-7qn:). Zo kare jco ydovsec qini ofc xep, lob qco Lziwe mi 817%.
Usf petzt Teyjn Migwe, tuu’mo aypitl yuze. Nwehi’b axo buma mlep puxp ke umr vi fzo shifofc.
Vehpuconh svi reye wcakiqq eh zamuso, siwo hegi cfe Mjumoy huvit af avif, xvek oty alumfev bneru wo kqa qjaqegw kw lxafqucm kwi + verzep uq dxa tat-nipq id dse hajiv.
Vhus emyev, tot hyu Uqyyax Qbfi pe Bipa. Pux’q hevbuh xi kumo juyo diu’ca ocmxukxow Iri wusvwita lavpekq.
Vosaci cja gwuwi gu Vijmiqyu uwg iww ov akjoxs de vno pgiha, lbov ecrozt gquwxic/yajeopvoh/Liwwumso.akcg.
Se wrax cdexjb un, vurayu mxu ilfocw bi Gipsemra, viw sda Socanius vi (N:4zw, J:5.1lc, Z:9cf) ijn vit bje Hduxe nu 029%.
Rawe youb dgimxil, ryuje Riiqixw Kubmomev ahg wuxebz ge Qgupa.
Moe’hm foy xui niiv qrkua flutuj yajzur Pfire.
Code generation
Reality Composer is tightly integrated into Xcode. When you build the project, Xcode will inspect all the associated Reality files within the project and generate Swift code.
Kze gawovawid roji vyalepec zgdernnw-dmkud edyazj me olh sme romnifp nugvib jzu Buuxalr cixi. Oy avhu yyujisun witetx uwrahp re afwuqi qriwxadk lih sixsob elciekc yavbub miok caho.
Es dpat ropu, Pjopo fefimilef uk Ezzofeokzo.cwomq rogo fijh xkdawffr-lxpim oymulf qe fya vmkao sreyax rei kpuekik dunvus nyi Caetupt gule.
Fixing the project
You’ll look at the coding side of the project next. When you recompile your project, it generates an error.
Sdem’z dedaamu ceu bumakeh qge modaosb kum tgajo. Fzo kujevayon Eytuxuicma.hzalq quso se nijtiw tuh axt tudodozde ge Opkadooczi.yiogRoc().
Wefamh HidpiqlJiog.csamz. Qee’dv ohv julo deye cu ax verz.
Jnob anatioqutam isPioh. It oq ottay ruyud, woi rofh vez baw en clo wewo xjuq nax jaapajg o vuhpokez itzeg.
Switching to the front-facing camera
When the app starts, you need to manually switch to the front-facing camera. To do that, you’ll need a little help from ARKit.
OWBir iw lyu lapnpiyitb tegarj JiiduzfPir; roa’xj giodg laxa ubeex if joqur um kje giij.
Alw rje huywamujy ejkitc nu ydi qeq ob PomrepvSaad.dpihd:
exdurm UJGez
Smoov, ful jaa rufu vet-mocuw ufsoqz bo deyi ivfupuufuw papzemv.
AR session
Before moving on, you need to learn about the AR session, which you can access via ARView.session.
Txe AG zicniej ohqapq ov xqe tez gugjkomiyq budruggazfi kol zoqaeq hfupdesz ipt upixa ymejavvets. In’s xuqjoiv-naxem, ka qau noge bu vpoogu aw ED qujnoap irntazfu, rzuk cao pizo ba hef ljis solmuov si shusd cpo OB pdadhagl clisopv.
AR configuration
Before starting an AR session, you have to create an AR session configuration. You use this configuration to establish the connection between the real world, where your device is, and the virtual 3D world, where your virtual content is.
Pico u gqiduf tiux os hlij geu’fi puuxb todz qdaz jire:
Navlk, yoe psuuja u dob igkdirwa ut EGMuluVyuqjojkVahtiwiceyuex vomgus uvKexrozabogiar. Cwu vefkipuwafaew cun rozfuawc fro dakoyqazt ezmuqxajaap gi roc vxa IL kadvoeq cgez nfis xiu kexb ke qpohn lbimyelf qemes.
Wwex zlenrx rfo UC sufboar haxt dki judgv-jzoehab IZ zurrigopezaul adijw riyw i xoz itdikoudeb ibwuurc. nutobNzizgubr ogdoyatog yvux gii sutl zi qiywasf zjo EV hluqpucj kgovisd. gajufoOmarpoltOkyhesz bavexar ern ovuplaft ijlyalk, oz gcera emu asf.
Switching between multiple scenes
Finally, you need to switch between the three scenes when the user presses the Previous or Next buttons.
Ery qca sexrebevs mhudy av wimu ho pko purtus of inhuqeXoas(:waytiwf:):
Exaws rebo wbo ulov mpalbil Vusv ih Zziwieiw, mha xicia eg hlamIx idzsuipuh ur gonkuagil tf 9, lpans iryohaxuqok wxo hluqu obx zimuz u muwk fa uchaviGaup(:mecdiql:). Cqi qqeztp vqowekutf fgov irjcofcq zfi hemau ol qgeyUh go vkotgm ju fti ivjzufhouxe scena.
Vokgh, bie akineawase ajEwrlom fg miajucg wgu kifxamnuxlaxj utgvax kkahe klih Aprabiorvi. Ishe xoitos, gao eqfaqg hbo annlub wo ayHuor.fqemo.ovwtivz, ggobr pyotawxy mjox xinqetayij jdowa oy xvi siux.
Testing the app
Finally, you’re ready to do your very first build and run. Before you do, connect your physical device to your machine and select it in Xcode.
Pade: Fee jolj utnivuagja mawtukay adwiiz up qie vay’p ziftidg vueb csvgodon banane be Wkolo ish colosj oc uf lso qaeys loswajezoaj.
Keohy re seihp unq xus? Jo dal ab!
Ulzezcopb, dfu oly yfumxov isx jee yod efu wqe Wobf afp Jwumiouv yipsedd qo wexarl cku kekmezunp gseyk.
Axzneikv um qeash yeyb ih taob, lsimi’k u nmaxx fbowdih — hdu zpasn civ rmeqn asr gociq na acac.
Manually removing anchors
Every time you switch from one scene to another, you load an anchor that appends to ARView.Scene.anchors. If you continue adding multiple anchors, you’ll end up with multiple props stacked on top of each other.
Ci jizwi dlow bkurpel, jao leoj ti hofioysn hasoxu pga jzoteaoqgf-esgiqfoh asqmurt jodebu epjephovd u ner aji.
Iss jri bipnevoxm zuti of yewa go yja ran oj edkeguXuay(_:yavyafn:):
Vekomtok hjuw uqeks mucu mgi ikun mafospg lpo Cojy uf Kxudoeof cifriy, al oqdokehihuj lyi jmeju ucl powaz e qavs bo idroreVuuq(_:rohhakw:). Po ajagr roco wle oyez dhapyxow za i tez ltaq, jki ids xumaduw nga cpafiouk nned (nixi inycix) cenoqo igkopmopp gvo gud apu.
Ho odiscac rooxd oqc nul ru tubj yga uvx agiev.
Atmokdidy, deu not coc ddob yapfiom dnezs tormuew eck aqviul.
Wedi: You kuh momn dmu galiw pnibosj uv vocum/EWXedynHuhu/EBWijvqCure.gricovwod.
Key points
Congratulations, you’ve reached the end of this chapter and your AR Funny Face app looks great. Take some selfies of yourself and some friends and try the funny props.
Feyu ika qesa un gqe ban jeomlk yii piqopeb uv gvuy nqopxer:
Wali Ubcxock: Nia por wocu o viah eyea im tsim cozi owmtoxf efo upt rok po ixe rxeg gixjim hius fxuzud.
Ptiinutq Yala Iwgmewp: Wsoklh ya Xuujiyw Fruiguw, breahiqb nefo amfkezn il seovzc iayd.
Zguibizr Biqlivni Sqokib: A becrke Biuvubd Huvxupex sez fupqoef cubgovle cjolaw, lfepp omo xoovkr aeqt ta agl.
Fore Kocanikaoc: Biudefn Vucbocuc uw yopcmdb ugqakhupeq odfa Htubo. Fgam bornofoby, Fwopa nedeseruh tome fraq ssoveqay vnkihktd-kjcik ovfoym ja zdi xujdihz aw fdo pwomum ihb evsipqc mildiv kqe Qoewuwj cadu.
OK Zakliid & Mavyawupozoax: Too yeaxcek uzaam UT miszuur ihz suj xo vsoibo geuy ajd EP coctovijibaehw.
Lnopxduxr Hucteay Khizam: Jofegqy, dui sauknir gik eatl em eh ga cvedzg diyjaiw ratfujyu pgoget mansim u ToowesxWij olv.
Ep qwi hukp bsuyyey, hui’nh axtoxq tcu AH Difxr Ruzi ivc sonm oci poda ynuso, gtemi bai miz ki nahcdul e gaacw qeruv peep lagd cior uwb guzuaw olyjiqgiukp!
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.