SteamVR_Render.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. using Valve.VR;
  7. public class SteamVR_Render : MonoBehaviour
  8. {
  9. public static EVREye eye { get; private set; }
  10. public static SteamVR_Render instance
  11. {
  12. get
  13. {
  14. if (SteamVR_Render._instance == null)
  15. {
  16. SteamVR_Render._instance = UnityEngine.Object.FindObjectOfType<SteamVR_Render>();
  17. if (SteamVR_Render._instance == null)
  18. {
  19. SteamVR_Render._instance = new GameObject("[SteamVR]").AddComponent<SteamVR_Render>();
  20. }
  21. }
  22. return SteamVR_Render._instance;
  23. }
  24. }
  25. private void OnDestroy()
  26. {
  27. SteamVR_Render._instance = null;
  28. }
  29. private void OnApplicationQuit()
  30. {
  31. SteamVR_Render.isQuitting = true;
  32. SteamVR.SafeDispose();
  33. }
  34. public static void Add(SteamVR_Camera vrcam)
  35. {
  36. if (!SteamVR_Render.isQuitting)
  37. {
  38. SteamVR_Render.instance.AddInternal(vrcam);
  39. }
  40. }
  41. public static void Remove(SteamVR_Camera vrcam)
  42. {
  43. if (!SteamVR_Render.isQuitting && SteamVR_Render._instance != null)
  44. {
  45. SteamVR_Render.instance.RemoveInternal(vrcam);
  46. }
  47. }
  48. public static SteamVR_Camera Top()
  49. {
  50. if (!SteamVR_Render.isQuitting)
  51. {
  52. return SteamVR_Render.instance.TopInternal();
  53. }
  54. return null;
  55. }
  56. private void AddInternal(SteamVR_Camera vrcam)
  57. {
  58. Camera component = vrcam.GetComponent<Camera>();
  59. int num = this.cameras.Length;
  60. SteamVR_Camera[] array = new SteamVR_Camera[num + 1];
  61. int num2 = 0;
  62. for (int i = 0; i < num; i++)
  63. {
  64. Camera component2 = this.cameras[i].GetComponent<Camera>();
  65. if (i == num2 && component2.depth > component.depth)
  66. {
  67. array[num2++] = vrcam;
  68. }
  69. array[num2++] = this.cameras[i];
  70. }
  71. if (num2 == num)
  72. {
  73. array[num2] = vrcam;
  74. }
  75. this.cameras = array;
  76. }
  77. private void RemoveInternal(SteamVR_Camera vrcam)
  78. {
  79. int num = this.cameras.Length;
  80. int num2 = 0;
  81. for (int i = 0; i < num; i++)
  82. {
  83. SteamVR_Camera x = this.cameras[i];
  84. if (x == vrcam)
  85. {
  86. num2++;
  87. }
  88. }
  89. if (num2 == 0)
  90. {
  91. return;
  92. }
  93. SteamVR_Camera[] array = new SteamVR_Camera[num - num2];
  94. int num3 = 0;
  95. for (int j = 0; j < num; j++)
  96. {
  97. SteamVR_Camera steamVR_Camera = this.cameras[j];
  98. if (steamVR_Camera != vrcam)
  99. {
  100. array[num3++] = steamVR_Camera;
  101. }
  102. }
  103. this.cameras = array;
  104. }
  105. private SteamVR_Camera TopInternal()
  106. {
  107. if (this.cameras.Length > 0)
  108. {
  109. return this.cameras[this.cameras.Length - 1];
  110. }
  111. return null;
  112. }
  113. public static bool pauseRendering
  114. {
  115. get
  116. {
  117. return SteamVR_Render._pauseRendering;
  118. }
  119. set
  120. {
  121. SteamVR_Render._pauseRendering = value;
  122. CVRCompositor compositor = OpenVR.Compositor;
  123. if (compositor != null)
  124. {
  125. compositor.SuspendRendering(value);
  126. }
  127. }
  128. }
  129. private IEnumerator RenderLoop()
  130. {
  131. for (;;)
  132. {
  133. yield return new WaitForEndOfFrame();
  134. if (!SteamVR_Render.pauseRendering)
  135. {
  136. CVRCompositor compositor = OpenVR.Compositor;
  137. if (compositor != null)
  138. {
  139. if (!compositor.CanRenderScene())
  140. {
  141. continue;
  142. }
  143. compositor.SetTrackingSpace(this.trackingSpace);
  144. }
  145. SteamVR_Overlay overlay = SteamVR_Overlay.instance;
  146. if (overlay != null)
  147. {
  148. overlay.UpdateOverlay();
  149. }
  150. this.RenderExternalCamera();
  151. }
  152. }
  153. yield break;
  154. }
  155. private void RenderExternalCamera()
  156. {
  157. if (this.externalCamera == null)
  158. {
  159. return;
  160. }
  161. if (!this.externalCamera.gameObject.activeInHierarchy)
  162. {
  163. return;
  164. }
  165. int num = (int)Mathf.Max(this.externalCamera.config.frameSkip, 0f);
  166. if (Time.frameCount % (num + 1) != 0)
  167. {
  168. return;
  169. }
  170. this.externalCamera.AttachToCamera(this.TopInternal());
  171. this.externalCamera.RenderNear();
  172. this.externalCamera.RenderFar();
  173. }
  174. private void OnInputFocus(params object[] args)
  175. {
  176. bool flag = (bool)args[0];
  177. if (flag)
  178. {
  179. if (this.pauseGameWhenDashboardIsVisible)
  180. {
  181. Time.timeScale = this.timeScale;
  182. }
  183. SteamVR_Camera.sceneResolutionScale = this.sceneResolutionScale;
  184. }
  185. else
  186. {
  187. if (this.pauseGameWhenDashboardIsVisible)
  188. {
  189. this.timeScale = Time.timeScale;
  190. Time.timeScale = 0f;
  191. }
  192. this.sceneResolutionScale = SteamVR_Camera.sceneResolutionScale;
  193. SteamVR_Camera.sceneResolutionScale = 0.5f;
  194. }
  195. }
  196. private void OnQuit(params object[] args)
  197. {
  198. Application.Quit();
  199. }
  200. private void OnEnable()
  201. {
  202. base.StartCoroutine("RenderLoop");
  203. SteamVR_Utils.Event.Listen("input_focus", new SteamVR_Utils.Event.Handler(this.OnInputFocus));
  204. SteamVR_Utils.Event.Listen("Quit", new SteamVR_Utils.Event.Handler(this.OnQuit));
  205. }
  206. private void OnDisable()
  207. {
  208. base.StopAllCoroutines();
  209. SteamVR_Utils.Event.Remove("input_focus", new SteamVR_Utils.Event.Handler(this.OnInputFocus));
  210. SteamVR_Utils.Event.Remove("Quit", new SteamVR_Utils.Event.Handler(this.OnQuit));
  211. }
  212. private void Awake()
  213. {
  214. if (this.externalCamera == null && File.Exists(this.externalCameraConfigPath))
  215. {
  216. GameObject original = Resources.Load<GameObject>("SteamVR_ExternalCamera");
  217. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(original);
  218. gameObject.gameObject.name = "External Camera";
  219. this.externalCamera = gameObject.transform.GetChild(0).GetComponent<SteamVR_ExternalCamera>();
  220. this.externalCamera.configPath = this.externalCameraConfigPath;
  221. this.externalCamera.ReadConfig();
  222. }
  223. }
  224. private void FixedUpdate()
  225. {
  226. }
  227. private void Update()
  228. {
  229. if (this.poseUpdater == null)
  230. {
  231. this.poseUpdater = new GameObject("poseUpdater")
  232. {
  233. transform =
  234. {
  235. parent = base.transform
  236. }
  237. }.AddComponent<SteamVR_UpdatePoses>();
  238. }
  239. SteamVR_Controller.Update();
  240. CVRSystem system = OpenVR.System;
  241. if (system != null)
  242. {
  243. VREvent_t vrevent_t = default(VREvent_t);
  244. uint uncbVREvent = (uint)Marshal.SizeOf(typeof(VREvent_t));
  245. for (int i = 0; i < 64; i++)
  246. {
  247. if (!system.PollNextEvent(ref vrevent_t, uncbVREvent))
  248. {
  249. break;
  250. }
  251. EVREventType eventType = (EVREventType)vrevent_t.eventType;
  252. if (eventType != EVREventType.VREvent_InputFocusCaptured)
  253. {
  254. if (eventType != EVREventType.VREvent_InputFocusReleased)
  255. {
  256. if (eventType != EVREventType.VREvent_HideRenderModels)
  257. {
  258. if (eventType != EVREventType.VREvent_ShowRenderModels)
  259. {
  260. string name = Enum.GetName(typeof(EVREventType), vrevent_t.eventType);
  261. if (name != null)
  262. {
  263. SteamVR_Utils.Event.Send(name.Substring(8), new object[]
  264. {
  265. vrevent_t
  266. });
  267. }
  268. }
  269. else
  270. {
  271. SteamVR_Utils.Event.Send("hide_render_models", new object[]
  272. {
  273. false
  274. });
  275. }
  276. }
  277. else
  278. {
  279. SteamVR_Utils.Event.Send("hide_render_models", new object[]
  280. {
  281. true
  282. });
  283. }
  284. }
  285. else if (vrevent_t.data.process.pid == 0U)
  286. {
  287. SteamVR_Utils.Event.Send("input_focus", new object[]
  288. {
  289. true
  290. });
  291. }
  292. }
  293. else if (vrevent_t.data.process.oldPid == 0U)
  294. {
  295. SteamVR_Utils.Event.Send("input_focus", new object[]
  296. {
  297. false
  298. });
  299. }
  300. }
  301. }
  302. Application.targetFrameRate = -1;
  303. Application.runInBackground = true;
  304. QualitySettings.maxQueuedFrames = -1;
  305. QualitySettings.vSyncCount = 0;
  306. if (this.lockPhysicsUpdateRateToRenderFrequency && Time.timeScale > 0f)
  307. {
  308. SteamVR instance = SteamVR.instance;
  309. if (instance != null)
  310. {
  311. Compositor_FrameTiming compositor_FrameTiming = default(Compositor_FrameTiming);
  312. compositor_FrameTiming.m_nSize = (uint)Marshal.SizeOf(typeof(Compositor_FrameTiming));
  313. instance.compositor.GetFrameTiming(ref compositor_FrameTiming, 0U);
  314. Time.fixedDeltaTime = Time.timeScale / instance.hmd_DisplayFrequency;
  315. Time.maximumDeltaTime = Time.fixedDeltaTime * compositor_FrameTiming.m_nNumFramePresents;
  316. }
  317. }
  318. }
  319. public bool pauseGameWhenDashboardIsVisible = true;
  320. public bool lockPhysicsUpdateRateToRenderFrequency = true;
  321. public SteamVR_ExternalCamera externalCamera;
  322. public string externalCameraConfigPath = "externalcamera.cfg";
  323. public ETrackingUniverseOrigin trackingSpace = ETrackingUniverseOrigin.TrackingUniverseStanding;
  324. private static SteamVR_Render _instance;
  325. private static bool isQuitting;
  326. private SteamVR_Camera[] cameras = new SteamVR_Camera[0];
  327. public TrackedDevicePose_t[] poses = new TrackedDevicePose_t[16];
  328. public TrackedDevicePose_t[] gamePoses = new TrackedDevicePose_t[0];
  329. private static bool _pauseRendering;
  330. private float sceneResolutionScale = 1f;
  331. private float timeScale = 1f;
  332. private SteamVR_UpdatePoses poseUpdater;
  333. }