-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathInsideSceneChecker.cs
90 lines (71 loc) · 1.99 KB
/
InsideSceneChecker.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
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System;
using System.Collections;
using Phantom.Environment.Scripts;
using UnityEngine;
using UnityEngine.Assertions;
public class InsideSceneChecker : MonoBehaviour
{
private const float MaxCeilingRayLength = 100.0f;
public static event Action<bool> UserInSceneChanged;
public static bool UserInScene { get; private set; }
[SerializeField] private SceneBoundsChecker sceneBoundsChecker;
[SerializeField] private OVRCameraRig cameraRig;
private bool _boundsSet;
private void Awake()
{
Assert.IsNotNull(sceneBoundsChecker);
Assert.IsNotNull(cameraRig);
}
private void OnEnable()
{
SceneBoundsChecker.WorldAligned += OnWorldAligned;
StartCoroutine(UserBoundsTest());
}
private void OnDisable()
{
SceneBoundsChecker.WorldAligned -= OnWorldAligned;
_boundsSet = false;
}
private void OnWorldAligned()
{
_boundsSet = true;
}
private IEnumerator UserBoundsTest()
{
// check 5 times a second.
var wait = new WaitForSeconds(0.2f);
while (!_boundsSet)
{
yield return null;
}
var head = cameraRig.centerEyeAnchor;
while (enabled)
{
var inBounds = PointInsideScene(head.position);
if (UserInScene != inBounds)
{
UserInScene = inBounds;
UserInSceneChanged?.Invoke(inBounds);
}
yield return wait;
}
}
public static bool PointInsideScene(Vector3 position)
{
return SceneQuery.TryGetRoomContainingPoint(position, out _);
}
#if UNITY_EDITOR
private void OnValidate()
{
if (sceneBoundsChecker == null)
{
sceneBoundsChecker = GetComponent<SceneBoundsChecker>();
}
if (cameraRig == null)
{
cameraRig = GetComponentInChildren<OVRCameraRig>(true);
}
}
#endif
}