Getting your holograms to stay in place, move with you, or in some cases position themselves relative to other holograms is a big part of creating Mixed Reality applications. This article will take you through our recommended solution using World Locking Tools, but we'll also cover manually setting up spatial anchors in your Unity projects. Before we jump into any code, it's important to understand how Unity handles coordinate space and anchors in its own engine.
World-scale coordinate systems
Today, when writing games, data visualization apps, or virtual reality apps, the typical approach is to establish one absolute world coordinate system that all other coordinates can reliably map back to. In that environment, you can always find a stable transform that defines a relationship between any two objects in that world. If you didn't move those objects, their relative transforms would always remain the same. This kind of global coordinate system is easy to get right when rendering a purely virtual world where you know all of the geometry in advance. Room-scale VR apps today typically establish this kind of absolute room-scale coordinate system with its origin on the floor.
In contrast, an untethered mixed reality device such as HoloLens has a dynamic sensor-driven understanding of the world, continuously adjusting its knowledge over time of the user's surroundings as they walk many meters across an entire floor of a building. In a world-scale experience, if you placed all your holograms in a naive rigid coordinate system, those holograms would end up drifting over time, either based on the world or relative to each other.
For example, the headset may currently believe two locations in the world to be 4 meters apart, and then later refine that understanding, learning that the locations are in fact 3.9 meters apart. If those holograms had initially been placed 4 meters apart in a single rigid coordinate system, one of them would then always appear 0.1 meters off from the real world.
You can manually place spatial anchors in Unity to maintain a hologram's position in the physical world when the user is mobile. However, this sacrifices the self-consistency within the virtual world. Different anchors are constantly moving in relation to one another, and are also moving through the global coordinate space. In this scenario, simple tasks like layout become difficult. Physics simulation can also be problematic.
World Locking Tools (WLT) gets you the best of both worlds, stabilizing a single rigid coordinate system using an internal supply of spatial anchors spread throughout the virtual scene as the user moves around. WLT analyzes the coordinates of the camera and those spatial anchors every frame. Instead of changing the coordinates of everything in the world to compensate for the corrections in the coordinates of the user's head, WLT just fixes the head's coordinates instead.
World Locking Tools provides a stable coordinate system that minimizes the visible inconsistencies between virtual and real world markers. World Locking Tools world-locks the entire scene with a shared pool of anchors, rather than locking each group of objects with the group's own individual anchor.
World Locking Tools automatically handles internal creation and management of spatial anchors. You don't need to interact with ARAnchorManager or WorldAnchor to keep your holograms world-locked.
For Unity 2019/2020 using OpenXR or the Windows XR Plugin, use ARAnchorManager.
For older Unity versions or WSA projects, use WorldAnchor.
To get started using the World Locking Tools, download the Mixed Reality Feature Tool. To learn more about the basics, see the main World Locking Tools documentation page for links to Overview, Quickstart, and other useful topics.
When your project is ready to go, run the configure scene utility from Mixed Reality > World Locking Tools:
Important
The Configure scene utility can be rerun at any time. For example, it should be rerun if the AR target has been changed from Legacy to XR SDK. If the scene is already properly configured, running the utility has no effect.
Visualizers
During early development, adding visualizers can be helpful to ensure WLT is setup and working properly. They can be removed for production performance, or if for any reason are no longer needed, using the Remove visualizers utility. More details on the visualizers can be found in the Tools documentation.
The Mixed Reality OpenXR Plugin supplies basic anchor functionality through an implementation of Unity’s ARFoundation ARAnchorManager. To learn the basics on ARAnchors in ARFoundation, visit the ARFoundation Manual for AR Anchor Manager.
This game object is now anchored to its current location in the physical world. You might see its Unity world coordinates adjust slightly over time to ensure physical alignment. See load a world anchor to find this anchored location again in a future app session.
Remove a World Anchor
If you no longer want the GameObject locked to a physical world location and don't intend on moving it this frame, call Destroy on the World Anchor component.
Destroy(gameObject.GetComponent<WorldAnchor>());
If you want to move the GameObject this frame, call DestroyImmediate instead.
A World Anchor might not be locatable in the physical world at a point in time. Unity then won't update the transform of the anchored object. This situation can also happen while an app is running. Failure to handle the change in locatability causes the object to not appear in the correct physical location in the world.
To be notified about locatability changes:
Subscribe to the OnTrackingChanged event. The OnTrackingChanged event is called whenever the underlying spatial anchor changes between a state of being locatable or not being locatable.
private void Anchor_OnTrackingChanged(WorldAnchor self, bool located)
{
// This simply activates/deactivates this object and all children when tracking changes
self.gameObject.SetActiveRecursively(located);
}
If anchors are located immediately, the isLocated property of the anchor is set to true when AddComponent<WorldAnchor>() returns. Therefore, the OnTrackingChanged event isn't triggered. A cleaner pattern is to call the OnTrackingChanged handler with the initial IsLocated state after attaching an anchor.
Spatial anchors save holograms in real-world space between application sessions. Once saved in the HoloLens anchor store, spatial anchors can be found and loaded in different sessions and are an ideal fallback when there's no internet connectivity.
Important
Local anchors are stored on device, while Azure Spatial Anchors are stored in the cloud. You can have local and Azure anchors in the same project without conflict. For more information about integrating Azure cloud services to store your anchors, see Azure Spatial Anchors.
By default, World Locking Tools restore Unity's coordinate system relative to the physical world across sessions on devices that support persistence of local spatial anchors. To have a hologram appear in the same place in the physical world after quitting and rerunning the application, the application only needs to restore the same pose to the hologram.
If the application needs finer control, you can disable Auto-Save and Auto-Load in the inspector, and manage persistence from a script. For more information, see Persist spatial coordinate systems.
World Locking Tools supports local anchor persistence only on HoloLens devices. For Android, iOS, and HoloLens devices, integrate with Azure Spatial Anchors to support persistence and sharing of coordinate spaces across sessions and devices. For more information and samples using World Locking Tools with Azure Spatial Anchors, see World Locking Tools (WLT) combined with Azure Spatial Anchors (ASA).
An API called the XRAnchorStore enables anchors to be persisted between sessions. The XRAnchorStore is a representation of the saved anchors on a device. You can persist anchors from ARAnchors in the Unity scene, load anchors from storage into new ARAnchors, or delete anchors from storage.
Note
You save and load these anchors on the same device. Cross-device anchors are supported through Azure Spatial Anchors.
Namespaces
For Unity 2020 and OpenXR:
using Microsoft.MixedReality.ARSubsystems.XRAnchorStore
or Unity 2019/2020 + Windows XR Plugin:
using UnityEngine.XR.WindowsMR.XRAnchorStore
Public methods
{
// A list of all persisted anchors, which can be loaded.
public IReadOnlyList<string> PersistedAnchorNames { get; }
// Clear all persisted anchors
public void Clear();
// Load a single persisted anchor by name. The ARAnchorManager will create this new anchor and report it in
// the ARAnchorManager.anchorsChanged event. The TrackableId returned here is the same TrackableId the
// ARAnchor will have when it is instantiated.
public TrackableId LoadAnchor(string name);
// Attempts to persist an existing ARAnchor with the given TrackableId to the local store. Returns true if
// the storage is successful, false otherwise.
public bool TryPersistAnchor(TrackableId id, string name);
// Removes a single persisted anchor from the anchor store. This will not affect any ARAnchors in the Unity
// scene, only the anchors in storage.
public void UnpersistAnchor(string name);
}
Get an anchor store reference
To load the XRAnchorStore with Unity 2020 and OpenXR, use extension method on the XRAnchorSubsystem, the subsystem of an ARAnchorManager:
public static Task<XRAnchorStore> LoadAnchorStoreAsync(this XRAnchorSubsystem anchorSubsystem)
To load the XRAnchorStore with Unity 2019/2020 and the Windows XR Plugin, use the extension method on the XRReferencePointSubsystem (Unity 2019) or XRAnchorSubsystem (Unity 2020), the subsystem of an ARReferencePointManager/ARAnchorManager:
// Unity 2019 + Windows XR Plugin
public static Task<XRAnchorStore> TryGetAnchorStoreAsync(this XRReferencePointSubsystem anchorSubsystem);
// Unity 2020 + Windows XR Plugin
public static Task<XRAnchorStore> TryGetAnchorStoreAsync(this XRAnchorSubsystem anchorSubsystem);
Load an anchor store
To load an anchor store in Unity 2020 and OpenXR, access it from an ARAnchorManager's subsystem as follows:
To see a full example of persisting / unpersisting anchors, check out the Anchors -> Anchors Sample GameObject and AnchorsSample.cs script in the [Mixed Reality OpenXR Plugin Sample Scene]((https://github.com/microsoft/OpenXR-Unity-MixedReality-Samples):
For hologram persistence in older Unity versions or WSA projects, use WorldAnchor.
The WorldAnchorStore creates holographic experiences where holograms stay in specific real world positions across instances of the application. Users can pin individual holograms wherever they want, and find them later in the same spot over app sessions.
The WorldAnchorStore lets you persist the location of world anchors across sessions. To persist holograms across sessions, keep separate track of GameObjects that use a particular world anchor. You can create a GameObject root with a world anchor, and anchor child holograms by it with a local position offset.
To load holograms from previous sessions:
Get the WorldAnchorStore.
Load world anchor app data, which gives you the ID of the world anchor.
Load the world anchor by its ID.
To save holograms for future sessions:
Get the WorldAnchorStore.
Save a world anchor, specifying an ID.
Save app data related to the world anchor along with the ID.
Get the WorldAnchorStore
Keep a reference to the WorldAnchorStore, so you know when it's ready to perform an operation. Since this call is asynchronous, as soon as the app starts up you can call:
WorldAnchorStore.GetAsync(StoreLoaded);
StoreLoaded is the handler when the WorldAnchorStore finishes loading:
You now have a reference to the WorldAnchorStore, which you can use to save and load specific world anchors.
Save a world anchor
To save a world anchor, name the world anchor and pass it in the WorldAnchorStore you got before. If you try to save two anchors to the same string, store.Save returns false. Delete the previous save before saving a new one.
private void SaveGame()
{
// Save data about holograms that this world anchor positions
if (!this.savedRoot) // Only save the root once
{
this.savedRoot = this.store.Save("rootGameObject", anchor);
Assert(this.savedRoot);
}
}
Load a world anchor
To load a world anchor:
private void LoadGame()
{
// Saved data about holograms that this world anchor positions:
this.savedRoot = this.store.Load("rootGameObject", rootGameObject);
if (!this.savedRoot)
{
// Game root not saved. Re-place objects or start over.
}
}
You can also use store.Delete() to remove an anchor you previously saved, and store.Clear() to remove all previously saved data.
Enumerate existing anchors
To list stored anchors, call GetAllIds.
string[] ids = this.store.GetAllIds();
for (int index = 0; index < ids.Length; index++)
{
Debug.Log(ids[index]);
}
Persist holograms for multiple devices
You can use Azure Spatial Anchors to create a durable cloud anchor from a local world anchor. Your app can locate the cloud anchor across multiple HoloLens, iOS, and Android devices, even if the devices aren't together at the same time. Because cloud anchors are persistent, multiple devices can see content rendered relative to that anchor in the same physical location over time.