OVRDebugInfo.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.VR;
  5. public class OVRDebugInfo : MonoBehaviour
  6. {
  7. private void Awake()
  8. {
  9. this.debugUIManager = new GameObject();
  10. this.debugUIManager.name = "DebugUIManager";
  11. this.debugUIManager.transform.parent = GameObject.Find("LeftEyeAnchor").transform;
  12. RectTransform rectTransform = this.debugUIManager.AddComponent<RectTransform>();
  13. rectTransform.sizeDelta = new Vector2(100f, 100f);
  14. rectTransform.localScale = new Vector3(0.001f, 0.001f, 0.001f);
  15. rectTransform.localPosition = new Vector3(0.01f, 0.17f, 0.53f);
  16. rectTransform.localEulerAngles = Vector3.zero;
  17. Canvas canvas = this.debugUIManager.AddComponent<Canvas>();
  18. canvas.renderMode = RenderMode.WorldSpace;
  19. canvas.pixelPerfect = false;
  20. }
  21. private void Update()
  22. {
  23. if (this.initUIComponent && !this.isInited)
  24. {
  25. this.InitUIComponents();
  26. }
  27. if (Input.GetKeyDown(KeyCode.Space) && this.riftPresentTimeout < 0f)
  28. {
  29. this.initUIComponent = true;
  30. this.showVRVars ^= true;
  31. }
  32. this.UpdateDeviceDetection();
  33. if (this.showVRVars)
  34. {
  35. this.debugUIManager.SetActive(true);
  36. this.UpdateVariable();
  37. this.UpdateStrings();
  38. }
  39. else
  40. {
  41. this.debugUIManager.SetActive(false);
  42. }
  43. }
  44. private void OnDestroy()
  45. {
  46. this.isInited = false;
  47. }
  48. private void InitUIComponents()
  49. {
  50. float num = 0f;
  51. int fontSize = 20;
  52. this.debugUIObject = new GameObject();
  53. this.debugUIObject.name = "DebugInfo";
  54. this.debugUIObject.transform.parent = GameObject.Find("DebugUIManager").transform;
  55. this.debugUIObject.transform.localPosition = new Vector3(0f, 100f, 0f);
  56. this.debugUIObject.transform.localEulerAngles = Vector3.zero;
  57. this.debugUIObject.transform.localScale = new Vector3(1f, 1f, 1f);
  58. if (!string.IsNullOrEmpty(this.strFPS))
  59. {
  60. this.fps = this.VariableObjectManager(this.fps, "FPS", num -= this.offsetY, this.strFPS, fontSize);
  61. }
  62. if (!string.IsNullOrEmpty(this.strIPD))
  63. {
  64. this.ipd = this.VariableObjectManager(this.ipd, "IPD", num -= this.offsetY, this.strIPD, fontSize);
  65. }
  66. if (!string.IsNullOrEmpty(this.strFOV))
  67. {
  68. this.fov = this.VariableObjectManager(this.fov, "FOV", num -= this.offsetY, this.strFOV, fontSize);
  69. }
  70. if (!string.IsNullOrEmpty(this.strHeight))
  71. {
  72. this.height = this.VariableObjectManager(this.height, "Height", num -= this.offsetY, this.strHeight, fontSize);
  73. }
  74. if (!string.IsNullOrEmpty(this.strDepth))
  75. {
  76. this.depth = this.VariableObjectManager(this.depth, "Depth", num -= this.offsetY, this.strDepth, fontSize);
  77. }
  78. if (!string.IsNullOrEmpty(this.strResolutionEyeTexture))
  79. {
  80. this.resolutionEyeTexture = this.VariableObjectManager(this.resolutionEyeTexture, "Resolution", num -= this.offsetY, this.strResolutionEyeTexture, fontSize);
  81. }
  82. if (!string.IsNullOrEmpty(this.strLatencies))
  83. {
  84. this.latencies = this.VariableObjectManager(this.latencies, "Latency", num - this.offsetY, this.strLatencies, 17);
  85. }
  86. this.initUIComponent = false;
  87. this.isInited = true;
  88. }
  89. private void UpdateVariable()
  90. {
  91. this.UpdateIPD();
  92. this.UpdateEyeHeightOffset();
  93. this.UpdateEyeDepthOffset();
  94. this.UpdateFOV();
  95. this.UpdateResolutionEyeTexture();
  96. this.UpdateLatencyValues();
  97. this.UpdateFPS();
  98. }
  99. private void UpdateStrings()
  100. {
  101. if (this.debugUIObject == null)
  102. {
  103. return;
  104. }
  105. if (!string.IsNullOrEmpty(this.strFPS))
  106. {
  107. this.fps.GetComponentInChildren<Text>().text = this.strFPS;
  108. }
  109. if (!string.IsNullOrEmpty(this.strIPD))
  110. {
  111. this.ipd.GetComponentInChildren<Text>().text = this.strIPD;
  112. }
  113. if (!string.IsNullOrEmpty(this.strFOV))
  114. {
  115. this.fov.GetComponentInChildren<Text>().text = this.strFOV;
  116. }
  117. if (!string.IsNullOrEmpty(this.strResolutionEyeTexture))
  118. {
  119. this.resolutionEyeTexture.GetComponentInChildren<Text>().text = this.strResolutionEyeTexture;
  120. }
  121. if (!string.IsNullOrEmpty(this.strLatencies))
  122. {
  123. this.latencies.GetComponentInChildren<Text>().text = this.strLatencies;
  124. this.latencies.GetComponentInChildren<Text>().fontSize = 14;
  125. }
  126. if (!string.IsNullOrEmpty(this.strHeight))
  127. {
  128. this.height.GetComponentInChildren<Text>().text = this.strHeight;
  129. }
  130. if (!string.IsNullOrEmpty(this.strDepth))
  131. {
  132. this.depth.GetComponentInChildren<Text>().text = this.strDepth;
  133. }
  134. }
  135. private void RiftPresentGUI(GameObject guiMainOBj)
  136. {
  137. this.riftPresent = this.ComponentComposition(this.riftPresent);
  138. this.riftPresent.transform.SetParent(guiMainOBj.transform);
  139. this.riftPresent.name = "RiftPresent";
  140. RectTransform component = this.riftPresent.GetComponent<RectTransform>();
  141. component.localPosition = new Vector3(0f, 0f, 0f);
  142. component.localScale = new Vector3(1f, 1f, 1f);
  143. component.localEulerAngles = Vector3.zero;
  144. Text componentInChildren = this.riftPresent.GetComponentInChildren<Text>();
  145. componentInChildren.text = this.strRiftPresent;
  146. componentInChildren.fontSize = 20;
  147. }
  148. private void UpdateDeviceDetection()
  149. {
  150. if (this.riftPresentTimeout >= 0f)
  151. {
  152. this.riftPresentTimeout -= Time.deltaTime;
  153. }
  154. }
  155. private GameObject VariableObjectManager(GameObject gameObject, string name, float posY, string str, int fontSize)
  156. {
  157. gameObject = this.ComponentComposition(gameObject);
  158. gameObject.name = name;
  159. gameObject.transform.SetParent(this.debugUIObject.transform);
  160. RectTransform component = gameObject.GetComponent<RectTransform>();
  161. component.localPosition = new Vector3(0f, posY -= this.offsetY, 0f);
  162. Text componentInChildren = gameObject.GetComponentInChildren<Text>();
  163. componentInChildren.text = str;
  164. componentInChildren.fontSize = fontSize;
  165. gameObject.transform.localEulerAngles = Vector3.zero;
  166. component.localScale = new Vector3(1f, 1f, 1f);
  167. return gameObject;
  168. }
  169. private GameObject ComponentComposition(GameObject GO)
  170. {
  171. GO = new GameObject();
  172. GO.AddComponent<RectTransform>();
  173. GO.AddComponent<CanvasRenderer>();
  174. GO.AddComponent<Image>();
  175. GO.GetComponent<RectTransform>().sizeDelta = new Vector2(350f, 50f);
  176. GO.GetComponent<Image>().color = new Color(0.02745098f, 0.1764706f, 0.2784314f, 0.78431374f);
  177. this.texts = new GameObject();
  178. this.texts.AddComponent<RectTransform>();
  179. this.texts.AddComponent<CanvasRenderer>();
  180. this.texts.AddComponent<Text>();
  181. this.texts.GetComponent<RectTransform>().sizeDelta = new Vector2(350f, 50f);
  182. this.texts.GetComponent<Text>().font = (Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font);
  183. this.texts.GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
  184. this.texts.transform.SetParent(GO.transform);
  185. this.texts.name = "TextBox";
  186. return GO;
  187. }
  188. private void UpdateIPD()
  189. {
  190. this.strIPD = string.Format("IPD (mm): {0:F4}", OVRManager.profile.ipd * 1000f);
  191. }
  192. private void UpdateEyeHeightOffset()
  193. {
  194. float eyeHeight = OVRManager.profile.eyeHeight;
  195. this.strHeight = string.Format("Eye Height (m): {0:F3}", eyeHeight);
  196. }
  197. private void UpdateEyeDepthOffset()
  198. {
  199. float eyeDepth = OVRManager.profile.eyeDepth;
  200. this.strDepth = string.Format("Eye Depth (m): {0:F3}", eyeDepth);
  201. }
  202. private void UpdateFOV()
  203. {
  204. this.strFOV = string.Format("FOV (deg): {0:F3}", OVRManager.display.GetEyeRenderDesc(VRNode.LeftEye).fov.y);
  205. }
  206. private void UpdateResolutionEyeTexture()
  207. {
  208. OVRDisplay.EyeRenderDesc eyeRenderDesc = OVRManager.display.GetEyeRenderDesc(VRNode.LeftEye);
  209. OVRDisplay.EyeRenderDesc eyeRenderDesc2 = OVRManager.display.GetEyeRenderDesc(VRNode.RightEye);
  210. float renderScale = VRSettings.renderScale;
  211. float num = (float)((int)(renderScale * (eyeRenderDesc.resolution.x + eyeRenderDesc2.resolution.x)));
  212. float num2 = (float)((int)(renderScale * Mathf.Max(eyeRenderDesc.resolution.y, eyeRenderDesc2.resolution.y)));
  213. this.strResolutionEyeTexture = string.Format("Resolution : {0} x {1}", num, num2);
  214. }
  215. private void UpdateLatencyValues()
  216. {
  217. OVRDisplay.LatencyData latency = OVRManager.display.latency;
  218. if (latency.render < 1E-06f && latency.timeWarp < 1E-06f && latency.postPresent < 1E-06f)
  219. {
  220. this.strLatencies = string.Format("Latency values are not available.", new object[0]);
  221. }
  222. else
  223. {
  224. this.strLatencies = string.Format("Render: {0:F3} TimeWarp: {1:F3} Post-Present: {2:F3}\nRender Error: {3:F3} TimeWarp Error: {4:F3}", new object[]
  225. {
  226. latency.render,
  227. latency.timeWarp,
  228. latency.postPresent,
  229. latency.renderError,
  230. latency.timeWarpError
  231. });
  232. }
  233. }
  234. private void UpdateFPS()
  235. {
  236. this.timeLeft -= Time.unscaledDeltaTime;
  237. this.accum += Time.unscaledDeltaTime;
  238. this.frames++;
  239. if ((double)this.timeLeft <= 0.0)
  240. {
  241. float num = (float)this.frames / this.accum;
  242. this.strFPS = string.Format("FPS: {0:F2}", num);
  243. this.timeLeft += this.updateInterval;
  244. this.accum = 0f;
  245. this.frames = 0;
  246. }
  247. }
  248. private GameObject debugUIManager;
  249. private GameObject debugUIObject;
  250. private GameObject riftPresent;
  251. private GameObject fps;
  252. private GameObject ipd;
  253. private GameObject fov;
  254. private GameObject height;
  255. private GameObject depth;
  256. private GameObject resolutionEyeTexture;
  257. private GameObject latencies;
  258. private GameObject texts;
  259. private string strRiftPresent;
  260. private string strFPS;
  261. private string strIPD;
  262. private string strFOV;
  263. private string strHeight;
  264. private string strDepth;
  265. private string strResolutionEyeTexture;
  266. private string strLatencies;
  267. private float updateInterval = 0.5f;
  268. private float accum;
  269. private int frames;
  270. private float timeLeft;
  271. private bool initUIComponent;
  272. private bool isInited;
  273. private float offsetY = 55f;
  274. private float riftPresentTimeout;
  275. private bool showVRVars;
  276. }