AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ObjectSurfaceObserver.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 UnityEngine;
5 
6 namespace HoloToolkit.Unity.SpatialMapping
7 {
9  {
10  [Tooltip("The room model to use when loading meshes in Unity.")]
11  public GameObject RoomModel;
12 
13  [Tooltip("If greater than or equal to zero, surface objects will claim to be updated at this period. This is useful when working with libraries that respond to updates (such as the SpatialUnderstanding library). If negative, surfaces will not claim to be updated.")]
14  public float SimulatedUpdatePeriodInSeconds = -1;
15 
16  private void Start()
17  {
18 #if UNITY_2017_2_OR_NEWER
19  if (!UnityEngine.XR.XRDevice.isPresent && Application.isEditor)
20 #else
21  if (!UnityEngine.VR.VRDevice.isPresent && Application.isEditor)
22 #endif
23  {
24  // When in the Unity editor and not remoting, try loading saved meshes from a model.
25  Load(RoomModel);
26 
27  if (GetMeshFilters().Count > 0)
28  {
29  SpatialMappingManager.Instance.SetSpatialMappingSource(this);
30  }
31  }
32  }
33 
38  public void Load(GameObject roomModel)
39  {
40  if (roomModel == null)
41  {
42  Debug.Log("No room model specified.");
43  return;
44  }
45 
46  GameObject roomObject = Instantiate(roomModel);
47  Cleanup();
48 
49  try
50  {
51  MeshFilter[] roomFilters = roomObject.GetComponentsInChildren<MeshFilter>();
52 
53  for (int iMesh = 0; iMesh < roomFilters.Length; iMesh++)
54  {
55  MeshFilter meshFilter = roomFilters[iMesh];
56 
57  SurfaceObject newSurfaceObject = CreateSurfaceObject(
58  mesh: meshFilter.sharedMesh,
59  objectName: "roomMesh-" + iMesh,
60  parentObject: transform,
61  meshID: iMesh
62  );
63 
64  newSurfaceObject.Object.transform.localPosition = meshFilter.transform.position;
65  newSurfaceObject.Object.transform.localRotation = meshFilter.transform.rotation;
66 
67  AddSurfaceObject(newSurfaceObject);
68  }
69  }
70  catch
71  {
72  Debug.Log("Failed to load object " + roomModel.name);
73  }
74  finally
75  {
76  if (roomObject != null)
77  {
78  DestroyImmediate(roomObject);
79  }
80  }
81  }
82 
83  private float lastUpdateUnscaledTimeInSeconds = 0;
84 
85  private void Update()
86  {
87  if (SimulatedUpdatePeriodInSeconds >= 0)
88  {
89  if ((Time.unscaledTime - lastUpdateUnscaledTimeInSeconds) >= SimulatedUpdatePeriodInSeconds)
90  {
91  for (int iSurface = 0; iSurface < SurfaceObjects.Count; iSurface++)
92  {
93  UpdateOrAddSurfaceObject(SurfaceObjects[iSurface]);
94  }
95 
96  lastUpdateUnscaledTimeInSeconds = Time.unscaledTime;
97  }
98  }
99  }
100  }
101 }
void Load(GameObject roomModel)
Loads the SpatialMapping mesh from the specified room object.
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
The SpatialMappingManager class allows applications to use a SurfaceObserver or a stored Spatial Mapp...