A Beginner’s Guide to ARKit Part-2
Overview
This article encapsulates the basic and foremost important classes and concepts that together formulates an AR experience for a user using ARKit.
If you are new to AR or ARKit and would like to read an introductory article, jump to Part-1
Fundamental Classes and Concepts
ARSCNView
ARSCNView
is a subclass of SCNView
which is the standard SceneKit
view for rendering 3D content. This renderer provides an ARSession
object which upon running automatically does the following —
- Renders the live video feed from the device camera as the scene background
- The world coordinate system of the view’s SceneKit scene directly responds to the AR world coordinate system established by the session configuration.
- Moves its SceneKit camera to match the real-world movement of the device.
ARSession
Apple is huge fan of sessions. For example, with URLSession
your application is able to send network requests and receive response in return. ARKit is also a session based framework which uses ARSession
class to coordinate major processes for ARKit such as motion tracking, camera feed analysis etc.
Configuration Objects
Configuration objects define how ARKit sets up and runs your augmented reality session. It helps AR configure, detect and track a certain type of content.
Anchors
ARAnchor
is an invisible null-object that can hold a 3D model at anchor's position in World Space. Think of ARAnchor just like it's a transform node with local axis (you can translate, rotate and scale it) for your model.
ARAnchor
doesn't automatically track a real world target. If you need automation, you have to use configuration objects
within rendered(...)
or session(...)
instance methods that you can call if you conformed to protocols ARSCNViewDelegate
or ARSessionDelegate
respectively.
Content in SceneKit
The SCNScene
is the root of a world and is viewed by SCNCamera
. When using ARSCNView
, SceneKit
uses AR inputs to control the camera.
The SCNScene
RootNode acts as the root in the hierarchy of SCNNodes. Generally every SCNNode
has a name and transform in 3D-space. A SCNNode can have one or more SCNNodes as child and each child is positioned relative to the parent SCNNode.
A SCNNode
can encompass components with behaviour such as -
ARRayCastQuery
Ray-Casting is a concept used to find the 3D position in real world by projecting an imaginary ray from a screen point onto the detected surface. ARKit can ray cast against a detected plane bounds or geometry or raw features called feature points. Ray-casting is generally used to request input from user for experiences such as where to place a virtual content in real world, interact with the virtual content etc.
Now that you are familiar with ARKit, Lets peek that interest with some magic. Don’t let the muggles convince you otherwise.
We begin by initializing an AR app in Xcode, I’ll name mine Daily Prophet. We aim to build an app that scans a Daily Prophet image and plays video on tracked images.
Xcode adds all the dependencies and sets up a default scene for us. Let’s begin with the code —
- Scrap a Daily Prophet image off the internet and take screenshot of the images you would like to track in your paper
- The next step is to incorporate our images for
ARImageRecognition
. LocateAssets.xcassets
folder and add AR Resource Group - Drag and drop images that you want to track
In View Controller, Now that we need to track images, we change the tracking from ARWorldTrackingConfiguration
to ARImageTrackingConfiguration
- In between creating and running the configuration, we need to tell it which images to track. Hence we create an object called trackedImages
ARSCNView
provides a delegate that hooks us with renderer overload methods. Hence we set theARSCNViewDelegate
to self inviewDidLoad
- In the
renderer
method we check if the anchor is ofARImageAnchor
type i.e. AR found the referenced image - Create video scene that can be superimposed on the image that AR tracked and placed an
ARImageAnchor
on.
So As Dumbledore once said
Of course it is happening inside your head, Harry, but why on earth should that mean it is not real?
Tada ! We our own Daily Prophet :)