SteamVR_Menu.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Example menu using OnGUI with SteamVR_Camera's overlay support
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using Valve.VR;
  8. namespace Valve.VR
  9. {
  10. public class SteamVR_Menu : MonoBehaviour
  11. {
  12. public Texture cursor, background, logo;
  13. public float logoHeight, menuOffset;
  14. public Vector2 scaleLimits = new Vector2(0.1f, 5.0f);
  15. public float scaleRate = 0.5f;
  16. SteamVR_Overlay overlay;
  17. Camera overlayCam;
  18. Vector4 uvOffset;
  19. float distance;
  20. public RenderTexture texture { get { return overlay ? overlay.texture as RenderTexture : null; } }
  21. public float scale { get; private set; }
  22. string scaleLimitX, scaleLimitY, scaleRateText;
  23. CursorLockMode savedCursorLockState;
  24. bool savedCursorVisible;
  25. void Awake()
  26. {
  27. scaleLimitX = string.Format("{0:N1}", scaleLimits.x);
  28. scaleLimitY = string.Format("{0:N1}", scaleLimits.y);
  29. scaleRateText = string.Format("{0:N1}", scaleRate);
  30. var overlay = SteamVR_Overlay.instance;
  31. if (overlay != null)
  32. {
  33. uvOffset = overlay.uvOffset;
  34. distance = overlay.distance;
  35. }
  36. }
  37. void OnGUI()
  38. {
  39. if (overlay == null)
  40. return;
  41. var texture = overlay.texture as RenderTexture;
  42. var prevActive = RenderTexture.active;
  43. RenderTexture.active = texture;
  44. if (Event.current.type == EventType.Repaint)
  45. GL.Clear(false, true, Color.clear);
  46. var area = new Rect(0, 0, texture.width, texture.height);
  47. // Account for screen smaller than texture (since mouse position gets clamped)
  48. if (Screen.width < texture.width)
  49. {
  50. area.width = Screen.width;
  51. overlay.uvOffset.x = -(float)(texture.width - Screen.width) / (2 * texture.width);
  52. }
  53. if (Screen.height < texture.height)
  54. {
  55. area.height = Screen.height;
  56. overlay.uvOffset.y = (float)(texture.height - Screen.height) / (2 * texture.height);
  57. }
  58. GUILayout.BeginArea(area);
  59. if (background != null)
  60. {
  61. GUI.DrawTexture(new Rect(
  62. (area.width - background.width) / 2,
  63. (area.height - background.height) / 2,
  64. background.width, background.height), background);
  65. }
  66. GUILayout.BeginHorizontal();
  67. GUILayout.FlexibleSpace();
  68. GUILayout.BeginVertical();
  69. if (logo != null)
  70. {
  71. GUILayout.Space(area.height / 2 - logoHeight);
  72. GUILayout.Box(logo);
  73. }
  74. GUILayout.Space(menuOffset);
  75. bool bHideMenu = GUILayout.Button("[Esc] - Close menu");
  76. GUILayout.BeginHorizontal();
  77. GUILayout.Label(string.Format("Scale: {0:N4}", scale));
  78. {
  79. var result = GUILayout.HorizontalSlider(scale, scaleLimits.x, scaleLimits.y);
  80. if (result != scale)
  81. {
  82. SetScale(result);
  83. }
  84. }
  85. GUILayout.EndHorizontal();
  86. GUILayout.BeginHorizontal();
  87. GUILayout.Label(string.Format("Scale limits:"));
  88. {
  89. var result = GUILayout.TextField(scaleLimitX);
  90. if (result != scaleLimitX)
  91. {
  92. if (float.TryParse(result, out scaleLimits.x))
  93. scaleLimitX = result;
  94. }
  95. }
  96. {
  97. var result = GUILayout.TextField(scaleLimitY);
  98. if (result != scaleLimitY)
  99. {
  100. if (float.TryParse(result, out scaleLimits.y))
  101. scaleLimitY = result;
  102. }
  103. }
  104. GUILayout.EndHorizontal();
  105. GUILayout.BeginHorizontal();
  106. GUILayout.Label(string.Format("Scale rate:"));
  107. {
  108. var result = GUILayout.TextField(scaleRateText);
  109. if (result != scaleRateText)
  110. {
  111. if (float.TryParse(result, out scaleRate))
  112. scaleRateText = result;
  113. }
  114. }
  115. GUILayout.EndHorizontal();
  116. if (SteamVR.active)
  117. {
  118. var vr = SteamVR.instance;
  119. GUILayout.BeginHorizontal();
  120. {
  121. var t = SteamVR_Camera.sceneResolutionScale;
  122. int w = (int)(vr.sceneWidth * t);
  123. int h = (int)(vr.sceneHeight * t);
  124. int pct = (int)(100.0f * t);
  125. GUILayout.Label(string.Format("Scene quality: {0}x{1} ({2}%)", w, h, pct));
  126. var result = Mathf.RoundToInt(GUILayout.HorizontalSlider(pct, 50, 200));
  127. if (result != pct)
  128. {
  129. SteamVR_Camera.sceneResolutionScale = (float)result / 100.0f;
  130. }
  131. }
  132. GUILayout.EndHorizontal();
  133. }
  134. overlay.highquality = GUILayout.Toggle(overlay.highquality, "High quality");
  135. if (overlay.highquality)
  136. {
  137. overlay.curved = GUILayout.Toggle(overlay.curved, "Curved overlay");
  138. overlay.antialias = GUILayout.Toggle(overlay.antialias, "Overlay RGSS(2x2)");
  139. }
  140. else
  141. {
  142. overlay.curved = false;
  143. overlay.antialias = false;
  144. }
  145. var tracker = SteamVR_Render.Top();
  146. if (tracker != null)
  147. {
  148. tracker.wireframe = GUILayout.Toggle(tracker.wireframe, "Wireframe");
  149. if (SteamVR.settings.trackingSpace == ETrackingUniverseOrigin.TrackingUniverseSeated)
  150. {
  151. if (GUILayout.Button("Switch to Standing"))
  152. SteamVR.settings.trackingSpace = ETrackingUniverseOrigin.TrackingUniverseStanding;
  153. if (GUILayout.Button("Center View"))
  154. {
  155. var system = OpenVR.System;
  156. if (system != null)
  157. system.ResetSeatedZeroPose();
  158. }
  159. }
  160. else
  161. {
  162. if (GUILayout.Button("Switch to Seated"))
  163. SteamVR.settings.trackingSpace = ETrackingUniverseOrigin.TrackingUniverseSeated;
  164. }
  165. }
  166. #if !UNITY_EDITOR
  167. if (GUILayout.Button("Exit"))
  168. Application.Quit();
  169. #endif
  170. GUILayout.Space(menuOffset);
  171. var env = System.Environment.GetEnvironmentVariable("VR_OVERRIDE");
  172. if (env != null)
  173. {
  174. GUILayout.Label("VR_OVERRIDE=" + env);
  175. }
  176. GUILayout.Label("Graphics device: " + SystemInfo.graphicsDeviceVersion);
  177. GUILayout.EndVertical();
  178. GUILayout.FlexibleSpace();
  179. GUILayout.EndHorizontal();
  180. GUILayout.EndArea();
  181. if (cursor != null)
  182. {
  183. float x = Input.mousePosition.x, y = Screen.height - Input.mousePosition.y;
  184. float w = cursor.width, h = cursor.height;
  185. GUI.DrawTexture(new Rect(x, y, w, h), cursor);
  186. }
  187. RenderTexture.active = prevActive;
  188. if (bHideMenu)
  189. HideMenu();
  190. }
  191. public void ShowMenu()
  192. {
  193. var overlay = SteamVR_Overlay.instance;
  194. if (overlay == null)
  195. return;
  196. var texture = overlay.texture as RenderTexture;
  197. if (texture == null)
  198. {
  199. Debug.LogError("<b>[SteamVR]</b> Menu requires overlay texture to be a render texture.");
  200. return;
  201. }
  202. SaveCursorState();
  203. Cursor.visible = true;
  204. Cursor.lockState = CursorLockMode.None;
  205. this.overlay = overlay;
  206. uvOffset = overlay.uvOffset;
  207. distance = overlay.distance;
  208. // If an existing camera is rendering into the overlay texture, we need
  209. // to temporarily disable it to keep it from clearing the texture on us.
  210. var cameras = Object.FindObjectsOfType(typeof(Camera)) as Camera[];
  211. foreach (var cam in cameras)
  212. {
  213. if (cam.enabled && cam.targetTexture == texture)
  214. {
  215. overlayCam = cam;
  216. overlayCam.enabled = false;
  217. break;
  218. }
  219. }
  220. var tracker = SteamVR_Render.Top();
  221. if (tracker != null)
  222. scale = tracker.origin.localScale.x;
  223. }
  224. public void HideMenu()
  225. {
  226. RestoreCursorState();
  227. if (overlayCam != null)
  228. overlayCam.enabled = true;
  229. if (overlay != null)
  230. {
  231. overlay.uvOffset = uvOffset;
  232. overlay.distance = distance;
  233. overlay = null;
  234. }
  235. }
  236. void Update()
  237. {
  238. if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Joystick1Button7))
  239. {
  240. if (overlay == null)
  241. {
  242. ShowMenu();
  243. }
  244. else
  245. {
  246. HideMenu();
  247. }
  248. }
  249. else if (Input.GetKeyDown(KeyCode.Home))
  250. {
  251. SetScale(1.0f);
  252. }
  253. else if (Input.GetKey(KeyCode.PageUp))
  254. {
  255. SetScale(Mathf.Clamp(scale + scaleRate * Time.deltaTime, scaleLimits.x, scaleLimits.y));
  256. }
  257. else if (Input.GetKey(KeyCode.PageDown))
  258. {
  259. SetScale(Mathf.Clamp(scale - scaleRate * Time.deltaTime, scaleLimits.x, scaleLimits.y));
  260. }
  261. }
  262. void SetScale(float scale)
  263. {
  264. this.scale = scale;
  265. var tracker = SteamVR_Render.Top();
  266. if (tracker != null)
  267. tracker.origin.localScale = new Vector3(scale, scale, scale);
  268. }
  269. void SaveCursorState()
  270. {
  271. savedCursorVisible = Cursor.visible;
  272. savedCursorLockState = Cursor.lockState;
  273. }
  274. void RestoreCursorState()
  275. {
  276. Cursor.visible = savedCursorVisible;
  277. Cursor.lockState = savedCursorLockState;
  278. }
  279. }
  280. }