DevStory

Part 2. Building an iOS AR App with ARKit

Sharing the development of a realistic iOS AR application using ARKit.

Part 1. Building a More Realistic AR App with Deep Learning
✔︎ Part 2. Building an iOS AR App with ARKit
Part 3. Creating a Custom YOLO v3 Model
Part 4. Deploying a YOLO v3 Model on iOS with CoreML


Developing the iOS AR Application

The application used in the project was built using some of the code from Apple's Sample Code Placing Objects and Handling 3D Interaction.

The iOS application was implemented using ARKit and SceneKit. There are already many articles on basic AR application development, so I'll only explain the project's core features: how to cast shadows from virtual objects and how to implement realistic virtual lights.

✔︎ Outline

  • How to cast shadows from virtual objects
  • Implementing realistic virtual lights

How to Cast Shadows from Virtual Objects

First, to render shadows for virtual objects, you need Plane Detection. Plane Detection can be easily implemented with ARKit's World Tracking. You can set the planes you want to detect through the PlaneDetection option of ARWorldTrackingConfiguration.

Floors are horizontal, and walls are vertical.

let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal, .vertical]

After that, all Plane information observed with the rear camera can be received as ARPlainAnchor objects in the session(_:didAdd:) function of ARSessionDelegate. Using this information, you place a transparent SCNPlane on which shadows will be rendered.

Placing a transparent SCNPlane

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
  guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
  if (planeAnchor.alignment == .vertical) { return }
  let width  = CGFloat(planeAnchor.extent.x)
  let height = CGFloat(planeAnchor.extent.z)
  let plane  = SCNPlane(width: width, height: height)
  let planeNode = SCNNode(geometry: plane)
  plane.firstMaterial!.colorBufferWriteMask = [] // transparent

  let x = CGFloat(planeAnchor.center.x)
  let y = CGFloat(planeAnchor.center.y)
  let z = CGFloat(planeAnchor.center.z)
  planeNode.position = SCNVector3(x, y, z)
  planeNode.eulerAngles.x = -.pi / 2
  node.addChildNode(planeNode)
}

Now that the floor to draw shadows on is done, let's place the lights next.


Implementing Realistic Virtual Lights

To create realistic virtual lights, you need the real light's position, intensity, and color temperature.

✔︎ Data needed to implement realistic virtual lights

  • The light's 3D position
  • The light's intensity and color temperature

For the light's position, I collected the real light's position using the Google Vision API and the YOLO model. For how, please refer to the Light Recognition Using Deep Learning section of Part 1.

For the light's intensity and color temperature, I used ARKit's Light Estimation. The Light Estimation object holds the intensity and color temperature values of the light coming in through the camera.

Having collected the real light's 3D position, intensity, and color temperature, you can put them into the properties of SceneKit's SCNLight to implement a virtual light at the same position as the real one, with the same intensity and color temperature. Also, by applying the Light Estimation information every frame, the virtual light's brightness can be updated according to the brightness of the real light.

SCNLight supports the following four types, and among them, shadow casting is only supported by the directional and spot types, and shadows are cast only if you change the castsShadow option to True.

The directional type was used in this project. Source - https://developer.apple.com

The directional type was used in this project.
Source - https://developer.apple.com

When you place a virtual light with the setup above, the virtual objects feel as realistic as if shadows were drawn by recognizing the real light.

Before (left) and after (right) light recognition: afterwards the AR object gets a shadow matching the real light's position and color, so it looks more real.
The position, intensity, and color temperature of the real light are reflected, so you can experience more realistic AR.

Closing

That concludes building a realistic iOS AR app, and in Part 3, I'll share how to create the YOLO model that detects the position of lights in real time.

Part 1. Building a More Realistic AR App with Deep Learning
✔︎ Part 2. Building an iOS AR App with ARKit
Part 3. Creating a Custom YOLO v3 Model
Part 4. Deploying a YOLO v3 Model on iOS with CoreML

Comments