Hololens2 Correct Usage of SpatialAnchors

330 Views Asked by At

I am trying to implement an Hololens 2 App using SpatialAnchors but it seems that I am not quite understanding the concept right. I am able to create, save and restore SpatialAnchors of the Windows.Perception.Spatial namespace, but whenever I restore them they are placed relative to the start position of the headset.

I use this snippet to create an anchor:

SpatialAnchor thisAnchor = SpatialAnchor.TryCreateRelativeTo(coord);

And this to get the relative SpatialCoordinateSystem (from this post)

public static bool UseSGIPSceneCoordinateSystem(out SpatialCoordinateSystem sGIPSceneCoordinateSystem)
{
  // gain access to the scene object
  SceneObserverAccessStatus accessStatus = Task.Run(RequestAccess).GetAwaiter().GetResult();

  if (accessStatus == SceneObserverAccessStatus.Allowed)
  {  
    Scene scene = Task.Run(GetSceneAsync).GetAwaiter().GetResult();
    sGIPSceneCoordinateSystem = SpatialGraphInteropPreview.CreateCoordinateSystemForNode(scene.OriginSpatialGraphNodeId);
    return true;
  }
  else
  {
    sGIPSceneCoordinateSystem = null;
    return false;
  }
}

I use this method to restore the SpatialAnchor I need:

public bool GetAnchor(string id, out SpatialAnchor anchor)
{
 
    [...]
 
    _anchors = _anchorStore.GetAllSavedAnchors();
   
    foreach (var kvp in _anchors)
    {
        if(kvp.Key == id)
        {
            anchor = kvp.Value;
            return true;
        }
    }
 
    anchor = null;              
    return false;
}

And this to process the restored Anchor and get a place in the scene for it:

     public void SomeOtherMethod(SpatialAnchor anchor)
    {
        SpatialCoordinateSystem showcaseCoordinateSystem = anchor.CoordinateSystem;
     
        //get the reference SpatialCoordinateSystem
        if (!UseSGIPSceneCoordinateSystem(out SpatialCoordinateSystem referenceCoordinateSystem))
            return;
     
        _anchorMatrix = showcaseCoordinateSystem.TryGetTransformTo(referenceCoordinateSystem);
     
        System.Numerics.Matrix4x4 _notNullMatrix = _anchorMatrix.Value;
        Matrix4x4 unityAnchorMatrix = _notNullMatrix.ToUnity();
        anchorGameObject.transform.FromMatrix(unityAnchorMatrix);
    }

I think that I am using the wrong SpatialCoordinateSystem but can´t find information on how to get a SpatialCoordinateSystem which the HoloLens2 generates for the physical spatial surrounding of the user that is persistent.

I am using: Unity 2020.3.13f1 OpenXR Plugin 1.3.1 MRTK 2.7.3 MRTK-OpenXR 1.2.1 MRTK SceneUnderstanding 0.6.0

I am also confused of the amount of different SpatialAnchor-Systems, reference coordinatesystems and the documentation. Every post I find about SpatialAnchors seems to use a different approach. There seems to be SpatialAnchor-Systems for the Unity WLT, UnityEngine.VR.WSA with WorldManager, Azure Spatial Anchors, UnityEngine.XR.WindowsMR.WindowsMREnvironment, etc. And unless I havent overseen it, the microsoft documentation is not really clear about which one to use and how to use it right.

I would be really thankful if someone could bring some light into this issue.

I posted this question also on forum.unity.com

0

There are 0 best solutions below