-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVRDevices.cs
170 lines (143 loc) · 5.57 KB
/
VRDevices.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
using System;
using System.Text;
using System.Numerics;
using OVRSharp;
using OVRSharp.Math;
using Valve.VR;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;
using OVR_App_Template.Devices;
namespace OVR_App_Template
{
public class VRDevices
{
private List<VRTrackedDevice> _trackedDevices = new List<VRTrackedDevice>();
public VRTrackedDevice[] trackedDevices
{
get
{
return _trackedDevices.ToArray();
}
}
public VRHMD HMD
{
get
{
return (VRHMD)trackedDevices[OpenVR.k_unTrackedDeviceIndex_Hmd]; //HMD is always zero, and must be connected
}
private set
{
trackedDevices[OpenVR.k_unTrackedDeviceIndex_Hmd] = value;
}
}
public VRController RightController
{
get
{
return (VRController)GetController(VRTrackedDeviceClass.RightController);
}
}
public VRController LeftController
{
get
{
return (VRController)GetController(VRTrackedDeviceClass.LeftController);
}
}
private readonly string[] OldETrackedDeviceClasses = new string[OpenVR.k_unMaxTrackedDeviceCount];
private readonly string[] CurrentETrackedDeviceClasses = new string[OpenVR.k_unMaxTrackedDeviceCount];
public VRDevices()
{
Console.CursorVisible = false;
//update method handles device initialization
Update();
}
private void InitializeDevices(TrackedDevicePose_t[] trackedDevicePoses)
{
Debug.WriteLine("==========Initializing Devices==========");
//clear list
_trackedDevices.Clear();
//looping through all possible devices
for (uint i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
{
if (trackedDevicePoses[i].eTrackingResult == ETrackingResult.Uninitialized)
continue;
ETrackedDeviceClass tClass = OpenVR.System.GetTrackedDeviceClass(i);
//HMD
if (tClass == ETrackedDeviceClass.HMD)
{
_trackedDevices.Add(new VRHMD(i, VRTrackedDeviceClass.HMD));
continue;
}
if (tClass == ETrackedDeviceClass.Controller && OpenVR.System.GetControllerRoleForTrackedDeviceIndex(i) == ETrackedControllerRole.LeftHand)
{
_trackedDevices.Add(new VRController(i, VRTrackedDeviceClass.LeftController));
continue;
}
if (tClass == ETrackedDeviceClass.Controller && OpenVR.System.GetControllerRoleForTrackedDeviceIndex(i) == ETrackedControllerRole.RightHand)
{
_trackedDevices.Add(new VRController(i, VRTrackedDeviceClass.RightController));
continue;
}
if (tClass == ETrackedDeviceClass.GenericTracker)
{
_trackedDevices.Add(new VRTracker(i, VRTrackedDeviceClass.Tracker));
continue;
}
}
for (uint i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
{
OldETrackedDeviceClasses[i] = CurrentETrackedDeviceClasses[i];
}
}
public void Update()
{
//query OpenVR for all device poses
TrackedDevicePose_t[] trackedDevicePoses = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
OpenVR.System.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, 0, trackedDevicePoses);
for (uint i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
{
//use this as a kind-of "UUID" for each device.
CurrentETrackedDeviceClasses[i] = (Enum.GetName(OpenVR.System.GetTrackedDeviceClass(i)) + " " + trackedDevicePoses[i].eTrackingResult.ToString()).ToString();
}
for (uint i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
{
//if the status or class changes for any given device ID, this will trigger an update.
if (OldETrackedDeviceClasses[i] != CurrentETrackedDeviceClasses[i])
{
InitializeDevices(trackedDevicePoses);
break;
}
}
for (int i = 0; i < trackedDevices.Length; i++)
{
trackedDevices[i].Update();
}
Console.SetCursorPosition(0, 0);
}
private VRTrackedDevice GetController(VRTrackedDeviceClass controllerClass)
{
VRTrackedDevice searchResult = null!; //postfix ! is a null-forgiving operator
if (controllerClass != VRTrackedDeviceClass.LeftController || controllerClass != VRTrackedDeviceClass.RightController)
{
return searchResult;
}
_trackedDevices.ForEach((device) =>
{
if (device.Type == controllerClass)
{
searchResult = device;
return;
}
});
return searchResult;
}
private void DebugWriteTheThing()
{
for(int i = 0; i < 10; i++)
{
Console.WriteLine(CurrentETrackedDeviceClasses[i] + " ||| " + OldETrackedDeviceClasses[i]);
}
}
}
}