OVRHandTest.cs
6.13 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
/************************************************************************************
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 UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Text;
public class OVRHandTest : MonoBehaviour
{
public class BoolMonitor
{
public delegate bool BoolGenerator();
private string m_name = "";
private BoolGenerator m_generator;
private bool m_prevValue = false;
private bool m_currentValue = false;
private bool m_currentValueRecentlyChanged = false;
private float m_displayTimeout = 0.0f;
private float m_displayTimer = 0.0f;
public BoolMonitor(string name, BoolGenerator generator, float displayTimeout = 0.5f)
{
m_name = name;
m_generator = generator;
m_displayTimeout = displayTimeout;
}
public void Update()
{
m_prevValue = m_currentValue;
m_currentValue = m_generator();
if (m_currentValue != m_prevValue)
{
m_currentValueRecentlyChanged = true;
m_displayTimer = m_displayTimeout;
}
if (m_displayTimer > 0.0f)
{
m_displayTimer -= Time.deltaTime;
if (m_displayTimer <= 0.0f)
{
m_currentValueRecentlyChanged = false;
m_displayTimer = 0.0f;
}
}
}
public void AppendToStringBuilder(ref StringBuilder sb)
{
sb.Append(m_name);
if (m_currentValue && m_currentValueRecentlyChanged)
sb.Append(": *True*\n");
else if (m_currentValue)
sb.Append(": True \n");
else if (!m_currentValue && m_currentValueRecentlyChanged)
sb.Append(": *False*\n");
else if (!m_currentValue)
sb.Append(": False \n");
}
}
public Text uiText;
private List<BoolMonitor> monitors;
private StringBuilder data;
private OVRPlugin.HandState hs_LH = new OVRPlugin.HandState();
private OVRPlugin.HandState hs_RH = new OVRPlugin.HandState();
private OVRPlugin.Skeleton skel_LH = new OVRPlugin.Skeleton();
private OVRPlugin.Skeleton skel_RH = new OVRPlugin.Skeleton();
private OVRPlugin.Mesh mesh_LH = new OVRPlugin.Mesh();
private OVRPlugin.Mesh mesh_RH = new OVRPlugin.Mesh();
private bool result_skel_LH = false;
private bool result_skel_RH = false;
private bool result_mesh_LH = false;
private bool result_mesh_RH = false;
void Start()
{
if (uiText != null)
{
uiText.supportRichText = false;
}
data = new StringBuilder(2048);
monitors = new List<BoolMonitor>()
{
new BoolMonitor("One", () => OVRInput.Get(OVRInput.Button.One)),
};
result_skel_LH = OVRPlugin.GetSkeleton(OVRPlugin.SkeletonType.HandLeft, out skel_LH);
result_skel_RH = OVRPlugin.GetSkeleton(OVRPlugin.SkeletonType.HandRight, out skel_RH);
result_mesh_LH = OVRPlugin.GetMesh(OVRPlugin.MeshType.HandLeft, out mesh_LH);
result_mesh_RH = OVRPlugin.GetMesh(OVRPlugin.MeshType.HandRight, out mesh_RH);
}
static string prevConnected = "";
static BoolMonitor controllers = new BoolMonitor("Controllers Changed", () => { return OVRInput.GetConnectedControllers().ToString() != prevConnected; });
void Update()
{
data.Length = 0;
OVRInput.Controller activeController = OVRInput.GetActiveController();
string activeControllerName = activeController.ToString();
data.AppendFormat("Active: {0}\n", activeControllerName);
string connectedControllerNames = OVRInput.GetConnectedControllers().ToString();
data.AppendFormat("Connected: {0}\n", connectedControllerNames);
data.AppendFormat("PrevConnected: {0}\n", prevConnected);
controllers.Update();
controllers.AppendToStringBuilder(ref data);
prevConnected = connectedControllerNames;
Vector3 pos = OVRInput.GetLocalControllerPosition(activeController);
data.AppendFormat("Position: ({0:F2}, {1:F2}, {2:F2})\n", pos.x, pos.y, pos.z);
Quaternion rot = OVRInput.GetLocalControllerRotation(activeController);
data.AppendFormat("Orientation: ({0:F2}, {1:F2}, {2:F2}, {3:F2})\n", rot.x, rot.y, rot.z, rot.w);
data.AppendFormat("HandTrackingEnabled: {0}\n", OVRPlugin.GetHandTrackingEnabled());
bool result_hs_LH = OVRPlugin.GetHandState(OVRPlugin.Step.Render, OVRPlugin.Hand.HandLeft, ref hs_LH);
data.AppendFormat("LH HS Query Res: {0}\n", result_hs_LH);
data.AppendFormat("LH HS Status: {0}\n", hs_LH.Status);
data.AppendFormat("LH HS Pose: {0}\n", hs_LH.RootPose);
data.AppendFormat("LH HS HandConf: {0}\n", hs_LH.HandConfidence);
bool result_hs_RH = OVRPlugin.GetHandState(OVRPlugin.Step.Render, OVRPlugin.Hand.HandRight, ref hs_RH);
data.AppendFormat("RH HS Query Res: {0}\n", result_hs_RH);
data.AppendFormat("RH HS Status: {0}\n", hs_RH.Status);
data.AppendFormat("RH HS Pose: {0}\n", hs_RH.RootPose);
data.AppendFormat("RH HS HandConf: {0}\n", hs_RH.HandConfidence);
data.AppendFormat("LH Skel Query Res: {0}\n", result_skel_LH);
data.AppendFormat("LH Skel Type: {0}\n", skel_LH.Type);
data.AppendFormat("LH Skel NumBones: {0}\n", skel_LH.NumBones);
data.AppendFormat("RH Skel Query Res: {0}\n", result_skel_RH);
data.AppendFormat("RH Skel Type: {0}\n", skel_RH.Type);
data.AppendFormat("RH Skel NumBones: {0}\n", skel_RH.NumBones);
data.AppendFormat("LH Mesh Query Res: {0}\n", result_mesh_LH);
data.AppendFormat("LH Mesh Type: {0}\n", mesh_LH.Type);
data.AppendFormat("LH Mesh NumVers: {0}\n", mesh_LH.NumVertices);
data.AppendFormat("RH Mesh Query Res: {0}\n", result_mesh_RH);
data.AppendFormat("RH Mesh Type: {0}\n", mesh_RH.Type);
data.AppendFormat("RH Mesh NumVers: {0}\n", mesh_RH.NumVertices);
for (int i = 0; i < monitors.Count; i++)
{
monitors[i].Update();
monitors[i].AppendToStringBuilder(ref data);
}
if (uiText != null)
{
uiText.text = data.ToString();
}
}
}