33
44using Microsoft . MixedReality . Toolkit . Core . Definitions . SpatialAwarenessSystem ;
55using Microsoft . MixedReality . Toolkit . Core . Definitions . Utilities ;
6+ using Microsoft . MixedReality . Toolkit . Core . Interfaces . SpatialAwarenessSystem ;
67using Microsoft . MixedReality . Toolkit . Core . Managers ;
8+ using Microsoft . MixedReality . Toolkit . Core . Utilities ;
79using UnityEngine ;
810using System . Collections . Generic ;
911
1012#if UNITY_WSA
1113using UnityEngine . XR . WSA ;
1214#endif // UNITY_WSA
1315
14- using UnityEngine . UI ;
15-
1616namespace Microsoft . MixedReality . Toolkit . Core . Devices . SpatialAwareness
1717{
1818 public class WindowsMixedRealitySpatialObserver : BaseSpatialObserver
@@ -82,6 +82,13 @@ public override void Destroy()
8282
8383 #region IMixedRealitySpatialAwarenessObserver implementation
8484
85+ private IMixedRealitySpatialAwarenessSystem spatialAwarenessSystem = null ;
86+
87+ /// <summary>
88+ /// The currently active instance of <see cref="IMixedRealitySpatialAwarenessSystem"/>.
89+ /// </summary>
90+ private IMixedRealitySpatialAwarenessSystem SpatialAwarenessSystem => spatialAwarenessSystem ?? ( spatialAwarenessSystem = MixedRealityManager . SpatialAwarenessSystem ) ;
91+
8592#if UNITY_WSA
8693 /// <summary>
8794 /// The surface observer providing the spatial data.
@@ -181,12 +188,14 @@ public override void StopObserving()
181188 /// </summary>
182189 private void CreateObserver ( )
183190 {
191+ if ( SpatialAwarenessSystem == null ) { return ; }
192+
184193 if ( observer == null )
185194 {
186195 observer = new SurfaceObserver ( ) ;
187196 ConfigureObserverVolume ( ) ;
188197
189- if ( MixedRealityManager . Instance . ActiveProfile . SpatialAwarenessProfile . StartupBehavior == AutoStartBehavior . AutoStart )
198+ if ( SpatialAwarenessSystem . StartupBehavior == AutoStartBehavior . AutoStart )
190199 {
191200 StartObserving ( ) ;
192201 }
@@ -239,6 +248,8 @@ private void CleanupObserver()
239248 /// </summary>
240249 private void UpdateObserver ( )
241250 {
251+ if ( SpatialAwarenessSystem == null ) { return ; }
252+
242253 // Only update the observer if it is running.
243254 if ( IsRunning && ! outstandingMeshObject . HasValue )
244255 {
@@ -250,8 +261,14 @@ private void UpdateObserver()
250261 RequestMesh ( meshWorkQueue . Dequeue ( ) ) ;
251262 }
252263 // If enough time has passed since the previous observer update...
253- else if ( Time . time - lastUpdated >= MixedRealityManager . Instance . ActiveProfile . SpatialAwarenessProfile . UpdateInterval )
264+ else if ( Time . time - lastUpdated >= SpatialAwarenessSystem . UpdateInterval )
254265 {
266+ // Update the observer location if it is not stationary
267+ if ( ! SpatialAwarenessSystem . IsStationaryObserver )
268+ {
269+ SpatialAwarenessSystem . ObserverOrigin = CameraCache . Main . transform . position ;
270+ }
271+
255272 // The application can update the observer volume at any time, make sure we are using the latest.
256273 ConfigureObserverVolume ( ) ;
257274
@@ -269,7 +286,6 @@ private void RequestMesh(SurfaceId surfaceId)
269286 {
270287 string meshName = ( "SpatialMesh - " + surfaceId . handle ) ;
271288
272- // todo:
273289 SpatialMeshObject newMesh ;
274290 WorldAnchor worldAnchor ;
275291
@@ -298,7 +314,7 @@ private void RequestMesh(SurfaceId surfaceId)
298314 newMesh . Filter ,
299315 worldAnchor ,
300316 newMesh . Collider ,
301- MixedRealityManager . SpatialAwarenessSystem . MeshTrianglesPerCubicMeter ,
317+ SpatialAwarenessSystem . MeshTrianglesPerCubicMeter ,
302318 true ) ;
303319
304320 if ( observer . RequestMeshAsync ( surfaceData , SurfaceObserver_OnDataReady ) )
@@ -331,7 +347,7 @@ protected void RemoveMeshObject(int id)
331347 ReclaimMeshObject ( mesh ) ;
332348
333349 // Send the mesh removed event
334- MixedRealityManager . SpatialAwarenessSystem . RaiseMeshRemoved ( id ) ;
350+ SpatialAwarenessSystem . RaiseMeshRemoved ( id ) ;
335351 }
336352 }
337353
@@ -365,8 +381,8 @@ protected void ReclaimMeshObject(SpatialMeshObject availableMeshObject)
365381 /// </summary>
366382 private void ConfigureObserverVolume ( )
367383 {
368- Vector3 newExtents = MixedRealityManager . SpatialAwarenessSystem . ObservationExtents ;
369- Vector3 newOrigin = MixedRealityManager . SpatialAwarenessSystem . ObserverOrigin ;
384+ Vector3 newExtents = SpatialAwarenessSystem . ObservationExtents ;
385+ Vector3 newOrigin = SpatialAwarenessSystem . ObserverOrigin ;
370386
371387 if ( currentObserverExtents . Equals ( newExtents ) &&
372388 currentObserverOrigin . Equals ( newOrigin ) )
@@ -434,21 +450,21 @@ private void SurfaceObserver_OnDataReady(SurfaceData cookedData, bool outputWrit
434450 outstandingMeshObject = null ;
435451
436452 // Apply the appropriate material to the mesh.
437- SpatialMeshDisplayOptions displayOption = MixedRealityManager . SpatialAwarenessSystem . MeshDisplayOption ;
453+ SpatialMeshDisplayOptions displayOption = SpatialAwarenessSystem . MeshDisplayOption ;
438454 if ( displayOption != SpatialMeshDisplayOptions . None )
439455 {
440456 meshObject . Renderer . enabled = true ;
441457 meshObject . Renderer . sharedMaterial = ( displayOption == SpatialMeshDisplayOptions . Visible ) ?
442- MixedRealityManager . SpatialAwarenessSystem . MeshVisibleMaterial :
443- MixedRealityManager . SpatialAwarenessSystem . MeshOcclusionMaterial ;
458+ SpatialAwarenessSystem . MeshVisibleMaterial :
459+ SpatialAwarenessSystem . MeshOcclusionMaterial ;
444460 }
445461 else
446462 {
447463 meshObject . Renderer . enabled = false ;
448464 }
449465
450466 // Recalculate the mesh normals if requested.
451- if ( MixedRealityManager . SpatialAwarenessSystem . MeshRecalculateNormals )
467+ if ( SpatialAwarenessSystem . MeshRecalculateNormals )
452468 {
453469 meshObject . Filter . sharedMesh . RecalculateNormals ( ) ;
454470 }
@@ -468,11 +484,11 @@ private void SurfaceObserver_OnDataReady(SurfaceData cookedData, bool outputWrit
468484
469485 if ( sendUpdatedEvent )
470486 {
471- MixedRealityManager . SpatialAwarenessSystem . RaiseMeshUpdated ( cookedData . id . handle , meshObject . GameObject ) ;
487+ SpatialAwarenessSystem . RaiseMeshUpdated ( cookedData . id . handle , meshObject . GameObject ) ;
472488 }
473489 else
474490 {
475- MixedRealityManager . SpatialAwarenessSystem . RaiseMeshAdded ( cookedData . id . handle , meshObject . GameObject ) ;
491+ SpatialAwarenessSystem . RaiseMeshAdded ( cookedData . id . handle , meshObject . GameObject ) ;
476492 }
477493 }
478494#endif // UNITY_WSA
0 commit comments