Image Tracking

Target File

To track content to an image you must generate a special target file from the source image that you'd like your content to track to. The ZapWorks CLI allows you to generate these easily via the following command:

zapworks train myImage.png

The ZapWorks CLI will generate a .zpt file (i.e. myImage.png.zpt) that should be placed in a folder named StreamingAssets in the root level of your project. The resulting .zpt file can be loaded into an image tracker by passing the filename to the Z.LoadZPTTarget(...) function.

The snippet below demonstrates how to initialize an image tracking target and load the relevant tracking file:

private IntPtr imageTracker;
private string targetFilename = "myImage.png.zpt";

public void OnZapparInitialised(IntPtr pipeline) 
{
    imageTracker = Z.ImageTrackerCreate(pipeline);
    StartCoroutine(Z.LoadZPTTarget(targetFilename, 
    TargetDataAvailableCallback));
}

private void TargetDataAvailableCallback(byte[] data)
{
    Z.ImageTrackerTargetLoadFromMemory(imageTracker, data);
}

Image Anchors

Each image tracker exposes anchors for images detected and tracked in the camera view. Currently, only one anchor is tracked by the camera.

The pose of an image anchor is exposed by passing in a zero based index to the Z.ImageTrackerAnchorPose(...) function, e.g.:

void Update() {
    if (Z.ImageTrackerAnchorCount(imageTracker) > 0)
    {
        Matrix4x4 cameraPose = ZapparCamera.Instance.Pose();
        Matrix4x4 imagePose = Z.ImageTrackerAnchorPose(imageTracker, 
        0, cameraPose, m_isMirrored);
        Matrix4x4 targetPose = Z.ConvertToUnityPose(imagePose);
    }
}

In your application you can set whether the pose of the camera is at the Unity origin, or whether the target is at the origin and the camera moves around. To drive the pose of the camera from an image target you should override the AnchorPoseCameraRelative function (and ensure your class is derived from ZapparTrackingTarget), like so:

public override Matrix4x4 AnchorPoseCameraRelative()
{
    if (Z.ImageTrackerAnchorCount(imageTracker) > 0)
    {
        return Z.ImageTrackerAnchorPoseCameraRelative(imageTracker, 
                0, /* anchor index */ 
                m_isMirrored);
    }
    return Matrix4x4.identity;
}
zapcode branded_zapcode i