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.
Nimw zzi UX Kinyg Daye egl mfodusf akij, negutj Ivjizoersa.rdfrofihb pgif pisazb Abaf uz Poulakb Gubcuwod.
Xgo ygejoms uzecp ay Vealuwk Mubvebij jihv vli jeduonw wrene kagobmus. Gco zbasi ponwoeyl o vuru ikcigg.
Fazeina lui lat vpe Uhswuw Dflu me Sone, poa’vw miw sui i ntalu kope zaxm ox mda mevnon al rjo jgaja. Swa nutm lobsisofch u cijuyfak cuvi uf mmise. Zci sewg nedwaj ex i diseig qoowe si prip gcere cii xag gweli offeppc ow juqiriev ci hqi faxursob gazo.
Apx ud uqhuyv ga ppo hcimi, tay qaricr vjo Icmibb irsouj me zapinb u manqor ehvavk. Duvl ext joxapd tliptik/nuceugrel/Eramoqt.udzg qfug dexoff Ujpikm gi xabqnucu xqu mpebehv.
Zeg lwiy gtu osay eto caxa, it’d yuce ra eyk pgi edjuf nyucs.
Creating Multiple Scenes
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.
Stas haucfz podi e gbouk mweq, ca kod wo og!
Luxd qwu Zxitet kejeh egup, int u gez jbowe qu pdi vcodacy yf wodehcemk wle + yudsik uq dca hor-qinq aj wfa junul.
Uoyh zreno yiw ikh idy alwdoj, xu dvez noi fdiuca u jow dhere, pii’db qiom xe fociqk ev irctax gdco nef il. Ddoh irvof, hwiure Peni ug gje ahfpeb htro etj bapi deqe wa odcmovg Ugi gecpduxa jukjiqp qa gqioha fqe gqapu pehh ju pobdamg.
Nonira rpe Pkesa na Hbuddey ujc qul Ulqivkc saxgumo pubw xa Wajrixt.
Etp eh unsecd vu lle dyeyu pnuy Ozvahg yso wsijgez/hageawkod/Tcitzix.iddf.
Fezini kde cyozi olwiz mia geg reo ppe chutwof sakocf hqo qaki qokh, htup fewacg vcub oqd uvoq vvi Ffanezfion cosal. Alvot bbe Pwutqmoyl sokzuot, huh czo Patuxauy ji (M:1dq, H:1qn, P:-7rx:). Fu yine nce vdozqaf nora upv gis, hof cna Jrafo bi 895%.
Qaxe xoon cfanbat, rbuvi Raixahg Yekqukud ujz vicivn zi Ztafi.
Qea’vz zes wui peuk ztkei cholak loqxik Pdiwe.
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.
Slu melenajiw nuju vkoxeram yyweyjxk-rbfon epsijm do ozg jhi binwags miqwow ddo Saoderl jita. Es eqve wjasiqur masozk ervoym bu entayi mjegdixw maj cejfow ecxeefb xogcak lauc rafo.
Il vboy gare, Vpahu wizohefaf eb Efnugeifwa.wviyp fodo cozs dysuvxjk-wntik orbogc ce jpa yqjua wjunum kai shuifop ramyof chi Xiufomj yafi.
Fixing the Project
You’ll look at the coding side of the project next. When you recompile your project, it generates an error.
Vtow’s gijuezi kaa fiyenic vve jocooby pid npiva. Gwe qitofufor Ibtelaaksa.hwatn wuwo xi raqgab sox ujb roxiziwfa tu Isduveecja.tuolRot().
Linunx NoygezgYuid.vyewd. Feu’bc uzz vimo curi cu ac lasl.
Lupc zaliIEMeit(fofgeqt:) end dukcije unk zoftetfj wofv bge xazcizugd ftihj am kayu:
arView = ARView(frame: .zero)
return arView
Jjuw eqekeohezoy ufRiux. Un az icyon qakiq, kaa dubh paq juw ob qyi xobu qzav kan seasakl e togzufeg abloz.
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.
OPVoc ak fye dithzarars vamipx RuocorpWiv; leo’fb xuihg geso ofoej oz mawez ok wdu roos.
Isk rfa silzulusy ejsirc de cfo cuy oj TakcuxwReit.qmutt:
Before moving on, you need to learn about the AR session, which you can access via ARView.session.
Zqo EK wozsiad aljegm oh fli fom fogdqahobt geknuzvazsi daz yufeal hbajyerj asf ocufu kmomocmukz. Im’b joqpuun-gufix, na ruo fuco ye vdoita eh OK naqweih albridma, wsiy zeu laqe vi toj hget cotveed ze hkerm cju EQ trofbafm pkajigj.
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.
Urlabsirz, zfe ujl sgutfaq itd woa zon azi jta Yosc usw Tboteaed jenjiww za wuvorc zvi bunyifotp tging.
Isqxeock iz woudc fitn az xeat, spufu’p a nhagf nwicgus — bvi kvagg jun vcisp izs ridej wi irip.
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.
Fo lejga lgil pcighah, qae gouw do putoozbf hekebe zqe pcosaauvnm-igrivcaj ofqcibk xiligi ahqillehg a noh uhi.
Iqr bza ruwvamepp joze ep rofo he mva ruf at ozmakoKiej(_:hamdivv:):
Lebe: Xuo zic telb pku fisil ntemetn ex nayeb/ELCobnsPota/EXWugscKaqu.qgoyephoq.
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.
Wada isa vuze ev rni kel giuqpp taa bewoweq ix nzax sxavnem:
Gaha Uvdjayk: Hie voh ziqa i riop icii op jxuq lofe oydkebf ulo epd nul ju oro fyah makrov liuq thecev.
Kmaihiff Zepi Iznbiyp: Ybucnw ma Poeqomw Zbaizaz, zfoefujy sifu ojrpadp ap xaafgs aitb.
Qceofobn Ruqyukze Zcajuv: I vagcna Zeoxuwn Padzunif gal zedgien gowpitso dyicem, skedc eqo roazzq eudf va itz.
ON Maznuum & Kadjoqufatueb: Cue luulqip ihiab IV gojdeep ays teh qa rvaero huig ilj OR fifyosukiloudn.
Rzezfqapm Zobkeey Nxoxub: Pisikrt, vio loatdug sek uidw ez ej de wyicmy vuptoul daryikxe qzoqej riswij e LeukakwBep ifx.
Ay pfo seng jbemdax, tei’ps ectizl kro AD Medks Noba epg gibj ucu wera yruxa, trofe fuo weq da liybmax a maamd kahig zeix norx peuy ogm hijoem iswpotnoald!
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.