11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License. See LICENSE in the project root for license information.
33
4- using System . Collections . Generic ;
54using Microsoft . MixedReality . Toolkit . SpatialAwareness ;
65using Microsoft . MixedReality . Toolkit . Utilities ;
76using UnityEngine ;
8- using UnityEngine . EventSystems ;
97
108namespace Microsoft . MixedReality . Toolkit . SpatialObjectMeshObserver
119{
@@ -19,9 +17,8 @@ namespace Microsoft.MixedReality.Toolkit.SpatialObjectMeshObserver
1917 "ObjectMeshObserver/Profiles/DefaultObjectMeshObserverProfile.asset" ,
2018 "MixedRealityToolkit.Providers" ) ]
2119 [ HelpURL ( "https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/SpatialAwareness/SpatialAwarenessGettingStarted.html" ) ]
22- public class SpatialObjectMeshObserver :
23- BaseSpatialObserver ,
24- IMixedRealitySpatialAwarenessMeshObserver ,
20+ public class SpatialObjectMeshObserver :
21+ BaseSpatialMeshObserver ,
2522 IMixedRealityCapabilityCheck
2623 {
2724 /// <summary>
@@ -59,36 +56,24 @@ public SpatialObjectMeshObserver(
5956
6057 private GameObject spatialMeshObject = null ;
6158
62- private MixedRealitySpatialAwarenessEventData < SpatialAwarenessMeshObject > meshEventData = null ;
59+ #region BaseSpatialMeshObserver Implementation
6360
6461 /// <summary>
6562 /// Reads the settings from the configuration profile.
6663 /// </summary>
67- private void ReadProfile ( )
64+ protected override void ReadProfile ( )
6865 {
66+ base . ReadProfile ( ) ;
67+
6968 SpatialObjectMeshObserverProfile profile = ConfigurationProfile as SpatialObjectMeshObserverProfile ;
7069 if ( profile == null ) { return ; }
7170
7271 // SpatialObjectMeshObserver settings
7372 spatialMeshObject = profile . SpatialMeshObject ;
74-
75- // IMixedRealitySpatialAwarenessObserver settings
76- StartupBehavior = profile . StartupBehavior ;
77- IsStationaryObserver = profile . IsStationaryObserver ;
78- ObservationExtents = profile . ObservationExtents ;
79- ObserverVolumeType = profile . ObserverVolumeType ;
80- UpdateInterval = profile . UpdateInterval ;
81-
82- // IMixedRealitySpatialAwarenessMeshObserver settings
83- DisplayOption = profile . DisplayOption ;
84- LevelOfDetail = profile . LevelOfDetail ;
85- MeshPhysicsLayer = profile . MeshPhysicsLayer ;
86- OcclusionMaterial = profile . OcclusionMaterial ;
87- RecalculateNormals = profile . RecalculateNormals ;
88- TrianglesPerCubicMeter = profile . TrianglesPerCubicMeter ;
89- VisibleMaterial = profile . VisibleMaterial ;
9073 }
9174
75+ #endregion BaseSpatialMeshObserver Implementation
76+
9277 #region IMixedRealityCapabilityCheck Implementation
9378
9479 /// <inheritdoc />
@@ -101,19 +86,6 @@ bool IMixedRealityCapabilityCheck.CheckCapability(MixedRealityCapability capabil
10186
10287 #region IMixedRealityDataProvider Implementation
10388
104- /// <inheritdoc />
105- public override void Initialize ( )
106- {
107- meshEventData = new MixedRealitySpatialAwarenessEventData < SpatialAwarenessMeshObject > ( EventSystem . current ) ;
108-
109- ReadProfile ( ) ;
110-
111- if ( StartupBehavior == AutoStartBehavior . AutoStart )
112- {
113- Resume ( ) ;
114- }
115- }
116-
11789 /// <inheritdoc />
11890 public override void Update ( )
11991 {
@@ -125,29 +97,32 @@ public override void Update()
12597 SendMeshObjects ( ) ;
12698 }
12799
100+ #endregion IMixedRealityDataProvider Implementation
101+
102+ #region BaseSpatialObserver Implementation
103+
128104 /// <inheritdoc />
129- public override void Reset ( )
105+ protected override void CreateObserver ( )
130106 {
131- CleanupObserver ( ) ;
132- Initialize ( ) ;
107+ if ( StartupBehavior == AutoStartBehavior . AutoStart )
108+ {
109+ Resume ( ) ;
110+ }
133111 }
134112
135113 /// <inheritdoc />
136- public override void Destroy ( )
114+ protected override void CleanupObserver ( )
137115 {
138- Disable ( ) ;
139- CleanupObserver ( ) ;
116+ if ( IsRunning )
117+ {
118+ Suspend ( ) ;
119+ }
140120 }
141121
142- #endregion IMixedRealityDataProvider Implementation
122+ #endregion BaseSpatialObserver Implementation
143123
144124 #region IMixedRealitySpatialAwarenessObserver Implementation
145125
146- private GameObject observedObjectParent = null ;
147-
148- /// <inheritdoc />
149- protected virtual GameObject ObservedObjectParent => observedObjectParent != null ? observedObjectParent : ( observedObjectParent = SpatialAwarenessSystem ? . CreateSpatialAwarenessObservationParent ( Name ) ) ;
150-
151126 /// <inheritdoc />
152127 public override void ClearObservations ( )
153128 {
@@ -166,32 +141,26 @@ public override void ClearObservations()
166141 sendObservations = true ;
167142 }
168143
169- private int currentMeshId = 0 ;
170-
171144 /// <inheritdoc />
172145 public override void Resume ( )
173146 {
174147 if ( IsRunning ) { return ; }
175148 IsRunning = true ;
176149 }
177150
178- /// <summary>
179- /// Event sent whenever a mesh is added.
180- /// </summary>
181- private static readonly ExecuteEvents . EventFunction < IMixedRealitySpatialAwarenessObservationHandler < SpatialAwarenessMeshObject > > OnMeshAdded =
182- delegate ( IMixedRealitySpatialAwarenessObservationHandler < SpatialAwarenessMeshObject > handler , BaseEventData eventData )
183- {
184- MixedRealitySpatialAwarenessEventData < SpatialAwarenessMeshObject > spatialEventData = ExecuteEvents . ValidateEventData < MixedRealitySpatialAwarenessEventData < SpatialAwarenessMeshObject > > ( eventData ) ;
185- handler . OnObservationAdded ( spatialEventData ) ;
186- } ;
187-
188151 /// <inheritdoc />
189152 public override void Suspend ( )
190153 {
191154 if ( ! IsRunning ) { return ; }
192155 IsRunning = false ;
193156 }
194157
158+ #endregion IMixedRealitySpatialAwarenessObserver Implementation
159+
160+ #region Helpers
161+
162+ private int currentMeshId = 0 ;
163+
195164 /// <summary>
196165 /// Sends the observations using the mesh data contained within the configured 3D model.
197166 /// </summary>
@@ -233,9 +202,7 @@ private void SendMeshObjects()
233202 /// </summary>
234203 private void RemoveMeshObject ( int meshId )
235204 {
236- SpatialAwarenessMeshObject meshObject = null ;
237-
238- if ( meshes . TryGetValue ( meshId , out meshObject ) )
205+ if ( meshes . TryGetValue ( meshId , out SpatialAwarenessMeshObject meshObject ) )
239206 {
240207 // Remove the mesh object from the collection.
241208 meshes . Remove ( meshId ) ;
@@ -250,128 +217,6 @@ private void RemoveMeshObject(int meshId)
250217 }
251218 }
252219
253- /// <summary>
254- /// Event sent whenever a mesh is discarded.
255- /// </summary>
256- private static readonly ExecuteEvents . EventFunction < IMixedRealitySpatialAwarenessObservationHandler < SpatialAwarenessMeshObject > > OnMeshRemoved =
257- delegate ( IMixedRealitySpatialAwarenessObservationHandler < SpatialAwarenessMeshObject > handler , BaseEventData eventData )
258- {
259- MixedRealitySpatialAwarenessEventData < SpatialAwarenessMeshObject > spatialEventData = ExecuteEvents . ValidateEventData < MixedRealitySpatialAwarenessEventData < SpatialAwarenessMeshObject > > ( eventData ) ;
260- handler . OnObservationRemoved ( spatialEventData ) ;
261- } ;
262-
263- #endregion IMixedRealitySpatialAwarenessObserver Implementation
264-
265- #region IMixedRealitySpatialAwarenessMeshObserver Implementation
266-
267- private SpatialAwarenessMeshDisplayOptions displayOption = SpatialAwarenessMeshDisplayOptions . Visible ;
268-
269- /// <inheritdoc />
270- public SpatialAwarenessMeshDisplayOptions DisplayOption
271- {
272- get => displayOption ;
273-
274- set
275- {
276- displayOption = value ;
277- ApplyUpdatedMeshDisplayOption ( displayOption ) ;
278- }
279- }
280-
281- /// <inheritdoc />
282- public SpatialAwarenessMeshLevelOfDetail LevelOfDetail { get ; set ; } = SpatialAwarenessMeshLevelOfDetail . Coarse ;
283-
284- private Dictionary < int , SpatialAwarenessMeshObject > meshes = new Dictionary < int , SpatialAwarenessMeshObject > ( ) ;
285-
286- /// <inheritdoc />
287- public IReadOnlyDictionary < int , SpatialAwarenessMeshObject > Meshes => new Dictionary < int , SpatialAwarenessMeshObject > ( meshes ) ;
288-
289- private int meshPhysicsLayer = 31 ;
290-
291- /// <inheritdoc />
292- public int MeshPhysicsLayer
293- {
294- get => meshPhysicsLayer ;
295-
296- set
297- {
298- if ( ( value < 0 ) || ( value > 31 ) )
299- {
300- Debug . LogError ( "Specified MeshPhysicsLayer is out of bounds. Please set a value between 0 and 31, inclusive." ) ;
301- return ;
302- }
303-
304- meshPhysicsLayer = value ;
305-
306- ApplyUpdatedPhysicsLayer ( ) ;
307- }
308- }
309-
310- /// <inheritdoc />
311- public int MeshPhysicsLayerMask => ( 1 << MeshPhysicsLayer ) ;
312-
313- /// <inheritdoc />
314- public bool RecalculateNormals { get ; set ; } = true ;
315-
316- /// <inheritdoc />
317- public int TrianglesPerCubicMeter { get ; set ; } = 0 ;
318-
319- private Material occlusionMaterial = null ;
320-
321- /// <inheritdoc />
322- public Material OcclusionMaterial
323- {
324- get => occlusionMaterial ;
325-
326- set
327- {
328- if ( value != occlusionMaterial )
329- {
330- occlusionMaterial = value ;
331-
332- if ( DisplayOption == SpatialAwarenessMeshDisplayOptions . Occlusion )
333- {
334- ApplyUpdatedMeshDisplayOption ( SpatialAwarenessMeshDisplayOptions . Occlusion ) ;
335- }
336- }
337- }
338- }
339-
340-
341- private Material visibleMaterial = null ;
342-
343- /// <inheritdoc />
344- public Material VisibleMaterial
345- {
346- get => visibleMaterial ;
347-
348- set
349- {
350- if ( value != visibleMaterial )
351- {
352- visibleMaterial = value ;
353-
354- if ( DisplayOption == SpatialAwarenessMeshDisplayOptions . Visible )
355- {
356- ApplyUpdatedMeshDisplayOption ( SpatialAwarenessMeshDisplayOptions . Visible ) ;
357- }
358- }
359- }
360- }
361-
362- /// <summary>
363- /// Stop the observer and releases resources.
364- /// </summary>
365- private void CleanupObserver ( )
366- {
367- if ( IsRunning )
368- {
369- Suspend ( ) ;
370- }
371-
372- ClearObservations ( ) ;
373- }
374-
375220 /// <summary>
376221 /// Applies the appropriate material, based on the current of the <see cref="SpatialAwarenessMeshDisplayOptions"/> property.
377222 /// </summary>
@@ -392,45 +237,6 @@ private void ApplyMeshMaterial(SpatialAwarenessMeshObject meshObject)
392237 meshObject . Renderer . enabled = enable ;
393238 }
394239
395- /// <summary>
396- /// Updates the material for each observed mesh,.
397- /// </summary>
398- /// <param name="option">
399- /// The <see cref="SpatialAwarenessMeshDisplayOptions"/> value to be used to determine the appropriate material.
400- /// </param>
401- private void ApplyUpdatedMeshDisplayOption ( SpatialAwarenessMeshDisplayOptions option )
402- {
403- bool enable = ( option != SpatialAwarenessMeshDisplayOptions . None ) ;
404-
405- foreach ( SpatialAwarenessMeshObject meshObject in Meshes . Values )
406- {
407- if ( ( meshObject ? . Renderer == null ) ) { continue ; }
408-
409- if ( enable )
410- {
411- meshObject . Renderer . sharedMaterial = ( option == SpatialAwarenessMeshDisplayOptions . Visible ) ?
412- VisibleMaterial :
413- OcclusionMaterial ;
414- }
415-
416- meshObject . Renderer . enabled = enable ;
417- }
418- }
419-
420- /// <summary>
421- /// Updates the mesh physics layer for current mesh observations.
422- /// </summary>
423- private void ApplyUpdatedPhysicsLayer ( )
424- {
425- foreach ( SpatialAwarenessMeshObject meshObject in Meshes . Values )
426- {
427- if ( meshObject ? . GameObject == null ) { continue ; }
428-
429- meshObject . GameObject . layer = MeshPhysicsLayer ;
430-
431- }
432- }
433-
434- #endregion IMixedRealitySpatialAwarenessMeshObserver Implementation
240+ #endregion Helpers
435241 }
436242}
0 commit comments