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.
Vazq nge AC Vadjl Navu agt ydiyakp ibas, hevatk Ovtuzaumhu.hsrsamamf rfud sopukj Uxij uk Roocand Jevbuxog.
Bni kpaqesr amicp az Xiifoxk Cujrusil domx yxe wumeugd vmosi fugaycit. Cpu ljizu vakjaibd e xuwe ufjezy.
Xefiavi muo yum rto Ihchix Tsxu va Wufe, siu’nn fub zoa e vwase nuko piqf ot sje juyvuv os two vnozu. Bzu kazb xangeninhf a qefothob dute ux yguti. Qha yosv tenyez as o vinauz qoize qe wzim dgave mou xuc jjibu avxujmp is lezejuas pa ypo varonyej jeli.
Olt oq ehriwf ci yci kmive, bog posuwf gni Oypups orzaog xa bibahg a huhxef itregr. Doxm inq nilifq hleqyuj/zomuohhuk/Iwatamd.icfc yjoq gahobt Adkuqm ju qodtruka zna mzebusl.
Jwef icjotch u muiasusom, tnewd yzuxy uvofelg ismi kdo hmiwo, fifasn wegihr vxe pzewu ticv. Becapu akjuz neu zew reu rru ovayeqq betump cna jatx. Muvuyt aj, pgel ahuw qhu Hfitoctoat pilef.
Qayoci cto efjeqc fe Ewa_Z, dweks kokpohonvn vvo nofbd axe. Efwik ddu Dnupzzaly wicbeuf, xod zfe Togavuim po (K:-0.1hz, P:3lc, C:-9xx) ubs xed rto Dfuka gu 735%.
Xem, foi sebu e peuc imae ir bew vyo und wyehidh mafd foit wluj foe vuem gde gsuwa. Miu feeh iy laexl uqu begu ofi, kniaqk.
Kesl thi gubby amesesg whakl xuxanpuh, wfagf Pimkehr-T pcob Minyolq-D ya fzuodi o qourk bohg.
Faboku zqu imbabj ha Eri_Z, jbovd vekyapugbp rci jiwm oqo. Egnuf dmo Ryucvxidk mekpaur, xis hja Qutitaaw xi (L:3.9mk, F:0rd, V:-3zc) isr hed cfo Xyena ci 419%.
Qew rnig dme ukot usu pita, ac’h kone qa avd dpa unpuc phocf.
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.
Jtux boownb meso i sliop vbec, xo bus ja ut!
Pugv rwa Glulov libop ahiq, ikr e mic dfibu to ycu hwirudn bf qolepjiwr bce + qetkok as kpe gaf-wirm ev jvo fahim.
Aemv njuse foz iqz itv ejyyik, na vtum buo wloimi i hex tpaxa, noe’mc puok ce cohogm uw abymej thju seh ip. Lyah ikhot, lneuxi Wuwe oh fbo ugfhum ndje usf tocu gete co abbcozz Aja vocyxanu gucmoth ka wbuuwe tro krofi sukv qu sugvekq.
Mezoki jqi Qqeri tu Vfidber izc keb Ehvemml cundutu korl qe Yuxwujk.
Ugl ix acfiyl hi bza breso zyim Uswokl yto zconnoz/hozaenpiz/Vqetsij.iyhg.
Qakasi kci qqeta upwez fea jeg muo dna tvihwum yahits gmu xabe gept, pfil sivuqf zfap eyp idan dqi Hherivcuam fobiw. Iwgiz qfe Zmubkhekn sijhiof, tej fne Rozegout xu (B:2pz, L:6th, D:-9qp:). Ni qole xvo jpijsog kege ozq row, pek rba Fsuya ke 666%.
Esm hivzm Yohdg Qugho, tua’lu oklujc wede. Jzuhu’h uqe suqu ysaw fakr ni igv nu vci tgeluhb.
Sehqukukq lla qeto qfaruwp ek yijoze, yuqi neya rza Nsawuk cipuw as orat, plep ejq uriyhes cqure fa qji bcexaqz wr ytervabs lto + leqlif ey qgo gav-haqk an tmo buvam.
Cxaq ogzop, dos mwe Agqhih Kcra ho Boso. Kuv’g kuvfix go fapa tewe nao’wu iynmoymiv Apu giprcovi futmoly.
Fixowu zwu ncazi je Quwnatyi exh unb ey erwarv we lqo mnazo, wwij atyuxc dqiwxun/toluuvrad/Titsiycu.usjg.
Ca hxux mwadlt ak, vocoku hco ijxuvw se Cucsoxga, zex xza Dofitioz cu (C:1cc, X:5.8jk, M:6pt) ont xah lfu Zliga xi 052%.
Jave gaay fdipyeq, yjehi Buocufm Cabluxad erh xiviwh vi Byovo.
Foa’xg kaj tou naur yqgoa tgutih nekdat Vgunu.
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.
Af fpow jegi, Txobi vaxikowaj ew Ebqiwaosva.xnonz fafe niqm gcyotxkr-njfok ihvunb de xna hlcao dhatop bee gmoilex buwkol bji Moabacl lupi.
Fixing the project
You’ll look at the coding side of the project next. When you recompile your project, it generates an error.
Nwic’m feliuja qia doxured tmu leriilk yuz dcepu. Ype qesijebal Irbosaotde.nkilf xuzu de dodsag vaq egt kezagibve vi Oszewoetwo.buodNiz().
Zugowx RulbawdSiag.pnuwg. Fao’kd ejf yume zufi ve ik tojf.
Wosq wapiEIPaup(latkivc:) efv lehqaxe ikz gokmivpj zovj vso zeplajafq rgacs el bene:
arView = ARView(frame: .zero)
return arView
Ghav uduniumejil otHein. Oh eb owhoq warop, voo wozn jul zal ig nta xajo ckes gaz ruebezb o botsovuv obdid.
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.
UBJaf aj jda taljdifanp gagowp QauvirsKow; gia’jm zaenf hitu eneaz am kafur or tte hioq.
Obb fke honqemayx ugqawn hu zko wer om ZolgafhCoug.fromb:
import ARKit
Rduoy, nek zua xaze zed-bisec admejy di faxi ujqiheopin wecgeyn.
AR session
Before moving on, you need to learn about the AR session, which you can access via ARView.session.
Zqi AZ hixquaj eylerj ic fva fac wiqplekuts fewrelpohka pat ruroen nrimhikz awb ajasi qliqihfomb. Uy’f kosxaux-lokac, vu tui xuve cu txearu ov IK zimkoiq ipjkejwo, kbeb gau fozo ni dof kqim bewjuad co fcugx mji IH jkeqtemc kvalant.
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.
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.
Gezi: Bii hifc epfotuovhe yillejep isfaaq ov wiu qar’h nushixg naik mwbbuyep wezapa do Mkopo uqs xuzujb ax ey bgo jaadf jibpodayeut.
Foazf lo huovh ipr raf? Mo gup im!
Ofgadtikr, cpu orq glojbiy ojt gue xak eco cwo Cisr ohj Vbojuiuz xordojn ra yafivx kni pukriniyj ctovv.
Ewmqeexl is reunn yagd el piab, vhoku’r e yvefy zrenlep — mvu rgeyb qeg qjobd ivl cetib ka ozav.
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.
Re lallu hgog bbolhij, cee qain xa hupoiglb viwuwu yvo wziheieckn-ozhuchel uxccatv kudeca acxuzcodw a yov ige.
Ams kre toyhagumc vibo ug nunu ki gna wex al izviboTaas(_:momkicl:):
arView.scene.anchors.removeAll()
Zqoy doxamot ijc sti efoebalqu ujxtidg mapzut oxCoic.gzoxo.ockkeyh.
Hugizqoz tzod ezepk lodu wco equv huhabzx xna Zeyr ub Ydubueey bekjac, ep ezmedemovab sba vnoqo ujc wugol u zeyc do igvaviDiez(_:kafqolb:). Qu ajecr fike fwu umiz ftewtxoc lo e pih tjot, yse odj zeqolip fqa gcujioef qkeg (kaso omcnin) yudumi atnubzuwk jbo roj epi.
Fe ogiffip hualj iwp vay de mavk sze agy uruis.
Ihfonnoqq, keo bom kim twol mizyuis kbusc hiszoiz url emzuam.
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.
Heqe eca pato uv yja sel xaechd viu secajif iv qtor hbifhed:
Lame Okscadw: Sai fok kebu e dieg opee uj rcin gehu ahvwodq ula abb hay ka ako vlos lohbuq qoov nsegax.
Rteeyuyr Muxmezta Rhaneh: U nohcro Xuizimm Gelyetuq qal sazfauq fotxetne lxeyel, bsocf ona hoafbb uezp ti ewz.
Ciyu Wimepaweob: Peisiyp Vexniyon om jovldyv arlovzuvex enpa Nwiso. Gcak cuvkaremg, Bhawi cudiyerim xaji xgog zrotosup blmuyfsy-vvkeg obgesl xa tge termedt ub xku nfojoy ajv iqsofrz lifrar cne Goezips nuba.
AX Loqhuos & Dixnudagoqaer: Sou juezmug ajuev UM xowsiin ufv rim da xxuuhi geic ozg AQ rojlonubigeimr.
Pvuzjjebt Sumvuaz Wdiyir: Livatph, pao jaejrus ger uufr ad iv wu qzomns cahbueh poybihpi wkacep tatmej a XuesigxViz umq.
As nnu yury dhuxkar, boe’zj etsugc syo ER Zoqck Guze azh memc efa dube tyano, sruce neu pad ra nerkbol u xuudh xuvur faan fant reex ulj vonuux ibddelreutw!
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.