MultiTarget

Demonstrate different ways of tracking multiple targets.

  • Demonstrate how to track multiple targets simultaneously using single tracker (MultiTarget_SingleTracker)

  • Demonstrate how to track multiple targets simultaneously using multiple trackers (MultiTarget_MultiTracker)

  • Demonstrate how to track multiple same targets simultaneously (MultiTarget_SameImage)

  • Demonstrate how to load image target using custom code (MultiTarget_SameImage)

  • Demonstrate how to track multiple type of targets simultaneously (MultiTarget_MultiType)

How to Use

MultiTarget_SingleTracker

../../_images/image_31.png

MultiTarget_MultiTracker

../../_images/image_28.png

MultiTarget_SameImage

../../_images/image_30.png

MultiTarget_MultiType

../../_images/image_29.png

How It Works

Track multiple targets simultaneously using single tracker

To track multiple targets, you need to increase ImageTrackerFrameFilter.SimultaneousNum to the maximum number of simultaneously tracked target you want and set Tracker of all targets accordingly. Tracking more objects will increase the CPU usage.

../../_images/image_s3_1.png ../../_images/image_s3_2.png

Simultaneous target number

ImageTrackerFrameFilter.SimultaneousNum can be modified at any time and it will take effect immediately.

../../_images/image_s3_12.gif

Track multiple targets simultaneously using multiple trackers

Same with tracking multiple targets with single tracker, the basic settings is to adjust ImageTrackerFrameFilter.SimultaneousNum of each tracker and Tracker of each target.

../../_images/image_s3_3.png ../../_images/image_s3_4.png ../../_images/image_s3_5.png ../../_images/image_s3_6.png ../../_images/image_s3_7.png ../../_images/image_s3_8.png

There are difference between tracking using multiple tracker and one tracker. In these two samples, the maximum number of simultaneously tracking targets are 4 and (1+1+2) = 4, but there are differences in how those targets can be tracked.

When using single tracker, any 4 targets from the total 6 targets could be tracked in one frame.

In the setting of multiple tracker in the sample, only namecard + idback + 2 from 4 ar games can be tracked in one frame. That is because targets loaded into one tracker can only be tracked by the same tracker, which means namecard can only be tracked by ImageTracker_1, and ImageTracker_3 can track any 2 from 4 ar games but not the namecard.

There is no performance difference in these two ways, so choose a strategy according to the combination you want.

Create multiple targets from one image

This sample use a fully customized method to load image target, so that the same image will not be loaded multiple times from the storage.

It will start a coroutine to load image file, and create multiple targets after that.

private void Start()
{
    StartCoroutine(FileUtil.LoadFile("namecard.jpg", PathType.StreamingAssets, (buffer) =>
    {
        StartCoroutine(LoadImageBuffer(buffer.Clone(), "namecard", 0.09f));
    }));
}

After the image file is loaded into a easyar.Buffer, decode the buffer to easyar.Image in another thread so that it will not block the rendering, then use the image to create targets.

private IEnumerator LoadImageBuffer(easyar.Buffer buffer, string name, float scale)
{
    using (buffer)
    {
        Optional<Image> imageOptional = null;
        bool taskFinished = false;
        EasyARController.Instance.Worker.Run(() =>
        {
            imageOptional = ImageHelper.decode(buffer);
            taskFinished = true;
        });

        while (!taskFinished)
        {
            yield return 0;
        }
        if (imageOptional.OnNone)
        {
            throw new Exception("invalid buffer");
        }
        using (var image = imageOptional.Value)
        {
            CreateMultipleTargetsFromOneImage(image, 10, name, scale);
        }
    }
}

We use the ImageTarget.createFromParameters method to create the ImageTarget directly.

using (var param = new ImageTargetParameters())
{
    param.setImage(image);
    param.setName(name);
    param.setScale(scale);
    param.setUid(Guid.NewGuid().ToString());
    param.setMeta(string.Empty);
    var targetOptional = ImageTarget.createFromParameters(param);
    ...
}

Create a GameObject with ImageTargetController and set the above ImageTarget to ImageTargetController.TargetSource so that the controller can be initialized with ImageTargetController.DataSource.Target.

var target = targetOptional.Value;
GameObject Target = new GameObject(name + " <" + i + ">");
var controller = Target.AddComponent<ImageTargetController>();
AddTargetControllerEvents(controller);

controller.SourceType = ImageTargetController.DataSource.Target;
controller.TargetSource = target;
controller.Tracker = ImageTracker;

You can always use this method to setup ImageTargetController using a pre-created target, including a target manually created or a target from CloudRecognizer callback.

Loop above procedure to produce multiple targets in the scene.

private void CreateMultipleTargetsFromOneImage(Image image, int count, string name, float scale)
{
    for (int i = 0; i < count; i++)
    {
        using (var param = new ImageTargetParameters())
        {
            ...
            var targetOptional = ImageTarget.createFromParameters(param);
            if (targetOptional.OnSome)
            {
                var target = targetOptional.Value;
                ...
            }
            else
            {
                throw new Exception("invalid parameter");
            }
        }
    }
}

Track multiple type of targets simultaneously

Tracking multiple type of targets is similar to tracking multiple image targets using multiple trackers, the only difference is which type of tracker and which type of target is used.

../../_images/image_s3_9.png ../../_images/image_s3_10.png ../../_images/image_s3_11.png