Table Of ContentMastering
ARKit
Apple’s Augmented Reality App
Development Platform
—
Jayven Nhan
Mastering ARKit
Apple’s Augmented Reality
App Development Platform
Jayven Nhan
Mastering ARKit: Apple’s Augmented Reality App Development Platform
Jayven Nhan
Waterloo, ON, Canada
ISBN-13 (pbk): 978-1-4842-7835-2 ISBN-13 (electronic): 978-1-4842-7836-9
https://doi.org/10.1007/978-1-4842-7836-9
Copyright © 2022 by Jayven Nhan
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Aaron Black
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a
California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for
reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub via the book's product page, located at https://github.com/
Apress/Mastering- ARKitFor more detailed information, please visit http://www.apress.com/
source- code.
Printed on acid-free paper
Table of Contents
About the Author ��������������������������������������������������������������������������������xv
About the Technical Reviewer ����������������������������������������������������������xvii
Chapter 1: Why Augmented Reality? ����������������������������������������������������1
The Three A’s: Accessibility, Assistance, Automation ���������������������������������������������3
Real Estate ������������������������������������������������������������������������������������������������������������5
Education ��������������������������������������������������������������������������������������������������������������8
Closing �������������������������������������������������������������������������������������������������������������9
Why ARKit? ����������������������������������������������������������������������������������������������������10
Apple’s Core Values ���������������������������������������������������������������������������������������10
Versus Cross-Platform Software �������������������������������������������������������������������11
Riding the Technology Momentum ����������������������������������������������������������������13
Apple’s Documentation ����������������������������������������������������������������������������������14
Apple’s Developer Community Documentation ���������������������������������������������14
The Apple Audience ���������������������������������������������������������������������������������������15
Conclusion �����������������������������������������������������������������������������������������������������15
Chapter 2: Introduction to ARKit: Under the Hood and Matrixes ��������17
What Is an Array? ������������������������������������������������������������������������������������������������19
What Is a Matrix? ������������������������������������������������������������������������������������������������19
Matrixes: Addition and Subtraction Operations ���������������������������������������������������20
Matrix Identity �����������������������������������������������������������������������������������������������������22
iii
Table of ConTenTs
Setting Up Playground ����������������������������������������������������������������������������������������23
Setting Up Scene View ����������������������������������������������������������������������������������������26
Matrix Translation �����������������������������������������������������������������������������������������������28
Matrix Rotation ����������������������������������������������������������������������������������������������������31
Matrix Scaling �����������������������������������������������������������������������������������������������������33
Up Next ���������������������������������������������������������������������������������������������������������������35
Graphics Frameworks for ARKit ��������������������������������������������������������������������������35
SpriteKit ��������������������������������������������������������������������������������������������������������������36
SceneKit ��������������������������������������������������������������������������������������������������������������36
Metal, RealityKit, Reality Composer ��������������������������������������������������������������������37
Up Next ���������������������������������������������������������������������������������������������������������������38
Session Life Cycles and Tracking Quality ������������������������������������������������������������38
Session Life Cycles ���������������������������������������������������������������������������������������������38
Not Available Tracking State ��������������������������������������������������������������������������39
Limited Tracking State �����������������������������������������������������������������������������������39
Normal Tracking State �����������������������������������������������������������������������������������40
Tracking Quality Transitions ��������������������������������������������������������������������������������40
Relocalization Without a World Map ��������������������������������������������������������������������41
Relocalization with a World Map �������������������������������������������������������������������������42
Up Next ���������������������������������������������������������������������������������������������������������������43
Chapter 3: Designing an Augmented Reality Experience��������������������45
Ideas �������������������������������������������������������������������������������������������������������������������46
Why Start with the User Experience? ������������������������������������������������������������46
Augmented Reality Benefits ��������������������������������������������������������������������������47
Idea Case Studies ������������������������������������������������������������������������������������������48
The Startup ���������������������������������������������������������������������������������������������������������52
iv
Table of ConTenTs
User Environment ������������������������������������������������������������������������������������������������52
Scale ��������������������������������������������������������������������������������������������������������������52
Table Scale ����������������������������������������������������������������������������������������������������53
Room Scale ���������������������������������������������������������������������������������������������������53
World Scale ���������������������������������������������������������������������������������������������������53
Defined and Undefined Space �����������������������������������������������������������������������54
Augmented Reality Experience Prototyping ��������������������������������������������������������54
Physical Prototyping �������������������������������������������������������������������������������������������55
Reality Composer ������������������������������������������������������������������������������������������������57
Conclusion ����������������������������������������������������������������������������������������������������������58
Chapter 4: Building Your First ARKit App with SceneKit ��������������������59
Purpose ���������������������������������������������������������������������������������������������������������������59
Content ����������������������������������������������������������������������������������������������������������������60
Creating a New Project ���������������������������������������������������������������������������������������60
Setting Up ARKit SceneKit View ��������������������������������������������������������������������������61
Connecting IBOutlet ��������������������������������������������������������������������������������������������64
Configuring ARSCNView Session ������������������������������������������������������������������������65
Allowing Camera Usage ��������������������������������������������������������������������������������������67
Adding 3D Object to ARSCNView �������������������������������������������������������������������������68
Adding Tap Gesture Recognizer to ARSCNView���������������������������������������������������71
Removing Object from ARSCNView ���������������������������������������������������������������������72
Adding Multiple Objects to ARSCNView ��������������������������������������������������������������73
Implementing Pan Gesture Recognizer on ARSCNView ��������������������������������������76
Trying Out the Final App ��������������������������������������������������������������������������������������78
Wrap Up ��������������������������������������������������������������������������������������������������������������78
v
Table of ConTenTs
Chapter 5: Understanding and Implementing 3D Objects �������������������79
What You’re Going to Learn ���������������������������������������������������������������������������������79
3 D Objects Creation Toolsets ������������������������������������������������������������������������������80
Online Resources for 3D Objects �������������������������������������������������������������������81
SceneKit Supported Format ��������������������������������������������������������������������������81
Getting Started with the Starter Project ��������������������������������������������������������81
Implementing a Single Node 3D Object ���������������������������������������������������������83
Adding Basic Lighting �����������������������������������������������������������������������������������������85
Implementing a Multiple Nodes 3D Object ����������������������������������������������������������87
Loading USDZ into Scene View ���������������������������������������������������������������������������89
Wrap Up ��������������������������������������������������������������������������������������������������������������91
Chapter 6: Detecting Horizontal Planes and Adding 3D Objects
with Raycast in SceneKit ��������������������������������������������������������������������93
What We Are Going to Learn �������������������������������������������������������������������������������93
What’s a Horizontal Plane? ���������������������������������������������������������������������������������95
Let’s Begin to Build the App ��������������������������������������������������������������������������������95
Detecting Horizontal Planes ��������������������������������������������������������������������������������96
Adding Objects on Horizontal Planes ����������������������������������������������������������������100
Raycasting for Physical World Surface Positioning �������������������������������������������102
Refactoring for Segmented Control and Adding Additional Objects ������������������104
Up Next �������������������������������������������������������������������������������������������������������������108
Chapter 7: Understanding Physics by Launching a Rocketship �������109
Physics Body Explained ������������������������������������������������������������������������������������110
Physics Body Types �������������������������������������������������������������������������������������������110
Static �����������������������������������������������������������������������������������������������������������110
vi
Table of ConTenTs
Dynamic �������������������������������������������������������������������������������������������������������111
Kinematic ����������������������������������������������������������������������������������������������������111
Creating a Physics Body������������������������������������������������������������������������������������111
Attaching a Static Physics Body ������������������������������������������������������������������������112
Attaching a Dynamic Physics Body �������������������������������������������������������������������114
Applying Force ��������������������������������������������������������������������������������������������������115
Adding SceneKit Particle System and Changing Physics Properties ����������������118
Adding Swipe Gestures �������������������������������������������������������������������������������������122
Showtime ����������������������������������������������������������������������������������������������������������123
Implementing Custom Object Collision Detection Logic �����������������������������������124
Category Bitmasks ��������������������������������������������������������������������������������������124
Collision Bitmasks ���������������������������������������������������������������������������������������124
Contact Test Bitmasks ���������������������������������������������������������������������������������125
Implementing the Solution ��������������������������������������������������������������������������125
Chapter 8: Light Estimation and Environment Texturing ������������������133
What You’ll Implement and Learn ���������������������������������������������������������������������133
Getting Started ��������������������������������������������������������������������������������������������������134
Creating a Sphere Node ������������������������������������������������������������������������������������135
Adding Light Node ���������������������������������������������������������������������������������������������136
Testing Light Properties ������������������������������������������������������������������������������������138
Showing/Hiding the Light Estimation Switch ����������������������������������������������������140
Working with the Light Estimation Switch ��������������������������������������������������������141
Implementing Light Estimation �������������������������������������������������������������������������141
Trying Out the App Demo ����������������������������������������������������������������������������������143
Environment Texturing ��������������������������������������������������������������������������������������145
Physically Based Lighting Model, Metalness, and Roughness ��������������������������146
vii
Table of ConTenTs
Chapter 9: 2D Image Recognition and Filter Modification ����������������153
What You’ll Build �����������������������������������������������������������������������������������������������154
Getting Started ��������������������������������������������������������������������������������������������������155
Enabling Image Recognition in ARKit ����������������������������������������������������������������156
Physical Image Size ������������������������������������������������������������������������������������������157
Image Properties �����������������������������������������������������������������������������������������������158
Setting Configuration for Image Recognition ����������������������������������������������������159
Recognizing Images with ARImageAnchor �������������������������������������������������������160
Testing the Demo App ���������������������������������������������������������������������������������������161
Overlaying 3D Objects on Detected Images ������������������������������������������������������163
Dynamically Create ARReferenceImage Programmatically �������������������������������164
Chapter 10: Saving and Restoring World Mapping Data to
Create a Persistence AR Experience �������������������������������������������������171
Getting Started ��������������������������������������������������������������������������������������������������172
Working with ARWorldMap��������������������������������������������������������������������������������173
Setting Up the World Map Local Document Directory ���������������������������������������174
Archiving an AR World Map as Data ������������������������������������������������������������������175
Saving an AR World Map Data into Your Document Directory ���������������������������175
Loading an AR World Map Data from Your Document Directory ������������������������177
Setting the Scene View Configuration’s Initial World Map ��������������������������������179
Persistence AR Demo ����������������������������������������������������������������������������������������179
Visualizing a World Map in 3D Space with ARPointCloud ���������������������������������180
Conclusion ��������������������������������������������������������������������������������������������������������187
Chapter 11: Advancing App with Real-Time Image Analysis,
Machine Learning, and Vision ����������������������������������������������������������189
Getting Started ��������������������������������������������������������������������������������������������������190
Creating an Image Analysis Request for Your Core ML Model ��������������������������190
viii
Table of ConTenTs
Classifying Camera’s Pixel Buffer from Video Frame ����������������������������������������194
Controlling Toy Robot with Hand Gesture Analysis ��������������������������������������������198
Adding Toy Robot upon Plane Detection �����������������������������������������������������������199
Showtime ����������������������������������������������������������������������������������������������������������201
Chapter 12: Crafting 3D Assets ���������������������������������������������������������205
Crafting a Tower in SpriteKit �����������������������������������������������������������������������������207
Crafting a Fireball in SceneKit ��������������������������������������������������������������������������213
Crafting a Troop in Reality Composer ����������������������������������������������������������������217
More Tools ���������������������������������������������������������������������������������������������������������237
Conclusion ��������������������������������������������������������������������������������������������������������238
Chapter 13: Creating Immersive Audio Experiences ������������������������239
Implementing Spatial Sound Awareness ����������������������������������������������������������240
Stereo to Mono Audio Channels Conversion Tools ��������������������������������������������244
Chapter 14: Working with SpriteKit and ARKit ���������������������������������245
Getting Started ��������������������������������������������������������������������������������������������������246
Working with SKScene ��������������������������������������������������������������������������������������247
What Is SKScene? ���������������������������������������������������������������������������������������247
Setting Up a Custom SKScene ���������������������������������������������������������������������247
Randomly Placing 2D Object in 3D Space ���������������������������������������������������������248
Positioning a SpriteKit Node on Tap ������������������������������������������������������������������250
Detecting Change in Time in SKScene ��������������������������������������������������������������252
Detecting Touches in SKScene ��������������������������������������������������������������������������254
Setting Up SKScene with View Controller ���������������������������������������������������������256
Setting Up the Time Label’s Timer ��������������������������������������������������������������������257
Creating the SpriteKit Node ������������������������������������������������������������������������������259
Working with ARSKViewDelegate ���������������������������������������������������������������������260
ix