SteamVR.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using System;
  2. using System.Text;
  3. using UnityEngine;
  4. using UnityEngine.VR;
  5. using Valve.VR;
  6. public class SteamVR : IDisposable
  7. {
  8. private SteamVR()
  9. {
  10. this.hmd = OpenVR.System;
  11. Debug.Log("Connected to " + this.hmd_TrackingSystemName + ":" + this.hmd_SerialNumber);
  12. this.compositor = OpenVR.Compositor;
  13. this.overlay = OpenVR.Overlay;
  14. uint num = 0u;
  15. uint num2 = 0u;
  16. this.hmd.GetRecommendedRenderTargetSize(ref num, ref num2);
  17. this.sceneWidth = num;
  18. this.sceneHeight = num2;
  19. float num3 = 0f;
  20. float num4 = 0f;
  21. float num5 = 0f;
  22. float num6 = 0f;
  23. this.hmd.GetProjectionRaw(EVREye.Eye_Left, ref num3, ref num4, ref num5, ref num6);
  24. float num7 = 0f;
  25. float num8 = 0f;
  26. float num9 = 0f;
  27. float num10 = 0f;
  28. this.hmd.GetProjectionRaw(EVREye.Eye_Right, ref num7, ref num8, ref num9, ref num10);
  29. this.tanHalfFov = new Vector2(Mathf.Max(new float[]
  30. {
  31. -num3,
  32. num4,
  33. -num7,
  34. num8
  35. }), Mathf.Max(new float[]
  36. {
  37. -num5,
  38. num6,
  39. -num9,
  40. num10
  41. }));
  42. this.textureBounds = new VRTextureBounds_t[2];
  43. this.textureBounds[0].uMin = 0.5f + 0.5f * num3 / this.tanHalfFov.x;
  44. this.textureBounds[0].uMax = 0.5f + 0.5f * num4 / this.tanHalfFov.x;
  45. this.textureBounds[0].vMin = 0.5f - 0.5f * num6 / this.tanHalfFov.y;
  46. this.textureBounds[0].vMax = 0.5f - 0.5f * num5 / this.tanHalfFov.y;
  47. this.textureBounds[1].uMin = 0.5f + 0.5f * num7 / this.tanHalfFov.x;
  48. this.textureBounds[1].uMax = 0.5f + 0.5f * num8 / this.tanHalfFov.x;
  49. this.textureBounds[1].vMin = 0.5f - 0.5f * num10 / this.tanHalfFov.y;
  50. this.textureBounds[1].vMax = 0.5f - 0.5f * num9 / this.tanHalfFov.y;
  51. this.sceneWidth /= Mathf.Max(this.textureBounds[0].uMax - this.textureBounds[0].uMin, this.textureBounds[1].uMax - this.textureBounds[1].uMin);
  52. this.sceneHeight /= Mathf.Max(this.textureBounds[0].vMax - this.textureBounds[0].vMin, this.textureBounds[1].vMax - this.textureBounds[1].vMin);
  53. this.aspect = this.tanHalfFov.x / this.tanHalfFov.y;
  54. this.fieldOfView = 2f * Mathf.Atan(this.tanHalfFov.y) * 57.29578f;
  55. this.eyes = new SteamVR_Utils.RigidTransform[]
  56. {
  57. new SteamVR_Utils.RigidTransform(this.hmd.GetEyeToHeadTransform(EVREye.Eye_Left)),
  58. new SteamVR_Utils.RigidTransform(this.hmd.GetEyeToHeadTransform(EVREye.Eye_Right))
  59. };
  60. if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL"))
  61. {
  62. this.graphicsAPI = EGraphicsAPIConvention.API_OpenGL;
  63. }
  64. else
  65. {
  66. this.graphicsAPI = EGraphicsAPIConvention.API_DirectX;
  67. }
  68. SteamVR_Utils.Event.Listen("initializing", new SteamVR_Utils.Event.Handler(this.OnInitializing));
  69. SteamVR_Utils.Event.Listen("calibrating", new SteamVR_Utils.Event.Handler(this.OnCalibrating));
  70. SteamVR_Utils.Event.Listen("out_of_range", new SteamVR_Utils.Event.Handler(this.OnOutOfRange));
  71. SteamVR_Utils.Event.Listen("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  72. SteamVR_Utils.Event.Listen("new_poses", new SteamVR_Utils.Event.Handler(this.OnNewPoses));
  73. }
  74. public static bool active
  75. {
  76. get
  77. {
  78. return SteamVR._instance != null;
  79. }
  80. }
  81. public static bool enabled
  82. {
  83. get
  84. {
  85. return SteamVR._enabled;
  86. }
  87. set
  88. {
  89. SteamVR._enabled = value;
  90. if (!SteamVR._enabled)
  91. {
  92. SteamVR.SafeDispose();
  93. }
  94. }
  95. }
  96. public static SteamVR instance
  97. {
  98. get
  99. {
  100. if (!SteamVR.enabled)
  101. {
  102. return null;
  103. }
  104. if (SteamVR._instance == null)
  105. {
  106. SteamVR._instance = SteamVR.CreateInstance();
  107. if (SteamVR._instance == null)
  108. {
  109. SteamVR._enabled = false;
  110. }
  111. }
  112. return SteamVR._instance;
  113. }
  114. }
  115. public static bool usingNativeSupport
  116. {
  117. get
  118. {
  119. return VRDevice.GetNativePtr() != IntPtr.Zero;
  120. }
  121. }
  122. private static SteamVR CreateInstance()
  123. {
  124. try
  125. {
  126. EVRInitError evrinitError = EVRInitError.None;
  127. if (!SteamVR.usingNativeSupport)
  128. {
  129. Debug.Log("OpenVR initialization failed. Ensure 'Virtual Reality Supported' is checked in Player Settings, and OpenVR is added to the list of Virtual Reality SDKs.");
  130. return null;
  131. }
  132. OpenVR.GetGenericInterface("IVRCompositor_014", ref evrinitError);
  133. if (evrinitError != EVRInitError.None)
  134. {
  135. SteamVR.ReportError(evrinitError);
  136. SteamVR.ShutdownSystems();
  137. return null;
  138. }
  139. OpenVR.GetGenericInterface("IVROverlay_011", ref evrinitError);
  140. if (evrinitError != EVRInitError.None)
  141. {
  142. SteamVR.ReportError(evrinitError);
  143. SteamVR.ShutdownSystems();
  144. return null;
  145. }
  146. }
  147. catch (Exception message)
  148. {
  149. Debug.LogError(message);
  150. return null;
  151. }
  152. return new SteamVR();
  153. }
  154. private static void ReportError(EVRInitError error)
  155. {
  156. if (error != EVRInitError.None)
  157. {
  158. if (error != EVRInitError.Init_VRClientDLLNotFound)
  159. {
  160. if (error != EVRInitError.Driver_RuntimeOutOfDate)
  161. {
  162. if (error != EVRInitError.VendorSpecific_UnableToConnectToOculusRuntime)
  163. {
  164. Debug.Log(OpenVR.GetStringForHmdError(error));
  165. }
  166. else
  167. {
  168. Debug.Log("SteamVR Initialization Failed! Make sure device is on, Oculus runtime is installed, and OVRService_*.exe is running.");
  169. }
  170. }
  171. else
  172. {
  173. Debug.Log("SteamVR Initialization Failed! Make sure device's runtime is up to date.");
  174. }
  175. }
  176. else
  177. {
  178. Debug.Log("SteamVR drivers not found! They can be installed via Steam under Library > Tools. Visit http://steampowered.com to install Steam.");
  179. }
  180. }
  181. }
  182. public CVRSystem hmd { get; private set; }
  183. public CVRCompositor compositor { get; private set; }
  184. public CVROverlay overlay { get; private set; }
  185. public static bool initializing { get; private set; }
  186. public static bool calibrating { get; private set; }
  187. public static bool outOfRange { get; private set; }
  188. public float sceneWidth { get; private set; }
  189. public float sceneHeight { get; private set; }
  190. public float aspect { get; private set; }
  191. public float fieldOfView { get; private set; }
  192. public Vector2 tanHalfFov { get; private set; }
  193. public VRTextureBounds_t[] textureBounds { get; private set; }
  194. public SteamVR_Utils.RigidTransform[] eyes { get; private set; }
  195. public string hmd_TrackingSystemName
  196. {
  197. get
  198. {
  199. return this.GetStringProperty(ETrackedDeviceProperty.Prop_TrackingSystemName_String);
  200. }
  201. }
  202. public string hmd_ModelNumber
  203. {
  204. get
  205. {
  206. return this.GetStringProperty(ETrackedDeviceProperty.Prop_ModelNumber_String);
  207. }
  208. }
  209. public string hmd_SerialNumber
  210. {
  211. get
  212. {
  213. return this.GetStringProperty(ETrackedDeviceProperty.Prop_SerialNumber_String);
  214. }
  215. }
  216. public float hmd_SecondsFromVsyncToPhotons
  217. {
  218. get
  219. {
  220. return this.GetFloatProperty(ETrackedDeviceProperty.Prop_SecondsFromVsyncToPhotons_Float);
  221. }
  222. }
  223. public float hmd_DisplayFrequency
  224. {
  225. get
  226. {
  227. return this.GetFloatProperty(ETrackedDeviceProperty.Prop_DisplayFrequency_Float);
  228. }
  229. }
  230. public string GetTrackedDeviceString(uint deviceId)
  231. {
  232. ETrackedPropertyError etrackedPropertyError = ETrackedPropertyError.TrackedProp_Success;
  233. uint stringTrackedDeviceProperty = this.hmd.GetStringTrackedDeviceProperty(deviceId, ETrackedDeviceProperty.Prop_AttachedDeviceId_String, null, 0u, ref etrackedPropertyError);
  234. if (stringTrackedDeviceProperty > 1u)
  235. {
  236. StringBuilder stringBuilder = new StringBuilder((int)stringTrackedDeviceProperty);
  237. this.hmd.GetStringTrackedDeviceProperty(deviceId, ETrackedDeviceProperty.Prop_AttachedDeviceId_String, stringBuilder, stringTrackedDeviceProperty, ref etrackedPropertyError);
  238. return stringBuilder.ToString();
  239. }
  240. return null;
  241. }
  242. private string GetStringProperty(ETrackedDeviceProperty prop)
  243. {
  244. ETrackedPropertyError etrackedPropertyError = ETrackedPropertyError.TrackedProp_Success;
  245. uint stringTrackedDeviceProperty = this.hmd.GetStringTrackedDeviceProperty(0u, prop, null, 0u, ref etrackedPropertyError);
  246. if (stringTrackedDeviceProperty > 1u)
  247. {
  248. StringBuilder stringBuilder = new StringBuilder((int)stringTrackedDeviceProperty);
  249. this.hmd.GetStringTrackedDeviceProperty(0u, prop, stringBuilder, stringTrackedDeviceProperty, ref etrackedPropertyError);
  250. return stringBuilder.ToString();
  251. }
  252. return (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) ? "<unknown>" : etrackedPropertyError.ToString();
  253. }
  254. private float GetFloatProperty(ETrackedDeviceProperty prop)
  255. {
  256. ETrackedPropertyError etrackedPropertyError = ETrackedPropertyError.TrackedProp_Success;
  257. return this.hmd.GetFloatTrackedDeviceProperty(0u, prop, ref etrackedPropertyError);
  258. }
  259. private void OnInitializing(params object[] args)
  260. {
  261. SteamVR.initializing = (bool)args[0];
  262. }
  263. private void OnCalibrating(params object[] args)
  264. {
  265. SteamVR.calibrating = (bool)args[0];
  266. }
  267. private void OnOutOfRange(params object[] args)
  268. {
  269. SteamVR.outOfRange = (bool)args[0];
  270. }
  271. private void OnDeviceConnected(params object[] args)
  272. {
  273. int num = (int)args[0];
  274. SteamVR.connected[num] = (bool)args[1];
  275. }
  276. private void OnNewPoses(params object[] args)
  277. {
  278. TrackedDevicePose_t[] array = (TrackedDevicePose_t[])args[0];
  279. this.eyes[0] = new SteamVR_Utils.RigidTransform(this.hmd.GetEyeToHeadTransform(EVREye.Eye_Left));
  280. this.eyes[1] = new SteamVR_Utils.RigidTransform(this.hmd.GetEyeToHeadTransform(EVREye.Eye_Right));
  281. for (int i = 0; i < array.Length; i++)
  282. {
  283. bool bDeviceIsConnected = array[i].bDeviceIsConnected;
  284. if (bDeviceIsConnected != SteamVR.connected[i])
  285. {
  286. SteamVR_Utils.Event.Send("device_connected", new object[]
  287. {
  288. i,
  289. bDeviceIsConnected
  290. });
  291. }
  292. }
  293. if ((long)array.Length > 0L)
  294. {
  295. ETrackingResult eTrackingResult = array[(int)((UIntPtr)0)].eTrackingResult;
  296. bool flag = eTrackingResult == ETrackingResult.Uninitialized;
  297. if (flag != SteamVR.initializing)
  298. {
  299. SteamVR_Utils.Event.Send("initializing", new object[]
  300. {
  301. flag
  302. });
  303. }
  304. bool flag2 = eTrackingResult == ETrackingResult.Calibrating_InProgress || eTrackingResult == ETrackingResult.Calibrating_OutOfRange;
  305. if (flag2 != SteamVR.calibrating)
  306. {
  307. SteamVR_Utils.Event.Send("calibrating", new object[]
  308. {
  309. flag2
  310. });
  311. }
  312. bool flag3 = eTrackingResult == ETrackingResult.Running_OutOfRange || eTrackingResult == ETrackingResult.Calibrating_OutOfRange;
  313. if (flag3 != SteamVR.outOfRange)
  314. {
  315. SteamVR_Utils.Event.Send("out_of_range", new object[]
  316. {
  317. flag3
  318. });
  319. }
  320. }
  321. }
  322. ~SteamVR()
  323. {
  324. this.Dispose(false);
  325. }
  326. public void Dispose()
  327. {
  328. this.Dispose(true);
  329. GC.SuppressFinalize(this);
  330. }
  331. private void Dispose(bool disposing)
  332. {
  333. SteamVR_Utils.Event.Remove("initializing", new SteamVR_Utils.Event.Handler(this.OnInitializing));
  334. SteamVR_Utils.Event.Remove("calibrating", new SteamVR_Utils.Event.Handler(this.OnCalibrating));
  335. SteamVR_Utils.Event.Remove("out_of_range", new SteamVR_Utils.Event.Handler(this.OnOutOfRange));
  336. SteamVR_Utils.Event.Remove("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  337. SteamVR_Utils.Event.Remove("new_poses", new SteamVR_Utils.Event.Handler(this.OnNewPoses));
  338. SteamVR.ShutdownSystems();
  339. SteamVR._instance = null;
  340. }
  341. private static void ShutdownSystems()
  342. {
  343. }
  344. public static void SafeDispose()
  345. {
  346. if (SteamVR._instance != null)
  347. {
  348. SteamVR._instance.Dispose();
  349. }
  350. }
  351. private static bool _enabled = true;
  352. private static SteamVR _instance;
  353. public static bool[] connected = new bool[16];
  354. public EGraphicsAPIConvention graphicsAPI;
  355. }