text
stringlengths
0
1.11k
UniTaskCompletionSource<IReadOnlyList<OVRSpatialAnchor>> utcs = new();
Debug.Log($"{nameof(SharedAnchorManager)}: Querying anchors: {string.Join(", ", anchorIds)}");
OVRSpatialAnchor.LoadUnboundAnchors(
new OVRSpatialAnchor.LoadOptions {
StorageLocation = OVRSpace.StorageLocation.Cloud,
Timeout = 0,
Uuids = anchorIds
},
async unboundAnchors => {
if (unboundAnchors == null) {
Debug.LogError(
$"{nameof(SharedAnchorManager)}: Failed to query anchors - {nameof(OVRSpatialAnchor.LoadUnboundAnchors)} returned null."
);
utcs.TrySetResult(null);
return;
}
if (unboundAnchors.Length != anchorIds.Length) {
Debug.LogError(
$"{nameof(SharedAnchorManager)}: {anchorIds.Length - unboundAnchors.Length}/{anchorIds.Length} anchors failed to relocalize."
);
}
var createdAnchors = new List<OVRSpatialAnchor>();
var createTasks = new List<UniTask>();
// Bind anchors
foreach (var unboundAnchor in unboundAnchors) {
var anchor = InstantiateAnchor();
try {
unboundAnchor.BindTo(anchor);
_sharedAnchors.Add(anchor);
createdAnchors.Add(anchor);
createTasks.Add(UniTask.WaitWhile(() => anchor.PendingCreation, PlayerLoopTiming.PreUpdate));
} catch {
Object.Destroy(anchor);
throw;
}
}
// Wait for anchors to be created
await UniTask.WhenAll(createTasks);
utcs.TrySetResult(createdAnchors);
}
);
return await utcs.Task;
}
The call to OVRSpatialAnchor.LoadUnboundAnchors() loads any spatial anchors given by this list of IDs:
Uuids = anchorIds
The host player sends these IDs as a list and the clients can retrieve them.
AlignmentAnchorManager class deep dive
The Colocation package picks the first spatial anchor and uses it to do the alignment. This assumes that any given anchor is visible to both players (host and client).
The AlignmentAnchorManager class is defined in /Packages/com.meta.xr.sdk.colocation/Anchors/AlignmentAnchorManager.cs of the Colocation package. Once the Colocation package retrieves the shared spatial anchors to use and create alignment, it calls this alignment coroutine:
private IEnumerator AlignmentCoroutine(OVRSpatialAnchor anchor, int alignmentCount) {
Debug.Log("AlignmentAnchorManager: called AlignmentCoroutine");
while (alignmentCount > 0) {
if (_anchorToAlignTo != null) {
_cameraRigTransform.position = Vector3.zero;
_cameraRigTransform.eulerAngles = Vector3.zero;
yield return null;
}
var anchorTransform = anchor.transform;
if (_cameraRigTransform != null) {
Debug.Log("AlignmentAnchorManager: CameraRigTransform is valid");
_cameraRigTransform.position = anchorTransform.InverseTransformPoint(Vector3.zero);
_cameraRigTransform.eulerAngles = new Vector3(0, -anchorTransform.eulerAngles.y, 0);
}
if (_playerHandsTransform != null) {
_playerHandsTransform.localPosition = -_cameraRigTransform.position;
_playerHandsTransform.localEulerAngles = -_cameraRigTransform.eulerAngles;
}
_anchorToAlignTo = anchor;
alignmentCount--;