/
HTCViveTrackerHapticProfile.cs
400 lines (357 loc) · 19.8 KB
/
HTCViveTrackerHapticProfile.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
using System.Collections.Generic;
using UnityEngine.Scripting;
using UnityEngine.XR.OpenXR.Input;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.XR;
using UnityEngine.InputSystem;
using System.Runtime.InteropServices;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
// using PoseControl = UnityEngine.XR.OpenXR.Input.PoseControl; // If using the old PoseControl
using PoseControl = UnityEngine.InputSystem.XR.PoseControl;
namespace UnityEngine.XR.OpenXR.Features.Interactions
{
/// <summary>
/// This <see cref="OpenXRInteractionFeature"/> enables the use of HTC Vive Tracker Haptics interaction profiles in OpenXR.
/// </summary>
#if UNITY_EDITOR
[UnityEditor.XR.OpenXR.Features.OpenXRFeature(
UiName = "HTC Vive Tracker Profile",
BuildTargetGroups = new[] { BuildTargetGroup.Standalone, BuildTargetGroup.WSA },
Company = "MASSIVE",
Desc = "Allows for mapping input to the HTC Vive Tracker Haptic interaction profile. Modified by Max Bennett for haptic output.",
DocumentationLink = Constants.k_DocumentationManualURL,
OpenxrExtensionStrings = HTCViveTrackerProfile.extensionName,
Version = "0.0.1",
Category = UnityEditor.XR.OpenXR.Features.FeatureCategory.Interaction,
FeatureId = featureId)]
#endif
public class HTCViveTrackerProfile : OpenXRInteractionFeature
{
/// <summary>
/// The feature id string. This is used to give the feature a well known id for reference.
/// </summary>
public const string featureId = "com.massive.openxr.feature.input.htcvivetracker";
/// <summary>
/// The interaction profile string used to reference the <a href="https://www.khronos.org/registry/OpenXR/specs/1.0/html/xrspec.html#:~:text=in%20this%20case.-,VIVE%20Tracker%20interaction%20profile,-Interaction%20profile%20path">HTC Vive Tracker Haptic</a>.
/// </summary>
public const string profile = "/interaction_profiles/htc/vive_tracker_htcx";
/// <summary>
/// The name of the OpenXR extension that supports the Vive Tracker
/// </summary>
public const string extensionName = "XR_HTCX_vive_tracker_interaction";
private const string kDeviceLocalizedName = "HTC Vive Tracker OpenXR";
/// <summary>
/// OpenXR user path definitions for the tracker.
/// </summary>
public static class TrackerUserPaths
{
/// <summary>
/// Path for user left foot
/// </summary>
public const string leftFoot = "/user/vive_tracker_htcx/role/left_foot";
/// <summary>
/// Path for user roght foot
/// </summary>
public const string rightFoot = "/user/vive_tracker_htcx/role/right_foot";
/// <summary>
/// Path for user left shoulder
/// </summary>
public const string leftShoulder = "/user/vive_tracker_htcx/role/left_shoulder";
/// <summary>
/// Path for user right shoulder
/// </summary>
public const string rightShoulder = "/user/vive_tracker_htcx/role/right_shoulder";
/// <summary>
/// Path for user left elbow
/// </summary>
public const string leftElbow = "/user/vive_tracker_htcx/role/left_elbow";
/// <summary>
/// Path for user right elbow
/// </summary>
public const string rightElbow = "/user/vive_tracker_htcx/role/right_elbow";
/// <summary>
/// Path for user left knee
/// </summary>
public const string leftKnee = "/user/vive_tracker_htcx/role/left_knee";
/// <summary>
/// Path for user right knee
/// </summary>
public const string rightKnee = "/user/vive_tracker_htcx/role/right_knee";
/// <summary>
/// Path for user waist
/// </summary>
public const string waist = "/user/vive_tracker_htcx/role/waist";
/// <summary>
/// Path for user chest
/// </summary>
public const string chest = "/user/vive_tracker_htcx/role/chest";
/// <summary>
/// Path for user custom camera
/// </summary>
public const string camera = "/user/vive_tracker_htcx/role/camera";
/// <summary>
/// Path for user keyboard
/// </summary>
public const string keyboard = "/user/vive_tracker_htcx/role/keyboard";
}
/// <summary>
/// OpenXR component path definitions for the tracker.
/// </summary>
public static class TrackerComponentPaths
{
/// <summary>
/// Constant for a pose interaction binding '.../input/grip/pose' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs.
/// </summary>
public const string grip = "/input/grip/pose";
/// <summary>
/// Constant for a haptic interaction binding '.../output/haptic' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs.
/// </summary>
public const string haptic = "/output/haptic";
}
/// <summary>
/// A base Input System device for XR Trackers, based off the TrackedDevice
/// </summary>
[InputControlLayout(isGenericTypeOfDevice = true, displayName = "XR Tracker")]
public class XRTracker : TrackedDevice
{
}
/// <summary>
/// An Input System device based off the <a href="https://www.khronos.org/registry/OpenXR/specs/1.0/html/xrspec.html#_htc_vive_controller_profile">HTC Vive Tracker Haptic</a>.
/// </summary>
[Preserve, InputControlLayout(displayName = "HTC Vive Tracker (OpenXR)", commonUsages = new[] { "Left Foot", "Right Foot", "Left Shoulder", "Right Shoulder", "Left Elbow", "Right Elbow", "Left Knee", "Right Knee", "Waist", "Chest", "Camera", "Keyboard" })]
public class XRViveTracker : XRTracker
{
/// <summary>
/// A <see cref="PoseControl"/> that represents information from the <see cref="HTCViveTrackerProfile.grip"/> OpenXR binding.
/// </summary>
[Preserve, InputControl(offset = 0, aliases = new[] { "device", "gripPose" }, usage = "Device", noisy = true)]
public PoseControl devicePose { get; private set; }
/// <summary>
/// A [Vector3Control](xref:UnityEngine.InputSystem.Controls.Vector3Control) required for back compatibility with the XRSDK layouts. This is the device position. For the Oculus Touch device, this is both the grip and the pointer position. This value is equivalent to mapping devicePose/position.
/// </summary>
[Preserve, InputControl(offset = 8, alias = "gripPosition", noisy = true)]
new public Vector3Control devicePosition { get; private set; }
/// <summary>
/// A [QuaternionControl](xref:UnityEngine.InputSystem.Controls.QuaternionControl) required for backwards compatibility with the XRSDK layouts. This is the device orientation. For the Oculus Touch device, this is both the grip and the pointer rotation. This value is equivalent to mapping devicePose/rotation.
/// </summary>
[Preserve, InputControl(offset = 20, alias = "gripOrientation", noisy = true)]
new public QuaternionControl deviceRotation { get; private set; }
[Preserve, InputControl(offset = 60)]
new public ButtonControl isTracked { get; private set; }
[Preserve, InputControl(offset = 64)]
new public IntegerControl trackingState { get; private set; }
/// <summary>
/// A <see cref="HapticControl"/> that represents the <see cref="HTCViveControllerProfile.haptic"/> binding.
/// </summary>
[Preserve, InputControl(usage = "Haptic")]
public HapticControl haptic { get; private set; }
/// <inheritdoc cref="OpenXRDevice"/>
protected override void FinishSetup()
{
base.FinishSetup();
devicePose = GetChildControl<PoseControl>("devicePose");
devicePosition = GetChildControl<Vector3Control>("devicePosition");
deviceRotation = GetChildControl<QuaternionControl>("deviceRotation");
isTracked = GetChildControl<ButtonControl>("isTracked");
trackingState = GetChildControl<IntegerControl>("trackingState");
haptic = GetChildControl<HapticControl>("haptic");
var capabilities = description.capabilities;
var deviceDescriptor = XRDeviceDescriptor.FromJson(capabilities);
if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerLeftFoot) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Left Foot");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerRightFoot) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Right Foot");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerLeftShoulder) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Left Shoulder");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerRightShoulder) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Right Shoulder");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerLeftElbow) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Left Elbow");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerRightElbow) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Right Elbow");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerLeftKnee) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Left Knee");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerRightKnee) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Right Knee");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerWaist) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Waist");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerChest) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Chest");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerCamera) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Camera");
else if ((deviceDescriptor.characteristics & (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerKeyboard) != 0)
InputSystem.InputSystem.SetDeviceUsage(this, "Keyboard");
Debug.Log("Device added");
}
}
/// <summary>
/// Registers the <see cref="ViveTracker"/> layout with the Input System.
/// </summary>
protected override void RegisterDeviceLayout()
{
InputSystem.InputSystem.RegisterLayout<XRTracker>();
InputSystem.InputSystem.RegisterLayout(typeof(XRViveTracker),
matches: new InputDeviceMatcher()
.WithInterface(XRUtilities.InterfaceMatchAnyVersion)
.WithProduct(kDeviceLocalizedName));
}
/// <summary>
/// Removes the <see cref="ViveTracker"/> layout from the Input System.
/// </summary>
protected override void UnregisterDeviceLayout()
{
InputSystem.InputSystem.RemoveLayout(nameof(XRViveTracker));
InputSystem.InputSystem.RemoveLayout(nameof(XRTracker));
}
//
// Summary:
// A set of bit flags describing XR.InputDevice characteristics.
//"Left Foot", "Right Foot", "Left Shoulder", "Right Shoulder", "Left Elbow", "Right Elbow", "Left Knee", "Right Knee", "Waist", "Chest", "Camera", "Keyboard"
[Flags]
public enum InputDeviceTrackerCharacteristics : uint
{
TrackerLeftFoot = 0x1000u,
TrackerRightFoot = 0x2000u,
TrackerLeftShoulder = 0x4000u,
TrackerRightShoulder = 0x8000u,
TrackerLeftElbow = 0x10000u,
TrackerRightElbow = 0x20000u,
TrackerLeftKnee = 0x40000u,
TrackerRightKnee = 0x80000u,
TrackerWaist = 0x100000u,
TrackerChest = 0x200000u,
TrackerCamera = 0x400000u,
TrackerKeyboard = 0x800000u
}
/// <inheritdoc/>
protected override void RegisterActionMapsWithRuntime()
{
ActionMapConfig actionMap = new ActionMapConfig()
{
name = "htcvivetracker",
localizedName = kDeviceLocalizedName,
desiredInteractionProfile = profile,
manufacturer = "HTC",
serialNumber = "",
deviceInfos = new List<DeviceConfig>()
{
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerLeftFoot,
userPath = TrackerUserPaths.leftFoot
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerRightFoot,
userPath = TrackerUserPaths.rightFoot
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerLeftShoulder,
userPath = TrackerUserPaths.leftShoulder
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerRightShoulder,
userPath = TrackerUserPaths.rightShoulder
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerLeftElbow,
userPath = TrackerUserPaths.leftElbow
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerRightElbow,
userPath = TrackerUserPaths.rightElbow
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerLeftKnee,
userPath = TrackerUserPaths.leftKnee
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerRightKnee,
userPath = TrackerUserPaths.rightKnee
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerWaist,
userPath = TrackerUserPaths.waist
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerChest,
userPath = TrackerUserPaths.chest
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerCamera,
userPath = TrackerUserPaths.camera
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics.TrackedDevice) | (InputDeviceCharacteristics)InputDeviceTrackerCharacteristics.TrackerKeyboard,
userPath = TrackerUserPaths.keyboard
}
},
actions = new List<ActionConfig>()
{
new ActionConfig()
{
name = "devicePose",
localizedName = "Device Pose",
type = ActionType.Pose,
usages = new List<string>()
{
"Device",
},
bindings = new List<ActionBinding>()
{
new ActionBinding()
{
interactionPath = TrackerComponentPaths.grip,
interactionProfileName = profile,
}
}
},
// Haptics
new ActionConfig()
{
name = "haptic",
localizedName = "Haptic Output",
type = ActionType.Vibrate,
usages = new List<string>() { "Haptic" },
bindings = new List<ActionBinding>()
{
new ActionBinding()
{
interactionPath = TrackerComponentPaths.haptic,
interactionProfileName = profile,
}
}
}
}
};
AddActionMap(actionMap);
}
protected override bool OnInstanceCreate(ulong xrInstance)
{
bool res = base.OnInstanceCreate(xrInstance);
if (OpenXRRuntime.IsExtensionEnabled("XR_HTCX_vive_tracker_interaction"))
{
Debug.Log("HTC Vive Tracker Haptic Extension Enabled");
}
else
{
Debug.Log("HTC Vive Tracker Haptic Extension Not Enabled");
}
return res;
}
}
}