-
Notifications
You must be signed in to change notification settings - Fork 24
Creating a 2D texture trackable at runtime from a downloaded image
The "2D" tracker allows runtime creation of 2D texured trackables without any pre-processing of the image ahead-of-time. This represents a significant advantage compared to the original NFT (natural feature tracker) trackables from earlier ARToolKit versions.
In arunityX, creation of a 2D trackable at runtime is achieved by placing the desired image into the filesystem on the device, and then passing the path to the image and the real-world image width (in Unity units, usually metres) to a factory method ARXTrackable.Add2D
.
void addMyTrackable()
{
string myImageURL = "https://github.com/artoolkitx/arunityx/raw/master/Documentation/Marker%20images/pinball.jpg";
float myImageWidth = 0.188f; // 188 mm wide.
string myImageTrackableTag = "pinball";
StartCoroutine(DownloadAndAdd2D(myImageURL, myImageWidth, (t) => {
t.Tag = myImageTrackableTag;
// Do other things with the trackable:
});
}
IEnumerator DownloadAndAdd2D(string url, float width, Action<ARXTrackable> onSuccess)
{
string downloadPath = Path.Combine(Application.temporaryCachePath, Path.GetRandomFileName);
using (www = UnityWebRequest.Get(url))
{
www.downloadHandler = new DownloadHandlerFile(downloadPath);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
// Attaches the ARXTrackable component to the same GameObject as the ARXController instance.
ARXTrackable t = ARXTrackable.Add2D(downloadPath, width);
onSuccess(t);
}
}
}
Once the ARXTrackable
has been added, a typical use case is to link it to the pose of an object in the scene. In the example below, it is assumed that the scene already contains an ARXOrigin
component attached to the root of our scene, and an ARXCamera
somewhere in the scene. We then locate the root of our AR scene by finding the ARXOrigin
component, create a new GameObject
under it and add an ARXTrackedObject
component to it, and finally link its pose (the position and orientation) to our ARXTrackable
by setting the TrackableTag
field on the ARXTrackedObject
to the same value we set on the ARXTrackable
above. This code fits below "Do other things with the trackable" above:
// Do other things with the trackable:
ARXOrigin root = FindObjectOfType<ARXOrigin>();
GameObject go = new GameObject("myTrackedObject");
// Place the object in the scene. No need to set position, rotation, or and scale
// as those will be set by ARXTrackedObject.
go.transform.parent = root.transform;
ARXTrackedObject to = go.AddComponent<ARXTrackedObject>();
to.TrackableTag = myImageTrackableTag;
On the next Unity Update()
call, the ARXTrackedObject
will locate the ARXTrackable
that it is linked to by the TrackableTag
, and if that trackable is visible the ARXTrackedObject
will set its own position and orientation ("pose") in the scene and activate any child objects.