-
Notifications
You must be signed in to change notification settings - Fork 56
/
SceneBoundsChecker.cs
298 lines (242 loc) · 9.25 KB
/
SceneBoundsChecker.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
289
290
291
292
293
294
295
296
297
298
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using Debug = UnityEngine.Debug;
namespace Phantom.Environment.Scripts
{
/// <summary>
/// This script checks the bounds of the scene against the floor
/// </summary>
public class SceneBoundsChecker : MonoBehaviour
{
private static Bounds? _currentBounds;
private static OVRSceneRoom _currentRoom;
[SerializeField] private OVRCameraRig cameraRig;
[SerializeField]
[Tooltip("Amount the floor must shift to trigger realignment.")]
private float maxShift = 0.05f;
[SerializeField]
[Tooltip("Amount the floor must rotate to trigger realignment.")]
private float maxRotation = 5f;
[SerializeField]
[Tooltip("Rotate the tracking space to make sure the room's floor is 0,0,0 and axis aligned.")]
private bool axisAlignFloor = true;
private Coroutine _boundsPollingCoroutine;
private OVRScenePlane _floorPlane;
private Transform _floorTransform;
private Transform _trackingSpaceTransform;
private Transform _headTransform;
private static event Action<OVRSceneRoom, Bounds> _boundsChanged;
/// <summary>
/// The user has moved from one room to another.
/// </summary>
public static event Action<OVRSceneRoom, Bounds> BoundsChanged
{
add
{
if (_currentBounds.HasValue) value?.Invoke(_currentRoom, _currentBounds.Value);
_boundsChanged += value;
}
remove => _boundsChanged -= value;
}
private static event Action _worldAligned;
/// <summary>
/// If "axis align floor" is true this will be invoked when we've moved
/// the user's first room to the origin.
/// </summary>
public static event Action WorldAligned
{
add
{
if (_currentBounds.HasValue) value?.Invoke();
_worldAligned += value;
}
remove => _worldAligned -= value;
}
private bool IsFloorAligned
{
get
{
if (!_floorPlane.enabled) return true;
var angle = Vector3.Angle(Vector3.forward, _floorTransform.up);
if (angle > maxRotation) return false;
var floorPos = Vector3.ProjectOnPlane(_floorTransform.position, Vector3.up);
var sqrMagnitude = floorPos.sqrMagnitude;
return sqrMagnitude < maxShift * maxShift;
}
}
private void Awake()
{
_headTransform = cameraRig.centerEyeAnchor;
SceneManager.activeSceneChanged += OnActiveSceneChanged;
}
private void OnEnable()
{
OVRManager.VrFocusAcquired += StartBoundsPolling;
OVRManager.VrFocusLost += CancelBoundsPolling;
StartBoundsPolling();
}
private void OnDisable()
{
OVRManager.VrFocusAcquired -= StartBoundsPolling;
OVRManager.VrFocusLost -= CancelBoundsPolling;
CancelBoundsPolling();
}
private void OnDestroy()
{
_currentBounds = null;
SceneManager.activeSceneChanged -= OnActiveSceneChanged;
}
// When user suspends the app to rescan we get an OnApplicationPause event
private void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
{
CancelBoundsPolling();
}
else
{
StartBoundsPolling();
}
}
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
if (!_currentBounds.HasValue) return;
var bounds = _currentBounds.Value;
Gizmos.color = Color.green;
Gizmos.DrawWireCube(bounds.center, bounds.size);
if (_trackingSpaceTransform != null)
{
var ray = new Ray(_trackingSpaceTransform.position, _trackingSpaceTransform.forward);
Gizmos.color = Color.yellow;
Gizmos.DrawRay(ray);
}
}
#endif
private void OnActiveSceneChanged(Scene a, Scene b)
{
// assume room has been destroyed and find the new one.
StartBoundsPolling();
}
private IEnumerator BoundsChangePolling()
{
_currentRoom = null;
var wait = new WaitForSeconds(0.25f);
OVRSceneRoom room = null;
// wait until room is loaded.
do
{
yield return null;
} while (!SceneQuery.TryGetRoomContainingPoint(cameraRig.centerEyeAnchor.position, out room));
var timeoutStopwatch = Stopwatch.StartNew();
while (room.Floor == null || room.Floor.Boundary.Count == 0 || room.Walls.Length == 0)
{
if (timeoutStopwatch.ElapsedMilliseconds > 3000)
{
Debug.LogWarning("Timed out waiting for walls in scene. Walls missing?");
break;
}
yield return null;
}
_trackingSpaceTransform = cameraRig.trackingSpace;
// OVRSceneRoom can get destroyed while we're waiting.
if (room == null || room.Floor == null)
{
CancelBoundsPolling();
StartBoundsPolling();
yield break;
}
_floorPlane = room.Floor;
_floorTransform = _floorPlane.transform;
if (!axisAlignFloor || IsFloorAligned)
{
_currentBounds = default;
_worldAligned?.Invoke();
}
while (enabled)
{
if (axisAlignFloor && !IsFloorAligned) AxisAlignFloor(_floorTransform, _trackingSpaceTransform);
// find the room the user is currently in.
var headRoom = SceneQuery.GetRoomContainingPoint(_headTransform.position);
// if it is not the same as the current room
if (headRoom != _currentRoom)
{
// calculate bounds and broadcast event.
_currentRoom = headRoom;
_currentBounds = SceneQuery.GetRoomBounds(headRoom);
_boundsChanged?.Invoke(_currentRoom, _currentBounds.Value);
}
yield return wait;
}
}
public void RecalculateBounds()
{
// get the room the user's camera is in.
var room = SceneQuery.GetRoomContainingPoint(cameraRig.centerEyeAnchor.position);
var bounds = SceneQuery.GetRoomBounds(room);
_boundsChanged?.Invoke(room, bounds);
_currentBounds = bounds;
}
private void StartBoundsPolling()
{
if (_boundsPollingCoroutine != null) StopCoroutine(_boundsPollingCoroutine);
_currentRoom = null;
_boundsPollingCoroutine = StartCoroutine(BoundsChangePolling());
}
private void CancelBoundsPolling()
{
if (_boundsPollingCoroutine != null)
{
StopCoroutine(_boundsPollingCoroutine);
_boundsPollingCoroutine = null;
}
}
/// <summary>
/// Move the tracking space so the floor is at 0,~0,0 and axis aligned.
/// Should result in tighter room bounds.
/// </summary>
/// <param name="floorTransform"></param>
/// <param name="trackingSpace"></param>
private static void AxisAlignFloor(Transform floorTransform, Transform trackingSpace)
{
// floor's up axis is actually pointing Unity forward.
var forward = Vector3.ProjectOnPlane(floorTransform.up, Vector3.up).normalized;
var angle = Vector3.SignedAngle(forward, Vector3.forward, Vector3.up);
var floorPos = Vector3.ProjectOnPlane(floorTransform.position, Vector3.up);
// shift the tracking space so the floorPos gets moved to 0,0,0.
trackingSpace.position -= floorPos;
// rotate the tracking space around the origin so forward and Vector3.forward are parallel.
trackingSpace.RotateAround(Vector3.zero, Vector3.up, angle);
_worldAligned?.Invoke();
}
public static bool PointInBounds(Vector3 point)
{
return _currentBounds.HasValue ? _currentBounds.Value.Contains(point) : false;
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(SceneBoundsChecker))]
public class SceneBoundsCheckerEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if (EditorApplication.isPlaying)
{
GUILayout.Space(16);
if (GUILayout.Button("Recalculate Bounds"))
{
var loader = target as SceneBoundsChecker;
loader.RecalculateBounds();
}
}
}
}
#endif
}