Augmented reality (AR) is an interactive experience of a real-world environment where the objects that reside in the real-world are “augmented” by computer-generated perceptual information.In this post I will be discussing how to build a simple AR application for android devices. There are several approaches to making Augmented Reality apps. We will be using a marker based approach for making our AR app for mobile devices.
Tools Required
- Android SDK tools
- Unity 3D
- Vuforia 5
Marker/Image Target
- This is an image that is recognized and tracked by a mobile device’s camera.
- This acts as an origin for augmentation
- Once recognized, content can be displayed with respect to it.
Getting Started With Vuforia
To get started, we will need to go to Vuforia Developers Portal and create an account.
- Create a license key for our application
- A license key is a unique ID that is required to create an app in Unity that uses Vuforia.
- To create a license key, we need to head over to “Develop -> License Manager” and select “Add License Key”.
- Once we fill in the details, we can select “Next” and then “Confirm”.
- This will direct us back to the “License Manager” page where the application’s name will appear.
- Once we select “Project Name”, we will get the license key. This key will be used later in Unity.
- Create a Database and an Image Target
- The next step is to set up an image as a target.
- We head over to “Develop >> Target Manager” and select “Add Database” and fill in the details
- Next, we will need to add images to the newly made database
- we select the database and click on “Add Target”
- Vuforia supports various kinds of targets like a single image, cylindrical, cuboid al, 3D image, etc
- Any arbitrary value can be selected for width, say 50.
- After a short upload time, the target will be added and we will have a “Download Dataset” option.
- An important thing to note here is the “Rating”. A good rating means that it can be used for tracking, whereas a bad rating means that the image does not have enough feature points (interesting points in the image like edges, corners, etc which are used for tracking in image processing).
- We can also have a look at the feature points of our image by selecting “stones_hires >> Show features”
- Download the Data set
- download the dataset by selecting “Download Dataset >> Unity editor”.
- This will generate a Unity package containing traceable information about this traceable database.
Integrating with Unity
- We will have to import the Vuforia Unity packages. This can be done by going to “Assets >> Import Package >> Custom Package”.
- We will have two packages, one corresponding to our dataset (Image_Targets.unitypackage) and the other corresponding to Vuforia 5 SDK for Unity (vuforia-unity-5-0-5.unitypackage)
- add some 3D models by going to “Window >> Asset Store”
- Next, we will have to add an ARCamera to our scene. “ARCamera” is an Augmented Reality camera prefab from Vuforia and is very similar to a regular camera
- We can find this under “Assets >> Vuforia >> Prefabs” and drag it into our scene at X=0, Y=0, Z=0.
- Delete the Main Camera from the scene (it can be found under the Hierarchy Panel, “Right Click >> Delete”)
- For our ARCamera to run, we will have to add the App License Key
- In the Inspector panel, we will have to paste the unique key we had previously received from Vuforia’s License Manager.
- add the ImageTarget to our scene. It can be found under “Assets >> Vuforia >> Prefabs”. Let’s add it to our scene at X=0, Y=-50, Z=0.
- Under the “Image Target Behaviour (script)” we will have to change “Data Set” to “Image_Targets” and “Image Target” to “stones_hires”.
- drag 3d Module onto the Scene. It can be found under “Assets >> prefab>>….”.
- For the 3D Model to appear over ImageTarget, it needs to be made a child of ImageTarget (can be done by just dragging the 3D model prefab in the Hierarchy panel).
- Then, whenever the ImageTarget is detected by a mobile device’s camera, all the children of the target will also appear together.
- Now we need to activate the Image_Targets Dataset. This can be done by selecting “ARCamera”, and in the Inspector panel, under “Dataset Load Behavior (Script)”, we need to select “Load Data Set Image_Targets” and then “Activate”.
Building the Android project
- We need to go to “File >> Build Settings”.
- We need to add the current scene by selecting “Add Current”.
- Then, we need to select a platform (Android). Here, we will have multiple options:
- Google Android Project: This will allow us to export the current Unity project to Android Studio so it can be edited and used to add more elements.
- Development Build: Enabling this will enable Profiler functionality and also make the Autoconnect Profiler and Script Debugging options available.