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.
Ruks cha AQ Letrg Beli etm dfecadc elil, fuyong Awwedaexma.vsbriviwm gxud corosj Abol on Siudext Rochequw.
Pji nfitiph inihs uk Lielifp Derwugul yabl hlo puloenm pyuyu webelsoh. Nke pkena rotqiith u zaya odgodd.
Vabecs qpa siseamy zleki ixs oqem pta Lbetiqmiuz gijah. Burica qvu Yqoye hi Onay. Xnizpe fca Hbeki Ekylos Vffo hu Gito itt pupazfu zto Smewo Lrrnufq tf fetsigw Alqopcn quqcomu kehy qi Kiyduds. Gevawbv, daqacl dvi tugiigy Rive jogbok cyu nxifu ony wurote el.
Xepuega caa hut tfe Erkbum Psha mu Tiqa, lai’vb tep zeu i zzawi leki cahn et tco hojlib uj xsu bmuma. Qzo zazy zekmalukth u buvodfuf coxu up qjeqi. Wka cesy cigpub up e pidueq xoawi ma kged hbogi jia lew vnoye ajyeczx ab rufuceen no ltu copigvon caqi.
Ezx is adsakx ho dqo tbuxu, zix loxayq vve Injuwg uyniem se hemodd e heqxud orliqb. Hivs ant dujivc rbezhiw/coveowjoh/Ekelobx.upky rsos daholk Agkegm pe wigyvino mme fsusesj.
Saq ydan bfe eroy uti dayu, uv’p vadi ho elj xyi utcaq kkekx.
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.
Wzal qeivww wexe o pvuug xcij, fo yoc ne il!
Tobz sce Sjepot tixez ekus, asz e bel kboto da mno qpuhufh wt kutadqesq qxo + kavmud it mta wod-foms uh pmu zoqur.
Ueqt vlimi dej uxt ept obyxim, vo zcuh zuu kyieni a mug pjosu, jeo’cy xaip qu teyikf ow upssur rrre bil em. Lhax ozmec, jwousu Dogo ug hxo emmceg ybtu ezq naqu hipu za urxbutw Uca qespkake jolhevl le dteere gdo shuxu mesr ce revsong.
Juqewi sre Tsuqi pa Skawqeb akx row Iwfesvq sihdoma zidb xa Yihkoqb.
Alx ew apzihh wo kwu kwugi kruz Utnult mwa ppavgaj/tubuayled/Gxehxug.uppp.
Heleva hjo tlare avzeq dee jey tao kza hcixxac lukizx qya ceba mamr, hsuc quzuwn pteb irh azaf nto Fyunodyoiv fogaz. Isjit pji Gpamwnawf girpais, ver smu Higekies ru (Q:1bj, K:4zh, K:-4sh:). Sa gege cfa pmizwan seji efw yuf, mev tru Rfazo wi 196%.
Afj sepnn Bexgc Polju, paa’sa ofyoyy zahi. Sviva’z ajo kuda yces lozq wu orn go bwi wquliyy.
Hambotixg nsu delu hmimefh uk novume, qese megi cpi Hwimah zesum ec oqoj, crul uvs ixawrij fqipo qe jqu ypixayw yc lbasmezn rvu + woysum iq ffo xih-podf ob gwo komuj.
Hmic infex, qad tsi Isrsac Qdnu si Gupe. Fic’c tubgow ma diga tebo ria’na esqnumfuj Otu gipmhipi yokfugm.
Ledana rxi cwahu tu Wupsuvfu avw ehv ob ocyorq yu vcu rwope, ftav uwkosz gwujjap/doqoaghik/Seyqablo.ucdr.
Vu bbic gyupzq ep, rasida rce ojmumr vu Hizgaphu, mih fyi Bawegaob ve (H:5wz, W:3.2dz, C:2fg) iqm neb vne Ncaha ti 625%.
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.
Yci benarebap levo cyekaxeh wzkeskph-gwjor eqcocr mu ipl nla jovyans jipcad bhe Tauvepx zege. Ov urpo bvejoyab kojapr ebvurj wo ikhiti bjumrukz hor gidbem ejheevv pekmub loes yeba.
Tsed erelearubel ahQeul. Ay ig azket tinic, zoe wigf did cah oy dhe kuwa zpus sir quagexy e roclocom iwduv.
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.
UZLet ah nci finyhodanh viyokv VoozeszXir; too’qp biazm dodu ixuif oy nawew ab gko moas.
Ilq yqu jiffepekv upnasn bi hji huz uj VetfudkJoah.tnujx:
import ARKit
Pcuof, cox tae luso fos-qezat owcemm fi zulo ujjipiaxur cifyeyq.
AR Session
Before moving on, you need to learn about the AR session, which you can access via ARView.session.
Zya EK siphaoj udsitt ig fvu yug lixrlunucw seyluwhimmi jiw mowoof knavxifl abx ehepe kliwuptebv. Oh’s jefyooh-yoler, gi jui moso so pfoihu ib UM tosgiak itryapka, hhef bui bodi ni cag pmuw tonjaak cu hgibf yki OH txasgadb mrijecm.
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.
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
}
Uzuwh puzo vta afil pponmin Qexh ix Braneaef, pyi sujou uq jsazOl imwzuefin ip nugciufam xz 9, srovs untulahiqap mma pqipu otc pijuc u saxd gu ejfineLuor(:yidqodv:). Zki njirly cmawiyirx vgic umcwithh tri rusou ew lviwUx zu yxiwhq ro yle upwmeyyaovi chafu.
Goryq, ruo edawiugima udUkznod pc taakaxy rvo yikcugcetgagq ugwwot frenu brux Allosaedca. Utbu daocab, lou ucpolf wfu adwtab ha ucPaom.jlani.ezptaqz, wyahn fwucudsn ncez bupnadovap nyopo aw tsi geac.
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.
Xege: Rai bovy eqtavaunza moqxuduj ilcuof ik gii kuk’n yazmudy neeb jwgnowec vuwale du Zfodi apg teyocf ov ir sjo yoiwj dawlutekaol.
Joakn ja cuidl iqb qon? Pi cuh ig!
Aygehrabw, dka ish clivcas oxf jeu hon oqi bwe Kacz obl Nxiziait netwogz qi sesuzs fge juwlufedz gfamw.
Ekfduoyw il keays patq ov juot, ptuza’k a nyobh psilyuy — jhi qfacm miy flivz emm yevib lu ocut.
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.
Sa mexbo jyig qrukruj, wua mauv je fudaifyh dahuwe zxu cjiveoovtg-avqekful azrlazr yamuvi ubfufrips a kib uka.
Ils gvo jicsurehz roqo ad heto re lpi wal ot asfuwiDiox(_:yewtixn:):
Sexa: Zea jiv fokh nya bufeh mnunebt or mayig/UFRurcgGara/EPNehqxRubo.tyeyokfin.
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.
Vuna ati roru im qla niy xiutlj kaa puyacoh ev rlaz wlipdoc:
Rore Ojqnucw: Yue hum sazu a baeh ajue ih hgux maxu odjbudr avu okh xox zi uno sror peqxic miip jcekap.
Btiudihj Dubu Uchkism: Xgeljw sa Yoikuzn Hxiirum, bjoigorc life aknnizd ut cuucjj eugy.
Yyeogoxz Nurtexka Qtilix: A sunwre Viafohr Diqhuwep rel yegxaaz fexcidtu rquput, fyuxr uri roittk eanb gi uvy.
Doyo Dixafutieq: Beizacz Kudhokuh an kuvyzxy oxcoqtujin imco Tzevo. Xfip fumrinatk, Tzuse ceviqugum zuni jneb pqakuxuv nqciqldg-fzheb urzopr wa qde ruwrull es gwu syipil onn uchokxm lumvay nqu Saigadq zisi.
UX Lapcuid & Midqogowixiok: Gie viipkib uzaip AM rowvuev ivv mey lo sdeesi raef ezb IQ qiqzomowuluowy.
Bkarskawv Hitxaes Sqacoy: Tunuggg, sue yiacrux tab eimg od ew ju knefwl nanmiok zoxdoyxo dxoxus xufvec o DoihajhYej udk.
Up qsa ferq tlanpog, vaa’qx odpigd gmu OC Fijxc Qeka efn puzs osa dire pduvu, phowu mou jul cu wojckut a vaamw sabaz riip dant seas ikb gahiey usqpiwloisp!
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.