1

With the new MRTK2 I'm looking to disable spatial mapping after we are done using it to place GameObjects. I'm stuck on what exactly to call in the namespace or on the service to do this at run time.

I've tried: MixedRealityToolkit.SpatialAwarenessSystem.SuspendObservers(); This has no effect. I could disable the entire "Spatial Awareness System" GameObject, but this would be a hack.

What I need is the proper call that would disable the system entirely so that resources are freed up when it is no longer useful?

Additionally, a little insight into how we are to access the service system correctly would be of great help.

DrDecipher
  • 37
  • 5

4 Answers4

5

You can use the following code to disable/enable the spatial awareness system:

if (disable)
{
    // disable
    MixedRealityToolkit.SpatialAwarenessSystem.Disable();
}
else
{
    // enable
    MixedRealityToolkit.SpatialAwarenessSystem.Enable()
}

You can use the following code to enable/disable just the visualization but keep the colliders on:

foreach(var observer in MixedRealityToolkit.SpatialAwarenessSystem.GetObservers())
{
    var meshObserver = observer as IMixedRealitySpatialAwarenessMeshObserver;
    if (meshObserver != null)
    {
        meshObserver.DisplayOption = SpatialAwarenessMeshDisplayOptions.None;
    }
}

You can read more documentation about the Spatial Awareness system in MRTK on the mrtk github.io site at Spatial Awareness System Usage guide

Julia Schwarz
  • 2,610
  • 1
  • 19
  • 25
  • Thank you Julia. This is of great assistance! – DrDecipher Jun 15 '19 at 23:03
  • Hey, I was also looking for enable/disable spatial mapping on runtime. I tried the line above but it will not start or stop spatial mapping and I dont know why. Did this lines work for you? – Perazim Sep 10 '19 at 10:47
0

I would have expected the SuspendObservers() method to result in no new meshes being displayed. Do you see the meshes changing after suspending?

It is by design for the meshes to remain visible until the application explicitly sets their visibility to None via the IMixedRealitySpatialAwarenessMeshObserver.DisplayOption property.

Thanks!

davidkline-ms
  • 206
  • 1
  • 4
0

Note the previous answer doesn't work due to recent changes to the MRTK framework.

Link for SpatialAwareness DataProvidershere

Code pasted from said link:

IMixedRealityDataProviderAccess dataProviderAccess =
    CoreServices.SpatialAwarenessSystem as IMixedRealityDataProviderAccess;

if (dataProviderAccess != null)
{
    IReadOnlyList<IMixedRealitySpatialAwarenessMeshObserver> observers =
        dataProviderAccess.GetDataProviders<IMixedRealitySpatialAwarenessMeshObserver>();

    foreach (IMixedRealitySpatialAwarenessMeshObserver observer in observers)
    {
        // Set the mesh to use the occlusion material
        observer.DisplayOption = SpatialMeshDisplayOptions.Occlusion;
    }
}
0
[AddComponentMenu("Scripts/MRTK/Examples/ClearSpatialObservations")]
public class ClearSpatialObservations : MonoBehaviour
{
    /// <summary>
    /// Indicates whether observations are to be cleared (true) or if the observer is to be resumed (false).
    /// </summary>
    private bool clearObservations = true;

    /// <summary>
    /// Toggles the state of the observers.
    /// </summary>
    public void ToggleObservers()
    {
        var spatialAwarenessSystem = CoreServices.SpatialAwarenessSystem;
        if (spatialAwarenessSystem != null)
        {
            if (clearObservations)
            {
                spatialAwarenessSystem.SuspendObservers();
                spatialAwarenessSystem.ClearObservations();
                clearObservations = false;
            }
            else
            {
                spatialAwarenessSystem.ResumeObservers();
                clearObservations = true;
            }
        }
    }
}