Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
OpenXRProvider.cs
Go to the documentation of this file.
1/* ​
2* Copyright (c) 2023 Go Touch VR SAS. All rights reserved. ​
3* ​
4*/
5#if ENABLE_OPENXR
6using UnityEngine;
8using UnityEngine.InputSystem;
9using UnityEngine.XR.OpenXR.Input;
10
11
12[assembly: UnityEngine.Scripting.AlwaysLinkAssembly]
13[assembly: UnityEngine.Scripting.Preserve]
15{
16
17 public sealed class OpenXRProvider : IHapticProvider
18 {
19 IH_HapticsInput m_IH_HapticsInput;
20 float minimumDuration = 0.016f;
21 int sampleRate = 150;
22
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";
28 #endregion
29
30 #region HAPTIC CHARACTERISTICS GETTERS
31 [UnityEngine.Scripting.Preserve]
32 public string DisplayName()
33 {
34 return DISPLAY_NAME;
35 }
36
37 [UnityEngine.Scripting.Preserve]
38 public string Description()
39 {
40 return DESCRIPTION;
41 }
42
43 [UnityEngine.Scripting.Preserve]
44 public string Manufacturer()
45 {
46 return MANUFACTURER;
47 }
48
49 [UnityEngine.Scripting.Preserve]
50 public string Version()
51 {
52 return VERSION;
53 }
54#endregion
55
56 #region PROVIDER LOOP
57 [UnityEngine.Scripting.Preserve]
58 public bool Init()
59 {
60 if (UnityEngine.XR.InputDevices.GetDeviceAtXRNode(UnityEngine.XR.XRNode.Head) == null)
61 {
62 UnityEngine.Debug.Log("XR HMD not found");
63 return false;
64 }
65 m_IH_HapticsInput = new IH_HapticsInput();
66
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)
70 {
71 UnityEngine.Debug.Log("OpenXR haptic provider started.");
72 }
73 return true;
74 }
75
76 [UnityEngine.Scripting.Preserve]
77 public bool IsPresent()
78 {
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);
82 return isPresent;
83 }
84
85 [UnityEngine.Scripting.Preserve]
86 public bool Clean()
87 {
88 return true;
89 }
90
91 [UnityEngine.Scripting.Preserve]
92 public void RenderHaptics()
93 {
94 double[] outputBuffer;
95 int size = Core.HAR.GetOutputBufferSize(Perception.Vibration, BodyPartID.Bp_Left_palm, 0, 0, 0, BufferDataType.PCM);
96 if (size > 0)
97 {
98 outputBuffer = new double[size];
99 Core.HAR.GetOutputBuffer(outputBuffer, size, Perception.Vibration, BodyPartID.Bp_Left_palm, 0, 0, 0, BufferDataType.PCM);
100 OpenXRInput.SendHapticImpulse(m_IH_HapticsInput.HapticsXR.Left, (float)AverageFromBuffer(outputBuffer, size), minimumDuration, UnityEngine.InputSystem.XR.XRController.leftHand);
101 }
102 else
103 {
104 OpenXRInput.StopHaptics(m_IH_HapticsInput.HapticsXR.Left, UnityEngine.InputSystem.XR.XRController.leftHand);
105 }
106
107 size = Core.HAR.GetOutputBufferSize(Perception.Vibration, BodyPartID.Bp_Right_palm, 0, 0, 0, BufferDataType.PCM);
108 if (size > 0)
109 {
110 outputBuffer = new double[size];
111 Core.HAR.GetOutputBuffer(outputBuffer, size, Perception.Vibration, BodyPartID.Bp_Right_palm, 0, 0, 0, BufferDataType.PCM);
112 OpenXRInput.SendHapticImpulse(m_IH_HapticsInput.HapticsXR.Right, (float)AverageFromBuffer(outputBuffer, size), minimumDuration, UnityEngine.InputSystem.XR.XRController.rightHand);
113 }
114 else
115 {
116 OpenXRInput.StopHaptics(m_IH_HapticsInput.HapticsXR.Right, UnityEngine.InputSystem.XR.XRController.rightHand);
117 }
118 }
119 #endregion
120
121 double AverageFromBuffer(double[] buffer, int bufferSize)
122 {
123 if (bufferSize <= 0)
124 {
125 return 0;
126 }
127
128 double average = 0;
129
130 for (int i = 0; i < bufferSize; i++)
131 {
132 average += buffer[i];
133 }
134
135 return average / bufferSize;
136 }
137
138 }
139
140}
141#endif
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.