forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectSurfaceObserver.cs
More file actions
93 lines (81 loc) · 3.1 KB
/
ObjectSurfaceObserver.cs
File metadata and controls
93 lines (81 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
namespace HoloToolkit.Unity.SpatialMapping
{
public class ObjectSurfaceObserver : SpatialMappingSource
{
[Tooltip("The room model to use when loading meshes in Unity.")]
public GameObject RoomModel;
[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.")]
public float SimulatedUpdatePeriodInSeconds = -1;
// Use this for initialization.
private void Start()
{
#if UNITY_EDITOR
if (!UnityEngine.VR.VRDevice.isPresent)
{
// When in the Unity editor and not remoting, try loading saved meshes from a model.
Load(RoomModel);
if (GetMeshFilters().Count > 0)
{
SpatialMappingManager.Instance.SetSpatialMappingSource(this);
}
}
#endif
}
/// <summary>
/// Loads the SpatialMapping mesh from the specified room object.
/// </summary>
/// <param name="roomModel">The room model to load meshes from.</param>
public void Load(GameObject roomModel)
{
if (roomModel == null)
{
Debug.Log("No room model specified.");
return;
}
GameObject roomObject = Instantiate(roomModel);
Cleanup();
try
{
MeshFilter[] roomFilters = roomObject.GetComponentsInChildren<MeshFilter>();
for (int iMesh = 0; iMesh < roomFilters.Length; iMesh++)
{
AddSurfaceObject(CreateSurfaceObject(
mesh: roomFilters[iMesh].sharedMesh,
objectName: "roomMesh-" + iMesh,
parentObject: transform,
meshID: iMesh
));
}
}
catch
{
Debug.Log("Failed to load object " + roomModel.name);
}
finally
{
if (roomObject != null)
{
DestroyImmediate(roomObject);
}
}
}
private float lastUpdateUnscaledTimeInSeconds = 0;
private void Update()
{
if (SimulatedUpdatePeriodInSeconds >= 0)
{
if ((Time.unscaledTime - lastUpdateUnscaledTimeInSeconds) >= SimulatedUpdatePeriodInSeconds)
{
for (int iSurface = 0; iSurface < SurfaceObjects.Count; iSurface++)
{
UpdateOrAddSurfaceObject(SurfaceObjects[iSurface]);
}
lastUpdateUnscaledTimeInSeconds = Time.unscaledTime;
}
}
}
}
}