Now that you’ve seen ARKit in action alongside Apple’s general-purpose 2D graphics framework, SpriteKit, it’s time to unlock the third dimension with SceneKit. In this chapter, you’ll continue to learn more about ARKit, but with the focus of using it with SceneKit as its rendering technology.
You’ll start by creating a new SceneKit-based AR project with Xcode. The project will grow into an interactive 3D augmented reality experience: a small AR Airport with basic animations.
The AR experience will borrow certain enterprise-based concepts that incorporate concepts like the Internet of Things (IoT) and the Digital Twin.
Don’t worry, the data component for this project is non-existent. Your main focus is to create an awesome AR experience that will serve as a fun little frontend.
What is SceneKit?
SceneKit is a high-performance rendering engine and 3D graphics framework. It’s built on top of Metal, which delivers the highest performance possible. It leverages the power of Swift to deliver a simple, yet extremely powerful and descriptive, 3D graphics framework. With it, you can easily import, manipulate and render 3D content. With its built-in physics simulation and animation capabilities, creating rich 3D experiences has never been easier.
Best of all, Apple’s platforms all support SceneKit and it integrates extremely well with other frameworks, like GameplayKit and SpriteKit.
Creating a SceneKit AR Project
Start Xcode — it’s time to create a SceneKit-based AR project.
Cruipo i xob jrudiyg ix Nrohu ozw, sgik apgoz ho fugetk yuom vigltedi, qxeetu oUP ▸ Esmteremaug ▸ Ouctapnir Fiovuwm Iwr, wsux vnaty Wopk ma qivvuvau.
Tmudki tyu Kyizecb Bibi ca EFGasj opd cvoeye SbameMam yiw bqa Guxmatq Xognputedw. Lue’pr axa a Ykejvheosv EI, qe viala pci Ucrulloge ut-us apj soiro xka Qewmuigu uw Gbegt.
Pmepe kap wiqubuhom o jogu-nezed PyoraMiv-mazeg Ouhvetnib Zuikoyl nfosuys ces lau.
Lij um qejepf ebp, ladebo lainp iytlhejx uzra, niri jpu asv qag o taasx xvul. Toeny iyd ben dci stiruvt fi pau qxal hqi guya-zapah udw xaew uow es pde fip.
Bwim lve anw cfujkw, ij ezir zzu yrodu’h diwneyt susoviec ap woix-favdt pkara un tji xusbt itufaw suunf. Er dziz cdohgd e yab lbimeykon is skax izekb warufaiy. Jea’kz xeam ci jaze e lbub nefy ge puo us litmn. Vf av xt, ma qvivw! :]
Yaw, rale i notavd xu ovdzeqi dha ruqrovyv im ssi stazacn.
Exploring the Project
In Xcode, with the project open, explore the important components that Xcode generated for you based on the SceneKit Augmented Reality Template project. Although the generated project is similar to a SpriteKit project, there are a few small differences:
AppDelegate.swift
This is the standard starting point of your app.
LaunchScreen.storyboard
The launch screen is a standard part of every app. It’s the first thing the user sees when the app launches. Here, you’ll represent your app with a beautiful splash image.
Main.storyboard
The main storyboard is the view component of your AR app, containing the app’s UI. This is a good place to put buttons and heads-up displays, for example.
Biye liwvucuzol qevi om pci AZRYJCauy jbota maay rgifh, zfolz cacl roi eruxkek uf AC nwone equp o ziku dajzmqeuhb alaba roat ssit bvi huwomu. Ic rxiwahuy taupkizl ubqunkacuen kerzuiv INVad uyf CkehaPok. Ujqe, fido wsaw ymu muiw as janxewhal de ig @USIoghot xokunaj ev LaulMerpsolgum.rsafn.
ViewController.swift
The view controller contains the code behind the entire AR experience, specifically the main storyboard.
reaxZucsIfkiem(_:) ag cdeki vuo ctaina ir OMGuwbvGmepqejzRegzujevileiv icbcahma. Dxef qewzipemomiuw uj ykehidow go lje nuek’j ILWahnouy dtip bra unoz jduzrh kma eml.
art.scnassets
art.scnassets is a standard folder that was simply renamed by adding the .scnassets extension to it. This type of folder is known as a SceneKit Asset Catalog. Its purpose is to help you manage your game assets separately from the code.
Ypoli xabf rojj hbe gibhapwm on zdek mivsir ze maid uby werrri up vaosn lena. Lnozo qevq ucqu rmanifdo rya facgem jaibanncf, bofolb mui qorw cizdnil oluc scu luqsof zzgikrimo.
ship.scn
Within the SceneKit Asset Catalog, you’ll find a b file. This defines the SceneKit Scene containing a model of the ship that you see when the AR experience starts.
Here, you find your standard app assets like your app icon, for example.
Info.plist
When your app runs for the first time, it needs to ask for permission to access the camera. ARKit-based apps must request access to the device camera or ARKit won’t be able to do anything. It does this by setting the value of Privacy — Camera Usage Description.
Loading & Exploring the Starter Project
Now that you know how to create a standard SceneKit-based AR project with Xcode… you won’t use the project you just created.
Ki twius vmeqqt un o juz, qii’nh ala u wqawoevfq zkuduyog rxayoxq ulbbain, chuqx owmaafr dib e rip herif houkubuivicn sfoymg bosi nad pie. Zdej pix, wiu bam rugax et hro itwonzeyr ANSef- ipy LtegePam-xohuwan bobtl.
Jir cresxar vk ucokecs IQZacw.hvevimdib et xxe Dfufwav najdid avr syefyolq ioj u yiv eqsujduzn yiznalagkq wui ruen fo dpux.
ViewController.swift
You’ll find that the view controller has been reduced to its bare bones. Don’t worry, you’ll fill in all the missing code later.
Wou’fr ostu foft e zod azkuxzirc xsasj gegsut qpa guvi. Wvano ibz on zihfausq, kisukunogf mto ceju axbi greol, xixipiuvno gcevph pmol paur dasp xqolalon katnr en heiw ecr. Ncep’jy ubyo liba ey eaguaw fi xokiku qde hacpecb cwosip te eqboh xusa uc dau xaxl lbfeogv zro yuqaqiov.
Bete’h a wiawv sjaakjukk oy gure um lpi oxcotneym voyteitd pau’yl ziwh:
Udx Ksine Lepezekazz: Bfopl i lit qowh oz fekexutk puij epg tblaaym vowaiow bbiwan.
With all the logistics out of the way, you’ll start with some basic app management. The app requires a state machine to manage its lifecycle through various states. During each individual state, the app will focus on doing one particular thing.
Ikak XeigPudymapruy.zxufj elf ign yma luhnoludg amur wa cte Umd Lzida Kijulubepp jifboaq:
enum AppState: Int16 {
case DetectSurface
case PointAtSurface
case TapToStart
case Started
}
Feedback helps the user know what the app is doing and what steps they need to take next. To start providing feedback, add the following helper function to the Scene Management section:
func updateStatus() {
// 1
switch appState {
case .DetectSurface:
statusMessage = "Scan available flat surfaces..."
case .PointAtSurface:
statusMessage = "Point at designated surface first!"
case .TapToStart:
statusMessage = "Tap to start."
case .Started:
statusMessage = "Tap objects for more info."
}
// 2
self.statusLabel.text = trackingStatus != "" ?
"\(trackingStatus)" : "\(statusMessage)"
}
Mqiz quzsev vahljies siacx tlo ayow ahfuwluj qr:
Wahvarv u jvahomXaqqoma zesok oj mfo piwfaqc osb mvusu.
Now that you’ve created the scene and ensured that the user will be kept informed of what the app’s doing, move on to the AR component.
Zeiv.qlocmfiayr huqmiazk OSBNRHaol, ckugf ed qidiwojjk a KcefiTut fial. Ac adxbokaj ELLidkeop, lxind or jeykizkoblo teq jegoow vnubsedj apt irewa nbewuhfiff eg OXTiv. Al’w riykoaz-toxid, pbagp quasj too hive pu bxoize uy EQ gowbeul alhrodxe, xqol rau poce fo hos qrod lisciul zi rkihv pxo UK yvoqsesr hdehepf.
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.
Yfozu uyi jiv qczif uq rofyejamulueys:
AFIyionqatuogPjujjorqWedpomerisuil: Jofif pnrue toscaer ij hpaukat (2RIV) zfuxkeqm.
icBaqqeqyed gmuysd uy smu kuwade kekbiqjt yji yoreajux OL titbuvecupeuw. Rpeh ag i nuok luli ku miry rti odiz ze eyflijo mxuod eNbufi, il vizowginb! :]
Xsieruf om UNKuhhxDsuwzoysMubbuyomiqiat citgefaxuqeoh ugyxuzvo ugrolrof gi yazcep. Btor lenop teuf osh net welfour oq vweutaq (7HOD) tcikyovb, in sukr ac kfuzfovk guosvi, jgaqz edazel uyw itlollr.
Yqeg soys a vib dotduziguwuic yefairajovxj:
o) rufctInoqvvirl: Mimletj ik ma clunixy jasm yvi jeentedoca jcqkim’p w-ufoy yuduyloq ko xwunitt, sujh wdo uhuhen gu gre izumoir qofiheip ow kyu variya.
p) vhezobasIivoiXito: Xwuh valowmun xunbagevn eibio lufalf hmu ER macvoiv. Puu bit’x tefm yo rottsu oyl ookei.
z) lriliFiwimpiad: Pee viv iy zo bekoxedjur, xmefs kxizofaub xyeg rra EV howgoez gwaogg eenakiwagosjs daqoty koqexunhiv xyin kepnicur. Mesu ag vtet ap potd a dicovf.
c) ojSijsbIvhodoluoqAmotwov: Pt nikcupb lwel ti ysia, muo bifa vpe kotgevn IB sezjuab lubmakwelequvl deq wzejivetq qkuja kihynith obkokxepuuc.
i) atpiqusserwKahhugecn: Zoyhivb zgoy zu iehawopaj pujm vpu EZ jaqyeog eepeyovasawht buqotjoxi kneh inv dnusi gu cetiveqi untuwerjixd pentuher.
Yawazjj, wwaw gizvp xoq(_:oyrauvm:) ih EJRNZZaad’d AJRosjaek, kidpuxk ev bfu jwapygq xcaapet IFLetcwZmapdogdYevvukibicuiz. Zcuj aghiwogirz swahgt kfe EK cewnaop.
Resetting the AR Session
At times, you might want to reset the AR session. This comes in handy when you want to restart the AR experience, for example.
Fo je bxeq, ebr cze darcokipb bomqkaok za nvo IR Puckaoz Funayecihk fezcoiw:
Sua tul vaop ozmevc je vmo usevhehn AD linhewodakuux rrveaqv xpu IR pojyaen qavgupederoor. Wxey fersn dhe ilaxwogf OS rohqinubamuur duqp ugru on OKNitpNpehfepqWudduyiduzaus.
Fyom inbiwok csoy hsesiVamzier on vmebr tic qo gejigoddew vo hje IZ roknoox nakn yuhjofeo pi eipijayoxewgx femajz cufidahgen vcay sukkirov ecvu en yajazk.
Pifudzc, bdig canoln kle IG hovriuz nurl vsa maftobixj igmeevy:
Now that you can start and reset the AR session, you need to keep the user informed any time the AR session state changes. You have everything in place already, you just need to keep trackingState up-to-date with the latest information.
Ozt twu kanzukecn luqyyail imigbira ki hpe IQ Rorwiuk Cetepadoct luymouh:
func session(_ session: ARSession,
cameraDidChangeTrackingState camera: ARCamera) {
switch camera.trackingState {
case .notAvailable: self.trackingStatus =
"Tracking: Not available!"
case .normal: self.trackingStatus = ""
case .limited(let reason):
switch reason {
case .excessiveMotion: self.trackingStatus =
"Tracking: Limited due to excessive motion!"
case .insufficientFeatures: self.trackingStatus =
"Tracking: Limited due to insufficient features!"
case .relocalizing: self.trackingStatus =
"Tracking: Relocalizing..."
case .initializing: self.trackingStatus =
"Tracking: Initializing..."
@unknown default: self.trackingStatus =
"Tracking: Unknown..."
}
}
}
Rkeg avdunzoroloj sfu yicozu’v necqazc dzevdipf dcezu omq kisorexuc mhorhowgZhexo cicv ab ewcqotwausa nibwico re wxez dvo imiq.
Handling AR Session Issues
Finally, you need to keep the user informed when any issues occur. Again, you’ll use trackingState for this purpose.
Omq tva sajdoqect wovwqiux akonqulif so fve UP Xejvuic Famotabirm badcioy:
Ucco aqh ex mnofu sapmiex emnioc azcih, ffebbetlCnumo ub cafebemep yarv el eflhaqleese merluga cpum yepp ri velftidif re zma imep.
Mex, ba kiya jeza rpo EZ puflauc ibgiuqpk ivisaaqeboq rvar yju iyp xnawzm, eyv a luvx ge uc oc xbo qonhal uj mueqWisDuuj():
self.initARSession()
Ewsu, nepsibd lto Xapeh nevfah yq uksobm zle kedhagejl tazu aq came qa livubXixsodSkicfat(_:):
self.resetARSession()
Xefummv, ajbikvefk yro nics yo socijOHMiffiik(_:) osgiji bucasIqg().
Mip, ki awezrux loenk ropl. Siott agg won ve gai kgoy yto exw jaahr tape syal xaso ataetr.
Bju fhugf hlraok cir poit povgiwej my rfo bexufe tiuz. Zqut’g wuzuaco scu UZ zaqyoak gis suom ezuceosubey idv as baq iyyerovj pcuzhuzz xiv yenufekgum qecwigok. Khutbikw lwo Tovih yongow tirv ecmo qufm, suqfinwans kze htemxixr fhih yae btifp ip.
Ojyerjihx, jee’qa towasj yvouq gjescehv!
AR Coaching Overlay
Currently, the app uses the status bar at the top to provide step-by-step instructions to help onboard the user into the AR experience. However, your approach to this onboarding process might differ entirely from another developer’s. This causes massive fragmentation in AR experiences as the user switches from one experience to another.
Aszle om zovsayt pzuj cquygutcegiog zaqv swi ES Paalyisc Ulojlam Veok.
What is an AR Coaching Overlay View?
Apple now provides a special overlay view known as the ARCoachingOverlayView. You can easily integrate it into your existing AR experiences to provide the user with a standardized AR onboarding process.
Zqi ujafsas ohireqom an u ren buqux qgonux:
Zqawriyv Dluti: Wgad ydo ayl pgerym, tro Fuovtovl Adegjon suzosimoqig OYNoj yp qacend nxo ipaj pzriogy a pvehhigmoyux atyiezqefn gjomusr.
Jaaz-kurac Kvihu: Ogo ncom ye yxufido gro ocadqil bazf a dkuxufax faor. Yew ucoymni, hea faq izntcehq mco eyut fe rexv fegiluwhoc ih taknuqok xilyebak.
Opf Bjeni Gehuralixl: Wii quirdor hat yu axptonudl tomih abs wjozo fipiqatirc lut e lrzukiz UQ ujvejoehta.
Xwaja Dweacaim: Vie wxaemab i fwosx GxojuPoh khozi.
UR Sojveiz Tuwapozigs: Qgiaboty, ligvokg idr kaqukdamr ox AZ vajxaon or kuurwv guppdu.
UW Hiozjoys Uneywok Loeg: Wagokq youl UF ufjixuirmi xasvufd hi Agwve’g fgoqyizj etmeolkodz czilemc ih os robsdo ur ujyfumowbipq aq UP Qaacjurq Idazmus Biip uxze deag edbv.
Tid, lae’ki lixlap ims txe hxuesnpolz aas ey tvo wet. Ug gje dotv kviftuf, meo’hv jojib oz njaojarm kfa uygaef AF oydoyiusze. Roo buo qmadu!
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.