AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SpatialProcessingTest.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity.SpatialMapping.Tests
8 {
13  public class SpatialProcessingTest : Singleton<SpatialProcessingTest>
14  {
15  [Tooltip("How much time (in seconds) that the SurfaceObserver will run after being started; used when 'Limit Scanning By Time' is checked.")]
16  public float scanTime = 30.0f;
17 
18  [Tooltip("Material to use when rendering Spatial Mapping meshes while the observer is running.")]
19  public Material defaultMaterial;
20 
21  [Tooltip("Optional Material to use when rendering Spatial Mapping meshes after the observer has been stopped.")]
22  public Material secondaryMaterial;
23 
24  [Tooltip("Minimum number of floor planes required in order to exit scanning/processing mode.")]
25  public uint minimumFloors = 1;
26 
30  private bool meshesProcessed = false;
31 
35  private void Start()
36  {
37  // Update surfaceObserver and storedMeshes to use the same material during scanning.
38  SpatialMappingManager.Instance.SetSurfaceMaterial(defaultMaterial);
39 
40  // Register for the MakePlanesComplete event.
41  SurfaceMeshesToPlanes.Instance.MakePlanesComplete += SurfaceMeshesToPlanes_MakePlanesComplete;
42  }
43 
47  private void Update()
48  {
49  // Check to see if the spatial mapping data has been processed yet.
50  if (!meshesProcessed)
51  {
52  // Check to see if enough scanning time has passed
53  // since starting the observer.
54  if ((Time.unscaledTime - SpatialMappingManager.Instance.StartTime) < scanTime)
55  {
56  // If we have a limited scanning time, then we should wait until
57  // enough time has passed before processing the mesh.
58  }
59  else
60  {
61  // The user should be done scanning their environment,
62  // so start processing the spatial mapping data...
63 
64  if (SpatialMappingManager.Instance.IsObserverRunning())
65  {
66  // Stop the observer.
67  SpatialMappingManager.Instance.StopObserver();
68  }
69 
70  // Call CreatePlanes() to generate planes.
71  CreatePlanes();
72 
73  // Set meshesProcessed to true.
74  meshesProcessed = true;
75  }
76  }
77  }
78 
84  private void SurfaceMeshesToPlanes_MakePlanesComplete(object source, System.EventArgs args)
85  {
86  // Collection of floor planes that we can use to set horizontal items on.
87  List<GameObject> floors = new List<GameObject>();
88  floors = SurfaceMeshesToPlanes.Instance.GetActivePlanes(PlaneTypes.Floor);
89 
90  // Check to see if we have enough floors (minimumFloors) to start processing.
91  if (floors.Count >= minimumFloors)
92  {
93  // Reduce our triangle count by removing any triangles
94  // from SpatialMapping meshes that intersect with active planes.
95  RemoveVertices(SurfaceMeshesToPlanes.Instance.ActivePlanes);
96 
97  // After scanning is over, switch to the secondary (occlusion) material.
98  SpatialMappingManager.Instance.SetSurfaceMaterial(secondaryMaterial);
99  }
100  else
101  {
102  // Re-enter scanning mode so the user can find more surfaces before processing.
103  SpatialMappingManager.Instance.StartObserver();
104 
105  // Re-process spatial data after scanning completes.
106  meshesProcessed = false;
107  }
108  }
109 
113  private void CreatePlanes()
114  {
115  // Generate planes based on the spatial map.
117  if (surfaceToPlanes != null && surfaceToPlanes.enabled)
118  {
119  surfaceToPlanes.MakePlanes();
120  }
121  }
122 
127  private void RemoveVertices(IEnumerable<GameObject> boundingObjects)
128  {
130  if (removeVerts != null && removeVerts.enabled)
131  {
132  removeVerts.RemoveSurfaceVerticesWithinBounds(boundingObjects);
133  }
134  }
135 
139  protected override void OnDestroy()
140  {
141  if (SurfaceMeshesToPlanes.Instance != null)
142  {
143  SurfaceMeshesToPlanes.Instance.MakePlanesComplete -= SurfaceMeshesToPlanes_MakePlanesComplete;
144  }
145 
146  base.OnDestroy();
147  }
148  }
149 }
The SpatialProcessingTest class allows applications to scan the environment for a specified amount of...
void RemoveSurfaceVerticesWithinBounds(IEnumerable< GameObject > boundingObjects)
Removes portions of the surface mesh that exist within the bounds of the boundingObjects.
RemoveSurfaceVertices will remove any vertices from the Spatial Mapping Mesh that fall within the bou...
override void OnDestroy()
Called when the GameObject is unloaded.
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
PlaneTypes
All possible plane types that a SurfacePlane can be.
Definition: SurfacePlane.cs:13
The SpatialMappingManager class allows applications to use a SurfaceObserver or a stored Spatial Mapp...
void MakePlanes()
Creates planes based on meshes gathered by the SpatialMappingManager&#39;s SurfaceObserver.
SurfaceMeshesToPlanes will find and create planes based on the meshes returned by the SpatialMappingM...
Singleton behaviour class, used for components that should only have one instance.
Definition: Singleton.cs:14