-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMRSceneManager.cs
More file actions
419 lines (340 loc) · 12.2 KB
/
MRSceneManager.cs
File metadata and controls
419 lines (340 loc) · 12.2 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Meta.XR.Samples;
using JetBrains.Annotations;
using Meta.XR.MRUtilityKit;
using Photon.Pun;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Events;
/// <summary>
/// Mixed Reality Scene Manager - handles loading, scanning, sharing, etc of MRUKRooms (often referred to as "scenes")
/// in the local mixed reality space.
/// </summary>
[MetaCodeSample("SpaceSharing")]
public class MRSceneManager : MonoBehaviour
{
//
// Static interface
public static void LoadSharedScene()
{
if (AssertInstance())
s_Instance.LoadSharedSceneImpl();
}
public static void LoadOrScanLocalScene()
{
if (AssertInstance())
s_Instance.LoadOrScanLocalSceneImpl();
}
public static void ShareLocalScene()
{
if (AssertInstance())
s_Instance.ShareLocalSceneImpl();
}
public static void SetSharedSceneUuids([CanBeNull] ICollection<Guid> sharedRoomUuids, Guid groupUuid)
{
if (AssertInstance())
s_Instance.SetSharedSceneUuidsImpl(sharedRoomUuids ?? Array.Empty<Guid>(), groupUuid);
}
public static void SetHostAlignment((Guid roomUuid, Pose pose)? alignment)
{
if (AssertInstance())
s_Instance.m_HostAlignment = alignment;
}
public static void Clear()
{
if (MRUK.Instance)
{
int nRooms = MRUK.Instance.Rooms.Count;
if (nRooms > 0)
{
MRUK.Instance.ClearScene();
Sampleton.Log($"<i>* Unloaded {nRooms} MRUKRooms *</i>");
}
}
if (s_Instance)
{
s_Instance.m_SharedGroupId = null;
s_Instance.m_SharedRoomIds = null;
s_Instance.m_HostAlignment = null;
}
}
//
// Instance interface
public void ToggleGlobalMesh()
{
if (!MRUK.Instance)
{
Sampleton.Error($"{nameof(ToggleGlobalMesh)} FAILED! (no MRUK instance)");
return;
}
if (MRUK.Instance.Rooms.Count == 0)
{
Sampleton.Error($"{nameof(ToggleGlobalMesh)} FAILED! (there are no rooms currently loaded)");
return;
}
if (!m_GlobalMeshMaterial)
{
Sampleton.Error($"{nameof(ToggleGlobalMesh)} FAILED! (this sample scene does not provide a global mesh material)");
return;
}
if (m_GlobalMesh)
{
m_GlobalMesh.HideMesh = !m_GlobalMesh.HideMesh;
return;
}
m_GlobalMesh = new GameObject("_globalMeshViz", typeof(EffectMesh)).GetComponent<EffectMesh>();
m_GlobalMesh.Labels = MRUKAnchor.SceneLabels.GLOBAL_MESH;
m_GlobalMesh.MeshMaterial = m_GlobalMeshMaterial;
m_GlobalMesh.HideMesh = false;
m_GlobalMesh.CreateMesh();
}
//
// private
static MRSceneManager s_Instance;
static bool AssertInstance()
{
Assert.IsNotNull(s_Instance, $"A {nameof(MRSceneManager)} instance must exist!");
return s_Instance.isActiveAndEnabled;
}
//
// Serialized fields
[SerializeField]
string m_HardcodedGroupUUID;
[SerializeField]
protected Material m_GlobalMeshMaterial;
[Space]
[SerializeField, Tooltip("OnSceneLoaded should update UI to reflect that room anchors have been loaded (regardless of shared vs local). Registered with MRUK.RegisterSceneLoadedCallback.")]
public UnityEvent OnSceneLoaded = new();
[SerializeField, Tooltip("OnSceneShared should update UI to reflect that room anchors have been shared to the current Photon Room or colocation, and should be loadable.")]
public UnityEvent OnSceneShared = new();
// Runtime fields
Guid? m_SharedGroupId;
ICollection<Guid> m_SharedRoomIds = Array.Empty<Guid>();
(Guid alignmentRoomUuid, Pose floorWorldPoseOnHost)? m_HostAlignment;
EffectMesh m_GlobalMesh;
//
// MonoBehaviour messages
void OnEnable()
{
Assert.IsNull(s_Instance, $"{nameof(MRSceneManager)}.s_Instance");
s_Instance = this;
}
void OnDisable()
{
if (s_Instance == this)
s_Instance = null;
if (Sampleton.GetActiveUI(out var ui))
{
ui.OnDisplayLobby -= Clear;
}
}
IEnumerator Start()
{
yield return null;
if (!MRUK.Instance)
{
Sampleton.Error($"{nameof(MRSceneManager)} can't do anything without an MRUK instance!");
enabled = false;
yield break;
}
MRUK.Instance.RegisterSceneLoadedCallback(OnSceneLoaded.Invoke);
if (Sampleton.GetActiveUI(out var ui))
{
ui.OnDisplayLobby += Clear;
}
}
//
// instance impl.
Guid GetSharedGroupId()
{
if (!m_SharedGroupId.HasValue)
{
if (m_HardcodedGroupUUID == "build")
{
m_SharedGroupId = Guid.Parse(Application.buildGUID);
Sampleton.Log($"* Using buildGUID as Group Sharing UUID: {m_SharedGroupId}");
}
else if (Guid.TryParse(m_HardcodedGroupUUID, out var parsed))
{
m_SharedGroupId = parsed;
Sampleton.Log($"* HARDCODED Group Sharing UUID parsed: {m_SharedGroupId}");
}
else
{
m_SharedGroupId = Guid.NewGuid();
Sampleton.Log($"* NEW Group Sharing UUID generated: {m_SharedGroupId}");
}
}
return m_SharedGroupId.Value;
}
bool IsReadyToShare(out MRUK mruk, out MRUKRoom curRoom, bool printReason = true)
{
mruk = MRUK.Instance;
curRoom = null;
if (!mruk || !mruk.IsInitialized)
{
if (printReason)
Sampleton.Error("- Can't share - no MRUK GameObject in scene, or it has not been initialized.");
return false;
}
curRoom = mruk.GetCurrentRoom();
if (!curRoom)
{
if (printReason)
Sampleton.Error("- Can't share - there are no (loaded) MRUKRooms to share.");
return false;
}
switch (Sampleton.ConnectMethod)
{
case ConnectMethod.Photon:
if (!Sampleton.PhotonRoomManager)
{
if (printReason)
Sampleton.Error("- Can't share - no PhotonRoomManager in scene.");
return false;
}
if (!PhotonRoomManager.CheckConnection(tryReconnect: false, logOnFail: false))
{
if (printReason)
Sampleton.Error("- Can't share - not connected to the Photon network");
return false;
}
if (!PhotonNetwork.InRoom)
{
if (printReason)
Sampleton.Error("- Can't share - not currently in a Photon Room.");
return false;
}
if (PhotonNetwork.CurrentRoom.PlayerCount == 0) // almost certainly dead code, but w/e
{
if (printReason)
Sampleton.Error("- Can't share - no users to share with or can't get user list from Photon");
return false;
}
break;
}
return true;
}
bool IsReadyForLoad(out Guid groupUuid, out ICollection<Guid> roomIds, bool printReason = true)
{
groupUuid = m_SharedGroupId.GetValueOrDefault();
roomIds = m_SharedRoomIds;
if (groupUuid == Guid.Empty)
{
if (printReason)
Sampleton.Error("Group Sharing UUID not yet shared/received.");
return false;
}
Sampleton.Log("Group Sharing UUID: " + m_SharedGroupId);
return true;
}
async void LoadSharedSceneImpl()
{
if (!IsReadyForLoad(out var groupUuid, out var roomIds))
return;
if (roomIds.Count == 0) // indicates "load everything idc"
Sampleton.Log($"Loading all rooms shared with group {groupUuid}...");
else if (roomIds.Count == 1)
Sampleton.Log($"Loading 1 room shared with group {groupUuid}...");
else
Sampleton.Log($"Loading {roomIds.Count} rooms shared with group {groupUuid}...");
foreach (var id in roomIds)
{
Sampleton.Log($"+ room: {id}");
}
var result = await MRUK.Instance.LoadSceneFromSharedRooms(
roomIds,
groupUuid,
alignmentData: m_HostAlignment,
removeMissingRooms: true
);
Sampleton.Log(
$"... MRUK.LoadSceneFromSharedRooms: {result.ForLogging()}",
result == 0 ? LogType.Log : result > 0 ? LogType.Warning : LogType.Error
);
}
async void LoadOrScanLocalSceneImpl()
{
if (Sampleton.PlayerFace.IsInLoadedRoom())
{
Sampleton.Warn($"{nameof(LoadOrScanLocalScene)} won't be called while already in a loaded scene.");
return;
}
Sampleton.Log($"{nameof(LoadOrScanLocalScene)}:");
// always request a scan if rooms have already been loaded but our face isn't in any of them:
if (MRUK.Instance.Rooms.Count > 0 && !await OVRScene.RequestSpaceSetup())
{
Sampleton.Error($"{nameof(OVRScene.RequestSpaceSetup)} FAILED! (not cancelled)");
return;
}
var result = await MRUK.Instance.LoadSceneFromDevice(
requestSceneCaptureIfNoDataFound: true,
removeMissingRooms: true
);
Sampleton.Log(
$"MRUK.LoadSceneFromDevice: {result.ForLogging()}",
result == 0 ? LogType.Log : result > 0 ? LogType.Warning : LogType.Error
);
}
async void ShareLocalSceneImpl()
{
if (!IsReadyToShare(out var mruk, out var curRoom, printReason: true))
return;
// TODO if scene ownership is mixed and shared to the same group UUID, this probably doesn't work as-is.
var groupId = GetSharedGroupId();
var result = await mruk.ShareRoomsAsync(mruk.Rooms, groupId);
if (!result.Success)
{
Sampleton.Error($"{nameof(ShareLocalScene)} FAILED: {result.Status.ForLogging()}");
return;
}
Sampleton.Log(
$"{nameof(ShareLocalScene)}: {result.Status.ForLogging()}\n" +
$"to group: {groupId}"
);
var roomIds = mruk.Rooms.Select(room => room.Anchor.Uuid).ToArray();
for (int i = 0; i < roomIds.Length; ++i)
{
var uuid = roomIds[i];
if (uuid == curRoom.Anchor.Uuid)
{
Sampleton.Log($"+ current room: {uuid}");
// our Photon impl. wants the first room id to be the current room the host is in:
(roomIds[0], roomIds[i]) = (roomIds[i], roomIds[0]);
}
else
{
Sampleton.Log($"+ side room: {uuid}");
}
}
var floor = curRoom.FloorAnchors.Count > 0 ? curRoom.FloorAnchors[0].transform
: curRoom.transform;
var floorPose = new Pose(floor.position, floor.rotation);
SetHostAlignment((curRoom.Anchor.Uuid, floorPose));
if (Sampleton.ConnectMethod == ConnectMethod.Photon)
{
PhotonRoomManager.PublishRoomData(groupId, roomIds, floorPose);
}
SetSharedSceneUuidsImpl(roomIds, groupId);
}
void SetSharedSceneUuidsImpl(ICollection<Guid> sharedRoomUuids, Guid groupUuid)
{
if (m_SharedRoomIds?.Count > 0 && m_SharedGroupId == groupUuid)
{
if (m_SharedRoomIds is not HashSet<Guid> allGroupRooms)
allGroupRooms = new HashSet<Guid>(m_SharedRoomIds);
allGroupRooms.UnionWith(sharedRoomUuids);
m_SharedRoomIds = allGroupRooms;
}
else
{
m_SharedRoomIds = sharedRoomUuids;
}
m_SharedGroupId = groupUuid;
OnSceneShared.Invoke();
}
}