Mercurial > hg > Game > Games
comparison Orchestland/Assets/OVR/Scripts/Util/OVRMainMenu.cs @ 1:f7675884f2a1
Add Orchestland project
author | Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2015 23:09:20 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:347d21cdfc22 | 1:f7675884f2a1 |
---|---|
1 /************************************************************************************ | |
2 | |
3 Copyright : Copyright 2014 Oculus VR, LLC. All Rights reserved. | |
4 | |
5 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); | |
6 you may not use the Oculus VR Rift SDK except in compliance with the License, | |
7 which is provided at the time of installation or download, or which | |
8 otherwise accompanies this software in either electronic or hard copy form. | |
9 | |
10 You may obtain a copy of the License at | |
11 | |
12 http://www.oculusvr.com/licenses/LICENSE-3.2 | |
13 | |
14 Unless required by applicable law or agreed to in writing, the Oculus VR SDK | |
15 distributed under the License is distributed on an "AS IS" BASIS, | |
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
17 See the License for the specific language governing permissions and | |
18 limitations under the License. | |
19 | |
20 ************************************************************************************/ | |
21 | |
22 // #define SHOW_DK2_VARIABLES | |
23 | |
24 // Use the Unity new GUI with Unity 4.6 or above. | |
25 #if UNITY_4_6 || UNITY_5_0 | |
26 #define USE_NEW_GUI | |
27 #endif | |
28 | |
29 using System; | |
30 using System.Collections; | |
31 using UnityEngine; | |
32 #if USE_NEW_GUI | |
33 using UnityEngine.UI; | |
34 # endif | |
35 | |
36 //------------------------------------------------------------------------------------- | |
37 // ***** OVRMainMenu | |
38 // | |
39 /// <summary> | |
40 /// OVRMainMenu is used to control the loading of different scenes. It also renders out | |
41 /// a menu that allows a user to modify various Rift settings, and allow for storing | |
42 /// these settings for recall later. | |
43 /// | |
44 /// A user of this component can add as many scenes that they would like to be able to | |
45 /// have access to. | |
46 /// | |
47 /// OVRMainMenu is currently attached to the OVRPlayerController prefab for convenience, | |
48 /// but can safely removed from it and added to another GameObject that is used for general | |
49 /// Unity logic. | |
50 /// | |
51 /// </summary> | |
52 public class OVRMainMenu : MonoBehaviour | |
53 { | |
54 /// <summary> | |
55 /// The amount of time in seconds that it takes for the menu to fade in. | |
56 /// </summary> | |
57 public float FadeInTime = 2.0f; | |
58 | |
59 /// <summary> | |
60 /// An optional texture that appears before the menu fades in. | |
61 /// </summary> | |
62 public UnityEngine.Texture FadeInTexture = null; | |
63 | |
64 /// <summary> | |
65 /// An optional font that replaces Unity's default Arial. | |
66 /// </summary> | |
67 public Font FontReplace = null; | |
68 | |
69 /// <summary> | |
70 /// The key that toggles the menu. | |
71 /// </summary> | |
72 public KeyCode MenuKey = KeyCode.Space; | |
73 | |
74 /// <summary> | |
75 /// The key that quits the application. | |
76 /// </summary> | |
77 public KeyCode QuitKey = KeyCode.Escape; | |
78 | |
79 /// <summary> | |
80 /// Scene names to show on-screen for each of the scenes in Scenes. | |
81 /// </summary> | |
82 public string [] SceneNames; | |
83 | |
84 /// <summary> | |
85 /// The set of scenes that the user can jump to. | |
86 /// </summary> | |
87 public string [] Scenes; | |
88 | |
89 private bool ScenesVisible = false; | |
90 | |
91 // Spacing for scenes menu | |
92 private int StartX = 490; | |
93 private int StartY = 250; | |
94 private int WidthX = 300; | |
95 private int WidthY = 23; | |
96 | |
97 // Spacing for variables that users can change | |
98 private int VRVarsSX = 553; | |
99 private int VRVarsSY = 250; | |
100 private int VRVarsWidthX = 175; | |
101 private int VRVarsWidthY = 23; | |
102 | |
103 private int StepY = 25; | |
104 | |
105 // Handle to OVRCameraRig | |
106 private OVRCameraRig CameraController = null; | |
107 | |
108 // Handle to OVRPlayerController | |
109 private OVRPlayerController PlayerController = null; | |
110 | |
111 // Controller buttons | |
112 private bool PrevStartDown; | |
113 private bool PrevHatDown; | |
114 private bool PrevHatUp; | |
115 | |
116 private bool ShowVRVars; | |
117 | |
118 private bool OldSpaceHit; | |
119 | |
120 // FPS | |
121 private float UpdateInterval = 0.5f; | |
122 private float Accum = 0; | |
123 private int Frames = 0; | |
124 private float TimeLeft = 0; | |
125 private string strFPS = "FPS: 0"; | |
126 | |
127 private string strIPD = "IPD: 0.000"; | |
128 | |
129 /// <summary> | |
130 /// Prediction (in ms) | |
131 /// </summary> | |
132 public float PredictionIncrement = 0.001f; // 1 ms | |
133 private string strPrediction = "Pred: OFF"; | |
134 | |
135 private string strFOV = "FOV: 0.0f"; | |
136 private string strHeight = "Height: 0.0f"; | |
137 | |
138 /// <summary> | |
139 /// Controls how quickly the player's speed and rotation change based on input. | |
140 /// </summary> | |
141 public float SpeedRotationIncrement = 0.05f; | |
142 private string strSpeedRotationMultipler = "Spd. X: 0.0f Rot. X: 0.0f"; | |
143 | |
144 private bool LoadingLevel = false; | |
145 private float AlphaFadeValue = 1.0f; | |
146 private int CurrentLevel = 0; | |
147 | |
148 // Rift detection | |
149 private bool HMDPresent = false; | |
150 private float RiftPresentTimeout = 0.0f; | |
151 private string strRiftPresent = ""; | |
152 | |
153 // Replace the GUI with our own texture and 3D plane that | |
154 // is attached to the rendder camera for true 3D placement | |
155 private OVRGUI GuiHelper = new OVRGUI(); | |
156 private GameObject GUIRenderObject = null; | |
157 private RenderTexture GUIRenderTexture = null; | |
158 | |
159 // We want to use new Unity GUI built in 4.6 for OVRMainMenu GUI | |
160 // Enable the UsingNewGUI option in the editor, | |
161 // if you want to use new GUI and Unity version is higher than 4.6 | |
162 #if USE_NEW_GUI | |
163 private GameObject NewGUIObject = null; | |
164 private GameObject RiftPresentGUIObject = null; | |
165 #endif | |
166 | |
167 /// <summary> | |
168 /// We can set the layer to be anything we want to, this allows | |
169 /// a specific camera to render it. | |
170 /// </summary> | |
171 public string LayerName = "Default"; | |
172 | |
173 /// <summary> | |
174 /// Crosshair rendered onto 3D plane. | |
175 /// </summary> | |
176 public UnityEngine.Texture CrosshairImage = null; | |
177 private OVRCrosshair Crosshair = new OVRCrosshair(); | |
178 | |
179 // Resolution Eye Texture | |
180 private string strResolutionEyeTexture = "Resolution: 0 x 0"; | |
181 | |
182 // Latency values | |
183 private string strLatencies = "Ren: 0.0f TWrp: 0.0f PostPresent: 0.0f"; | |
184 | |
185 // Vision mode on/off | |
186 private bool VisionMode = true; | |
187 #if SHOW_DK2_VARIABLES | |
188 private string strVisionMode = "Vision Enabled: ON"; | |
189 #endif | |
190 | |
191 // We want to hold onto GridCube, for potential sharing | |
192 // of the menu RenderTarget | |
193 OVRGridCube GridCube = null; | |
194 | |
195 // We want to hold onto the VisionGuide so we can share | |
196 // the menu RenderTarget | |
197 OVRVisionGuide VisionGuide = null; | |
198 | |
199 #region MonoBehaviour Message Handlers | |
200 /// <summary> | |
201 /// Awake this instance. | |
202 /// </summary> | |
203 void Awake() | |
204 { | |
205 // Find camera controller | |
206 OVRCameraRig[] CameraControllers; | |
207 CameraControllers = gameObject.GetComponentsInChildren<OVRCameraRig>(); | |
208 | |
209 if(CameraControllers.Length == 0) | |
210 Debug.LogWarning("OVRMainMenu: No OVRCameraRig attached."); | |
211 else if (CameraControllers.Length > 1) | |
212 Debug.LogWarning("OVRMainMenu: More then 1 OVRCameraRig attached."); | |
213 else{ | |
214 CameraController = CameraControllers[0]; | |
215 #if USE_NEW_GUI | |
216 OVRUGUI.CameraController = CameraController; | |
217 #endif | |
218 } | |
219 | |
220 // Find player controller | |
221 OVRPlayerController[] PlayerControllers; | |
222 PlayerControllers = gameObject.GetComponentsInChildren<OVRPlayerController>(); | |
223 | |
224 if(PlayerControllers.Length == 0) | |
225 Debug.LogWarning("OVRMainMenu: No OVRPlayerController attached."); | |
226 else if (PlayerControllers.Length > 1) | |
227 Debug.LogWarning("OVRMainMenu: More then 1 OVRPlayerController attached."); | |
228 else{ | |
229 PlayerController = PlayerControllers[0]; | |
230 #if USE_NEW_GUI | |
231 OVRUGUI.PlayerController = PlayerController; | |
232 #endif | |
233 } | |
234 | |
235 #if USE_NEW_GUI | |
236 // Create canvas for using new GUI | |
237 NewGUIObject = new GameObject(); | |
238 NewGUIObject.name = "OVRGUIMain"; | |
239 NewGUIObject.transform.parent = GameObject.Find("LeftEyeAnchor").transform; | |
240 RectTransform r = NewGUIObject.AddComponent<RectTransform>(); | |
241 r.sizeDelta = new Vector2(100f, 100f); | |
242 r.localScale = new Vector3(0.001f, 0.001f, 0.001f); | |
243 r.localPosition = new Vector3(0.01f, 0.17f, 0.53f); | |
244 r.localEulerAngles = Vector3.zero; | |
245 | |
246 Canvas c = NewGUIObject.AddComponent<Canvas>(); | |
247 c.renderMode = RenderMode.WorldSpace; | |
248 c.pixelPerfect = false; | |
249 #endif | |
250 } | |
251 | |
252 /// <summary> | |
253 /// Start this instance. | |
254 /// </summary> | |
255 void Start() | |
256 { | |
257 AlphaFadeValue = 1.0f; | |
258 CurrentLevel = 0; | |
259 PrevStartDown = false; | |
260 PrevHatDown = false; | |
261 PrevHatUp = false; | |
262 ShowVRVars = false; | |
263 OldSpaceHit = false; | |
264 strFPS = "FPS: 0"; | |
265 LoadingLevel = false; | |
266 ScenesVisible = false; | |
267 | |
268 // Set the GUI target | |
269 GUIRenderObject = GameObject.Instantiate(Resources.Load("OVRGUIObjectMain")) as GameObject; | |
270 | |
271 if(GUIRenderObject != null) | |
272 { | |
273 // Chnge the layer | |
274 GUIRenderObject.layer = LayerMask.NameToLayer(LayerName); | |
275 | |
276 if(GUIRenderTexture == null) | |
277 { | |
278 int w = Screen.width; | |
279 int h = Screen.height; | |
280 | |
281 // We don't need a depth buffer on this texture | |
282 GUIRenderTexture = new RenderTexture(w, h, 0); | |
283 GuiHelper.SetPixelResolution(w, h); | |
284 // NOTE: All GUI elements are being written with pixel values based | |
285 // from DK1 (1280x800). These should change to normalized locations so | |
286 // that we can scale more cleanly with varying resolutions | |
287 GuiHelper.SetDisplayResolution(1280.0f, 800.0f); | |
288 } | |
289 } | |
290 | |
291 // Attach GUI texture to GUI object and GUI object to Camera | |
292 if(GUIRenderTexture != null && GUIRenderObject != null) | |
293 { | |
294 GUIRenderObject.GetComponent<Renderer>().material.mainTexture = GUIRenderTexture; | |
295 | |
296 if(CameraController != null) | |
297 { | |
298 // Grab transform of GUI object | |
299 Vector3 ls = GUIRenderObject.transform.localScale; | |
300 Vector3 lp = GUIRenderObject.transform.localPosition; | |
301 Quaternion lr = GUIRenderObject.transform.localRotation; | |
302 | |
303 // Attach the GUI object to the camera | |
304 GUIRenderObject.transform.parent = CameraController.centerEyeAnchor; | |
305 // Reset the transform values (we will be maintaining state of the GUI object | |
306 // in local state) | |
307 | |
308 GUIRenderObject.transform.localScale = ls; | |
309 GUIRenderObject.transform.localPosition = lp; | |
310 GUIRenderObject.transform.localRotation = lr; | |
311 | |
312 // Deactivate object until we have completed the fade-in | |
313 // Also, we may want to deactive the render object if there is nothing being rendered | |
314 // into the UI | |
315 GUIRenderObject.SetActive(false); | |
316 } | |
317 } | |
318 | |
319 // Make sure to hide cursor | |
320 if(Application.isEditor == false) | |
321 { | |
322 #if UNITY_5_0 | |
323 Cursor.visible = false; | |
324 Cursor.lockState = CursorLockMode.Locked; | |
325 #else | |
326 Cursor.visible = false; | |
327 Screen.lockCursor = true; | |
328 #endif | |
329 } | |
330 | |
331 // CameraController updates | |
332 if(CameraController != null) | |
333 { | |
334 // Add a GridCube component to this object | |
335 GridCube = gameObject.AddComponent<OVRGridCube>(); | |
336 GridCube.SetOVRCameraController(ref CameraController); | |
337 | |
338 // Add a VisionGuide component to this object | |
339 VisionGuide = gameObject.AddComponent<OVRVisionGuide>(); | |
340 VisionGuide.SetOVRCameraController(ref CameraController); | |
341 VisionGuide.SetFadeTexture(ref FadeInTexture); | |
342 VisionGuide.SetVisionGuideLayer(ref LayerName); | |
343 } | |
344 | |
345 // Crosshair functionality | |
346 Crosshair.Init(); | |
347 Crosshair.SetCrosshairTexture(ref CrosshairImage); | |
348 Crosshair.SetOVRCameraController (ref CameraController); | |
349 Crosshair.SetOVRPlayerController(ref PlayerController); | |
350 | |
351 // Check for HMD and sensor | |
352 CheckIfRiftPresent(); | |
353 | |
354 #if USE_NEW_GUI | |
355 if (!string.IsNullOrEmpty(strRiftPresent)){ | |
356 ShowRiftPresentGUI(); | |
357 } | |
358 #endif | |
359 } | |
360 | |
361 /// <summary> | |
362 /// Update this instance. | |
363 /// </summary> | |
364 void Update() | |
365 { | |
366 if(LoadingLevel == true) | |
367 return; | |
368 | |
369 // Main update | |
370 UpdateFPS(); | |
371 | |
372 // CameraController updates | |
373 if(CameraController != null) | |
374 { | |
375 UpdateIPD(); | |
376 | |
377 UpdateRecenterPose(); | |
378 UpdateVisionMode(); | |
379 UpdateFOV(); | |
380 UpdateEyeHeightOffset(); | |
381 UpdateResolutionEyeTexture(); | |
382 UpdateLatencyValues(); | |
383 } | |
384 | |
385 // PlayerController updates | |
386 if(PlayerController != null) | |
387 { | |
388 UpdateSpeedAndRotationScaleMultiplier(); | |
389 UpdatePlayerControllerMovement(); | |
390 } | |
391 | |
392 // MainMenu updates | |
393 UpdateSelectCurrentLevel(); | |
394 | |
395 // Device updates | |
396 UpdateDeviceDetection(); | |
397 | |
398 // Crosshair functionality | |
399 Crosshair.UpdateCrosshair(); | |
400 | |
401 #if USE_NEW_GUI | |
402 if (ShowVRVars && RiftPresentTimeout <= 0.0f) | |
403 { | |
404 NewGUIObject.SetActive(true); | |
405 UpdateNewGUIVars(); | |
406 OVRUGUI.UpdateGUI(); | |
407 } | |
408 else | |
409 { | |
410 NewGUIObject.SetActive(false); | |
411 } | |
412 #endif | |
413 | |
414 // Toggle Fullscreen | |
415 if(Input.GetKeyDown(KeyCode.F11)) | |
416 Screen.fullScreen = !Screen.fullScreen; | |
417 | |
418 if (Input.GetKeyDown(KeyCode.M)) | |
419 OVRManager.display.mirrorMode = !OVRManager.display.mirrorMode; | |
420 | |
421 // Escape Application | |
422 if (Input.GetKeyDown(QuitKey)) | |
423 Application.Quit(); | |
424 } | |
425 | |
426 /// <summary> | |
427 /// Updates Variables for new GUI. | |
428 /// </summary> | |
429 #if USE_NEW_GUI | |
430 void UpdateNewGUIVars() | |
431 { | |
432 #if SHOW_DK2_VARIABLES | |
433 // Print out Vision Mode | |
434 OVRUGUI.strVisionMode = strVisionMode; | |
435 #endif | |
436 // Print out FPS | |
437 OVRUGUI.strFPS = strFPS; | |
438 | |
439 // Don't draw these vars if CameraController is not present | |
440 if (CameraController != null) | |
441 { | |
442 OVRUGUI.strPrediction = strPrediction; | |
443 OVRUGUI.strIPD = strIPD; | |
444 OVRUGUI.strFOV = strFOV; | |
445 OVRUGUI.strResolutionEyeTexture = strResolutionEyeTexture; | |
446 OVRUGUI.strLatencies = strLatencies; | |
447 } | |
448 | |
449 // Don't draw these vars if PlayerController is not present | |
450 if (PlayerController != null) | |
451 { | |
452 OVRUGUI.strHeight = strHeight; | |
453 OVRUGUI.strSpeedRotationMultipler = strSpeedRotationMultipler; | |
454 } | |
455 | |
456 OVRUGUI.strRiftPresent = strRiftPresent; | |
457 } | |
458 #endif | |
459 | |
460 void OnGUI() | |
461 { | |
462 // Important to keep from skipping render events | |
463 if (Event.current.type != EventType.Repaint) | |
464 return; | |
465 | |
466 #if !USE_NEW_GUI | |
467 // Fade in screen | |
468 if(AlphaFadeValue > 0.0f) | |
469 { | |
470 AlphaFadeValue -= Mathf.Clamp01(Time.deltaTime / FadeInTime); | |
471 if(AlphaFadeValue < 0.0f) | |
472 { | |
473 AlphaFadeValue = 0.0f; | |
474 } | |
475 else | |
476 { | |
477 GUI.color = new Color(0, 0, 0, AlphaFadeValue); | |
478 GUI.DrawTexture( new Rect(0, 0, Screen.width, Screen.height ), FadeInTexture ); | |
479 return; | |
480 } | |
481 } | |
482 #endif | |
483 // We can turn on the render object so we can render the on-screen menu | |
484 if(GUIRenderObject != null) | |
485 { | |
486 if (ScenesVisible || | |
487 ShowVRVars || | |
488 Crosshair.IsCrosshairVisible() || | |
489 RiftPresentTimeout > 0.0f || | |
490 VisionGuide.GetFadeAlphaValue() > 0.0f) | |
491 { | |
492 GUIRenderObject.SetActive(true); | |
493 } | |
494 else | |
495 { | |
496 GUIRenderObject.SetActive(false); | |
497 } | |
498 } | |
499 | |
500 //*** | |
501 // Set the GUI matrix to deal with portrait mode | |
502 Vector3 scale = Vector3.one; | |
503 Matrix4x4 svMat = GUI.matrix; // save current matrix | |
504 // substitute matrix - only scale is altered from standard | |
505 GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale); | |
506 | |
507 // Cache current active render texture | |
508 RenderTexture previousActive = RenderTexture.active; | |
509 | |
510 // if set, we will render to this texture | |
511 if(GUIRenderTexture != null && GUIRenderObject.activeSelf) | |
512 { | |
513 RenderTexture.active = GUIRenderTexture; | |
514 GL.Clear (false, true, new Color (0.0f, 0.0f, 0.0f, 0.0f)); | |
515 } | |
516 | |
517 // Update OVRGUI functions (will be deprecated eventually when 2D renderingc | |
518 // is removed from GUI) | |
519 GuiHelper.SetFontReplace(FontReplace); | |
520 | |
521 // If true, we are displaying information about the Rift not being detected | |
522 // So do not show anything else | |
523 if(GUIShowRiftDetected() != true) | |
524 { | |
525 GUIShowLevels(); | |
526 GUIShowVRVariables(); | |
527 } | |
528 | |
529 // The cross-hair may need to go away at some point, unless someone finds it | |
530 // useful | |
531 Crosshair.OnGUICrosshair(); | |
532 | |
533 // Since we want to draw into the main GUI that is shared within the MainMenu, | |
534 // we call the OVRVisionGuide GUI function here | |
535 VisionGuide.OnGUIVisionGuide(); | |
536 | |
537 // Restore active render texture | |
538 if (GUIRenderObject.activeSelf) | |
539 { | |
540 RenderTexture.active = previousActive; | |
541 } | |
542 | |
543 // *** | |
544 // Restore previous GUI matrix | |
545 GUI.matrix = svMat; | |
546 } | |
547 #endregion | |
548 | |
549 #region Internal State Management Functions | |
550 /// <summary> | |
551 /// Updates the FPS. | |
552 /// </summary> | |
553 void UpdateFPS() | |
554 { | |
555 TimeLeft -= Time.deltaTime; | |
556 Accum += Time.timeScale/Time.deltaTime; | |
557 ++Frames; | |
558 | |
559 // Interval ended - update GUI text and start new interval | |
560 if( TimeLeft <= 0.0 ) | |
561 { | |
562 // display two fractional digits (f2 format) | |
563 float fps = Accum / Frames; | |
564 | |
565 if(ShowVRVars == true)// limit gc | |
566 strFPS = System.String.Format("FPS: {0:F2}",fps); | |
567 | |
568 TimeLeft += UpdateInterval; | |
569 Accum = 0.0f; | |
570 Frames = 0; | |
571 } | |
572 } | |
573 | |
574 /// <summary> | |
575 /// Updates the IPD. | |
576 /// </summary> | |
577 void UpdateIPD() | |
578 { | |
579 if(ShowVRVars == true) // limit gc | |
580 { | |
581 strIPD = System.String.Format("IPD (mm): {0:F4}", OVRManager.profile.ipd * 1000.0f); | |
582 } | |
583 } | |
584 | |
585 void UpdateRecenterPose() | |
586 { | |
587 if(Input.GetKeyDown(KeyCode.R)) | |
588 { | |
589 OVRManager.display.RecenterPose(); | |
590 } | |
591 } | |
592 | |
593 /// <summary> | |
594 /// Updates the vision mode. | |
595 /// </summary> | |
596 void UpdateVisionMode() | |
597 { | |
598 if (Input.GetKeyDown(KeyCode.F2)) | |
599 { | |
600 VisionMode = !VisionMode; | |
601 OVRManager.tracker.isEnabled = VisionMode; | |
602 | |
603 #if SHOW_DK2_VARIABLES | |
604 strVisionMode = VisionMode ? "Vision Enabled: ON" : "Vision Enabled: OFF"; | |
605 #endif | |
606 } | |
607 } | |
608 | |
609 /// <summary> | |
610 /// Updates the FOV. | |
611 /// </summary> | |
612 void UpdateFOV() | |
613 { | |
614 if(ShowVRVars == true)// limit gc | |
615 { | |
616 OVRDisplay.EyeRenderDesc eyeDesc = OVRManager.display.GetEyeRenderDesc(OVREye.Left); | |
617 | |
618 strFOV = System.String.Format ("FOV (deg): {0:F3}", eyeDesc.fov.y); | |
619 } | |
620 } | |
621 | |
622 /// <summary> | |
623 /// Updates resolution of eye texture | |
624 /// </summary> | |
625 void UpdateResolutionEyeTexture() | |
626 { | |
627 if (ShowVRVars == true) // limit gc | |
628 { | |
629 OVRDisplay.EyeRenderDesc leftEyeDesc = OVRManager.display.GetEyeRenderDesc(OVREye.Left); | |
630 OVRDisplay.EyeRenderDesc rightEyeDesc = OVRManager.display.GetEyeRenderDesc(OVREye.Right); | |
631 | |
632 float scale = OVRManager.instance.nativeTextureScale * OVRManager.instance.virtualTextureScale; | |
633 float w = (int)(scale * (float)(leftEyeDesc.resolution.x + rightEyeDesc.resolution.x)); | |
634 float h = (int)(scale * (float)Mathf.Max(leftEyeDesc.resolution.y, rightEyeDesc.resolution.y)); | |
635 | |
636 strResolutionEyeTexture = System.String.Format("Resolution : {0} x {1}", w, h); | |
637 } | |
638 } | |
639 | |
640 /// <summary> | |
641 /// Updates latency values | |
642 /// </summary> | |
643 void UpdateLatencyValues() | |
644 { | |
645 #if !UNITY_ANDROID || UNITY_EDITOR | |
646 if (ShowVRVars == true) // limit gc | |
647 { | |
648 OVRDisplay.LatencyData latency = OVRManager.display.latency; | |
649 if (latency.render < 0.000001f && latency.timeWarp < 0.000001f && latency.postPresent < 0.000001f) | |
650 strLatencies = System.String.Format("Ren : N/A TWrp: N/A PostPresent: N/A"); | |
651 else | |
652 strLatencies = System.String.Format("Ren : {0:F3} TWrp: {1:F3} PostPresent: {2:F3}", | |
653 latency.render, | |
654 latency.timeWarp, | |
655 latency.postPresent); | |
656 } | |
657 #endif | |
658 } | |
659 | |
660 /// <summary> | |
661 /// Updates the eye height offset. | |
662 /// </summary> | |
663 void UpdateEyeHeightOffset() | |
664 { | |
665 if(ShowVRVars == true)// limit gc | |
666 { | |
667 float eyeHeight = OVRManager.profile.eyeHeight; | |
668 | |
669 strHeight = System.String.Format ("Eye Height (m): {0:F3}", eyeHeight); | |
670 } | |
671 } | |
672 | |
673 /// <summary> | |
674 /// Updates the speed and rotation scale multiplier. | |
675 /// </summary> | |
676 void UpdateSpeedAndRotationScaleMultiplier() | |
677 { | |
678 float moveScaleMultiplier = 0.0f; | |
679 PlayerController.GetMoveScaleMultiplier(ref moveScaleMultiplier); | |
680 if(Input.GetKeyDown(KeyCode.Alpha7)) | |
681 moveScaleMultiplier -= SpeedRotationIncrement; | |
682 else if (Input.GetKeyDown(KeyCode.Alpha8)) | |
683 moveScaleMultiplier += SpeedRotationIncrement; | |
684 PlayerController.SetMoveScaleMultiplier(moveScaleMultiplier); | |
685 | |
686 float rotationScaleMultiplier = 0.0f; | |
687 PlayerController.GetRotationScaleMultiplier(ref rotationScaleMultiplier); | |
688 if(Input.GetKeyDown(KeyCode.Alpha9)) | |
689 rotationScaleMultiplier -= SpeedRotationIncrement; | |
690 else if (Input.GetKeyDown(KeyCode.Alpha0)) | |
691 rotationScaleMultiplier += SpeedRotationIncrement; | |
692 PlayerController.SetRotationScaleMultiplier(rotationScaleMultiplier); | |
693 | |
694 if(ShowVRVars == true)// limit gc | |
695 strSpeedRotationMultipler = System.String.Format ("Spd.X: {0:F2} Rot.X: {1:F2}", | |
696 moveScaleMultiplier, | |
697 rotationScaleMultiplier); | |
698 } | |
699 | |
700 /// <summary> | |
701 /// Updates the player controller movement. | |
702 /// </summary> | |
703 void UpdatePlayerControllerMovement() | |
704 { | |
705 if(PlayerController != null) | |
706 PlayerController.SetHaltUpdateMovement(ScenesVisible); | |
707 } | |
708 | |
709 /// <summary> | |
710 /// Updates the select current level. | |
711 /// </summary> | |
712 void UpdateSelectCurrentLevel() | |
713 { | |
714 ShowLevels(); | |
715 | |
716 if (!ScenesVisible) | |
717 return; | |
718 | |
719 CurrentLevel = GetCurrentLevel(); | |
720 | |
721 if (Scenes.Length != 0 | |
722 && (OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.A) | |
723 || Input.GetKeyDown(KeyCode.Return))) | |
724 { | |
725 LoadingLevel = true; | |
726 Application.LoadLevelAsync(Scenes[CurrentLevel]); | |
727 } | |
728 } | |
729 | |
730 /// <summary> | |
731 /// Shows the levels. | |
732 /// </summary> | |
733 /// <returns><c>true</c>, if levels was shown, <c>false</c> otherwise.</returns> | |
734 bool ShowLevels() | |
735 { | |
736 if (Scenes.Length == 0) | |
737 { | |
738 ScenesVisible = false; | |
739 return ScenesVisible; | |
740 } | |
741 | |
742 bool curStartDown = OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.Start); | |
743 bool startPressed = (curStartDown && !PrevStartDown) || Input.GetKeyDown(KeyCode.RightShift); | |
744 PrevStartDown = curStartDown; | |
745 | |
746 if (startPressed) | |
747 { | |
748 ScenesVisible = !ScenesVisible; | |
749 } | |
750 | |
751 return ScenesVisible; | |
752 } | |
753 | |
754 /// <summary> | |
755 /// Gets the current level. | |
756 /// </summary> | |
757 /// <returns>The current level.</returns> | |
758 int GetCurrentLevel() | |
759 { | |
760 bool curHatDown = false; | |
761 if(OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.Down) == true) | |
762 curHatDown = true; | |
763 | |
764 bool curHatUp = false; | |
765 if(OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.Down) == true) | |
766 curHatUp = true; | |
767 | |
768 if((PrevHatDown == false) && (curHatDown == true) || | |
769 Input.GetKeyDown(KeyCode.DownArrow)) | |
770 { | |
771 CurrentLevel = (CurrentLevel + 1) % SceneNames.Length; | |
772 } | |
773 else if((PrevHatUp == false) && (curHatUp == true) || | |
774 Input.GetKeyDown(KeyCode.UpArrow)) | |
775 { | |
776 CurrentLevel--; | |
777 if(CurrentLevel < 0) | |
778 CurrentLevel = SceneNames.Length - 1; | |
779 } | |
780 | |
781 PrevHatDown = curHatDown; | |
782 PrevHatUp = curHatUp; | |
783 | |
784 return CurrentLevel; | |
785 } | |
786 | |
787 #endregion | |
788 | |
789 #region Internal GUI Functions | |
790 | |
791 /// <summary> | |
792 /// Show the GUI levels. | |
793 /// </summary> | |
794 void GUIShowLevels() | |
795 { | |
796 if(ScenesVisible == true) | |
797 { | |
798 // Darken the background by rendering fade texture | |
799 GUI.color = new Color(0, 0, 0, 0.5f); | |
800 GUI.DrawTexture( new Rect(0, 0, Screen.width, Screen.height ), FadeInTexture ); | |
801 GUI.color = Color.white; | |
802 | |
803 if(LoadingLevel == true) | |
804 { | |
805 string loading = "LOADING..."; | |
806 GuiHelper.StereoBox (StartX, StartY, WidthX, WidthY, ref loading, Color.yellow); | |
807 return; | |
808 } | |
809 | |
810 for (int i = 0; i < SceneNames.Length; i++) | |
811 { | |
812 Color c; | |
813 if(i == CurrentLevel) | |
814 c = Color.yellow; | |
815 else | |
816 c = Color.black; | |
817 | |
818 int y = StartY + (i * StepY); | |
819 | |
820 GuiHelper.StereoBox (StartX, y, WidthX, WidthY, ref SceneNames[i], c); | |
821 } | |
822 } | |
823 } | |
824 | |
825 /// <summary> | |
826 /// Show the VR variables. | |
827 /// </summary> | |
828 void GUIShowVRVariables() | |
829 { | |
830 bool SpaceHit = Input.GetKey(MenuKey); | |
831 if ((OldSpaceHit == false) && (SpaceHit == true)) | |
832 { | |
833 if (ShowVRVars == true) | |
834 { | |
835 ShowVRVars = false; | |
836 } | |
837 else | |
838 { | |
839 ShowVRVars = true; | |
840 #if USE_NEW_GUI | |
841 OVRUGUI.InitUIComponent = ShowVRVars; | |
842 #endif | |
843 } | |
844 } | |
845 | |
846 OldSpaceHit = SpaceHit; | |
847 | |
848 // Do not render if we are not showing | |
849 if (ShowVRVars == false) | |
850 return; | |
851 | |
852 | |
853 | |
854 #if !USE_NEW_GUI | |
855 int y = VRVarsSY; | |
856 #if SHOW_DK2_VARIABLES | |
857 // Print out Vision Mode | |
858 GuiHelper.StereoBox (VRVarsSX, y += StepY, VRVarsWidthX, VRVarsWidthY, | |
859 ref strVisionMode, Color.green); | |
860 #endif | |
861 | |
862 // Draw FPS | |
863 GuiHelper.StereoBox(VRVarsSX, y += StepY, VRVarsWidthX, VRVarsWidthY, | |
864 ref strFPS, Color.green); | |
865 | |
866 // Don't draw these vars if CameraController is not present | |
867 if (CameraController != null) | |
868 { | |
869 GuiHelper.StereoBox(VRVarsSX, y += StepY, VRVarsWidthX, VRVarsWidthY, | |
870 ref strPrediction, Color.white); | |
871 GuiHelper.StereoBox(VRVarsSX, y += StepY, VRVarsWidthX, VRVarsWidthY, | |
872 ref strIPD, Color.yellow); | |
873 GuiHelper.StereoBox(VRVarsSX, y += StepY, VRVarsWidthX, VRVarsWidthY, | |
874 ref strFOV, Color.white); | |
875 GuiHelper.StereoBox(VRVarsSX, y += StepY, VRVarsWidthX, VRVarsWidthY, | |
876 ref strResolutionEyeTexture, Color.white); | |
877 GuiHelper.StereoBox(VRVarsSX, y += StepY, VRVarsWidthX, VRVarsWidthY, | |
878 ref strLatencies, Color.white); | |
879 } | |
880 | |
881 // Don't draw these vars if PlayerController is not present | |
882 if (PlayerController != null) | |
883 { | |
884 GuiHelper.StereoBox(VRVarsSX, y += StepY, VRVarsWidthX, VRVarsWidthY, | |
885 ref strHeight, Color.yellow); | |
886 GuiHelper.StereoBox(VRVarsSX, y += StepY, VRVarsWidthX, VRVarsWidthY, | |
887 ref strSpeedRotationMultipler, Color.white); | |
888 } | |
889 #endif | |
890 } | |
891 | |
892 // RIFT DETECTION | |
893 | |
894 /// <summary> | |
895 /// Checks to see if HMD and / or sensor is available, and displays a | |
896 /// message if it is not. | |
897 /// </summary> | |
898 void CheckIfRiftPresent() | |
899 { | |
900 HMDPresent = OVRManager.display.isPresent; | |
901 | |
902 if (!HMDPresent) | |
903 { | |
904 RiftPresentTimeout = 15.0f; | |
905 | |
906 if (!HMDPresent) | |
907 strRiftPresent = "NO HMD DETECTED"; | |
908 #if USE_NEW_GUI | |
909 OVRUGUI.strRiftPresent = strRiftPresent; | |
910 #endif | |
911 } | |
912 } | |
913 | |
914 /// <summary> | |
915 /// Show if Rift is detected. | |
916 /// </summary> | |
917 /// <returns><c>true</c>, if show rift detected was GUIed, <c>false</c> otherwise.</returns> | |
918 bool GUIShowRiftDetected() | |
919 { | |
920 #if !USE_NEW_GUI | |
921 if(RiftPresentTimeout > 0.0f) | |
922 { | |
923 GuiHelper.StereoBox (StartX, StartY, WidthX, WidthY, | |
924 ref strRiftPresent, Color.white); | |
925 | |
926 return true; | |
927 } | |
928 #else | |
929 if(RiftPresentTimeout < 0.0f) | |
930 DestroyImmediate(RiftPresentGUIObject); | |
931 #endif | |
932 return false; | |
933 } | |
934 | |
935 /// <summary> | |
936 /// Updates the device detection. | |
937 /// </summary> | |
938 void UpdateDeviceDetection() | |
939 { | |
940 if(RiftPresentTimeout > 0.0f) | |
941 RiftPresentTimeout -= Time.deltaTime; | |
942 } | |
943 | |
944 /// <summary> | |
945 /// Show rift present GUI with new GUI | |
946 /// </summary> | |
947 void ShowRiftPresentGUI() | |
948 { | |
949 #if USE_NEW_GUI | |
950 RiftPresentGUIObject = new GameObject(); | |
951 RiftPresentGUIObject.name = "RiftPresentGUIMain"; | |
952 RiftPresentGUIObject.transform.parent = GameObject.Find("LeftEyeAnchor").transform; | |
953 | |
954 RectTransform r = RiftPresentGUIObject.AddComponent<RectTransform>(); | |
955 r.sizeDelta = new Vector2(100f, 100f); | |
956 r.localPosition = new Vector3(0.01f, 0.17f, 0.53f); | |
957 r.localEulerAngles = Vector3.zero; | |
958 r.localScale = new Vector3(0.001f, 0.001f, 0.001f); | |
959 Canvas c = RiftPresentGUIObject.AddComponent<Canvas>(); | |
960 c.renderMode = RenderMode.WorldSpace; | |
961 c.pixelPerfect = false; | |
962 OVRUGUI.RiftPresentGUI(RiftPresentGUIObject); | |
963 #endif | |
964 } | |
965 #endregion | |
966 } |