Skip to content

Instantly share code, notes, and snippets.

@rogueyoshi
Last active August 28, 2020 00:02
Show Gist options
  • Save rogueyoshi/2fa3bde3630f9c20bdfdef3381f47585 to your computer and use it in GitHub Desktop.
Save rogueyoshi/2fa3bde3630f9c20bdfdef3381f47585 to your computer and use it in GitHub Desktop.
ARFoundation Simple Re-Localization using only Tracked Images
// A simple way to create persistent AR experiences just from a single (or more) tracked image alone.
// After the image is found, ARFoundation SLAM works it's magic for position tracking.
// To use: Create your scene in Unity as you would for a normal project and place one or more objects corresponding to the sync point.
// This presumes you have a scene with objects in Unity space corresponding to where they would appear physically.
// Make sure that the objects in Unity space corresponding to the image targets has the same pose as it would in physical space.
// Requires an ARTrackedImageManagerComponent
private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs imageEventArgs)
{
foreach (var updatedImage in imageEventArgs.updated)
{
switch (updatedImage.trackingState)
{
case TrackingState.Tracking:
// Grab the name and pose of the Tracked Image from the AR Session.
var imageName = updatedImage.referenceImage.name;
var imagePosition = updatedImage.transform.position;
var imageRotation = updatedImage.transform.localRotation * Quaternion.AngleAxis(90, Vector3.right) * Quaternion.AngleAxis(180, Vector3.up);
// Find the corresponding Game Object (which should have the approximate pose as the Tracked Image)
var syncObject = GameObject.Find(imageName);
if (syncObject)
{
// Re-Orient the AR Session / Camera to look at the object in Unity space with the same pose as the Tracked Image in physical space.
m_ARSessionOrigin.MakeContentAppearAt(syncObject.transform, imagePosition, imageRotation);
return;
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment