Search code examples
unity-game-engineaugmented-realityhololensmrtk

MRTK V2 - Enable/Disable Spatial Mapping at Runtime


I know that this question has already been asked here twice, but the answers did not fix my problem. I need to enable spatial mapping on runtime. After scanning my environment I want to disable it, or hide at least the visualization of polygons, so I can save some fps. But by disabling spatial mapping I still want to have the colliders of my environment.

What I tried:
1. This example from this post did nothing.

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

2. Trying to disable the visualization gives me every time a nullreference. I guess GetObservers is giving null back or maybe meshOserver is null:

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

3. The example given by mrtk in there SpatialAwarenessMeshDemo scene, shows how to start and stop the observer. By starting everything starts fine but after suspending and clearing the observers the whole spatial map disappears, so my cursor does not align to my environment. So this is not what I need.

   SpatialAwarenessSystem.ResumeObservers(); //start

   SpatialAwarenessSystem.SuspendObservers();//stop
   SpatialAwarenessSystem.ClearObservations();


What I have right now:
My Spatial Awareness Profile looks like this:

enter image description here
enter image description here
My code starts the spatial mapping with ResumeObservers, the foreach-loop gives me a nullreference and SuspendObserver is comment out, because it disables the whole spatial map thing:

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

            //SpatialAwarenessSystem.SuspendObservers();
            //SpatialAwarenessSystem.ClearObservations();
            _isObserverRunning = false;
        }
        else
        {
            SpatialAwarenessSystem.ResumeObservers();
            _isObserverRunning = true;
        }

Question: How do I start and stop spatial mapping the right way, so that I can save some performance and still have the colliders of the spatial map to interact with.

My specs:
MRTK v2.0.0
Unity 2019.2.0f1
Visual Studio 2017

!--Edit--inlcuding-Solution--!
1. With option #1 I was wrong. It does what its meant for, but I used it the wrong way. If you disable for example SpatialAwarenessSystem while running the spatial mapping process, it disables the whole process including the created spatial map. So after that you cant interact with the invironment.
2. What worked for me was using for the start ResumeObservers() in combination with setting display option to visible and for stopping spatial mapping the method SuspendObservers() in combination with display option none.
3. The Nullreference if fixed by rewritting and casting to IMixedRealityDataProviderAccess:

if (CoreServices.SpatialAwarenessSystem is IMixedRealityDataProviderAccess provider)
{
    foreach (var observer in provider.GetDataProviders())
    {
        if (observer is IMixedRealitySpatialAwarenessMeshObserver meshObs)
        {
            meshObs.DisplayOption = option;
        }
    }
}

4. Performance: To get your fps back after starting an observer, you really need to disable the system via MixedRealityToolkit.SpatialAwarenessSystem.Disable();, but this will of course disable also the spatial map, so you cant interactive with it anymore.


Solution

  • @Perazim,

    The recommendation is based on your option #3. Call ResumeObservers() to start and SuspendObservers() to stop. There is no need to call ClearObservations() unless you wish to have them removed from your scene.

    The example calls ClearObservations() to illustrate what was, at the time, a new feature added to the Spatial Awareness system.

    Please file an issue on GitHub (https://github.com/microsoft/MixedRealityToolkit-Unity/issues) for #1 (failure of Enable() and Disable() to impact the system). Those methods should behave as advertised.

    Thank you! David