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