OVRHaptics.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. public static class OVRHaptics
  6. {
  7. static OVRHaptics()
  8. {
  9. OVRHaptics.Config.Load();
  10. OVRHaptics.m_outputs = new OVRHaptics.OVRHapticsOutput[]
  11. {
  12. new OVRHaptics.OVRHapticsOutput(1u),
  13. new OVRHaptics.OVRHapticsOutput(2u)
  14. };
  15. OVRHaptics.Channels = new OVRHaptics.OVRHapticsChannel[]
  16. {
  17. OVRHaptics.LeftChannel = new OVRHaptics.OVRHapticsChannel(0u),
  18. OVRHaptics.RightChannel = new OVRHaptics.OVRHapticsChannel(1u)
  19. };
  20. }
  21. public static void Process()
  22. {
  23. OVRHaptics.Config.Load();
  24. for (int i = 0; i < OVRHaptics.m_outputs.Length; i++)
  25. {
  26. OVRHaptics.m_outputs[i].Process();
  27. }
  28. }
  29. public static readonly OVRHaptics.OVRHapticsChannel[] Channels;
  30. public static readonly OVRHaptics.OVRHapticsChannel LeftChannel;
  31. public static readonly OVRHaptics.OVRHapticsChannel RightChannel;
  32. private static readonly OVRHaptics.OVRHapticsOutput[] m_outputs;
  33. public static class Config
  34. {
  35. static Config()
  36. {
  37. OVRHaptics.Config.Load();
  38. }
  39. public static int SampleRateHz { get; private set; }
  40. public static int SampleSizeInBytes { get; private set; }
  41. public static int MinimumSafeSamplesQueued { get; private set; }
  42. public static int MinimumBufferSamplesCount { get; private set; }
  43. public static int OptimalBufferSamplesCount { get; private set; }
  44. public static int MaximumBufferSamplesCount { get; private set; }
  45. public static void Load()
  46. {
  47. OVRPlugin.HapticsDesc controllerHapticsDesc = OVRPlugin.GetControllerHapticsDesc(2u);
  48. OVRHaptics.Config.SampleRateHz = controllerHapticsDesc.SampleRateHz;
  49. OVRHaptics.Config.SampleSizeInBytes = controllerHapticsDesc.SampleSizeInBytes;
  50. OVRHaptics.Config.MinimumSafeSamplesQueued = controllerHapticsDesc.MinimumSafeSamplesQueued;
  51. OVRHaptics.Config.MinimumBufferSamplesCount = controllerHapticsDesc.MinimumBufferSamplesCount;
  52. OVRHaptics.Config.OptimalBufferSamplesCount = controllerHapticsDesc.OptimalBufferSamplesCount;
  53. OVRHaptics.Config.MaximumBufferSamplesCount = controllerHapticsDesc.MaximumBufferSamplesCount;
  54. }
  55. }
  56. public class OVRHapticsChannel
  57. {
  58. public OVRHapticsChannel(uint outputIndex)
  59. {
  60. this.m_output = OVRHaptics.m_outputs[(int)((UIntPtr)outputIndex)];
  61. }
  62. public void Preempt(OVRHapticsClip clip)
  63. {
  64. this.m_output.Preempt(clip);
  65. }
  66. public void Queue(OVRHapticsClip clip)
  67. {
  68. this.m_output.Queue(clip);
  69. }
  70. public void Mix(OVRHapticsClip clip)
  71. {
  72. this.m_output.Mix(clip);
  73. }
  74. public void Clear()
  75. {
  76. this.m_output.Clear();
  77. }
  78. private OVRHaptics.OVRHapticsOutput m_output;
  79. }
  80. private class OVRHapticsOutput
  81. {
  82. public OVRHapticsOutput(uint controller)
  83. {
  84. this.m_controller = controller;
  85. }
  86. public void Process()
  87. {
  88. OVRPlugin.HapticsState controllerHapticsState = OVRPlugin.GetControllerHapticsState(this.m_controller);
  89. float num = Time.realtimeSinceStartup - this.m_prevSamplesQueuedTime;
  90. if (this.m_prevSamplesQueued > 0)
  91. {
  92. int num2 = this.m_prevSamplesQueued - (int)(num * (float)OVRHaptics.Config.SampleRateHz + 0.5f);
  93. if (num2 < 0)
  94. {
  95. num2 = 0;
  96. }
  97. if (controllerHapticsState.SamplesQueued - num2 == 0)
  98. {
  99. this.m_numPredictionHits++;
  100. }
  101. else
  102. {
  103. this.m_numPredictionMisses++;
  104. }
  105. if (num2 > 0 && controllerHapticsState.SamplesQueued == 0)
  106. {
  107. this.m_numUnderruns++;
  108. }
  109. this.m_prevSamplesQueued = controllerHapticsState.SamplesQueued;
  110. this.m_prevSamplesQueuedTime = Time.realtimeSinceStartup;
  111. }
  112. int num3 = OVRHaptics.Config.OptimalBufferSamplesCount;
  113. if (this.m_lowLatencyMode)
  114. {
  115. float num4 = 1000f / (float)OVRHaptics.Config.SampleRateHz;
  116. float num5 = num * 1000f;
  117. int num6 = (int)Mathf.Ceil(num5 / num4);
  118. int num7 = OVRHaptics.Config.MinimumSafeSamplesQueued + num6;
  119. if (num7 < num3)
  120. {
  121. num3 = num7;
  122. }
  123. }
  124. if (controllerHapticsState.SamplesQueued > num3)
  125. {
  126. return;
  127. }
  128. if (num3 > OVRHaptics.Config.MaximumBufferSamplesCount)
  129. {
  130. num3 = OVRHaptics.Config.MaximumBufferSamplesCount;
  131. }
  132. if (num3 > controllerHapticsState.SamplesAvailable)
  133. {
  134. num3 = controllerHapticsState.SamplesAvailable;
  135. }
  136. int num8 = 0;
  137. int num9 = 0;
  138. while (num8 < num3 && num9 < this.m_pendingClips.Count)
  139. {
  140. int num10 = num3 - num8;
  141. int num11 = this.m_pendingClips[num9].Clip.Count - this.m_pendingClips[num9].ReadCount;
  142. if (num10 > num11)
  143. {
  144. num10 = num11;
  145. }
  146. if (num10 > 0)
  147. {
  148. int length = num10 * OVRHaptics.Config.SampleSizeInBytes;
  149. int byteOffset = num8 * OVRHaptics.Config.SampleSizeInBytes;
  150. int startIndex = this.m_pendingClips[num9].ReadCount * OVRHaptics.Config.SampleSizeInBytes;
  151. Marshal.Copy(this.m_pendingClips[num9].Clip.Samples, startIndex, this.m_nativeBuffer.GetPointer(byteOffset), length);
  152. this.m_pendingClips[num9].ReadCount += num10;
  153. num8 += num10;
  154. }
  155. num9++;
  156. }
  157. int num12 = this.m_pendingClips.Count - 1;
  158. while (num12 >= 0 && this.m_pendingClips.Count > 0)
  159. {
  160. if (this.m_pendingClips[num12].ReadCount >= this.m_pendingClips[num12].Clip.Count)
  161. {
  162. this.m_pendingClips.RemoveAt(num12);
  163. }
  164. num12--;
  165. }
  166. int num13 = num3 - (controllerHapticsState.SamplesQueued + num8);
  167. if (num13 < OVRHaptics.Config.MinimumBufferSamplesCount - num8)
  168. {
  169. num13 = OVRHaptics.Config.MinimumBufferSamplesCount - num8;
  170. }
  171. if (num13 > controllerHapticsState.SamplesAvailable)
  172. {
  173. num13 = controllerHapticsState.SamplesAvailable;
  174. }
  175. if (num13 > 0)
  176. {
  177. int length2 = num13 * OVRHaptics.Config.SampleSizeInBytes;
  178. int byteOffset2 = num8 * OVRHaptics.Config.SampleSizeInBytes;
  179. int startIndex2 = 0;
  180. Marshal.Copy(this.m_paddingClip.Samples, startIndex2, this.m_nativeBuffer.GetPointer(byteOffset2), length2);
  181. num8 += num13;
  182. }
  183. if (num8 > 0)
  184. {
  185. OVRPlugin.HapticsBuffer hapticsBuffer;
  186. hapticsBuffer.Samples = this.m_nativeBuffer.GetPointer(0);
  187. hapticsBuffer.SamplesCount = num8;
  188. OVRPlugin.SetControllerHaptics(this.m_controller, hapticsBuffer);
  189. this.m_prevSamplesQueued = OVRPlugin.GetControllerHapticsState(this.m_controller).SamplesQueued;
  190. this.m_prevSamplesQueuedTime = Time.realtimeSinceStartup;
  191. }
  192. }
  193. public void Preempt(OVRHapticsClip clip)
  194. {
  195. this.m_pendingClips.Clear();
  196. this.m_pendingClips.Add(new OVRHaptics.OVRHapticsOutput.ClipPlaybackTracker(clip));
  197. }
  198. public void Queue(OVRHapticsClip clip)
  199. {
  200. this.m_pendingClips.Add(new OVRHaptics.OVRHapticsOutput.ClipPlaybackTracker(clip));
  201. }
  202. public void Mix(OVRHapticsClip clip)
  203. {
  204. int num = 0;
  205. int num2 = 0;
  206. int num3 = clip.Count;
  207. while (num3 > 0 && num < this.m_pendingClips.Count)
  208. {
  209. int num4 = this.m_pendingClips[num].Clip.Count - this.m_pendingClips[num].ReadCount;
  210. num3 -= num4;
  211. num2 += num4;
  212. num++;
  213. }
  214. if (num3 > 0)
  215. {
  216. num2 += num3;
  217. }
  218. if (num > 0)
  219. {
  220. OVRHapticsClip ovrhapticsClip = new OVRHapticsClip(num2);
  221. int i = 0;
  222. for (int j = 0; j < num; j++)
  223. {
  224. OVRHapticsClip clip2 = this.m_pendingClips[j].Clip;
  225. for (int k = this.m_pendingClips[j].ReadCount; k < clip2.Count; k++)
  226. {
  227. if (OVRHaptics.Config.SampleSizeInBytes == 1)
  228. {
  229. byte sample = 0;
  230. if (i < clip.Count && k < clip2.Count)
  231. {
  232. sample = (byte)Mathf.Clamp((int)(clip.Samples[i] + clip2.Samples[k]), 0, 255);
  233. i++;
  234. }
  235. else if (k < clip2.Count)
  236. {
  237. sample = clip2.Samples[k];
  238. }
  239. ovrhapticsClip.WriteSample(sample);
  240. }
  241. }
  242. }
  243. while (i < clip.Count)
  244. {
  245. if (OVRHaptics.Config.SampleSizeInBytes == 1)
  246. {
  247. ovrhapticsClip.WriteSample(clip.Samples[i]);
  248. }
  249. i++;
  250. }
  251. this.m_pendingClips[0] = new OVRHaptics.OVRHapticsOutput.ClipPlaybackTracker(ovrhapticsClip);
  252. for (int l = 1; l < num; l++)
  253. {
  254. this.m_pendingClips.RemoveAt(1);
  255. }
  256. }
  257. else
  258. {
  259. this.m_pendingClips.Add(new OVRHaptics.OVRHapticsOutput.ClipPlaybackTracker(clip));
  260. }
  261. }
  262. public void Clear()
  263. {
  264. this.m_pendingClips.Clear();
  265. }
  266. private bool m_lowLatencyMode = true;
  267. private int m_prevSamplesQueued;
  268. private float m_prevSamplesQueuedTime;
  269. private int m_numPredictionHits;
  270. private int m_numPredictionMisses;
  271. private int m_numUnderruns;
  272. private List<OVRHaptics.OVRHapticsOutput.ClipPlaybackTracker> m_pendingClips = new List<OVRHaptics.OVRHapticsOutput.ClipPlaybackTracker>();
  273. private uint m_controller;
  274. private OVRNativeBuffer m_nativeBuffer = new OVRNativeBuffer(OVRHaptics.Config.MaximumBufferSamplesCount * OVRHaptics.Config.SampleSizeInBytes);
  275. private OVRHapticsClip m_paddingClip = new OVRHapticsClip();
  276. private class ClipPlaybackTracker
  277. {
  278. public ClipPlaybackTracker(OVRHapticsClip clip)
  279. {
  280. this.Clip = clip;
  281. }
  282. public int ReadCount { get; set; }
  283. public OVRHapticsClip Clip { get; set; }
  284. }
  285. }
  286. }