SteamVR_Menu.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using UnityEngine;
  3. using Valve.VR;
  4. public class SteamVR_Menu : MonoBehaviour
  5. {
  6. public RenderTexture texture
  7. {
  8. get
  9. {
  10. return (!this.overlay) ? null : (this.overlay.texture as RenderTexture);
  11. }
  12. }
  13. public float scale { get; private set; }
  14. private void Awake()
  15. {
  16. this.scaleLimitX = string.Format("{0:N1}", this.scaleLimits.x);
  17. this.scaleLimitY = string.Format("{0:N1}", this.scaleLimits.y);
  18. this.scaleRateText = string.Format("{0:N1}", this.scaleRate);
  19. SteamVR_Overlay instance = SteamVR_Overlay.instance;
  20. if (instance != null)
  21. {
  22. this.uvOffset = instance.uvOffset;
  23. this.distance = instance.distance;
  24. }
  25. }
  26. private void OnGUI()
  27. {
  28. if (this.overlay == null)
  29. {
  30. return;
  31. }
  32. RenderTexture renderTexture = this.overlay.texture as RenderTexture;
  33. RenderTexture active = RenderTexture.active;
  34. RenderTexture.active = renderTexture;
  35. if (Event.current.type == EventType.Repaint)
  36. {
  37. GL.Clear(false, true, Color.clear);
  38. }
  39. Rect screenRect = new Rect(0f, 0f, (float)renderTexture.width, (float)renderTexture.height);
  40. if (Screen.width < renderTexture.width)
  41. {
  42. screenRect.width = (float)Screen.width;
  43. this.overlay.uvOffset.x = -(float)(renderTexture.width - Screen.width) / (float)(2 * renderTexture.width);
  44. }
  45. if (Screen.height < renderTexture.height)
  46. {
  47. screenRect.height = (float)Screen.height;
  48. this.overlay.uvOffset.y = (float)(renderTexture.height - Screen.height) / (float)(2 * renderTexture.height);
  49. }
  50. GUILayout.BeginArea(screenRect);
  51. if (this.background != null)
  52. {
  53. GUI.DrawTexture(new Rect((screenRect.width - (float)this.background.width) / 2f, (screenRect.height - (float)this.background.height) / 2f, (float)this.background.width, (float)this.background.height), this.background);
  54. }
  55. GUILayout.BeginHorizontal(new GUILayoutOption[0]);
  56. GUILayout.FlexibleSpace();
  57. GUILayout.BeginVertical(new GUILayoutOption[0]);
  58. if (this.logo != null)
  59. {
  60. GUILayout.Space(screenRect.height / 2f - this.logoHeight);
  61. GUILayout.Box(this.logo, new GUILayoutOption[0]);
  62. }
  63. GUILayout.Space(this.menuOffset);
  64. bool flag = GUILayout.Button("[Esc] - Close menu", new GUILayoutOption[0]);
  65. GUILayout.BeginHorizontal(new GUILayoutOption[0]);
  66. GUILayout.Label(string.Format("Scale: {0:N4}", this.scale), new GUILayoutOption[0]);
  67. float num = GUILayout.HorizontalSlider(this.scale, this.scaleLimits.x, this.scaleLimits.y, new GUILayoutOption[0]);
  68. if (num != this.scale)
  69. {
  70. this.SetScale(num);
  71. }
  72. GUILayout.EndHorizontal();
  73. GUILayout.BeginHorizontal(new GUILayoutOption[0]);
  74. GUILayout.Label(string.Format("Scale limits:", new object[0]), new GUILayoutOption[0]);
  75. string text = GUILayout.TextField(this.scaleLimitX, new GUILayoutOption[0]);
  76. if (text != this.scaleLimitX && float.TryParse(text, out this.scaleLimits.x))
  77. {
  78. this.scaleLimitX = text;
  79. }
  80. string text2 = GUILayout.TextField(this.scaleLimitY, new GUILayoutOption[0]);
  81. if (text2 != this.scaleLimitY && float.TryParse(text2, out this.scaleLimits.y))
  82. {
  83. this.scaleLimitY = text2;
  84. }
  85. GUILayout.EndHorizontal();
  86. GUILayout.BeginHorizontal(new GUILayoutOption[0]);
  87. GUILayout.Label(string.Format("Scale rate:", new object[0]), new GUILayoutOption[0]);
  88. string text3 = GUILayout.TextField(this.scaleRateText, new GUILayoutOption[0]);
  89. if (text3 != this.scaleRateText && float.TryParse(text3, out this.scaleRate))
  90. {
  91. this.scaleRateText = text3;
  92. }
  93. GUILayout.EndHorizontal();
  94. if (SteamVR.active)
  95. {
  96. SteamVR instance = SteamVR.instance;
  97. GUILayout.BeginHorizontal(new GUILayoutOption[0]);
  98. float sceneResolutionScale = SteamVR_Camera.sceneResolutionScale;
  99. int num2 = (int)(instance.sceneWidth * sceneResolutionScale);
  100. int num3 = (int)(instance.sceneHeight * sceneResolutionScale);
  101. int num4 = (int)(100f * sceneResolutionScale);
  102. GUILayout.Label(string.Format("Scene quality: {0}x{1} ({2}%)", num2, num3, num4), new GUILayoutOption[0]);
  103. int num5 = Mathf.RoundToInt(GUILayout.HorizontalSlider((float)num4, 50f, 200f, new GUILayoutOption[0]));
  104. if (num5 != num4)
  105. {
  106. SteamVR_Camera.sceneResolutionScale = (float)num5 / 100f;
  107. }
  108. GUILayout.EndHorizontal();
  109. }
  110. this.overlay.highquality = GUILayout.Toggle(this.overlay.highquality, "High quality", new GUILayoutOption[0]);
  111. if (this.overlay.highquality)
  112. {
  113. this.overlay.curved = GUILayout.Toggle(this.overlay.curved, "Curved overlay", new GUILayoutOption[0]);
  114. this.overlay.antialias = GUILayout.Toggle(this.overlay.antialias, "Overlay RGSS(2x2)", new GUILayoutOption[0]);
  115. }
  116. else
  117. {
  118. this.overlay.curved = false;
  119. this.overlay.antialias = false;
  120. }
  121. SteamVR_Camera steamVR_Camera = SteamVR_Render.Top();
  122. if (steamVR_Camera != null)
  123. {
  124. steamVR_Camera.wireframe = GUILayout.Toggle(steamVR_Camera.wireframe, "Wireframe", new GUILayoutOption[0]);
  125. SteamVR_Render instance2 = SteamVR_Render.instance;
  126. if (instance2.trackingSpace == ETrackingUniverseOrigin.TrackingUniverseSeated)
  127. {
  128. if (GUILayout.Button("Switch to Standing", new GUILayoutOption[0]))
  129. {
  130. instance2.trackingSpace = ETrackingUniverseOrigin.TrackingUniverseStanding;
  131. }
  132. if (GUILayout.Button("Center View", new GUILayoutOption[0]))
  133. {
  134. CVRSystem system = OpenVR.System;
  135. if (system != null)
  136. {
  137. system.ResetSeatedZeroPose();
  138. }
  139. }
  140. }
  141. else if (GUILayout.Button("Switch to Seated", new GUILayoutOption[0]))
  142. {
  143. instance2.trackingSpace = ETrackingUniverseOrigin.TrackingUniverseSeated;
  144. }
  145. }
  146. if (GUILayout.Button("Exit", new GUILayoutOption[0]))
  147. {
  148. Application.Quit();
  149. }
  150. GUILayout.Space(this.menuOffset);
  151. string environmentVariable = Environment.GetEnvironmentVariable("VR_OVERRIDE");
  152. if (environmentVariable != null)
  153. {
  154. GUILayout.Label("VR_OVERRIDE=" + environmentVariable, new GUILayoutOption[0]);
  155. }
  156. GUILayout.Label("Graphics device: " + SystemInfo.graphicsDeviceVersion, new GUILayoutOption[0]);
  157. GUILayout.EndVertical();
  158. GUILayout.FlexibleSpace();
  159. GUILayout.EndHorizontal();
  160. GUILayout.EndArea();
  161. if (this.cursor != null)
  162. {
  163. float x = Input.mousePosition.x;
  164. float y = (float)Screen.height - Input.mousePosition.y;
  165. float width = (float)this.cursor.width;
  166. float height = (float)this.cursor.height;
  167. GUI.DrawTexture(new Rect(x, y, width, height), this.cursor);
  168. }
  169. RenderTexture.active = active;
  170. if (flag)
  171. {
  172. this.HideMenu();
  173. }
  174. }
  175. public void ShowMenu()
  176. {
  177. SteamVR_Overlay instance = SteamVR_Overlay.instance;
  178. if (instance == null)
  179. {
  180. return;
  181. }
  182. RenderTexture renderTexture = instance.texture as RenderTexture;
  183. if (renderTexture == null)
  184. {
  185. Debug.LogError("Menu requires overlay texture to be a render texture.");
  186. return;
  187. }
  188. this.SaveCursorState();
  189. Cursor.visible = true;
  190. Cursor.lockState = CursorLockMode.None;
  191. this.overlay = instance;
  192. this.uvOffset = instance.uvOffset;
  193. this.distance = instance.distance;
  194. Camera[] array = UnityEngine.Object.FindObjectsOfType(typeof(Camera)) as Camera[];
  195. foreach (Camera camera in array)
  196. {
  197. if (camera.enabled && camera.targetTexture == renderTexture)
  198. {
  199. this.overlayCam = camera;
  200. this.overlayCam.enabled = false;
  201. break;
  202. }
  203. }
  204. SteamVR_Camera steamVR_Camera = SteamVR_Render.Top();
  205. if (steamVR_Camera != null)
  206. {
  207. this.scale = steamVR_Camera.origin.localScale.x;
  208. }
  209. }
  210. public void HideMenu()
  211. {
  212. this.RestoreCursorState();
  213. if (this.overlayCam != null)
  214. {
  215. this.overlayCam.enabled = true;
  216. }
  217. if (this.overlay != null)
  218. {
  219. this.overlay.uvOffset = this.uvOffset;
  220. this.overlay.distance = this.distance;
  221. this.overlay = null;
  222. }
  223. }
  224. private void SetScale(float scale)
  225. {
  226. this.scale = scale;
  227. SteamVR_Camera steamVR_Camera = SteamVR_Render.Top();
  228. if (steamVR_Camera != null)
  229. {
  230. steamVR_Camera.origin.localScale = new Vector3(scale, scale, scale);
  231. }
  232. }
  233. private void SaveCursorState()
  234. {
  235. this.savedCursorVisible = Cursor.visible;
  236. this.savedCursorLockState = Cursor.lockState;
  237. }
  238. private void RestoreCursorState()
  239. {
  240. Cursor.visible = this.savedCursorVisible;
  241. Cursor.lockState = this.savedCursorLockState;
  242. }
  243. public Texture cursor;
  244. public Texture background;
  245. public Texture logo;
  246. public float logoHeight;
  247. public float menuOffset;
  248. public Vector2 scaleLimits = new Vector2(0.1f, 5f);
  249. public float scaleRate = 0.5f;
  250. private SteamVR_Overlay overlay;
  251. private Camera overlayCam;
  252. private Vector4 uvOffset;
  253. private float distance;
  254. private string scaleLimitX;
  255. private string scaleLimitY;
  256. private string scaleRateText;
  257. private CursorLockMode savedCursorLockState;
  258. private bool savedCursorVisible;
  259. }