Quick Introduction to Image Tracking using AR Foundation

Augmented Reality development has been on a hype since late 2017 when Google launched its ARcore SDK for android devices and Apple launched its ARkit SDK for IOS devices.

But there was a big drawback to that, as developers weren't able to build cross-platform apps for android and IOS devices as both of them had different SDK tools.

Then Unity launched the AR Foundation SDK where AR developers were able to build cross-platform apps by just switching the platform inside the Unity engine.

AR Foundation Chart

Today’s Outline

Today, we will be implementing a basic AR app based on “Image tracking” with absolutely no code required by Unity’s AR Foundation.

What is Image Tracking?

Image Tracking is an augmented reality feature that gives apps the ability to detect 2D images, triggering digitally augmented content to appear in the form of videos, slideshows, 360° panoramas, sound, text, 3D animations, etc.

Example of an Image Tracking Handheld AR app

Requirements

  1. An installation of a stable Unity Engine on Windows or MAC
  2. An AR supported Android or IOS device.

Let’s Get Started

Create a Unity 3D project and call it “Image Tracking” or whatever you like. You can select whatever template you like as we will be changing it later in our Scene. Fill up any Project Name and Location as per your convenience. Click on create when you are done filling up.

Creating a new project

Setting up Unity 3D for AR Development

There are some build setting that we must be aware of before deploying an AR application. I will be walking you through the build settings for deploying an AR app for Android. But in case you are an IOS user, then give a response below, I will then share a tutorial below which IOS users can follow for setting up the build settings for IOS.

Step 1: Open up the Build Settings -> Switch platform to Android -> And click on Player Settings

Step 2: Open up the drop-down menu for Other Settings and change the package name to some like “com.yourcompanyname.yourappname”.

Step 3: Disable Multithreaded Rendering as ARcore doesn't support it yet.

Step 4: Enable Auto Graphics API as ARcore doesn't support the Vulkan Graphics API.

Step 5: Set the minimum API level to be 24, as its the minimum API level required by ARcore.

Step 6: Leave the rests to default.

Setting up build settings

Installing the AR foundation package

Now, head over to the Unity package manager by Window -> Package Manager and Install the latest stable build of AR foundation and ARcore or ARkit considering your current platform Android or IOS.

Installing AR Foundation Packages

Note: The AR SubSystems package is automatically installed after installing the AR Foundation package.

Setting up our AR Scene

Note we have an extra column named XR while importing inside the scene. If you are unable to see that kindly recheck if you have installed the correct packages.

Setting up the AR Scene — I

Moving forward, we will be needing AR Session Origin and AR Session. Note that we have an AR Camera inside the AR Session Origin. This will be the Default Camera of our scene and therefore we can go and delete the Main Camera imported after selecting the 3D Template.

Don’t forget to save your scene.

Setting up the AR Scene — II

Implementing Image Tracking

Let's go to AR Session Origin and add the “AR Tracked Image Manager” Script after clicking the add components button.

Adding AR Tracked Image Manager Script

So, here we will be having three components:

  1. Reference Library: The images which you want to track.
  2. Max number of moving images: As the name suggests.
  3. Tracked Image Prefab: The prefab to display after the image is tracked.

So, as we have seen that we have to provide a Reference Library to the AR Tracked Image Manager Script, we will go ahead and create one.

Creating a Reference Image Library

After creating the Reference Image Library we should add an Image which will be the used for tracking. You can get some images from here.

Note: You can add as many images as you want just click on Add Images and drag the image inside it.

Adding Image to the Reference Image Library

Great, now as our Reference Image Library is ready, we will be now needing the Tracked Image Prefab.

Now, as we are going to implement a video poster we will create a Cube Prefab and then add the video player to it. You can get some videos from here. So, let's do it.

Step 1: Create a 3D Object Cube and Set up the dimensions.

Step 2: Drag it down to our Assets folder and then add the video player to it.

Step 3: You may enable “loop” and disable “wait for first frame” as per your wish.

Well now, we have the prefab set up.

Setting up the Tracked Image Prefab

Now, drag your Reference Image Library and your Tracked Image Prefab inside the AR Tracked Image Manager Script of the AR Session Origin.

AR Session Origin

Great now, our AR app is ready to build and run. Head over to build settings and build and run your app.

Note: Make sure you have connected your Android device with the USB cable and have USB debugging enabled. If you are facing any issues, you can follow the steps given here.

Some of the issues you may come across

Issue 1: If you are getting this dialog box after build and run. Then you can simply visit this site for the fix. The Unity 2020 build may not face this issue.

Build Failure Dialog Box

Issue 2: If your video player is inverting the video you may do the following changes.

Sirius Black Video Settings

Final Build

Now is the time that we test our hard work that we spend the last couple of minutes. Let’s go Build Settings -> Build and Run.

It must Build successfully and you may have a similar output.

The Output of our Image Tracking AR Project

You can explore other examples of this project here.

Conclusion

Congratulations guys, you made it through. You can now implement Image Tracking in your AR application. But now the question arises is, what’s next?

AR Foundation provides a lot of other features such as Plane Detection, World Tracking, etc. You can find the complete documentation here and explore various sections by yourself.

Hopefully, this tutorial was helpful to you guys. In case, if anyone’s stuck you may clone the project from here. Also, you all are free to ask any of your doubts below or connect with me on LinkedIn, I would be happy to help you out.

Thank you for reading, see you guys next time. And don’t forget to leave a bunch of claps, if this blog helped you.

--

--