-
Notifications
You must be signed in to change notification settings - Fork 59
/
ColocationDriverNetObj.cs
131 lines (109 loc) · 4.57 KB
/
ColocationDriverNetObj.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
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System;
using System.Collections.Generic;
using com.meta.xr.colocation;
using Cysharp.Threading.Tasks;
using Fusion;
using Fusion.Sockets;
using Oculus.Platform.Models;
using UnityEngine;
namespace Discover.Colocation
{
public class ColocationDriverNetObj : NetworkBehaviour
{
public static ColocationDriverNetObj Instance { get; private set; }
public static Action<bool> OnColocationCompletedCallback;
public static bool SkipColocation;
[SerializeField] private PhotonNetworkData m_networkData;
[SerializeField] private PhotonNetworkMessenger m_networkMessenger;
[SerializeField] private GameObject m_anchorPrefab;
private SharedAnchorManager m_sharedAnchorManager;
private AutomaticColocationLauncher m_colocationLauncher;
private User m_oculusUser;
private ulong m_playerDeviceUid;
private Transform m_ovrCameraRigTransform;
private void Awake()
{
Debug.Assert(Instance == null, $"{nameof(ColocationDriverNetObj)} instance already exists");
Instance = this;
}
private void OnDestroy()
{
if (Instance == this)
{
Instance = null;
}
}
public override void Spawned()
{
Init();
}
private async void Init()
{
m_ovrCameraRigTransform = FindObjectOfType<OVRCameraRig>().transform;
m_oculusUser = await OculusPlatformUtils.GetLoggedInUser();
m_playerDeviceUid = OculusPlatformUtils.GetUserDeviceGeneratedUid();
SetupForColocation();
}
private void SetupForColocation()
{
Debug.Log("SetUpAndStartColocation: Initialize messenger");
m_networkMessenger.RegisterLocalPlayer(m_playerDeviceUid);
// Instantiates the manager for the Oculus shared anchors, specifying the desired anchor prefab.
Debug.Log("SetupForColocation: Instantiating shared anchor manager");
m_sharedAnchorManager = new SharedAnchorManager { AnchorPrefab = m_anchorPrefab };
NetworkAdapter.SetConfig(m_networkData, m_networkMessenger);
Debug.Log("SetupForColocation: Initializing Colocation for the player");
// Starts the colocation alignment process
m_colocationLauncher = new AutomaticColocationLauncher();
m_colocationLauncher.Init(
NetworkAdapter.NetworkData,
NetworkAdapter.NetworkMessenger,
m_sharedAnchorManager,
m_ovrCameraRigTransform.gameObject,
m_playerDeviceUid,
m_oculusUser?.ID ?? default
);
// Hooks the event to react to the colocation ready state
m_colocationLauncher.ColocationReady += OnColocationReady;
m_colocationLauncher.ColocationFailed += OnColocationFailed;
if (HasStateAuthority)
{
m_colocationLauncher.CreateColocatedSpace();
}
else
{
// Don't try to colocate if we join remotely
if (SkipColocation)
{
OnColocationCompletedCallback?.Invoke(false);
}
else
{
m_colocationLauncher.ColocateAutomatically();
}
}
}
private void OnColocationReady()
{
Debug.Log("Colocation is Ready!");
// The AlignCameraToAnchor scripts updates on every frame which messes up Physics and create frame spikes.
// We need to disable it and add our own align manager that is applied only on recenter
var alignCamBehaviour = FindObjectOfType<AlignCameraToAnchor>();
if (alignCamBehaviour != null)
{
alignCamBehaviour.enabled = false;
var alignmentGameObject = alignCamBehaviour.gameObject;
var alignManager = alignmentGameObject.AddComponent<AlignCameraToAnchorManager>();
alignManager.CameraAlignmentBehaviour = alignCamBehaviour;
alignManager.RealignToAnchor();
}
OnColocationCompletedCallback?.Invoke(true);
}
private void OnColocationFailed(ColocationFailedReason reason)
{
Debug.Log($"Colocation failed! ({reason})");
OnColocationCompletedCallback?.Invoke(false);
}
}
}