/************************************************************************************
Copyright : Copyright 2014 Oculus VR, LLC. All Rights reserved.
Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-3.2
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
// #define SHOW_DK2_VARIABLES
// Use the Unity new GUI with Unity 4.6 or above.
#if UNITY_4_6 || UNITY_5_0
#define USE_NEW_GUI
#endif
using System;
using System.Collections;
using UnityEngine;
#if USE_NEW_GUI
using UnityEngine.UI;
# endif
//-------------------------------------------------------------------------------------
// ***** OVRMainMenu
//
///
/// OVRMainMenu is used to control the loading of different scenes. It also renders out
/// a menu that allows a user to modify various Rift settings, and allow for storing
/// these settings for recall later.
///
/// A user of this component can add as many scenes that they would like to be able to
/// have access to.
///
/// OVRMainMenu is currently attached to the OVRPlayerController prefab for convenience,
/// but can safely removed from it and added to another GameObject that is used for general
/// Unity logic.
///
///
public class OVRMainMenu : MonoBehaviour
{
///
/// The amount of time in seconds that it takes for the menu to fade in.
///
public float FadeInTime = 2.0f;
///
/// An optional texture that appears before the menu fades in.
///
public UnityEngine.Texture FadeInTexture = null;
///
/// An optional font that replaces Unity's default Arial.
///
public Font FontReplace = null;
///
/// The key that toggles the menu.
///
public KeyCode MenuKey = KeyCode.Space;
///
/// The key that quits the application.
///
public KeyCode QuitKey = KeyCode.Escape;
///
/// Scene names to show on-screen for each of the scenes in Scenes.
///
public string [] SceneNames;
///
/// The set of scenes that the user can jump to.
///
public string [] Scenes;
private bool ScenesVisible = false;
// Spacing for scenes menu
private int StartX = 490;
private int StartY = 250;
private int WidthX = 300;
private int WidthY = 23;
// Spacing for variables that users can change
private int VRVarsSX = 553;
private int VRVarsSY = 250;
private int VRVarsWidthX = 175;
private int VRVarsWidthY = 23;
private int StepY = 25;
// Handle to OVRCameraRig
private OVRCameraRig CameraController = null;
// Handle to OVRPlayerController
private OVRPlayerController PlayerController = null;
// Controller buttons
private bool PrevStartDown;
private bool PrevHatDown;
private bool PrevHatUp;
private bool ShowVRVars;
private bool OldSpaceHit;
// FPS
private float UpdateInterval = 0.5f;
private float Accum = 0;
private int Frames = 0;
private float TimeLeft = 0;
private string strFPS = "FPS: 0";
private string strIPD = "IPD: 0.000";
///
/// Prediction (in ms)
///
public float PredictionIncrement = 0.001f; // 1 ms
private string strPrediction = "Pred: OFF";
private string strFOV = "FOV: 0.0f";
private string strHeight = "Height: 0.0f";
///
/// Controls how quickly the player's speed and rotation change based on input.
///
public float SpeedRotationIncrement = 0.05f;
private string strSpeedRotationMultipler = "Spd. X: 0.0f Rot. X: 0.0f";
private bool LoadingLevel = false;
private float AlphaFadeValue = 1.0f;
private int CurrentLevel = 0;
// Rift detection
private bool HMDPresent = false;
private float RiftPresentTimeout = 0.0f;
private string strRiftPresent = "";
// Replace the GUI with our own texture and 3D plane that
// is attached to the rendder camera for true 3D placement
private OVRGUI GuiHelper = new OVRGUI();
private GameObject GUIRenderObject = null;
private RenderTexture GUIRenderTexture = null;
// We want to use new Unity GUI built in 4.6 for OVRMainMenu GUI
// Enable the UsingNewGUI option in the editor,
// if you want to use new GUI and Unity version is higher than 4.6
#if USE_NEW_GUI
private GameObject NewGUIObject = null;
private GameObject RiftPresentGUIObject = null;
#endif
///
/// We can set the layer to be anything we want to, this allows
/// a specific camera to render it.
///
public string LayerName = "Default";
///
/// Crosshair rendered onto 3D plane.
///
public UnityEngine.Texture CrosshairImage = null;
private OVRCrosshair Crosshair = new OVRCrosshair();
// Resolution Eye Texture
private string strResolutionEyeTexture = "Resolution: 0 x 0";
// Latency values
private string strLatencies = "Ren: 0.0f TWrp: 0.0f PostPresent: 0.0f";
// Vision mode on/off
private bool VisionMode = true;
#if SHOW_DK2_VARIABLES
private string strVisionMode = "Vision Enabled: ON";
#endif
// We want to hold onto GridCube, for potential sharing
// of the menu RenderTarget
OVRGridCube GridCube = null;
// We want to hold onto the VisionGuide so we can share
// the menu RenderTarget
OVRVisionGuide VisionGuide = null;
#region MonoBehaviour Message Handlers
///
/// Awake this instance.
///
void Awake()
{
// Find camera controller
OVRCameraRig[] CameraControllers;
CameraControllers = gameObject.GetComponentsInChildren();
if(CameraControllers.Length == 0)
Debug.LogWarning("OVRMainMenu: No OVRCameraRig attached.");
else if (CameraControllers.Length > 1)
Debug.LogWarning("OVRMainMenu: More then 1 OVRCameraRig attached.");
else{
CameraController = CameraControllers[0];
#if USE_NEW_GUI
OVRUGUI.CameraController = CameraController;
#endif
}
// Find player controller
OVRPlayerController[] PlayerControllers;
PlayerControllers = gameObject.GetComponentsInChildren();
if(PlayerControllers.Length == 0)
Debug.LogWarning("OVRMainMenu: No OVRPlayerController attached.");
else if (PlayerControllers.Length > 1)
Debug.LogWarning("OVRMainMenu: More then 1 OVRPlayerController attached.");
else{
PlayerController = PlayerControllers[0];
#if USE_NEW_GUI
OVRUGUI.PlayerController = PlayerController;
#endif
}
#if USE_NEW_GUI
// Create canvas for using new GUI
NewGUIObject = new GameObject();
NewGUIObject.name = "OVRGUIMain";
NewGUIObject.transform.parent = GameObject.Find("LeftEyeAnchor").transform;
RectTransform r = NewGUIObject.AddComponent();
r.sizeDelta = new Vector2(100f, 100f);
r.localScale = new Vector3(0.001f, 0.001f, 0.001f);
r.localPosition = new Vector3(0.01f, 0.17f, 0.53f);
r.localEulerAngles = Vector3.zero;
Canvas c = NewGUIObject.AddComponent