2
0

SteamVR_Menu.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. var tracker = SteamVR_Render.Top();
  135. if (tracker != null)
  136. {
  137. tracker.wireframe = GUILayout.Toggle(tracker.wireframe, "Wireframe");
  138. if (SteamVR.settings.trackingSpace == ETrackingUniverseOrigin.TrackingUniverseSeated)
  139. {
  140. if (GUILayout.Button("Switch to Standing"))
  141. SteamVR.settings.trackingSpace = ETrackingUniverseOrigin.TrackingUniverseStanding;
  142. if (GUILayout.Button("Center View"))
  143. {
  144. var chaperone = OpenVR.Chaperone;
  145. if (chaperone != null)
  146. chaperone.ResetZeroPose(SteamVR.settings.trackingSpace);
  147. }
  148. }
  149. else
  150. {
  151. if (GUILayout.Button("Switch to Seated"))
  152. SteamVR.settings.trackingSpace = ETrackingUniverseOrigin.TrackingUniverseSeated;
  153. }
  154. }
  155. #if !UNITY_EDITOR
  156. if (GUILayout.Button("Exit"))
  157. Application.Quit();
  158. #endif
  159. GUILayout.Space(menuOffset);
  160. var env = System.Environment.GetEnvironmentVariable("VR_OVERRIDE");
  161. if (env != null)
  162. {
  163. GUILayout.Label("VR_OVERRIDE=" + env);
  164. }
  165. GUILayout.Label("Graphics device: " + SystemInfo.graphicsDeviceVersion);
  166. GUILayout.EndVertical();
  167. GUILayout.FlexibleSpace();
  168. GUILayout.EndHorizontal();
  169. GUILayout.EndArea();
  170. if (cursor != null)
  171. {
  172. float x = Input.mousePosition.x, y = Screen.height - Input.mousePosition.y;
  173. float w = cursor.width, h = cursor.height;
  174. GUI.DrawTexture(new Rect(x, y, w, h), cursor);
  175. }
  176. RenderTexture.active = prevActive;
  177. if (bHideMenu)
  178. HideMenu();
  179. }
  180. public void ShowMenu()
  181. {
  182. var overlay = SteamVR_Overlay.instance;
  183. if (overlay == null)
  184. return;
  185. var texture = overlay.texture as RenderTexture;
  186. if (texture == null)
  187. {
  188. Debug.LogError("<b>[SteamVR]</b> Menu requires overlay texture to be a render texture.", this);
  189. return;
  190. }
  191. SaveCursorState();
  192. Cursor.visible = true;
  193. Cursor.lockState = CursorLockMode.None;
  194. this.overlay = overlay;
  195. uvOffset = overlay.uvOffset;
  196. distance = overlay.distance;
  197. // If an existing camera is rendering into the overlay texture, we need
  198. // to temporarily disable it to keep it from clearing the texture on us.
  199. var cameras = Object.FindObjectsOfType(typeof(Camera)) as Camera[];
  200. foreach (var cam in cameras)
  201. {
  202. if (cam.enabled && cam.targetTexture == texture)
  203. {
  204. overlayCam = cam;
  205. overlayCam.enabled = false;
  206. break;
  207. }
  208. }
  209. var tracker = SteamVR_Render.Top();
  210. if (tracker != null)
  211. scale = tracker.origin.localScale.x;
  212. }
  213. public void HideMenu()
  214. {
  215. RestoreCursorState();
  216. if (overlayCam != null)
  217. overlayCam.enabled = true;
  218. if (overlay != null)
  219. {
  220. overlay.uvOffset = uvOffset;
  221. overlay.distance = distance;
  222. overlay = null;
  223. }
  224. }
  225. void Update()
  226. {
  227. if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Joystick1Button7))
  228. {
  229. if (overlay == null)
  230. {
  231. ShowMenu();
  232. }
  233. else
  234. {
  235. HideMenu();
  236. }
  237. }
  238. else if (Input.GetKeyDown(KeyCode.Home))
  239. {
  240. SetScale(1.0f);
  241. }
  242. else if (Input.GetKey(KeyCode.PageUp))
  243. {
  244. SetScale(Mathf.Clamp(scale + scaleRate * Time.deltaTime, scaleLimits.x, scaleLimits.y));
  245. }
  246. else if (Input.GetKey(KeyCode.PageDown))
  247. {
  248. SetScale(Mathf.Clamp(scale - scaleRate * Time.deltaTime, scaleLimits.x, scaleLimits.y));
  249. }
  250. }
  251. void SetScale(float scale)
  252. {
  253. this.scale = scale;
  254. var tracker = SteamVR_Render.Top();
  255. if (tracker != null)
  256. tracker.origin.localScale = new Vector3(scale, scale, scale);
  257. }
  258. void SaveCursorState()
  259. {
  260. savedCursorVisible = Cursor.visible;
  261. savedCursorLockState = Cursor.lockState;
  262. }
  263. void RestoreCursorState()
  264. {
  265. Cursor.visible = savedCursorVisible;
  266. Cursor.lockState = savedCursorLockState;
  267. }
  268. }
  269. }