Introduction to Model Targets in Unity

Animated gif using Unity and Model Targets

This guide walks you through the process of placing AR content onto physical objects using Model Targets. For an optimal tracking experience, we recommend using Model Targets in conjuction with Positional Device Tracker.

Creating your first Model Target Experience

  1. Open Unity 2017.3 or newer.
  2. Create a new Unity project.
  3. Open the Edit menu -> Project Settings -> Player. In the XR Settings section, make sure that Vuforia Augmented Reality Supported is checked. Note that turning on this checkbox may require the installation of addition resources in your project.
Vuforia Image
  1. Remove the default “Main Camera” from the scene. Replace the default Main Camera with an ARCamera by going to the GameObject menu -> Vuforia Engine -> AR Camera.
  2. If you haven't already done so, create a Model Target dataset using the Model Target Generator. You will be using the *.unitypackage file generated from this app.
  3. Import the Model Target dataset: go to the Assets menu -> Import Package -> Custom Package… and select the *.unitypackage file created in the previous step.
  4. Obtain a License Key for your project. Please refer to the Vuforia License Manager guide on how to obtain a Vuforia Engine 7 license key. Licenses generated with a previous version of Vuforia Engine will not work with Model Targets. Note that a license key is not necessary when using the Viking Lander model.
  5. Open the Vuforia Configuration window (Window menu -> Vuforia Configuration” and expand the Global section if it is not already expanded. Paste your license key in the App License Key text field.
Vuforia Image

The next steps differ depending on the type of Model Target dataset you have:

Configuration for a single object with manual Guide View selection

If you have a Model Target dataset representing a single object, and the object defines just a single Guide View (or you want to switch between Guide Views manually), do the following:

  1. Go to the GameObject menu -> Vuforia Engine -> Model Targets -> Model Target to create a ModelTarget GameObject in your scene. Select the newly-created GameObject the Scene hierarchy window.
Screenshot of Unity showing GameObject menu, Vuforia Engine menu, Model Targets menu, Model Target entry selected
  1. In the Inspector window, select the appropriate Database in the Model Reco Behaviour component (it should match the *.unitypackage you imported earlier):
Vuforia Image
  1. Make sure that the Physical Length, Physical Width and Physical Height fields match the dimensions of your real-world object, in meters. (If you are using a miniature version of a large scale real-world object for development, you will need to change the dimensions to match the real-world object when shipping your production app. To make this easier, you might want to consider having separate ModelTarget GameObjects for development and production versions of your app.)
  1. Also in the Model Target Behaviour component, set the Guide View Mode to Guide View 2D
Vuforia Image
  1. You will notice that in your scene there is a preview model representing the Model Target. The purpose of this model is to aid you in placing an augmentation relative to specific parts of the physical object itself - it is only visible in the Scene view of the Unity Editor. If you start Play mode or build your app, it will not appear. 
Vuforia Image
  1. Attach a standard webcam to your computer and press the Play button in Unity. (Or, deploy the app to your device.)
  2. You will notice an outlined representation of the object to be tracked - this is the Guide View.
Vuforia Image
  1. Align the Guide View with your real-world object. You will notice the Guide View disappear, which indicates that tracking has started, but there will be no visible augmentation for now.
  2. Stop Play mode if necessary and return the Scene Hierarchy.
  1. To customize your AR experience, place custom content as child objects of the ModelTarget Game Object. Align your content relative to the preview model.
Vuforia Image

Align your content relative to the preview model.

Vuforia Image
  1. Again, with a standard webcam attached to your computer, press the Play button in Unity. (Or, deploy to your device). Once you align your camera/device so that the Guide View matches the real object, you will see the augmentation.
Vuforia Image

Manually switching between Guide Views

If your Model Target defines multiple Guide Views, you can switch between them in a C# script with the following code:

var modelTarget = FindObjectOfType<ModelTargetBehaviour>().ModelTarget;
// the index to activate must between 0 and modelTarget.GetNumGuideViews()-1
int guideViewIndexToActivate = ...;
modelTarget.SetActiveGuideViewIndex(guideViewIndexToActivate);

 

 

Configuration for multiple objects and/or multiple Guide Views

If you have a trained Model Target dataset representing multiple objects and/or multiple Guide Views, do the following:

  1. Go to the GameObject menu -> Vuforia Engine -> Model Targets -> Model Recognition to create a ModelRecognition GameObject in your scene. Select the newly created GameObject in the Scene hierarchy window. 
Screenshot of Unity showing GameObject menu, Vuforia Engine menu, Model Targets menu, Model Recognition entry selected
  1. In the Inspector window, select the appropriate Database in the Model Reco Behaviour component (it should match the *.unitypackage you imported earlier):
Screenshot of Unity demonstrating selection of the correct Model Target database in the Advanced Reco component
  1. Click on the Instantiate Model Targets button, which will automatically create separate ModelTarget GameObjects for each of the Guide Views of all of the objects in your Dataset:
Screenshot of Unity showing the result of clicking the

The dataset demonstrated above, Vuforia_Motorcyle_Marslander, contains two Model Targets, Vuforia_Motorcycle and Vuforia_MarsLander. The Vuforia_Motorcycle Model Target has two Guide Views named Left and RightThe Vuforia_MarsLander Model Target has two Guide Views name Front and Back

Note how, when we clicked on the Instantiate Model Targets button, four ModelTarget GameObjects were instantiated for this dataset: one for each of the Guide Views of all of the Model Targets in the dataset.

  1. Configure each of the instantiated ModelTarget GameObjects by following steps 3-10 outlined in the guide for single Model Target datasets, above.

    Note that, when running the app, a Guide View will only appear if Vuforia Engine recognizes one of the objects in the Model Target dataset.

    Note also that the Guide View might change if another object comes into view, or if you move the camera around to look at the same object from a different angle, depending on how the Guide View detection range was configured in the Model Target Generator.

To add advanced occlusion functionality to your applications, refer to the Unity Asset Store.

Learn More

Model Targets Overview

Model Targets API Overview

Model Target Generator User Guide