Search code examples
mrtk

MRTKv2: How get the spatial awareness observers


A common question when working with Spatial Awareness (aka Spatial Mapping) in MRTK is how to access the spatial observers to configure them individually.

The question arises since the GetObserver methods of the IMixedRealitySpatialAwarenessSystem interface are marked as obsolete.


Solution

  • The version 2.0.0 release of MRTK has standardized the pattern for accessing data providers across services in order to

    1. Provide a consistent pattern across all service types.
    2. Enable services to add data provider support without requiring a breaking interface change.

    The following example demonstrates this pattern to access spatial awareness mesh observers.

    if (CoreServices.SpatialAwarenessSystem != null)
    {
        IMixedRealityDataProviderAccess dataProviderAccess = 
            CoreServices.SpatialAwarenessSystem as IMixedRealityDataProviderAccess;
    
        if (dataProviderAccess != null)
        {
            IReadOnlyList<IMixedRealitySpatialAwarenessMeshObserver> observers =
            dataProviderAccess.GetDataProviders<IMixedRealitySpatialAwarenessMeshObserver>();
    
            // Modify the observer(s)
            foreach (IMixedRealitySpatialAwarenessMeshObserver observer in observers)
            {
                // Set the mesh to use the occlusion material
                observer.DisplayOption = SpatialMeshDisplayOptions.Occlusion;
            }
        }
    }