ImageTracking_Targets

Attention

The content of this page will be updated soon.

Demonstrate image tracking feature.

  • Demonstrate different methods to create targets

  • Demonstrate how to dynamically create targets

  • Demonstrate how to create a target from ImageTargetData

  • Demonstrate how to load and unload targets

  • Demonstrate how to use different center mode

  • Demonstrate how to use different horizontal flip mode

How to Use

../../_images/image_26.png
Mark 1: Whether open the camera.
Mark 2: Switch camera using device index.
Mark 3: Switch horizontal flip mode of the camera image rendering.
Mark 4: Switch world center mode.
Mark 5: Unload/load all image targets in the scene.
Mark 6: Stop/start tracking.
Mark 7: Display system status and operation hint.

How It Works

Targets in the scene

If you want to replace the image in the sample to your own, make sure to choose a .jpg or .png file with rich texture. It is also suggested to read EasyAR Planar Image Tracking to help choosing a trackable image.

../../_images/image_s1_110.png

Target can be setup directly from the inspector of Unity editor.

  • Active Controller Strategy: Targets and their children will hide when the target is not being tracked. If you need to keep it display when lost, change the corresponding option or disable the script and and write your own strategy.

  • Source: Targets are created from Image File or Target Data File depending on this value.

  • Path Type: StreamingAssets here, so the Path will use a path relative to StreamingAssets.

  • Path: Image path relative to StreamingAssets in this sample.

  • Name: Name of the target, choose a world easy to remember.

  • Scale: It is set according to physical size of the image width in real world.

  • Tracker: The Tracker to load ImageTarget.

Gizmo are displayed if not disabled in the global settings when the target setting is valid. This will not show in the game view. The namecard image in the game view is a quad in the scene under the ImageTarget-namecard node in the scene.

The transform of the cube and duck have been adjusted so that their bottom will be aligned with the target image. When you run this scene and track the target, the cube and duck will show just on top of the image.

Create targets in script – from images

You can create targets from image Texture2D in the scripts. It is basically just moving the inspector setup to the script. You first need to create an empty GameObject and add ImageTargetController.

private ImageTargetController CreateTargetNode(string targetName)
{
    GameObject go = new GameObject(targetName);
    var targetController = go.AddComponent<ImageTargetController>();
    ...
}

Then set parameters of ImageTargetController like what is done is the inspector.

targetController = CreateTargetNode("ImageTarget-" + texture.name);
targetController.Tracker = imageTracker;
targetController.Source = new ImageTargetController.Texture2DSourceData
{
    Texture = texture,
    Name = texture.name,
    Scale = 0.1f,
};

Add a child object to the GameObject so it will be displayed when the target is tracked.

var panda2 = Instantiate(Panda);
panda2.transform.parent = targetController.gameObject.transform;

Target events

Target events are used for custom operations, the sample use the events to output some logs. You can delete the logs if they are not used, and you can use the events to handle your game logic. Notice that TargetLost could be called during OnDestroy, object should be checked to be before access.

controller.TargetDataLoad += (status) =>
{
    Debug.Log($"Load data from {controller.Source.GetType()} resource into target {(controller.Target == null ? string.Empty : $"{{id = {controller.Target.runtimeID()}, name = {controller.Target.name()}}} ")}=> {status}");
};
controller.TargetFound += () =>
{
    Debug.Log($"Found target {{id = {controller.Target.runtimeID()}, name = {controller.Target.name()}}}");
};
controller.TargetLost += () =>
{
    if (!controller) { return; }
    Debug.Log($"Lost target {{id = {controller.Target.runtimeID()}, name = {controller.Target.name()}}}");
};

Target load and unload

Target load and unload is very easy. Set ImageTargetController.Tracker to null, the target will be unloaded, and set it to some tracker will make the target loaded into the tracker immediately.

public void UnloadTargets()
{
    foreach (var target in targets)
    {
        target.Tracker = null;
    }
}

public void LoadTargets()
{
    foreach (var target in targets)
    {
        target.Tracker = imageTracker;
    }
}

Tracking on/off

Image tracking can be turned on or off using ImageTrackerFrameFilter.enabled. You can turn the tracking off when it is not used to save performance, it will not turn off the camera or any other tracking features.

public void Tracking(bool on)
{
    imageTracker.enabled = on;
}

Camera on/off

Camera device can be turned on or off using VideoCameraDevice.enabled. If a camera has turned off, the tracker will not receive any frames and the whole AR chain will stop.

public void EnableCamera(bool enable)
{
    cameraDevice.enabled = enable;
}

Center mode

Three mode of ARSession.CenterMode are valid in object sensing.

In ARSession.ARCenterMode.Camera, the camera do not automatically move when the device moves.

../../_images/image_s1_2.gif

In ARSession.ARCenterMode.FirstTarget or ARSession.ARCenterMode.SpecificTarget, the camera will move automatically when the device moves, and the target stay.

../../_images/image_s1_3.gif

To show the differences between ARSession.ARCenterMode.FirstTarget and ARSession.ARCenterMode.SpecificTarget, we change the tracker settings to track two targets.

In ARSession.ARCenterMode.FirstTarget mode, the camera will always move when there is some target being tracked, and the center will be the first tracked target or any other target when it is lost.

../../_images/image_s1_4.gif

When the center target not changing, ARSession.ARCenterMode.SpecificTarget mode will always use the specified target as center when it is tracked, and the camera stay when the target is lost.

../../_images/image_s1_5.gif

ARSession.CenterMode can be modified at any time and it will take effect immediately.

../../_images/image_s1_6.gif

Flip camera image horizontally

ARSession.HorizontalFlip.FrontCamera and ARSession.HorizontalFlip.BackCamera controls how the camera is mirrored. When the camera image is mirrored, the camera projection or the target scale will change so that a tracking behavior continues.

ARHorizontalFlipMode.None do not make a flip.

../../_images/image_s1_71.png

In ARHorizontalFlipMode.World mode, the camera image will be flipped in rendering, the camera projection matrix will be changed to do flip rendering. Target scale will not change. Changing of projection matrix will have side effect to all other objects displayed in the scene, you can use ARHorizontalFlipMode.Target if it annoys you.

../../_images/image_s1_81.png

In ARHorizontalFlipMode.Target mode, the camera image will be flipped in rendering, the target scale will be changed to do flip rendering. Camera projection matrix will not change.

../../_images/image_s1_91.png

ARSession.HorizontalFlipNormal and ARSession.HorizontalFlipFront can be modified at any time and it will take effect immediately.

../../_images/image_s1_10.gif