SteamVR_Controller.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using System;
  2. using UnityEngine;
  3. using Valve.VR;
  4. public class SteamVR_Controller
  5. {
  6. public static SteamVR_Controller.Device Input(int deviceIndex)
  7. {
  8. if (SteamVR_Controller.devices == null)
  9. {
  10. SteamVR_Controller.devices = new SteamVR_Controller.Device[16];
  11. uint num = 0U;
  12. while ((ulong)num < (ulong)((long)SteamVR_Controller.devices.Length))
  13. {
  14. SteamVR_Controller.devices[(int)((UIntPtr)num)] = new SteamVR_Controller.Device(num);
  15. num += 1U;
  16. }
  17. }
  18. return SteamVR_Controller.devices[deviceIndex];
  19. }
  20. public static void Update()
  21. {
  22. int num = 0;
  23. while ((long)num < 16L)
  24. {
  25. SteamVR_Controller.Input(num).Update();
  26. num++;
  27. }
  28. }
  29. public static int GetDeviceIndex(SteamVR_Controller.DeviceRelation relation, ETrackedDeviceClass deviceClass = ETrackedDeviceClass.Controller, int relativeTo = 0)
  30. {
  31. int result = -1;
  32. SteamVR_Utils.RigidTransform t = (relativeTo >= 16) ? SteamVR_Utils.RigidTransform.identity : SteamVR_Controller.Input(relativeTo).transform.GetInverse();
  33. CVRSystem system = OpenVR.System;
  34. if (system == null)
  35. {
  36. return result;
  37. }
  38. float num = float.MinValue;
  39. int num2 = 0;
  40. while ((long)num2 < 16L)
  41. {
  42. if (num2 != relativeTo && system.GetTrackedDeviceClass((uint)num2) == deviceClass)
  43. {
  44. SteamVR_Controller.Device device = SteamVR_Controller.Input(num2);
  45. if (device.connected)
  46. {
  47. if (relation == SteamVR_Controller.DeviceRelation.First)
  48. {
  49. return num2;
  50. }
  51. Vector3 vector = t * device.transform.pos;
  52. float num3;
  53. if (relation == SteamVR_Controller.DeviceRelation.FarthestRight)
  54. {
  55. num3 = vector.x;
  56. }
  57. else if (relation == SteamVR_Controller.DeviceRelation.FarthestLeft)
  58. {
  59. num3 = -vector.x;
  60. }
  61. else
  62. {
  63. Vector3 vector2 = new Vector3(vector.x, 0f, vector.z);
  64. Vector3 normalized = vector2.normalized;
  65. float num4 = Vector3.Dot(normalized, Vector3.forward);
  66. Vector3 vector3 = Vector3.Cross(normalized, Vector3.forward);
  67. if (relation == SteamVR_Controller.DeviceRelation.Leftmost)
  68. {
  69. num3 = ((vector3.y <= 0f) ? num4 : (2f - num4));
  70. }
  71. else
  72. {
  73. num3 = ((vector3.y >= 0f) ? num4 : (2f - num4));
  74. }
  75. }
  76. if (num3 > num)
  77. {
  78. result = num2;
  79. num = num3;
  80. }
  81. }
  82. }
  83. num2++;
  84. }
  85. return result;
  86. }
  87. private static SteamVR_Controller.Device[] devices;
  88. public class ButtonMask
  89. {
  90. public const ulong System = 1UL;
  91. public const ulong ApplicationMenu = 2UL;
  92. public const ulong Grip = 4UL;
  93. public const ulong Axis0 = 4294967296UL;
  94. public const ulong Axis1 = 8589934592UL;
  95. public const ulong Axis2 = 17179869184UL;
  96. public const ulong Axis3 = 34359738368UL;
  97. public const ulong Axis4 = 68719476736UL;
  98. public const ulong Touchpad = 4294967296UL;
  99. public const ulong Trigger = 8589934592UL;
  100. }
  101. public class Device
  102. {
  103. public Device(uint i)
  104. {
  105. this.index = i;
  106. }
  107. public uint index { get; private set; }
  108. public bool valid { get; private set; }
  109. public bool connected
  110. {
  111. get
  112. {
  113. this.Update();
  114. return this.pose.bDeviceIsConnected;
  115. }
  116. }
  117. public bool hasTracking
  118. {
  119. get
  120. {
  121. this.Update();
  122. return this.pose.bPoseIsValid;
  123. }
  124. }
  125. public bool outOfRange
  126. {
  127. get
  128. {
  129. this.Update();
  130. return this.pose.eTrackingResult == ETrackingResult.Running_OutOfRange || this.pose.eTrackingResult == ETrackingResult.Calibrating_OutOfRange;
  131. }
  132. }
  133. public bool calibrating
  134. {
  135. get
  136. {
  137. this.Update();
  138. return this.pose.eTrackingResult == ETrackingResult.Calibrating_InProgress || this.pose.eTrackingResult == ETrackingResult.Calibrating_OutOfRange;
  139. }
  140. }
  141. public bool uninitialized
  142. {
  143. get
  144. {
  145. this.Update();
  146. return this.pose.eTrackingResult == ETrackingResult.Uninitialized;
  147. }
  148. }
  149. public SteamVR_Utils.RigidTransform transform
  150. {
  151. get
  152. {
  153. this.Update();
  154. return new SteamVR_Utils.RigidTransform(this.pose.mDeviceToAbsoluteTracking);
  155. }
  156. }
  157. public Vector3 velocity
  158. {
  159. get
  160. {
  161. this.Update();
  162. return new Vector3(this.pose.vVelocity.v0, this.pose.vVelocity.v1, -this.pose.vVelocity.v2);
  163. }
  164. }
  165. public Vector3 angularVelocity
  166. {
  167. get
  168. {
  169. this.Update();
  170. return new Vector3(-this.pose.vAngularVelocity.v0, -this.pose.vAngularVelocity.v1, this.pose.vAngularVelocity.v2);
  171. }
  172. }
  173. public VRControllerState_t GetState()
  174. {
  175. this.Update();
  176. return this.state;
  177. }
  178. public VRControllerState_t GetPrevState()
  179. {
  180. this.Update();
  181. return this.prevState;
  182. }
  183. public TrackedDevicePose_t GetPose()
  184. {
  185. this.Update();
  186. return this.pose;
  187. }
  188. public void Update()
  189. {
  190. if (Time.frameCount != this.prevFrameCount)
  191. {
  192. this.prevFrameCount = Time.frameCount;
  193. this.prevState = this.state;
  194. CVRSystem system = OpenVR.System;
  195. if (system != null)
  196. {
  197. this.valid = system.GetControllerStateWithPose(SteamVR_Render.instance.trackingSpace, this.index, ref this.state, ref this.pose);
  198. this.UpdateHairTrigger();
  199. }
  200. }
  201. }
  202. public bool GetPress(ulong buttonMask)
  203. {
  204. this.Update();
  205. return (this.state.ulButtonPressed & buttonMask) != 0UL;
  206. }
  207. public bool GetPressDown(ulong buttonMask)
  208. {
  209. this.Update();
  210. return (this.state.ulButtonPressed & buttonMask) != 0UL && (this.prevState.ulButtonPressed & buttonMask) == 0UL;
  211. }
  212. public bool GetPressUp(ulong buttonMask)
  213. {
  214. this.Update();
  215. return (this.state.ulButtonPressed & buttonMask) == 0UL && (this.prevState.ulButtonPressed & buttonMask) != 0UL;
  216. }
  217. public bool GetPress(EVRButtonId buttonId)
  218. {
  219. return this.GetPress(1UL << (int)buttonId);
  220. }
  221. public bool GetPressDown(EVRButtonId buttonId)
  222. {
  223. return this.GetPressDown(1UL << (int)buttonId);
  224. }
  225. public bool GetPressUp(EVRButtonId buttonId)
  226. {
  227. return this.GetPressUp(1UL << (int)buttonId);
  228. }
  229. public bool GetTouch(ulong buttonMask)
  230. {
  231. this.Update();
  232. return (this.state.ulButtonTouched & buttonMask) != 0UL;
  233. }
  234. public bool GetTouchDown(ulong buttonMask)
  235. {
  236. this.Update();
  237. return (this.state.ulButtonTouched & buttonMask) != 0UL && (this.prevState.ulButtonTouched & buttonMask) == 0UL;
  238. }
  239. public bool GetTouchUp(ulong buttonMask)
  240. {
  241. this.Update();
  242. return (this.state.ulButtonTouched & buttonMask) == 0UL && (this.prevState.ulButtonTouched & buttonMask) != 0UL;
  243. }
  244. public bool GetTouch(EVRButtonId buttonId)
  245. {
  246. return this.GetTouch(1UL << (int)buttonId);
  247. }
  248. public bool GetTouchDown(EVRButtonId buttonId)
  249. {
  250. return this.GetTouchDown(1UL << (int)buttonId);
  251. }
  252. public bool GetTouchUp(EVRButtonId buttonId)
  253. {
  254. return this.GetTouchUp(1UL << (int)buttonId);
  255. }
  256. public Vector2 GetAxis(EVRButtonId buttonId = EVRButtonId.k_EButton_Axis0)
  257. {
  258. this.Update();
  259. switch (buttonId)
  260. {
  261. case EVRButtonId.k_EButton_Axis0:
  262. return new Vector2(this.state.rAxis0.x, this.state.rAxis0.y);
  263. case EVRButtonId.k_EButton_Axis1:
  264. return new Vector2(this.state.rAxis1.x, this.state.rAxis1.y);
  265. case EVRButtonId.k_EButton_Axis2:
  266. return new Vector2(this.state.rAxis2.x, this.state.rAxis2.y);
  267. case EVRButtonId.k_EButton_Axis3:
  268. return new Vector2(this.state.rAxis3.x, this.state.rAxis3.y);
  269. case EVRButtonId.k_EButton_Axis4:
  270. return new Vector2(this.state.rAxis4.x, this.state.rAxis4.y);
  271. default:
  272. return Vector2.zero;
  273. }
  274. }
  275. public void TriggerHapticPulse(ushort durationMicroSec = 500, EVRButtonId buttonId = EVRButtonId.k_EButton_Axis0)
  276. {
  277. CVRSystem system = OpenVR.System;
  278. if (system != null)
  279. {
  280. uint unAxisId = (uint)(buttonId - EVRButtonId.k_EButton_Axis0);
  281. system.TriggerHapticPulse(this.index, unAxisId, (char)durationMicroSec);
  282. }
  283. }
  284. private void UpdateHairTrigger()
  285. {
  286. this.hairTriggerPrevState = this.hairTriggerState;
  287. float x = this.state.rAxis1.x;
  288. if (this.hairTriggerState)
  289. {
  290. if (x < this.hairTriggerLimit - this.hairTriggerDelta || x <= 0f)
  291. {
  292. this.hairTriggerState = false;
  293. }
  294. }
  295. else if (x > this.hairTriggerLimit + this.hairTriggerDelta || x >= 1f)
  296. {
  297. this.hairTriggerState = true;
  298. }
  299. this.hairTriggerLimit = ((!this.hairTriggerState) ? Mathf.Min(this.hairTriggerLimit, x) : Mathf.Max(this.hairTriggerLimit, x));
  300. }
  301. public bool GetHairTrigger()
  302. {
  303. this.Update();
  304. return this.hairTriggerState;
  305. }
  306. public bool GetHairTriggerDown()
  307. {
  308. this.Update();
  309. return this.hairTriggerState && !this.hairTriggerPrevState;
  310. }
  311. public bool GetHairTriggerUp()
  312. {
  313. this.Update();
  314. return !this.hairTriggerState && this.hairTriggerPrevState;
  315. }
  316. private VRControllerState_t state;
  317. private VRControllerState_t prevState;
  318. private TrackedDevicePose_t pose;
  319. private int prevFrameCount = -1;
  320. public float hairTriggerDelta = 0.1f;
  321. private float hairTriggerLimit;
  322. private bool hairTriggerState;
  323. private bool hairTriggerPrevState;
  324. }
  325. public enum DeviceRelation
  326. {
  327. First,
  328. Leftmost,
  329. Rightmost,
  330. FarthestLeft,
  331. FarthestRight
  332. }
  333. }