Corner and surface detection in AR Part 1

Introduction

AR technology is getting more and more popular these days. Two big companies have their own implementation of it. Google has ARCore supporting devices with Android and Apple has ARKit for iOS. These technologies require high computing power so only newer devices are supported.

Both ARCore and ARKit have a feature to detect horizontal and vertical surfaces. Let’s check it out and do something interesting.

How about placing some cool object right in the corner of your room? Soon you will be able to do it with your phone!

 

Importing ARKit to Unity

First step is to create new project in Unity (in this tutorial version 2018.2.0f2 of Unity will be used) and import ARKit plugin. There are two ways to import it:

  1. Download plugin from asset store: https://assetstore.unity.com/packages/essentials/tutorial-projects/unity-arkit-plugin-92515
  2. Download plugin from official repository https://bitbucket.org/Unity-Technologies/unity-arkit-plugin and simply copy folder UnityARKitPlugin to your Unity project.
Picture 1 UnityARKitPlugin import from repository.

Picture 1 UnityARKitPlugin import from repository.

 

 

 

 

 

 

I would recommend using option 2. Thanks to using repository you will be up to date with all amendments and fixes.

Get started with UnityARKit Plugin

If this is your first time with UnityARKit Plugin I would recommend you to visit Unity blog site (https://blogs.unity3d.com/2018/02/16/developing-for-arkit-1-5-update-using-unity-arkit-plugin/) to make this start easier.

For quick start copy the scene EditorTestScene from UnityARKitPlugin/ARKitRemote folder. Then remove objects Cube and HitCubeParent. Now change important setting for ARCameraManager – change PlaneDetection to Horizontal and Vertical.

Picture 2 UnityARCameraManager settings in scene.

Now you can build the app and check out how ARKit works on the device. Before you start building, make sure you added CameraUsageDescription. You can do it in Unity Player Settings in Other Settings:

Picture 3 Camera Usage Description in Unity.

or later on in XCode in Project Info:

Picture 4 Camera Usage Description in Xcode.
Picture 5 ARKit default surfaces detection.

Detected surfaces

So you already have an application that recognizes surfaces. It works quite precisely on floor with carpeting or on walls with posters. Current technology cannot handle plain surface such as white walls. Then you can get some sheet of paper or poster and put it on the wall. This will help the app detect surfaces quickly and precisely.

Now let’s checkout how to handle data given by ARKit. Searching through the code of ARKitPlugin you can find methods that are responsible for spawning objects after detecting the surface.

Code Snippet 1 UnityARAnchorManager – on surface detect event.

As you can see in code there are three events: ARAnchorAddedEvent, ARAnchorUpdatedEvent and ARAnchorRemovedEvent. We are interested the most in the first one.

Now create simple script to handle data returning by ARKit Plugin. From class ARPlaneAnchor we can get position and rotation of detected surface. These two values will be useful in next steps.

Code Snippet 2 CustomSurfaceDetector -how to get position and rotation of  detected surfaces from AR.

Knowing rotation of each detected plane we can easily calculate rotation between surfaces and designate three perpendicular surfaces. Three perpendicular planes will always create a corner.

If the second surface is perpendicular to the first one then we have to find third one that is perpendicular both to first and second one. That will give us certainty that we are detecting a room corner.

Code Snippet 3 CustomSurfaceDetector – Calculate perpendicular surfaces.

Summary

Now you should know how to use ARKit to detect surfaces in your room using mobile app. You also know what data you can get from ARKit Plugin and how to use it.

For now you can display debug logs on screen about detected and perpendicular surfaces and check app if it works properly.

Picture 6 Application preview.

In the next part you will learn how to replace default game objects that ARKit Plugin is creating with your own objects with proper position and rotation. You will also learn how to move and rotate these object so they will look better for you to imagine how surfaces are placed in 3D space.

related
Tutorial
[Tutorial] – Create a Procedurally Generated Terrain for a 2D Infinite Runner Game in Unity
Have you ever been thinking of creating infinite runner game with the Unity? This kind of games...
11
Google Play InstantIntermediate
Google Play Instant Games – How to add Google Play Instant to Google Play Store
Previous post, available here: Google Play Instant Games - Development and Optimization, mentioned...
0
Google Play InstantIntermediate
Google Play Instant Games – Development and Optimization
In the last part of our post available here: Google Play Instant Games - Basics and Business, we...
0
Call The Knights!
We are here for you.
Please contact us with regards to a Unity project below.



The Knights appreciate your decision!
Expect the first news soon!
hire us!