BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Util / OVRSceneSampleController.cs

OVRSceneSampleController.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRSceneSampleController.cs 6.2 KiB Download Raw
1/************************************************************************************
2Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
3
4Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
5the Utilities SDK except in compliance with the License, which is provided at the time of installation
6or download, or which otherwise accompanies this software in either electronic or hard copy form.
7
8You may obtain a copy of the License at
9https://developer.oculus.com/licenses/utilities-1.31
10
11Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
12under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
13ANY KIND, either express or implied. See the License for the specific language governing
14permissions and limitations under the License.
15************************************************************************************/
16
17using UnityEngine;
18using System.Collections;
19
20/// <summary>
21/// Sample that allows you to play with various VR settings.
22/// </summary>
23public class OVRSceneSampleController : MonoBehaviour
24{
25 /// <summary>
26 /// The key that quits the application.
27 /// </summary>
28 public KeyCode quitKey = KeyCode.Escape;
29
30 /// <summary>
31 /// An optional texture that appears before the menu fades in.
32 /// </summary>
33 public Texture fadeInTexture = null;
34
35 /// <summary>
36 /// Controls how quickly the player's speed and rotation change based on input.
37 /// </summary>
38 public float speedRotationIncrement = 0.05f;
39
40 private OVRPlayerController playerController = null;
41
42 // Handle to OVRCameraRig
43 private OVRCameraRig cameraController = null;
44
45 /// <summary>
46 /// We can set the layer to be anything we want to, this allows
47 /// a specific camera to render it.
48 /// </summary>
49 public string layerName = "Default";
50
51 // Vision mode on/off
52 private bool visionMode = true;
53
54 // We want to hold onto GridCube, for potential sharing
55 // of the menu RenderTarget
56 OVRGridCube gridCube = null;
57
58#if SHOW_DK2_VARIABLES
59 private string strVisionMode = "Vision Enabled: ON";
60#endif
61
62 #region MonoBehaviour Message Handlers
63 /// <summary>
64 /// Awake this instance.
65 /// </summary>
66 void Awake()
67 {
68 // Find camera controller
69 OVRCameraRig[] cameraControllers;
70 cameraControllers = gameObject.GetComponentsInChildren<OVRCameraRig>();
71
72 if (cameraControllers.Length == 0)
73 {
74 Debug.LogWarning("OVRMainMenu: No OVRCameraRig attached.");
75 }
76 else if (cameraControllers.Length > 1)
77 {
78 Debug.LogWarning("OVRMainMenu: More then 1 OVRCameraRig attached.");
79 }
80 else
81 {
82 cameraController = cameraControllers[0];
83 }
84
85 // Find player controller
86 OVRPlayerController[] playerControllers;
87 playerControllers = gameObject.GetComponentsInChildren<OVRPlayerController>();
88
89 if (playerControllers.Length == 0)
90 {
91 Debug.LogWarning("OVRMainMenu: No OVRPlayerController attached.");
92 }
93 else if (playerControllers.Length > 1)
94 {
95 Debug.LogWarning("OVRMainMenu: More then 1 OVRPlayerController attached.");
96 }
97 else
98 {
99 playerController = playerControllers[0];
100 }
101 }
102
103 /// <summary>
104 /// Start this instance.
105 /// </summary>
106 void Start()
107 {
108 // Make sure to hide cursor
109 if (Application.isEditor == false)
110 {
111 Cursor.visible = false;
112 Cursor.lockState = CursorLockMode.Locked;
113 }
114
115 // CameraController updates
116 if (cameraController != null)
117 {
118 // Add a GridCube component to this object
119 gridCube = gameObject.AddComponent<OVRGridCube>();
120 gridCube.SetOVRCameraController(ref cameraController);
121 }
122 }
123
124
125 /// <summary>
126 /// Update this instance.
127 /// </summary>
128 void Update()
129 {
130 // Recenter pose
131 UpdateRecenterPose();
132
133 // Turn On/Off Vision Mode
134 UpdateVisionMode();
135
136 // Update Speed and Rotation Scale
137 if (playerController != null)
138 UpdateSpeedAndRotationScaleMultiplier();
139
140 // Toggle Fullscreen
141 if (Input.GetKeyDown(KeyCode.F11))
142 Screen.fullScreen = !Screen.fullScreen;
143
144 if (Input.GetKeyDown(KeyCode.M))
145#if UNITY_2017_2_OR_NEWER
146 UnityEngine.XR.XRSettings.showDeviceView = !UnityEngine.XR.XRSettings.showDeviceView;
147#else
148 UnityEngine.VR.VRSettings.showDeviceView = !UnityEngine.VR.VRSettings.showDeviceView;
149#endif
150
151#if !UNITY_ANDROID || UNITY_EDITOR
152 // Escape Application
153 if (Input.GetKeyDown(quitKey))
154 Application.Quit();
155#endif
156 }
157 #endregion
158
159 /// <summary>
160 /// Updates the vision mode.
161 /// </summary>
162 void UpdateVisionMode()
163 {
164 if (Input.GetKeyDown(KeyCode.F2))
165 {
166 visionMode ^= visionMode;
167 OVRManager.tracker.isEnabled = visionMode;
168 }
169 }
170
171 /// <summary>
172 /// Updates the speed and rotation scale multiplier.
173 /// </summary>
174 void UpdateSpeedAndRotationScaleMultiplier()
175 {
176 float moveScaleMultiplier = 0.0f;
177 playerController.GetMoveScaleMultiplier(ref moveScaleMultiplier);
178
179 if (Input.GetKeyDown(KeyCode.Alpha7))
180 {
181 moveScaleMultiplier -= speedRotationIncrement;
182 }
183 else if (Input.GetKeyDown(KeyCode.Alpha8))
184 {
185 moveScaleMultiplier += speedRotationIncrement;
186 }
187
188 playerController.SetMoveScaleMultiplier(moveScaleMultiplier);
189
190 float rotationScaleMultiplier = 0.0f;
191 playerController.GetRotationScaleMultiplier(ref rotationScaleMultiplier);
192
193 if (Input.GetKeyDown(KeyCode.Alpha9))
194 {
195 rotationScaleMultiplier -= speedRotationIncrement;
196 }
197 else if (Input.GetKeyDown(KeyCode.Alpha0))
198 {
199 rotationScaleMultiplier += speedRotationIncrement;
200 }
201
202 playerController.SetRotationScaleMultiplier(rotationScaleMultiplier);
203 }
204
205 /// <summary>
206 /// Recenter pose
207 /// </summary>
208 void UpdateRecenterPose()
209 {
210 if (Input.GetKeyDown(KeyCode.R))
211 OVRManager.display.RecenterPose();
212 }
213}