OVRHand.cs
6.42 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
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
/************************************************************************************
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
https://developer.oculus.com/licenses/oculussdk/
Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
ANY KIND, either express or implied. See the License for the specific language governing
permissions and limitations under the License.
************************************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DefaultExecutionOrder(-90)]
public class OVRHand : MonoBehaviour,
OVRSkeleton.IOVRSkeletonDataProvider,
OVRSkeletonRenderer.IOVRSkeletonRendererDataProvider,
OVRMesh.IOVRMeshDataProvider,
OVRMeshRenderer.IOVRMeshRendererDataProvider
{
public enum Hand
{
None = OVRPlugin.Hand.None,
HandLeft = OVRPlugin.Hand.HandLeft,
HandRight = OVRPlugin.Hand.HandRight,
}
public enum HandFinger
{
Thumb = OVRPlugin.HandFinger.Thumb,
Index = OVRPlugin.HandFinger.Index,
Middle = OVRPlugin.HandFinger.Middle,
Ring = OVRPlugin.HandFinger.Ring,
Pinky = OVRPlugin.HandFinger.Pinky,
Max = OVRPlugin.HandFinger.Max,
}
public enum TrackingConfidence
{
Low = OVRPlugin.TrackingConfidence.Low,
High = OVRPlugin.TrackingConfidence.High
}
[SerializeField]
private Hand HandType = Hand.None;
[SerializeField]
private Transform _pointerPoseRoot = null;
private GameObject _pointerPoseGO;
private OVRPlugin.HandState _handState = new OVRPlugin.HandState();
public bool IsDataValid { get; private set; }
public bool IsDataHighConfidence { get; private set; }
public bool IsTracked { get; private set; }
public bool IsSystemGestureInProgress { get; private set; }
public bool IsPointerPoseValid { get; private set; }
public Transform PointerPose { get; private set; }
public float HandScale { get; private set; }
public TrackingConfidence HandConfidence { get; private set; }
public bool IsDominantHand { get; private set; }
private void Awake()
{
_pointerPoseGO = new GameObject();
PointerPose = _pointerPoseGO.transform;
if (_pointerPoseRoot != null)
{
PointerPose.SetParent(_pointerPoseRoot, false);
}
GetHandState(OVRPlugin.Step.Render);
}
private void Update()
{
GetHandState(OVRPlugin.Step.Render);
}
private void FixedUpdate()
{
if (OVRPlugin.nativeXrApi != OVRPlugin.XrApi.OpenXR)
{
GetHandState(OVRPlugin.Step.Physics);
}
}
private void GetHandState(OVRPlugin.Step step)
{
if (OVRPlugin.GetHandState(step, (OVRPlugin.Hand)HandType, ref _handState))
{
IsTracked = (_handState.Status & OVRPlugin.HandStatus.HandTracked) != 0;
IsSystemGestureInProgress = (_handState.Status & OVRPlugin.HandStatus.SystemGestureInProgress) != 0;
IsPointerPoseValid = (_handState.Status & OVRPlugin.HandStatus.InputStateValid) != 0;
IsDominantHand = (_handState.Status & OVRPlugin.HandStatus.DominantHand) != 0;
PointerPose.localPosition = _handState.PointerPose.Position.FromFlippedZVector3f();
PointerPose.localRotation = _handState.PointerPose.Orientation.FromFlippedZQuatf();
HandScale = _handState.HandScale;
HandConfidence = (TrackingConfidence)_handState.HandConfidence;
IsDataValid = true;
IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
}
else
{
IsTracked = false;
IsSystemGestureInProgress = false;
IsPointerPoseValid = false;
PointerPose.localPosition = Vector3.zero;
PointerPose.localRotation = Quaternion.identity;
HandScale = 1.0f;
HandConfidence = TrackingConfidence.Low;
IsDataValid = false;
IsDataHighConfidence = false;
}
}
public bool GetFingerIsPinching(HandFinger finger)
{
return IsDataValid && (((int)_handState.Pinches & (1 << (int)finger)) != 0);
}
public float GetFingerPinchStrength(HandFinger finger)
{
if (IsDataValid
&& _handState.PinchStrength != null
&& _handState.PinchStrength.Length == (int)OVRPlugin.HandFinger.Max)
{
return _handState.PinchStrength[(int)finger];
}
return 0.0f;
}
public TrackingConfidence GetFingerConfidence(HandFinger finger)
{
if (IsDataValid
&& _handState.FingerConfidences != null
&& _handState.FingerConfidences.Length == (int)OVRPlugin.HandFinger.Max)
{
return (TrackingConfidence)_handState.FingerConfidences[(int)finger];
}
return TrackingConfidence.Low;
}
OVRSkeleton.SkeletonType OVRSkeleton.IOVRSkeletonDataProvider.GetSkeletonType()
{
switch (HandType)
{
case Hand.HandLeft:
return OVRSkeleton.SkeletonType.HandLeft;
case Hand.HandRight:
return OVRSkeleton.SkeletonType.HandRight;
case Hand.None:
default:
return OVRSkeleton.SkeletonType.None;
}
}
OVRSkeleton.SkeletonPoseData OVRSkeleton.IOVRSkeletonDataProvider.GetSkeletonPoseData()
{
var data = new OVRSkeleton.SkeletonPoseData();
data.IsDataValid = IsDataValid;
if (IsDataValid)
{
data.RootPose = _handState.RootPose;
data.RootScale = _handState.HandScale;
data.BoneRotations = _handState.BoneRotations;
data.IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
}
return data;
}
OVRSkeletonRenderer.SkeletonRendererData OVRSkeletonRenderer.IOVRSkeletonRendererDataProvider.GetSkeletonRendererData()
{
var data = new OVRSkeletonRenderer.SkeletonRendererData();
data.IsDataValid = IsDataValid;
if (IsDataValid)
{
data.RootScale = _handState.HandScale;
data.IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
data.ShouldUseSystemGestureMaterial = IsSystemGestureInProgress;
}
return data;
}
OVRMesh.MeshType OVRMesh.IOVRMeshDataProvider.GetMeshType()
{
switch (HandType)
{
case Hand.None:
return OVRMesh.MeshType.None;
case Hand.HandLeft:
return OVRMesh.MeshType.HandLeft;
case Hand.HandRight:
return OVRMesh.MeshType.HandRight;
default:
return OVRMesh.MeshType.None;
}
}
OVRMeshRenderer.MeshRendererData OVRMeshRenderer.IOVRMeshRendererDataProvider.GetMeshRendererData()
{
var data = new OVRMeshRenderer.MeshRendererData();
data.IsDataValid = IsDataValid;
if (IsDataValid)
{
data.IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
data.ShouldUseSystemGestureMaterial = IsSystemGestureInProgress;
}
return data;
}
}