-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathSharedAnchor.cs
More file actions
558 lines (432 loc) · 16.2 KB
/
SharedAnchor.cs
File metadata and controls
558 lines (432 loc) · 16.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Meta.XR.Samples;
using System;
using System.Collections.Generic;
using System.Linq;
using Photon.Pun;
using TMPro;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;
using Sampleton = SampleController;
/// <summary>
/// Controls anchor data and anchor control panel behavior.
/// </summary>
/// <remarks>
/// You can locate key API calls throughout the sample code by searching for "KEY API CALL" (use CTRL+F in most IDEs).
/// </remarks>
[RequireComponent(typeof(OVRSpatialAnchor))]
[MetaCodeSample("SharedSpatialAnchors")]
public class SharedAnchor : MonoBehaviour
{
//
// Static interface
public static IReadOnlyCollection<SharedAnchor> All
=> s_Instances.Values.Where(a => a).ToArray();
public static IReadOnlyCollection<SharedAnchor> AllMine
=> s_Instances.Values.Where(a => a && a.Source.IsMine).ToArray();
public static bool Find(Guid uuid, out SharedAnchor anchor)
=> s_Instances.TryGetValue(uuid, out anchor) && anchor;
static readonly Dictionary<Guid, SharedAnchor> s_Instances = new();
public static async void ShareAllMineTo(ulong oneUser, bool reshareOnly = false)
{
Sampleton.Log($"{nameof(ShareAllMineTo)}({oneUser}{(reshareOnly ? $", {nameof(reshareOnly)}" : "")})");
// need to ask the OVR backend if this user ID is valid for our app to use as a share target:
if (!OVRSpaceUser.TryCreate(oneUser, out var spaceUser))
{
Sampleton.LogError("- ERR: uid is not a valid space user here.");
return;
}
var anchors = (
from a in AllMine
where !reshareOnly || a.ShareSucceeded
select a
).ToList();
if (anchors.Count == 0)
{
Sampleton.Log($"- SKIP: no loaded anchors to share.");
return;
}
// KEY API CALL: static OVRSpatialAnchor.ShareAsync(anchors, users)
var shareResult = await OVRSpatialAnchor.ShareAsync(anchors.Select(a => a.SpatialAnchor), new[] { spaceUser });
string loggedResult = shareResult.ForLogging();
bool success = shareResult.IsSuccess();
var uuids = new HashSet<Guid>(anchors.Count);
foreach (var anchor in anchors)
{
anchor.ShareSucceeded = success;
uuids.Add(anchor.Uuid);
}
if (!success)
{
Sampleton.LogError($"- Share FAILED: {loggedResult}");
return;
}
Sampleton.Log($"+ Shared {uuids.Count} spatial anchors: {loggedResult}");
PhotonAnchorManager.PublishAnchorsToUsers(uuids, new[] { oneUser });
}
public static async void ShareAllMineTo(IReadOnlyCollection<ulong> users, bool reshareOnly = false)
{
Sampleton.Log($"{nameof(ShareAllMineTo)}:");
var anchors = (
from a in AllMine
where !reshareOnly || a.ShareSucceeded
select a
).ToList();
if (anchors.Count == 0)
{
Sampleton.Log($"- SKIP: no loaded anchors to share.");
return;
}
var spaceUsers = new List<OVRSpaceUser>(users.Count);
foreach (var uid in users)
{
if (OVRSpaceUser.TryCreate(uid, out var spaceUser))
{
spaceUsers.Add(spaceUser);
}
else
{
Sampleton.LogError($"- ERR: {uid} is not a valid space user here.");
return;
}
}
Sampleton.Log($"+ sharing {anchors.Count} anchors to {spaceUsers.Count} uids..");
// KEY API CALL: static OVRSpatialAnchor.ShareAsync(anchors, users)
var shareResult = await OVRSpatialAnchor.ShareAsync(anchors.Select(a => a.SpatialAnchor), spaceUsers);
string loggedResult = shareResult.ForLogging();
bool success = shareResult.IsSuccess();
var uuids = new HashSet<Guid>(anchors.Count);
foreach (var anchor in anchors)
{
anchor.ShareSucceeded = success;
uuids.Add(anchor.Uuid);
}
if (!success)
{
Sampleton.LogError($"- Share FAILED: {loggedResult}");
return;
}
Sampleton.Log($"+ Shared {uuids.Count} spatial anchors: {loggedResult}");
PhotonAnchorManager.PublishAnchorsToUsers(AllMine.Select(a => a.Uuid), users);
}
//
// Public Properties & UnityEvent Callbacks (for Buttons)
public OVRSpatialAnchor SpatialAnchor => m_SpatialAnchor;
public Guid Uuid => SpatialAnchor && SpatialAnchor.Created ? SpatialAnchor.Uuid
: Source.Uuid;
public bool IsSaved
{
get => LocallySaved.AnchorIsRemembered(Uuid);
set
{
if (value)
LocallySaved.RememberAnchor(Uuid, Source.IsMine);
else
LocallySaved.ForgetAnchor(Uuid);
UpdateUI();
}
}
public bool ShareSucceeded
{
get => m_ShareSucceeded;
set
{
m_ShareAttempted = true;
m_ShareSucceeded = value;
UpdateUI();
}
}
bool m_ShareAttempted;
bool m_ShareSucceeded;
public AnchorSource Source
{
get => m_Source;
set
{
if (m_Source.Equals(value))
return;
if (m_Source.IsSet)
{
Sampleton.LogError($"{name}.{nameof(Source)} is already set!");
return;
}
m_Source = value;
m_ShareSucceeded = value.Origin == AnchorSource.Type.FromSpaceUserShare;
}
}
public async void OnSaveLocalButtonPressed()
{
if (Uuid == Guid.Empty)
{
Sampleton.LogError($"{nameof(OnSaveLocalButtonPressed)}: Not saving! null, missing, or invalid OVRSpatialAnchor reference");
return;
}
if (IsSaved)
{
IsSaved = false;
// only "un"saves the UUID serialized by the app logic~
// (distinct from the "Erase" API call)
LocallySaved.CommitToDisk();
return;
}
Sampleton.Log($"{nameof(OnSaveLocalButtonPressed)}: {Uuid}");
// API call: instance OVRSpatialAnchor.SaveAnchorAsync()
var saveResult = await SpatialAnchor.SaveAnchorAsync();
// note: saving is not "KEY" since it is no longer required for apps to save their anchors before sharing;
// saving is now done automatically when you call any sharing API.
string loggedResult = saveResult.Status.ForLogging();
if (!saveResult.Success)
{
Sampleton.LogError($"- Saving Spatial Anchor FAILED: {loggedResult}");
return;
}
Sampleton.Log($"+ Saved Spatial Anchor: {loggedResult}");
IsSaved = true;
LocallySaved.CommitToDisk();
}
public void OnHideButtonPressed()
{
var uuid = Uuid;
Sampleton.Log($"{nameof(OnHideButtonPressed)}: {uuid}");
Destroy(gameObject);
// NOTE: "Destroy" == "Hide" for anchors only if they have been saved locally or shared.
// Otherwise, this is a proper deletion as far as your app is concerned; even if you saved its Uuid prior to
// destroying the anchor, there is no guarantee you'll be able to load it back (although it isn't guaranteed
// that you *can't* load it back, either.. better safe than ???!)
}
public async void OnEraseButtonPressed()
{
if (!SpatialAnchor)
{
Sampleton.Log($"{nameof(OVRSpatialAnchor.EraseAnchorAsync)}: NO-OP (anchor already destroyed)");
return;
}
var uuid = Uuid;
Sampleton.Log($"{nameof(OVRSpatialAnchor.EraseAnchorAsync)}: {uuid.Brief()}");
var anchor = SpatialAnchor;
m_SpatialAnchor = null;
// API call: instance OVRSpatialAnchor.EraseAnchorAsync()
var eraseResult = await anchor.EraseAnchorAsync();
string loggedResult = eraseResult.Status.ForLogging();
if (!eraseResult.Success)
{
Sampleton.LogError($"- Erasing Spatial Anchor FAILED: {loggedResult}");
m_SpatialAnchor = anchor;
return;
}
Sampleton.Log($"+ Erased Spatial Anchor: {loggedResult}");
LocallySaved.IgnoreAnchor(uuid);
LocallySaved.CommitToDisk();
Destroy(gameObject);
}
public void OnShareButtonPressed()
{
ShareWithRoom();
}
public void OnAlignButtonPressed()
{
if (!ShareSucceeded)
{
Sampleton.Log(
"OnAlignButtonPressed: You must successfully share an anchor with the room before you can designate" +
" it as the room's alignment anchor.",
LogType.Error
);
return;
}
Sampleton.Log("OnAlignButtonPressed");
var offset = new Pose(transform.position, transform.rotation);
Alignment.SetMRUKOrigin(this, offset);
PhotonAnchorManager.PublishAlignmentAnchor(Uuid, offset);
}
public void ShareWithRoom()
{
Sampleton.Log($"{nameof(ShareWithRoom)}:");
// userIds is an array of scoped Oculus User IDs of peers in this Photon Room:
if (!IsReadyToShare(out var userIds, printReason: true))
return;
// (note: Photon won't have any valid IDs if your app hasn't gone through the DUC & Horizon store entitlement steps yet!)
ShareToUsers(userIds);
}
public void UpdateUI()
{
gameObject.name = $"{Source}:{Uuid:N}";
if (m_AnchorName)
{
m_AnchorName.text = $"{Uuid}\n({nameof(Source)}: {Source})";
}
if (m_ShareIcon)
{
m_ShareIcon.color = ShareSucceeded ? SampleColors.Green
: m_ShareAttempted ? SampleColors.Red
: SampleColors.Gray;
}
if (m_SaveIcon)
{
m_SaveIcon.color = IsSaved ? SampleColors.Green
: SampleColors.Gray;
}
if (m_AlignIcon)
{
m_AlignIcon.color = Alignment.MRUKWorldLockAnchor == this ? SampleColors.Green
: SampleColors.Gray;
}
}
//
// Fields & Constants
[SerializeField]
TextMeshProUGUI m_AnchorName;
[SerializeField]
Image m_SaveIcon;
[SerializeField]
GameObject m_ShareButton;
[SerializeField]
Image m_ShareIcon;
[SerializeField]
Image m_AlignIcon;
[SerializeField, HideInInspector]
OVRSpatialAnchor m_SpatialAnchor;
AnchorSource m_Source;
//
// MonoBehaviour Messages
void OnValidate()
{
if (!TryGetComponent(out m_SpatialAnchor))
{
Debug.LogError($"<{nameof(SharedAnchor)}> on '{name}': please fix my refs in the inspector!", this);
}
}
async void Awake()
{
var canvas = GetComponentInChildren<Canvas>();
if (canvas)
canvas.enabled = false; // don't render controls until creation & localization is complete
// handy API: instance OVRSpatialAnchor.WhenCreatedAsync()
if (await SpatialAnchor.WhenCreatedAsync())
{
var pose = new Pose(transform.position, transform.rotation);
Sampleton.Log($"{nameof(SharedAnchor)}: Created! {pose.Brief()}");
}
else
{
Sampleton.LogError($"{nameof(SharedAnchor)}: FAILED TO CREATE!\n- destroying instance..");
Destroy(gameObject);
return;
}
var uuid = Uuid;
Sampleton.Log($"+ {uuid}");
if (!Source.IsSet)
Source = AnchorSource.New(uuid);
// handy API: instance OVRSpatialAnchor.WhenLocalizedAsync()
if (await SpatialAnchor.WhenLocalizedAsync())
{
var pose = new Pose(transform.position, transform.rotation);
// note: this pose can differ from the one reported earlier when this is not a brand-new anchor.
Sampleton.Log($"+ {uuid.Brief()} Localized! {pose.Brief()}");
}
else
{
Sampleton.LogError($"- {uuid.Brief()} Localization FAILED!");
Destroy(gameObject);
return;
}
s_Instances[uuid] = this;
if (PhotonAnchorManager.CheckIsAlignmentAnchor(this, out var offsetOnHost))
{
Sampleton.Log($"+ {uuid.Brief()} IsAlignmentAnchor");
Alignment.SetMRUKOrigin(this, offsetOnHost);
}
if (canvas)
{
UpdateUI();
canvas.enabled = true;
}
}
void OnDestroy()
{
var uuid = Uuid;
_ = s_Instances.Remove(uuid);
if (!IsSaved && uuid != Guid.Empty)
{
Sampleton.Log(
$"WARN: anchor {uuid} was destroyed but never saved!\n (It *may* be gone forever.)",
LogType.Warning
);
}
if (Alignment.MRUKWorldLockAnchor == this) // && !Sampleton.IsChangingScenes
{
Sampleton.Log(
$"WARN: destroyed anchor {uuid.Brief()} was previously designated as an alignment anchor.",
LogType.Warning
);
Alignment.ResetMRUKOrigin();
}
}
//
// impl. methods
bool IsReadyToShare(out IReadOnlyCollection<ulong> userIds, bool printReason = true)
{
userIds = null;
if (!SpatialAnchor)
{
if (printReason)
Sampleton.LogError("- Can't share - no associated spatial anchor");
return false;
}
if (!PhotonNetwork.IsConnected)
{
if (printReason)
Sampleton.LogError("- Can't share - not connected to the Photon network");
return false;
}
userIds = PhotonAnchorManager.RoomUserIds;
if (userIds is null || userIds.Count == 0)
{
if (printReason)
Sampleton.LogError("- Can't share - no users to share with or can't get user list from Photon");
return false;
}
return true;
}
async void ShareToUsers(IReadOnlyCollection<ulong> userIds)
{
Assert.IsNotNull(SpatialAnchor, "SpatialAnchor != null");
// essentially need to ask the OVR backend if each of these IDs is valid for us to use for sharing:
var spaceUsers = new List<OVRSpaceUser>(userIds.Count);
foreach (ulong id in userIds)
{
// (maybe a) KEY API CALL: static OVRSpaceUser.TryCreate(platformUserId, out spaceUser)
if (OVRSpaceUser.TryCreate(id, out var user))
{
Sampleton.Log($" + anchor {Uuid.Brief()} --> user {id}");
spaceUsers.Add(user);
// p.s. - it's a maybe because we are moving toward promoting group sharing over the "vanilla" user-based sharing.
// iff you are using the user-based sharing in your app, *then* this becomes a KEY API call.
}
else
{
Sampleton.LogError($" - FAILED to create OVRSpaceUser for {id}");
}
}
if (spaceUsers.Count == 0)
{
Sampleton.LogError($"{nameof(ShareToUsers)} FAILED: No valid users to share with!");
return;
}
// KEY API CALL: instance OVRSpatialAnchor.ShareAsync(users)
var shareResult = await SpatialAnchor.ShareAsync(spaceUsers);
string loggedResult = shareResult.ForLogging();
ShareSucceeded = shareResult.IsSuccess();
if (ShareSucceeded)
{
IsSaved = true; // shared anchors are implicitly cloud-saved
Sampleton.Log($" + Share Result: {loggedResult}");
// In this context of the sample, we need to transmit this now-shared anchor's id by some other means to our
// peers. The "means" that we presently choose to demo with is Photon Realtime + PUN.
PhotonAnchorManager.PublishAnchorToUsers(Uuid, userIds);
return;
}
Sampleton.LogError($" - Share FAILED: {loggedResult}");
}
}