8using UnityEngine.InputSystem;
9using UnityEngine.XR.OpenXR.Input;
12[assembly: UnityEngine.Scripting.AlwaysLinkAssembly]
13[assembly: UnityEngine.Scripting.Preserve]
17 public sealed class OpenXRProvider : IHapticProvider
19 IH_HapticsInput m_IH_HapticsInput;
20 float minimumDuration = 0.016f;
23 #region HAPTIC CHARACTERISTICS FIELDS
24 private const string DISPLAY_NAME =
"OpenXR device";
25 private const string DESCRIPTION =
"XR controller for OpenXR device";
26 private const string MANUFACTURER =
"Unknown";
27 private const string VERSION =
"1.0";
30 #region HAPTIC CHARACTERISTICS GETTERS
31 [UnityEngine.Scripting.Preserve]
32 public string DisplayName()
37 [UnityEngine.Scripting.Preserve]
38 public string Description()
43 [UnityEngine.Scripting.Preserve]
44 public string Manufacturer()
49 [UnityEngine.Scripting.Preserve]
50 public string Version()
57 [UnityEngine.Scripting.Preserve]
60 if (UnityEngine.XR.InputDevices.GetDeviceAtXRNode(UnityEngine.XR.XRNode.Head) ==
null)
62 UnityEngine.Debug.Log(
"XR HMD not found");
65 m_IH_HapticsInput =
new IH_HapticsInput();
67 Core.HAR.AddBodyPart(
Perception.Vibration,
BodyPartID.Bp_Left_palm, 1, 1, 1, sampleRate,
false,
false,
false,
true);
68 Core.HAR.AddBodyPart(
Perception.Vibration,
BodyPartID.Bp_Right_palm, 1, 1, 1, sampleRate,
false,
false,
false,
true);
69 if (HapticManager.DebugSwitch)
71 UnityEngine.Debug.Log(
"OpenXR haptic provider started.");
76 [UnityEngine.Scripting.Preserve]
77 public bool IsPresent()
79 UnityEngine.XR.HapticCapabilities caps =
new UnityEngine.XR.HapticCapabilities();
80 bool isPresent = UnityEngine.XR.InputDevices.GetDeviceAtXRNode(UnityEngine.XR.XRNode.LeftHand).TryGetHapticCapabilities(out caps);
81 isPresent |= UnityEngine.XR.InputDevices.GetDeviceAtXRNode(UnityEngine.XR.XRNode.RightHand).TryGetHapticCapabilities(out caps);
85 [UnityEngine.Scripting.Preserve]
91 [UnityEngine.Scripting.Preserve]
92 public void RenderHaptics()
94 double[] outputBuffer;
98 outputBuffer =
new double[size];
100 OpenXRInput.SendHapticImpulse(m_IH_HapticsInput.HapticsXR.Left, (
float)AverageFromBuffer(outputBuffer, size), minimumDuration, UnityEngine.InputSystem.XR.XRController.leftHand);
104 OpenXRInput.StopHaptics(m_IH_HapticsInput.HapticsXR.Left, UnityEngine.InputSystem.XR.XRController.leftHand);
110 outputBuffer =
new double[size];
112 OpenXRInput.SendHapticImpulse(m_IH_HapticsInput.HapticsXR.Right, (
float)AverageFromBuffer(outputBuffer, size), minimumDuration, UnityEngine.InputSystem.XR.XRController.rightHand);
116 OpenXRInput.StopHaptics(m_IH_HapticsInput.HapticsXR.Right, UnityEngine.InputSystem.XR.XRController.rightHand);
121 double AverageFromBuffer(
double[] buffer,
int bufferSize)
130 for (
int i = 0; i < bufferSize; i++)
132 average += buffer[i];
135 return average / bufferSize;
BufferDataType
Enumeration for types of haptic buffer data.
BodyPartID
Enumeration for identifying different body parts for haptic effects.
Perception
Enumeration for different types of haptic perceptions.