Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
7.7 KiB

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