-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathSceneDataLoader.cs
288 lines (242 loc) · 9.86 KB
/
SceneDataLoader.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using OVRSimpleJSON;
using PhantoUtils;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Events;
using Classification = OVRSceneManager.Classification;
namespace Phantom.Environment.Scripts
{
// This class loads scene data from either Scene API or a JSON file.
public class SceneDataLoader : MonoBehaviour
{
public enum SceneDataSource
{
SceneApi,
StaticMeshData
}
// Scene API data
private static readonly string[] MeshClassifications =
{ "GlobalMesh", Classification.GlobalMesh };
// A reference to the OVRSceneManager prefab.
[SerializeField] private OVRSceneManager ovrSceneManagerPrefab;
// The root transform of the OVRSceneManager.
[SerializeField] private Transform sceneRoot;
[SerializeField] private SceneDataLoaderSettings settings;
[SerializeField] private bool loadAllRooms = false;
// UnityEvent fired when scene data is loaded.
public UnityEvent<Transform> SceneDataLoaded = new();
// UnityEvent fired when scene data is not available.
public UnityEvent NoSceneModelAvailable = new();
// UnityEvent fired when a new scene model is available.
public UnityEvent NewSceneModelAvailable = new();
private OVRSceneManager ovrSceneManager;
private Transform staticMesh;
private void Awake()
{
Assert.IsNotNull(ovrSceneManagerPrefab, $"{nameof(ovrSceneManagerPrefab)} cannot be null.");
Assert.IsNotNull(sceneRoot, $"{nameof(sceneRoot)} cannot be null.");
}
private IEnumerator Start()
{
yield return null;
LoadMeshes();
}
private void OnDestroy()
{
if (ovrSceneManager != null) ovrSceneManager.SceneModelLoadedSuccessfully -= OnSceneAPIDataLoaded;
}
#if UNITY_EDITOR
private void OnValidate()
{
if (sceneRoot == null) sceneRoot = transform;
}
#endif
/// <summary>
/// Loads the scene model from the headset or the provided JSON data.
public void LoadMeshes()
{
if (settings.LoadSceneOnStart)
{
Debug.Log($"{Application.productName}: Loading scene.");
#if UNITY_EDITOR
if (!OVRManager.isHmdPresent && !string.IsNullOrEmpty(settings.SceneJson))
{
LoadStaticMesh(settings.SceneJson);
return;
}
#endif
switch (settings.SceneDataSource)
{
// Scene API data.
case SceneDataSource.SceneApi:
StartCoroutine(LoadSceneAPIData());
break;
// Static mesh data.
case SceneDataSource.StaticMeshData:
LoadStaticMesh(settings.SceneJson);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
/// <summary>
/// Load the scene model from the headset
/// </summary>
private IEnumerator LoadSceneAPIData()
{
Debug.Log($"{Application.productName}: Loading scene model.");
if (ovrSceneManager == null)
{
// Scene Manager from previous scene.
var existingManager = FindObjectOfType<OVRSceneManager>();
if (existingManager != null) DestroyImmediate(existingManager.gameObject);
ovrSceneManager = Instantiate(ovrSceneManagerPrefab, transform);
ovrSceneManager.ActiveRoomsOnly = !loadAllRooms;
}
// Set the initial room root.
ovrSceneManager.InitialAnchorParent = sceneRoot;
// Wait for the manager to fully load the scene so we can get its dimensions and create
ovrSceneManager.SceneModelLoadedSuccessfully += () =>
{
Debug.Log($"{Application.productName}: {nameof(SceneDataLoader)}: SceneModelLoadedSuccessfully ");
OnSceneAPIDataLoaded();
};
// Wait until the manager has completed one update to start the loading process.
ovrSceneManager.SceneCaptureReturnedWithoutError += () =>
{
Debug.Log(
$"{Application.productName}: {nameof(SceneDataLoader)}: SceneCaptureReturnedWithoutError ");
};
// Catch the various errors that can occur when the scene capture is started.
ovrSceneManager.UnexpectedErrorWithSceneCapture += () =>
{
Debug.LogError(
$"{Application.productName}: {nameof(SceneDataLoader)}: UnexpectedErrorWithSceneCapture ");
NoSceneModelAvailable?.Invoke();
};
ovrSceneManager.NoSceneModelToLoad += () =>
{
Debug.LogError($"{Application.productName}: {nameof(SceneDataLoader)}: NoSceneModelToLoad ");
NoSceneModelAvailable?.Invoke();
};
ovrSceneManager.NewSceneModelAvailable += () =>
{
Debug.Log($"{Application.productName}: {nameof(SceneDataLoader)}: NewSceneModelAvailable ");
if (ovrSceneManager.LoadSceneModel())
{
NewSceneModelAvailable?.Invoke();
}
};
yield return null;
}
/// <summary>
/// Rescan the scene to update the list of available meshes.
/// </summary>
public void Rescan()
{
ovrSceneManager.RequestSceneCapture();
}
/// <summary>
/// Load a static mesh from a JSON string.
/// </summary>
private void LoadStaticMesh(string jsonText)
{
var json = JSON.Parse(jsonText);
var instantiatedMesh = JsonSceneBuilder.SpawnSceneRoom(json, sceneRoot, ovrSceneManagerPrefab);
if (settings.CenterStaticMesh)
// Center mesh on tracking space.
AlignStaticMesh(instantiatedMesh);
SceneDataLoaded?.Invoke(sceneRoot);
var debugSceneEntities = gameObject.AddComponent<DebugSceneEntities>();
debugSceneEntities.StaticSceneModelLoaded();
}
/// <summary>
/// Shift static mesh so it's centered around the origin and floors match.
/// </summary>
/// <param name="root"></param>
private void AlignStaticMesh(Transform root)
{
// FIXME: find the room the user is closest to and center that room around user.
var meshFilters = root.GetComponentsInChildren<MeshFilter>();
MeshFilter globalMesh = null;
if (meshFilters.Length == 1)
globalMesh = meshFilters[0];
else
foreach (var mf in meshFilters)
{
if (mf.TryGetComponent<OVRSemanticClassification>(out var semanticClassification)
&& semanticClassification.Contains(OVRSceneManager.Classification.GlobalMesh))
{
globalMesh = mf;
break;
}
}
if (globalMesh == null)
{
return;
}
var bounds = globalMesh.mesh.bounds;
var meshTransform = globalMesh.transform;
var worldMin = meshTransform.TransformPoint(bounds.min);
var worldCenter = meshTransform.TransformPoint(bounds.center);
worldCenter.y = worldMin.y;
root.position -= worldCenter;
}
private void OnSceneAPIDataLoaded()
{
SceneDataLoaded?.Invoke(sceneRoot);
}
public OVRSceneAnchor GetSceneMeshPrefab()
{
foreach (var prefabOverride in ovrSceneManagerPrefab.PrefabOverrides)
{
if (prefabOverride.ClassificationLabel == OVRSceneManager.Classification.GlobalMesh)
{
return prefabOverride.Prefab;
}
}
return null;
}
/// <summary>
/// Brittle method for finding the global mesh in a prefab that contains multiple static meshes.
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
private static MeshFilter FindGlobalMeshFilter(Transform root)
{
var meshFilters = root.GetComponentsInChildren<MeshFilter>();
if (meshFilters.Length == 1) return meshFilters[0];
foreach (var mf in meshFilters)
{
var tf = mf.transform;
while (tf != root)
{
if (tf.name.Contains("GlobalMesh", StringComparison.InvariantCultureIgnoreCase)) return mf;
tf = tf.parent;
}
}
return null;
}
public static void AddAnchorReferenceCount(OVRSceneAnchor anchor)
{
var anchorReferenceCountDictionary = typeof(OVRSceneAnchor).GetField("AnchorReferenceCountDictionary",
BindingFlags.NonPublic | BindingFlags.Static);
if (anchorReferenceCountDictionary != null)
{
var refCountStaticField = anchorReferenceCountDictionary.GetValue(null);
if (refCountStaticField is Dictionary<OVRSpace, int> refCountDictionary)
{
refCountDictionary[anchor.Space] = 1;
anchorReferenceCountDictionary.SetValue(null, refCountDictionary);
}
}
}
}
}