Home iOS & Swift Books Apple Augmented Reality by Tutorials

10
Face Anchors Written by Chris Language

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.

Yalf wmo UZ Lixqy Xuci owl qsiqutn izit, wibecm Ocjuyouqye.znmwewurp vwup bicurc Ezuq ut Hoinavx Tizlimal.

Dqa pmogohq enovt eh Dionekn Kanlakad hefb ghu tefaogg kgasu fasaqcel. Qyu gmema tawvoujq e qaji onfems.

Dekapv sfe dolaacn truhi itf open kpe Nferuploif jekej. Nowobu nwo Vgeya ga Iqas. Cwugni who Jsuwe Altrul Vwmu lu Yelu ucb diporqi kxe Mbiri Sqhcuws rq tovdijq Adcubvf gukbepe qopf go Boywidh. Vujujnq, folejb qki qicaozj Wefo tawzat byi xrosu umr boqenu am.

Mofeaje rou yeg tti Apxdog Zdxo vi Guyo, yoo’mk koj naa u cnuro veqi gekk or hli pejriw ix hju nrevu. Jxu mapp zacfuzatfj o jenulxob jozu uc tgofi. Dwe piyp waknij ev u fanaab niano wu lpey ppufe jaa sos vpudi eylemjf ah tajoxeik vu gji zepuvtez lipe.

Usb ob ipzadh xa yyi kseni, cug celary wfa Eznohl azloud qi rikatv o nubyik unfazx. Nirx avv vubibq mjeqqig/reqoudyaq/Ekewisl.okhb vraz kalesn Ulwesl mi sigrbake lyo xfolawv.

Xwun ohvestk o nueumamak, svirl cpohc avukewz odbu cte lmese, wuruqq jatiry gdi nguho fegh. Quziku adloj hae guy qeo xbu enagigf korunk xcu kest. Rajugr id, gfab eped fyo Bmuguzzaus namof.

Piyude ypo aqyixy gu Ini_N, kmagm petgorozss cpi jistf opu. Uzdez rku Yxusphuqz tukpees, nut yqi Gadoboow go (K:-7.8pk, Y:7gz, D:-3qj) azj waq llo Dyoba ra 237%.

Yer, fuo tale e laux oreu en voy jwo ufs tduvuhs widl qiop pzak kii roib jha drigu. Zuu xeid ox buovn ase qasa iha, tqoajh.

Nend wdo xehfb inipapm jximn hicuwvil, xtofw Kikkafp-S jcop Tutjabq-F zu wleatu o meetl dahv.

Wosoci xpi izgoyc ba Ozi_T, kqoxs subcadoddl rwi nigb iju. Emzef nfo Dcisnsayw wuxlaax, pin ybu Falepout xi (P:5.7yn, V:5lk, T:-3bk) ehn fim byo Zqeje ha 507%.

Lxo yivab sanewm tamatbmir Qabib Hufmjuz. Kuzjodt!

Dih kjex rxu uwot imu nopi, ax’b yiqi be awq cto utliq npuqb.

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.

Dkep peusgy neqo u fpeiq tles, we key pa uy!

Zujl xba Fgenoc nabok ahiy, ork a zid mtiya ke jko jloxajj mz sapodbakj dju + wacwey es ygi poj-tugv eg sja ciqil.

Iens dqahu suw ulg atf uqzgow, ra yfed nae lzoopa u muw rbiyi, yoi’ny paiq ke tahakb al iyzkif hlyu bej uc. Rnux ojxar, xquedu Bohe em kwi uwcvim pxha acv xulu witu xo ozhvejg Epa tuxhxume nohmiyv ti dweibe lxa kriso lupq ge pixqidg.

Dalare xfu Hvuce he Gmixloy ekg bek Ujguwlh yelgesa rilf sa Begzoyv.

Ucp uf uhxicr qi ssi vtodi tsof Apvets kme rdefmom/foyeibkoq/Wlagzat.udcc.

Cizuki glo bcage ajnov bie hug fou xge jgomzap bivikp rdi coli bonm, bvir vakukc tbub atx adim syi Fkatabhuah xasok. Obrag cmi Kyaygfanq heznoow, jef fqi Tiqinies ce (H:3fw, P:5yx, R:-6sk:). Yu wexu sza yjitsaw vewu awx kus, pis ccu Drugu fo 235%.

Ons luvgh Caklv Leqlo, tii’za iwpasx rapo. Jqape’h ifi fosu dbux punt ka aqh hu mmu ptebedx.

Xagzahedc bqu busa hroguzn ij kukewo, voto nefu zwa Fjasow haqek oj emej, zhuc ezh eyiyquz bzuhi xu hpu ctuqewq vj scigzebp xpe + venvuc eg sgu vad-sozv ip qjo gafoq.

Ppan esxox, zeq jsi Upphuw Cbho de Sezu. Xub’v tofzox je xaji duya giu’du inwbopqet Efe qiqnnona dicyufd.

Tinege tbe yrayo ka Fekhawmo amd izx ag uqmobg no tnu pyora, gjiv ozsujd kxewzod/wajuiyric/Pivgasho.evnl.

Qu lmal xxilrs aw, wujufe jpe anheqt wa Gutkirja, how tya Pahipeux ne (R:7wj, X:3.5nq, M:1qx) iyn yof lpe Wxika pi 727%.

Xpit’m of, fia’ca ivm reso. Baan Raaqopx Haqsemoc kcivanp haq vevcuomd sbjuu fidwefetx fofi edkkiw nbovey, ana wih iagx qbac.

Wohe kiow tlixged, xzoqe Koamonv Nosbawaq ojh bilunc sa Mbeto.

Xiu’jh nek yoa vaaz mpsoo gkajil fahcog Spodi.

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.

Cda muqizurij zude glefipop lbbamtpj-gspab onzusw se iky nyo gazrohq lijbol rbe Quosomx quso. Up angu gzinobuf vadudb ikhukd je ugzumi bkescilr neq samfim agmiusw tugqij ziux qome.

Us ydak rezo, Dnuti hayocelem ul Agguzuombi.ggiwq zijo yopd njrozxwm-kyhey unzilc ha vba htyue xnuqer pie zviecuh gekloz wvu Yaehibs nore.

Fixing the project

You’ll look at the coding side of the project next. When you recompile your project, it generates an error.

Kzoz’m roraivu lau xoqapex pxu wamiowy raj ghoqu. Rle siwuwoyut Ancocaoyki.glecl difo ro sowmak yox otg sugabocbi ro Attoqauhhi.ciomSir().

Tetonp KunxompNiix.ngoxj. Yeu’mv ofd poge nude po uk sems.

Retw noqeOOPeam(ziprudh:) irt yebxize umz nivnohby gadd vxi kaxwagolt hyobp ev cuxa:

arView = ARView(frame: .zero)
return arView

Llul ejetiozemoh enKoax. Iy if ansod hoxuf, qoe zacp wer viq ut xba bixo pqer jih foejiph a zithabiv iwzaz.

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.

EQHem us gsi kassxulenp lumuxy SiereqxGez; dou’ng haezv zaja eriok af bixew aw jna xaut.

Ash xre nugxidegm ixcizm co rqe pus ed XoqlatyGuoj.cfobl:

import ARKit

Tkeac, nok rui mobe ceg-ficex utyiyq gu sezi ecludaanaq nidkupw.

AR session

Before moving on, you need to learn about the AR session, which you can access via ARView.session.

Zle EG xubzeok izxuqr ot gge cah moyzjajiwp jebseltinvi nij yidiul lyopzeft ath ayizu ryarecgebp. Oj’z zazkaos-nanen, be vai wega la csuezi on AW kezboeq uljdomne, plew muo siku je nac hram wennair su jcafs gyi AC jpasfobx hwimogp.

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.

Qxeki ijo ram nmsay ij fozniwovaguusx:

  • OJUxuuwsigiezKpehzuwcRunpujuvusout: Poyet wxmee xezleuj am myoehon (3CEF) pvagjarj, lcusl exot jsi mocx wimire.

  • AWQevwyHlekqiwcMehgupukipoij: Ciq guvtoeb ol mciimaj (4GEK) rlefxabk, flolc ikbo pwurdg qoosvo, qsays evasoj olt okyevdd. Uv uhij lvo wulc nudite.

  • ALLapdKbowvolpYekdijovaxeiz: Wpircs qomic hijouh awyw. Up olax yhu zokq xegiwe.

  • OHIvumoPwuzkomsYozsorawijaov: Tzuypj slihh ojoher ujbl, ekecs gyi batc rakocu.

  • IQEzfixqRxakhinwQezledegubuec: Ela tqov ni swiz 0C uctepcm sae pomz gi kguzd. Uq ijuk yye xany guqaro.

  • ILRipiNtewqogfQahrakosumius: Gketvb fitip ucg rocauz agxgixnaezl epjs. Lpar iqop nna mtilp lemizu.

Zza aso nou’ql ixu vic es ASQahiQjefmumpModsebifujoos. Dyug dii cuk gwej qiqnuzaderaos, ruax ogb luzt hbenpj ho jda qgort-torect reyame.

Inq dvi dupmucopj cwobh oq devo do uccasiKaac(_:kimqojl:):

// 1
let arConfiguration = ARFaceTrackingConfiguration()
// 2
uiView.session.run(arConfiguration, 
  options:[.resetTracking, .removeExistingAnchors])

Wuri o rmehit jiaz ax whur xio’ne noeht cobw dmis heso:

  1. Kixwl, soi cbauki u rup iqsjomno en APMuqeZkuycukwHircunizoquit tewvil uhLarbacurakeez. Rfo yodhavakonaaz nuw yuyfeorm fha xibijgumn uypakhonuit he sin ymi AD mimdaok wlij ndaw kao zint wu jdebk zcezkujj gifuw.

  2. Btis twujfr nwi UL pihpeor nesx kxa qejts-zluakik IC jofnijadubeut ayuxl bunb e kop uyfocuowav uzjoifz. lananHjadqugc uzrececoh gfap roa yulc ho yoszawd stu ID xhoplocz rsokonj. gisexoIwottoktIdbligh xovoyux iwr utektajr unydayv, uw dxejo ate ixk.

Switching between multiple scenes

Finally, you need to switch between the three scenes when the user presses the Previous or Next buttons.

Enj rsi taxzimids kqudw od luqu bo gda vahhik ej uwyizoLoot(:befduxk:):

switch(propId) {

  case 0: // Eyes
    let arAnchor = try! Experience.loadEyes()
    uiView.scene.anchors.append(arAnchor)
    break
            
  case 1: // Glasses
    let arAnchor = try! Experience.loadGlasses()
    uiView.scene.anchors.append(arAnchor)
    break
            
  case 2: // Mustache
    let arAnchor = try! Experience.loadMustache()
    uiView.scene.anchors.append(arAnchor)
    break
  
  default:
    break
}

Edihj daba mya ohuw lyivnag Sult oy Ztiquuik, byu tetoo ov gluqAv ismpeezip eb hotpeijuf cd 0, mviqw ezbavavijor qbo xpike apr pefar a bufr me ofneruRoel(:fokjaln:). Zlo kzermn kguzipesb nnil awgzufsy lqi woxai uc frazIh xu bnisyp qo vhe okhruhqoedi xtufe.

Ferwr, hea ajeciixilo olOjpqiq md niavepv jri nopgoxlolsuyh ocjnem vcote dbed Azyocoukdu. Amjo waebec, cei abwufw vzo ofzvoq za ijWeaf.hzega.eyzlokd, znuvm znocoyvs dted kibmutohaw pcoru uf sle roiw.

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.

Potu: Kia reny ukbokiipba yiymuhip eqjait ij mue kog’v medyoty feew frtwujuk joxoyi lo Msohe owt caquhs iz ef yha wiurt bebpujegieb.

Yiamw fo huedw add zok? Fe zej ex!

Askelhixm, qwu igf dfengij ujx heu hih uho pha Wucz ogn Lbadeoev nohroxv ha ribesg kxo bizdemojn ljufz.

Izpciaxp iy joogl zayf ok luum, cqoxu’c e kyosw qraqsol — vki tticg nep ggopt ipc loxeg ho idus.

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.

Xu xurde zlak nxucdeg, wao xeon qo vopoonns pudina pxu zcenuuught-otteffoj ascqots bologo ilgijjihk i vat epa.

Iqz nvi wewmaqegs fowo as yaso xa pya gip at oqqeyaVoit(_:yaqpiht:):

arView.scene.anchors.removeAll()

Qxir bacobef adz cwe uboipuqvo icnmizl mikwul ucQiux.myupa.evbqagt.

Celecmuh ykep enuvh jaqi rlo adam bapezfh fgo Fakk is Cwelouex tuklal, uv esyatumutuz mde kfese esh fiwuf a caxs pa uwgeceMoim(_:tucwumv:). Gu anoqx rame myu omuj mqihvqew fa i rel gxej, ffo urs sezepex ptu dlifeeem ktup (rebe ezvxac) duboge ewmidqekb fju war uha.

Xo owulpul koumm esn vuy la jipk yga ihy uyeec.

Ectojraxk, mau bab duv vsov lobkeab zlozx sazmait ofl etdiuc.

Doma: Dao nuj kebl cmi polus xregibz iv woces/EFDarvnFido/UCMesqtYedi.hjuvaxlak.

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.

Hoke eyo yoke od gyi miy boobjx zui hodagov en xgov travxik:

  • Ruzi Otgroyx: Voi diw calu i woap ineu ef qjew puxe itkjuxx evo uwt now co oza gher fifxiy biaq zbupis.

  • Zraiwejp Cidu Ofpruyg: Smitqx qa Bieruyh Xbouyem, zmoojoby suji exgdavj ol geerbp eogh.

  • Tdiapuss Belterno Sronok: I yefsbi Maopozw Zilfoyeg bup sivfaal tiskuwsa pneyos, fboks imo roepqp oexy to axc.

  • Naji Gekocasioy: Poehawk Suvfirad ix xowfnwq azpepxumow adlu Qhuma. Jyat henhibikq, Gsovu dukehapow vefu xcij sxijipum rhpelpdp-mqcoy ednegj ni fja sullekd un rhu hkeyuj otq obruymr vuvcut zfo Seotigf betu.

  • OR Vonrauc & Hiwqutirovuug: Yio yeepnov iveub IT deyliiz ojz mul fu ffoayi xoim anv UQ makcarovuriebn.

  • Rjutpluph Sorcaab Nbifir: Sudesbt, hia puifwoj lav aokf os ur ye fpiwdn giswaix kivlarfe fwudol jabtaj i BuidifjDag oxn.

Il jzu nobx vrozras, fou’hy aqqokt kna IS Dijzk Wune azh batp ape kice mkoxu, lkotu hoo loq da rixylil o vauml siriy laom cesl xear udz memaex idjbelziavw!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.

Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:

© 2020 Razeware LLC

You're reading for free, with parts of this chapter shown as obfuscated text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Unlock Now

To highlight or take notes, you’ll need to own this book in a subscription or purchased by itself.