Apple ARKit framework allows developers to create augmented reality experiences for iPhone and iPad. It is supported on A9 and higher capable devices.

World Tracking

Under the hood ARKit combines data from device Camera and Motion sensors, apply some complex math and provides device’s position and transform in virtual space with high degree of accuracy. You need to use hit-testing methods to find real-world surfaces corresponding to a point in the camera image. Hit-test results are used to place virtual content in your scene.

Light Estimation

ARKit uses Camera sensor to estimate the total amount of light available in a scene.

Surface Detection

As a bonus ARKit session can be configured to automatically detect flat surfaces like floors and tables and report their position and sizes. When plane is detected ARKit may continue changing the plane anchor’s position, extent, and transform.

Rendering

ARKit provides 3 ways of scene rendering: SceneKit ( ARSCNView ), SpriteKit ( ARSKView ) and Metal. When SceneKit and SpriteKit is used, ARKit automatically displays a live camera image as view backdrop, renders the 2D or 3D overlay content and apply light to create the illusion of that content inhabiting the real world. In case of Metal you to need to make everything manually using camera images, light estimation and position-tracking information to display overlay content.

ARKit main classes

ARSession. A shared object that manages augmented reality experiences. Here is an entry point of ARKit. As a the first step you need to configure and start a session. After that you can pause and reset it.

ARAnchor — A real-world position and orientation that can be used for placing objects in an AR scene. Custom anchors are added and removed throught a ARSession. ARKit also automatically adds anchors when you enable the planeDetection option in a world tracking session.

ARFrame — A video image and position tracking information captured as part of an AR session. It contains a raw camera image, current intermediate results of the scene analysis (feature points) and estimated light condition.

ARCamera — Information about the camera position and imaging characteristics for a captured video frame in an AR session.

Coding

It is required to install Xcode 9 Beta.

Create a new project from “Single View App” template. Set the Project name and language Swift.

Add NSCameraUsageDescription to Info.plist

Add NSCameraUsageDescription key to target’s Info.plist with some string describing your camera usage reason.

Init ARKit view

Import ARKit and SceneKit frameworks Create a lazy sceneView variable to store ARSCNView instance Add sceneView to our host view Resize sceneView Start ARKit session

Build and run and you should see your first ARKit application in action. For now it displays just still camera view.

Track ARKit status

It takes some time for ARKit to analyse current scene and provide us with some results. We need to set a delegate to ARSCNView to track its status changes.

Conform our ViewController to ARSCNViewDelegate protocol. Create a label to display status information. Add the label to host view. Update the label position. Implement protocol function to display status changes.

Place an object into virtual world

If tracking status is “normal” we are ready to place virtual objects into our scene. Lets handle a tap gesture and place a sphere on that position. If a sphere is placed then calculate a distance between last spheres.

Init UITapGestureRecognizer and add it our scene. Handle a tap on screen and get tap location in view’s coordinates system. Perform hit testing against feature points found by ARKit. Here we are searching for a point in 3D space corresponding to a 2D point in the view. This is where ARKit magic happens! As a result it returns a list of ARHitTestResult. Get 3D point coordinates from an ARHitTestResult. Create a SphereNode with found coordinates. Look at full source code for more information. Add the sphere to our scene. Calculate distance between 2 coordinates.

Source Code

Complete demo project is available on GitHub

Available on AppStore!

Fully functional ARKit ruler application is available on AppStore.