-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathXRInputManager.cs
More file actions
132 lines (101 loc) · 4.08 KB
/
XRInputManager.cs
File metadata and controls
132 lines (101 loc) · 4.08 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
// Copyright (c) Meta Platforms, Inc. and affiliates.
#if HAS_META_AVATARS
using Oculus.Avatar2;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Meta.Utilities.Input
{
public class XRInputManager : OvrAvatarInputManager
{
private const string LOG_SCOPE = "xrInput";
[SerializeField]
[Tooltip("Optional. If added, it will use input directly from OVRCameraRig instead of doing its own calculations.")]
private OVRCameraRig m_ovrCameraRig;
[SerializeField] private XRInputControlActions m_controlActions;
// Only used in editor, produces warnings when packaging
#pragma warning disable CS0414 // is assigned but its value is never used
[SerializeField]
private bool m_debugDrawTrackingLocations;
#pragma warning restore CS0414 // is assigned but its value is never used
protected void Awake()
{
// Debug Drawing
#if UNITY_EDITOR
#if UNITY_2019_3_OR_NEWER
SceneView.duringSceneGui += OnSceneGUI;
#else
SceneView.onSceneGUIDelegate += OnSceneGUI;
#endif
#endif
m_controlActions.EnableActions();
}
private void Start()
{
#if HAS_META_AVATARS
if (BodyTrackingContext is not null and OvrAvatarBodyTrackingContext ovrBodyTracking)
{
ovrBodyTracking.InputTrackingDelegate = new XRInputTrackingDelegate(m_ovrCameraRig, true);
ovrBodyTracking.InputControlDelegate = new XRInputControlDelegate(m_controlActions);
}
#endif
}
protected override void OnDestroyCalled()
{
#if UNITY_EDITOR
#if UNITY_2019_3_OR_NEWER
SceneView.duringSceneGui -= OnSceneGUI;
#else
SceneView.onSceneGUIDelegate -= OnSceneGUI;
#endif
#endif
base.OnDestroyCalled();
}
public XRInputControlActions.Controller GetActions(bool forLeftHand)
{
return forLeftHand ? m_controlActions.LeftController : m_controlActions.RightController;
}
public Transform GetAnchor(bool forLeftHand)
{
return forLeftHand ? m_ovrCameraRig.leftHandAnchor : m_ovrCameraRig.rightHandAnchor;
}
#if UNITY_EDITOR
#region Debug Drawing
private void OnSceneGUI(SceneView sceneView)
{
if (m_debugDrawTrackingLocations) DrawTrackingLocations();
}
private void DrawTrackingLocations()
{
if (BodyTrackingContext is not OvrAvatarBodyTrackingContext ovrBodyTracking)
{
return;
}
var inputTrackingState = ovrBodyTracking.InputTrackingState;
var radius = 0.2f;
Quaternion orientation;
float OuterRadius() => radius + 0.25f;
Vector3 Forward() => orientation * Vector3.forward;
Handles.color = Color.blue;
_ = Handles.RadiusHandle(Quaternion.identity, inputTrackingState.headset.position, radius);
orientation = inputTrackingState.headset.orientation;
Handles.DrawLine(inputTrackingState.headset.position + Forward() * radius,
inputTrackingState.headset.position + Forward() * OuterRadius());
radius = 0.1f;
Handles.color = Color.yellow;
_ = Handles.RadiusHandle(Quaternion.identity, inputTrackingState.leftController.position, radius);
orientation = inputTrackingState.leftController.orientation;
Handles.DrawLine(inputTrackingState.leftController.position + Forward() * radius,
inputTrackingState.leftController.position + Forward() * OuterRadius());
Handles.color = Color.yellow;
_ = Handles.RadiusHandle(Quaternion.identity, inputTrackingState.rightController.position, radius);
orientation = inputTrackingState.rightController.orientation;
Handles.DrawLine(inputTrackingState.rightController.position + Forward() * radius,
inputTrackingState.rightController.position + Forward() * OuterRadius());
}
#endregion
#endif // UNITY_EDITOR
}
}
#endif