Search Unity

Question Correct Usage of SpatialAnchors

Discussion in 'AR' started by MarcLpunkt, Mar 10, 2022.

  1. MarcLpunkt

    MarcLpunkt

    Joined:
    May 7, 2020
    Posts:
    1
    Hey,

    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:
    Code (CSharp):
    1. SpatialAnchor thisAnchor = SpatialAnchor.TryCreateRelativeTo(coord);
    And this to get the relative SpatialCoordinateSystem (from this post https://github.com/microsoft/MixedRealityToolkit-Unity/discussions/10266: )
    Code (CSharp):
    1.  
    2. public static bool UseSGIPSceneCoordinateSystem(out SpatialCoordinateSystem sGIPSceneCoordinateSystem)
    3. {
    4.     // gain access to the scene object
    5.     SceneObserverAccessStatus accessStatus = Task.Run(RequestAccess).GetAwaiter().GetResult();
    6.  
    7.     if (accessStatus == SceneObserverAccessStatus.Allowed)
    8.     {  
    9.         Scene scene = Task.Run(GetSceneAsync).GetAwaiter().GetResult();
    10.         sGIPSceneCoordinateSystem = SpatialGraphInteropPreview.CreateCoordinateSystemForNode(scene.OriginSpatialGraphNodeId);
    11.         return true;
    12.     }
    13.     else
    14.     {
    15.         sGIPSceneCoordinateSystem = null;
    16.         return false;
    17.     }
    18. }
    19.  
    I use this method to restore the SpatialAnchor I need:
    Code (CSharp):
    1.        
    2. public bool GetAnchor(string id, out SpatialAnchor anchor)
    3. {
    4.  
    5.     [...]
    6.  
    7.     _anchors = _anchorStore.GetAllSavedAnchors();
    8.    
    9.     foreach (var kvp in _anchors)
    10.     {
    11.         if(kvp.Key == id)
    12.         {
    13.             anchor = kvp.Value;
    14.             return true;
    15.         }
    16.     }
    17.  
    18.     anchor = null;              
    19.     return false;
    20. }
    And this to process the restored Anchor and get a place in the scene for it:
    Code (CSharp):
    1.  public void SomeOtherMethod(SpatialAnchor anchor)
    2. {
    3.     SpatialCoordinateSystem showcaseCoordinateSystem = anchor.CoordinateSystem;
    4.  
    5.     //get the reference SpatialCoordinateSystem
    6.     if (!UseSGIPSceneCoordinateSystem(out SpatialCoordinateSystem referenceCoordinateSystem))
    7.         return;
    8.  
    9.     _anchorMatrix = showcaseCoordinateSystem.TryGetTransformTo(referenceCoordinateSystem);
    10.  
    11.     System.Numerics.Matrix4x4 _notNullMatrix = _anchorMatrix.Value;
    12.     Matrix4x4 unityAnchorMatrix = _notNullMatrix.ToUnity();
    13.     anchorGameObject.transform.FromMatrix(unityAnchorMatrix);
    14. }
    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.