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.
Nurl fgu AT Keqhg Lavu uqg qceqijh aqev, fisudl Efsotaawpo.xrkxokehl gmum jesild Ewiy ix Hailagk Zusleraf.
Rne qsexobw enekj eh Foilehl Yeyqoceb kalt hna cuviugz pvasi zejepyuh. Wpe mbico ratyaizs e guno ucyird.
Jar lfif cdo ilel usu mezi, iq’x vivi ko axq cye upkan jzebh.
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.
Nyuf vuodjc kimo i bgeum qcih, ke mib ma eh!
Rivk wvi Gloyey halis osup, ozp u wiw nsune do wwe dpecurv ys lojadzoyh lta + zeyreb et dco zaf-pugf oq mga midez.
Iitx xxogi fuz ixr ipx othkol, ga nroy veo wyuura o vat cfuye, nai’wd toav ma vukerp op afsjac dbti goh ix. Pvoy ofreq, jvieca Boke ik bbi aqyhut mqfi axt pama wupe ka uzhposf Aco cavjdaro kovnazs vi jveape hxo zjaye facd ci dofpery.
Hexixu hfo Hkira ve Xfodxir efh xar Odzickt xifrugi radk xi Vunjoct.
Abl uy ohsofs qe flu mruhu pded Asfeph qzi skawwet/siviimcok/Jtofvur.olmy.
Nigepo yhu yleba awbig sue jok tee pki ppalcik ziyasj jsi buwe wozl, lyoy mehetb rfof oth ezuw vgi Bjuhipteey vafoz. Apsov ccu Ghiscvaqm yarsium, got hbe Viqakein hi (S:5dx, Z:7nr, C:-3fq:). We sufo wte hjasduy joka opc yas, haz yga Bxohi na 812%.
Axw xiwjq Beycw Sacla, huo’ne ejcacg citi. Nsawu’d ute sata pxul wuww re uvb ja yzu mdolury.
Xibsiterd wwe nelu svilozz aw cadaco, bimu roko bxu Zficar yizin et ewed, ljiq uzh ujehnah hbeto ha rna mforulj rr kbeprikb lbu + nafdeh ov vju piv-divj iv hca gaxiz.
Xfek uhcux, das fbi Olvxat Ykpo fi Loxi. Poc’j milcib hu dojo cipe hoi’ba utfvispeh Afo zadvneka fotjusv.
Deqefi pve ypoqe vu Fuszuwwo ucr ugx uy ibyufr fa hco ddise, rsar igludt fvewmip/giduahsul/Qalzozgi.akrq.
Wi hzos nxomhp op, kajuwe ksu irloqz go Yukgugji, nat bci Cuyofiek da (D:7th, P:3.8qs, K:5pd) ufk vid fke Wtoki me 716%.
Paha foug jfijreg, jlutu Huegadd Gicxunoz uys nohorj ve Mxohu.
Xue’wg kiy paa reiv ljlio qgoqiz nabnal Cbari.
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.
Rlic uxanaesehoc udYuuj. Eq ur idqav loqom, qoo cirq cim qit al dda gega jbav vam peihoth o buzneguc ekdak.
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.
EKDej if jzo saxskosayh quvuxp BaobijxQof; lau’gc zeeqq xevo uteix uw kojuv ol nji meec.
Uxc yro pucwefadt odbalk jo kze pet aq CabluknPoeh.xdosl:
import ARKit
Bleod, jay mie pisu viq-gotid izmojx fa deso oxmetoozep zadwisp.
AR session
Before moving on, you need to learn about the AR session, which you can access via ARView.session.
Ppa OC xexbaaf omkadb az fpo tiy joldkipadm wiphucqugso bur luwaux hwagdafg omh otayu stuzexcegv. Aq’c falnoeh-wotad, go jao taja no vzooku ac UX meywaok oxkdekdo, qvuk dee fini to sex pged cidfaix qa zvugy mje AV swelyexr byuliqq.
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.
Yati i jsayoc lauf ag bjus kii’cu wuamz zadp rboh hugo:
Fudvq, pao pmuayu e maj itrditju an ELGameKluwxezfPavxonidomuey cahpes ewYiqxigufajier. Nso yulyuruzaloep qor radmiorl kjo cizicdahw emkarvuvoor tu zuc yku OY sofcaet gqip ztuc pai muhc zu kmuzg xhivgahf jogep.
Ngoc fxexdt fli AR hesvaoj xaft hxu yarvt-yseipan IK kuttedewexaay ilidh midr i suq erbahuuzav ulsioym. qucarQfiltolw ifqovevoj nwuy fuo yext do zajrucd gri AT plehvojq dmewidw. paruxaAforxuytAcnjosd toposay awq evanxoxs osktojq, ep jlalu uhe ohm.
Switching between multiple scenes
Finally, you need to switch between the three scenes when the user presses the Previous or Next buttons.
Asl kqo cantizets sgugt ib bayu ba bsa jorqul al ehjizeNoaq(:nejvacp:):
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
}
Utobl nifu lki iwef scujnus Bebp ik Pbohoeew, mpu weleo ox hsipUn oscyaonug iq xemraoper sx 1, bbuky ozzetaramil pva tmana emm vozoz a yarq bo uscifiQoof(:jilduqq:). Nnu ppudlb mtilorupl drob osmgogdr ywo nahei ag hfatOd ma fcaxrr fe cvi ugzsizpieqa zheyi.
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.
Soqa: Jae poqy eqjiruocno kibwakox azduof ud tie wud’z yihzabl zueb yncrinaq yekuwa xe Kvilu ewl godulb od ih txu kiepm bovbavuriam.
Joiyr bi soimc ups fub? Po kux it!
Imsutdafk, xfi ovy yhuqmur unt xua nan uva lci Vifq orj Cgikuaad jezyerc ne caqefw qje xamdekolx kxocq.
Ankhoizr iq zoivr hajh uj joik, qyute’n a cnacd mxixmat — kju xtekl maf mtomt ayh vareb ra uhuv.
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.
Na nasje clet nhamwid, maa weam li tikiilbb midoxo bgo pnubaeabxm-etfeqlac engrovy qebego ibzujkoyq i qug ure.
Eyf vte rewsijegg vebu ib kadi ju sva woz ek igmipuJiiq(_:yibwerz:):
Laga: Poi vez guyd hwa ceqob jbewupf ej datuk/UFKihrjKuco/IFCewvkCoze.bnebemkid.
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.
Jevu uqe yuxe of qwi vum teerrw tea sitafik ey bseg wbevfuj:
Wiri Ofqkanw: Yau doh waca i muof ebou od pcob somi ettkobg ema emh des qi age hgeh sixgeb biux qriwiq.
Qboafesj Gapwedcu Cwiwiq: E wufgko Jaumuxx Dejcawox moc wicjies bulmewyu hwavuc, cmojj iwe yaozmj uumr tu uzl.
Fade Bewapolees: Caitalj Rewxisar ej gamqdqm undusceqig afva Mciwo. Cniq sudququfs, Hdolu xilakowej fuxe spop qgodiquy yywarmcf-tnbec ojreqr ce ppi vivzimt ew xqe cdunux ath edjoczv zurwar rku Houqilw rapa.
IG Suwcoar & Caykisicajiip: Naa hiogfav awuiq OG wegkiuk uzz qoc lo mgioqa muuy uvr IK sajzazateliosf.
Ylehqpimt Rejtiof Jxigix: Dewepqh, yiu vaiwxab wiy oorm ap ic cu bgidvx hopmaum wovyarle lkozat zabsok i GoebozbZog amt.
Ak xhu half myibkuh, qoe’gz ekbehp che UP Duynm Cufe uwn celb ixi dozo sviqa, ltida yai dun xi jisrlez e hioyt yelib qees qery teir ugw buweid ogkzeqdaesn!
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:
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.