Monday, April 25, 2016

AR (Augmented Reality) Tutorial using Vuforia and Unity

How to make an AR app for Android Using Unity and Vuforia



Installing Unity

If you don't have Unity, download it here https://unity3d.com/get-unity/download

Select on the button that says Choose Your Unity + Download. Scroll down, under Personal Edition, click on free download. Now download the install on the next page. Click download installer. Make sure Android Build support (or iOS Build Support for iPhones) is checked. Ensure you have the same boxes checked as below (with iOS as an option).




After the installation is complete, lets install Vuforia.


Installing Vuforia


Scroll down the page a little bit and look for a button that says Download SDK ->
Click that button.

On the next page click Download for Unity.
(Note there is a download for Android and IOS. For using Unity those are not what we need.)

If you do not have an account (assuming you don't) click the Create account now button.
Follow the instructions on creating an account.

Now that you have an account and download the Unity version.  You should have a file called vuforia-unity-5-5-9.unitypackage. (Or something similar depending on the version). For now just keep track were you downloaded the file.

Now we have to set Vuforia up. Go to their main page. Click on the Develop tab at the top. You should have License Manager highlighted. Click on Add license Key.


After clicking Add License Key, you should see something like below. Fill it out and click next




After clicking next you have to accept the terms on their next page by clicking confirm. Then it should take you back to the License Manager.




Once it finishes, it should be present on the screen. (I now have 2 of them up, note sometimes its not active right away)

Click on the file name (AR_Program) and it should look like below.



The blue box should have a key. This key you need to copy in paste into Unity. Save this key. We will use it later.


Now we need to Create a Database in Vuforia. Go back to Vuforia, make sure Develop is selected. Now click on Target Manager. Then click on Add Database.  Add a name. Lets call it ImageDB , ensure device is selected and click create. After it is done, it should look like below.

Now Click on ImageDB. Now we need an image to upload. Right click this image and save image as rocks.jpg
.

Once this file is saved lets add the target. Click Add Target, and select our new image. Click browser, select the image. Choose 50 as the width.






Now Click Add. It may show processing under status. Just give it a few minutes and it should process. Wait 1 Minute and click refresh.


Once its done processing, it should show a rating and say active. Our rocks got 4/5 stars. Thats is acceptable for now.


Now click on the rocks. The following screen should show. If you want to see the image mapping, click show Features. Copy the Target ID. We will need this later.








Click back, then check the box next to rocks. Then click Download Dataset (All) A window should pop up.



Select Unity editor, and click download. Now you should have a second file downloaded for unity. (Remember you had one earlier)



Open Unity


Now lets Open Unity. Name your project ARprogram. Make sure 3d is Red and click create project.

Now lets import that packages we made. Go to Assets, Import Packages, Custom Package. Select the

Assets, Import Package, Custom Package. Pick one of the two packages and click open. Ensure all is selected and click import.  Repeat this step for the other package.

(vunforia-unity.5....package and ImageDB.unitypackage)




Now lets find an asset from the asset store. You can really download and use almost any 3d Image, but lets do a butterfly. Search for Butterfly and download the one below.


Now we need to add a ARCamera to our scene. The camera is a prefab from Vuforia and is like the regular camera. Delete the regular camera. Now go to Assets, Vuforia, Prefabs and drag ARCamera to our scene. Ensure all positions and rotations are all 0.








Now lets add the License Key. Double click on the ARCcamera.







 Fill in App License Key. (Remember this is the big key from their website, from Vuforia's License Manager)

Yours should look the same as the posted pictures. Lets make the positions 0 , 1, 0.




Now lets add ImageTarget to our scene. (Drop it in the hierarchy)






Now Set your image Target Behavior scrap to the following.


Now drag the butterfly prefab onto the scene. Make sure to drop butterfly on Image Target.




Change the scale to 0.5 0.5 0.5. And change the position to 0, 1 , 0.

Now Click back on ARCamera. Under Database Load Behavior (Script) check load Image DB Database box, and then click active below it.



Now go to file, build settings, click on player settings. (Note: Make sure android is selected. If its not select it, and click switch platform. Then click  Player Settings. )

On the top right change the default company to ARapp. Then down below change bundle identifier. change to



Now your program is ready to run. Save the scene and save the project. You can run it. Hold the picture of the rocks in front of the camera and push play. It should work.

Now lets port it to android.

File Build settings. If no scene is in the scene in build, click add open scene. Then Click Build.

If in the middle of a build a box comes up with Select android SDK root folder, just find android studies SDK root folder. If you don't have it, download it here. Once installed, it should be in the path
/Users/Username/Library/Android/sdk


Note most mac computers show Library as hidden. If so right click, and click on show view options, then check the box Show Library Folder.






Now you should be able to see the path to the sdks.

Select the root folder. The build should continue to build.

If anymore pop ups come up, it might say update SDK. Go ahead and update it. The build should continue.

Now your phone should be getting permission access. Click on on the phone and check always from this computer. (as it might be prompt a lot)

Android devices requires debugging mode on to work.

Old Android:
To enable debugging mode go to settings, applications, development, and check usb debugging.

2+ years old Android:
For newer androids ,  click settings , developer options, check and turn on developer options at the top, then check usb debugging and click ok.

New androids:
For news androids, to go settings  click about phone, go to build number,  click it like 5 times, you will see a message saying you are 3 clicks away from being a developer. Click it. Now you should see you are now a developer.  Now to back to settings, developer options, turn it on at the top, then go down to USB debugging and check the box.

After all this, it should build and run on your android devise. Check your folders, the app should be there.


Extra

Better Mapping Image:
This image should give 5/5 starts for mapping.