Table of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
Chapter 1: Understanding Augmented Reality and ARKit . . . . . . . . . . . . . . . . . . . . . . . . . . .11
Augmented Reality on Mobile Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Introducing ARKit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
System Requirements for ARKit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Chapter 2: Getting to Know ARKit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Understanding the Swift Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
Understanding the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Creating Augmented Reality with the Single View App Template . . . . . . . . . . . . .40
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Chapter 3: World Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
Displaying the World Origin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Resetting the World Origin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66
Displaying Shapes at Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
Adding and Removing Multiple Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87
Chapter 4: Working with Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Displaying Different Geometric Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
Displaying Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97
Adding Textures to Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102
Changing the Transparency of Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110
Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
Chapter 5: Working with Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122
Using Color, Intensity, and Temperature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123
Using a Spotlight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152
Chapter 6: Positioning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153
Defining Relative Positions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153
Combining Geometric Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170
Chapter 7: Rotating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171
Rotating Objects Using Euler Angles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171
Relational Object Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188
Chapter 8: Drawing on the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210
Chapter 9: Adding Touch Gestures to Augmented Reality . . . . . . . . . . . . . . . . . . . . . . . .212
Recognizing Touch Gestures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217
Identifying Touch Gestures on Virtual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . .220
Identifying Swipe Gestures on Virtual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . .226
Identifying Virtual Objects with Pan Gestures . . . . . . . . . . . . . . . . . . . . . . . . . . .230
Identifying Long Press Gestures on Virtual Objects . . . . . . . . . . . . . . . . . . . . . .237
Adding Pinch and Rotation Gestures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245
Chapter 10: Interacting with Augmented Reality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246
Scaling with the Pinch Touch Gesture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251
Rotating with the Rotation Touch Gesture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255
Moving Virtual Objects with the Pan Gesture . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267
Chapter 11: Plane Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268
Displaying Planes as Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276
Placing Virtual Objects on a Horizontal Plane . . . . . . . . . . . . . . . . . . . . . . . . . . .288
Detecting Vertical Planes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304
Chapter 12: Physics on Virtual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305
Applying Force on Virtual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316
Colliding with Virtual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323
Detecting Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .330
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337
Chapter 13: Interacting with the Real World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
Detecting Points in the Real World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .351
Defining a Point in the Real World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .358
Measuring Distance Between Virtual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .368
Chapter 14: Image Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .370
Storing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .374
Detecting Multiple Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379
Displaying Information in Augmented Reality . . . . . . . . . . . . . . . . . . . . . . . . . . . . .384
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .394
Chapter 15: Displaying Video and Virtual Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .396
Displaying Virtual Objects in Mid-Air . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .398
Displaying Video on a Plane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .404
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .410
Chapter 16: Image Tracking and Object Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412
Detecting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .423
Scanning an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .423
Detecting Objects in an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .431
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .440
Chapter 17: Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .442
Saving a World Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .449
Loading a World Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .452
Clearing an Augmented Reality View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .456
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465
Appendix A: Converting 3D Model Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466
Converting COLLADA (.dae) to SceneKit (.scn) . . . . . . . . . . . . . . . . . . . . . . . . . . . .467
Convert 3D Models into a COLLADA (.dae) File . . . . . . . . . . . . . . . . . . . . . . . . . . . .468
Appendix B: Creating Virtual Objects Visually . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .473
Creating a SceneKit Assets Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .474
Creating a SceneKit (.scn) File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .475
Adding Virtual Objects to a SceneKit (.scn) File . . . . . . . . . . . . . . . . . . . . . . . .477
Customizing Virtual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .479
Linking Virtual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .480
Displaying a SceneKit (.scn) File in Augmented Reality . . . . . . . . . . . . . . . . . .481
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .483
Explore how to use ARKit to create iOS apps and learn the basics of augmented reality while diving into ARKit specific topics. This book reveals how augmented reality allows you to view the screen on an iOS device, aim the camera at a nearby scene, and view both the real items in that scene as well as a graphic image overlaid on to that scene.
You’ll start by accessing the camera and teaching your app to track the world around its device. You'll then see how to position nodes and create augmented reality shapes and textures. Next you’ll have your creations interact with their environment by programming workable physics, detecting planes, measuring distance, and applying virtual force. Finally you’ll learn how to hit test and troubleshoot your applications to ensure they interact with the real world around them seamlessly.
ARKit is Apple’s software framework for creating augmented reality apps on iOS devices such as the iPhone and iPad. Unlike virtual reality that creates an entirely artificial world for the user to view and explore, Beginning ARKit for iPhone and iPad will show you how augmented reality places artificial items in an actual scene displayed by an iOS device’s camera.
Programmers familiar with the basics of Swift programming who want to dive into developing iOS applications with Swift.