| 1 | /************************************************************************************ |
| 2 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 3 | |
| 4 | Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use |
| 5 | the Utilities SDK except in compliance with the License, which is provided at the time of installation |
| 6 | or download, or which otherwise accompanies this software in either electronic or hard copy form. |
| 7 | |
| 8 | You may obtain a copy of the License at |
| 9 | https://developer.oculus.com/licenses/utilities-1.31 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, the Utilities SDK distributed |
| 12 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 13 | ANY KIND, either express or implied. See the License for the specific language governing |
| 14 | permissions and limitations under the License. |
| 15 | ************************************************************************************/ |
| 16 | |
| 17 | using UnityEngine; |
| 18 | using System.Collections; |
| 19 | using UnityEngine.UI; |
| 20 | |
| 21 | //------------------------------------------------------------------------------------- |
| 22 | /// <summary> |
| 23 | /// Shows debug information on a heads-up display. |
| 24 | /// </summary> |
| 25 | public class OVRDebugInfo : MonoBehaviour |
| 26 | { |
| 27 | #region GameObjects for Debug Information UIs |
| 28 | GameObject debugUIManager; |
| 29 | GameObject debugUIObject; |
| 30 | GameObject riftPresent; |
| 31 | GameObject fps; |
| 32 | GameObject ipd; |
| 33 | GameObject fov; |
| 34 | GameObject height; |
| 35 | GameObject depth; |
| 36 | GameObject resolutionEyeTexture; |
| 37 | GameObject latencies; |
| 38 | GameObject texts; |
| 39 | #endregion |
| 40 | |
| 41 | #region Debug strings |
| 42 | string strRiftPresent = null; // "VR DISABLED" |
| 43 | string strFPS = null; // "FPS: 0"; |
| 44 | string strIPD = null; // "IPD: 0.000"; |
| 45 | string strFOV = null; // "FOV: 0.0f"; |
| 46 | string strHeight = null; // "Height: 0.0f"; |
| 47 | string strDepth = null; // "Depth: 0.0f"; |
| 48 | string strResolutionEyeTexture = null; // "Resolution : {0} x {1}" |
| 49 | string strLatencies = null; // "R: {0:F3} TW: {1:F3} PP: {2:F3} RE: {3:F3} TWE: {4:F3}" |
| 50 | #endregion |
| 51 | |
| 52 | /// <summary> |
| 53 | /// Variables for FPS |
| 54 | /// </summary> |
| 55 | float updateInterval = 0.5f; |
| 56 | float accum = 0.0f; |
| 57 | int frames = 0; |
| 58 | float timeLeft = 0.0f; |
| 59 | |
| 60 | /// <summary> |
| 61 | /// Managing for UI initialization |
| 62 | /// </summary> |
| 63 | bool initUIComponent = false; |
| 64 | bool isInited = false; |
| 65 | |
| 66 | /// <summary> |
| 67 | /// UIs Y offset |
| 68 | /// </summary> |
| 69 | float offsetY = 55.0f; |
| 70 | |
| 71 | /// <summary> |
| 72 | /// Managing for rift detection UI |
| 73 | /// </summary> |
| 74 | float riftPresentTimeout = 0.0f; |
| 75 | |
| 76 | /// <summary> |
| 77 | /// Turn on / off VR variables |
| 78 | /// </summary> |
| 79 | bool showVRVars = false; |
| 80 | |
| 81 | #region MonoBehaviour handler |
| 82 | |
| 83 | /// <summary> |
| 84 | /// Initialization |
| 85 | /// </summary> |
| 86 | void Awake() |
| 87 | { |
| 88 | // Create canvas for using new GUI |
| 89 | debugUIManager = new GameObject(); |
| 90 | debugUIManager.name = "DebugUIManager"; |
| 91 | debugUIManager.transform.parent = GameObject.Find("LeftEyeAnchor").transform; |
| 92 | |
| 93 | RectTransform rectTransform = debugUIManager.AddComponent<RectTransform>(); |
| 94 | rectTransform.sizeDelta = new Vector2(100f, 100f); |
| 95 | rectTransform.localScale = new Vector3(0.001f, 0.001f, 0.001f); |
| 96 | rectTransform.localPosition = new Vector3(0.01f, 0.17f, 0.53f); |
| 97 | rectTransform.localEulerAngles = Vector3.zero; |
| 98 | |
| 99 | Canvas canvas = debugUIManager.AddComponent<Canvas>(); |
| 100 | canvas.renderMode = RenderMode.WorldSpace; |
| 101 | canvas.pixelPerfect = false; |
| 102 | } |
| 103 | |
| 104 | /// <summary> |
| 105 | /// Updating VR variables and managing UI present |
| 106 | /// </summary> |
| 107 | void Update() |
| 108 | { |
| 109 | if (initUIComponent && !isInited) |
| 110 | { |
| 111 | InitUIComponents(); |
| 112 | } |
| 113 | |
| 114 | if (Input.GetKeyDown(KeyCode.Space) && riftPresentTimeout < 0.0f) |
| 115 | { |
| 116 | initUIComponent = true; |
| 117 | showVRVars ^= true; |
| 118 | } |
| 119 | |
| 120 | UpdateDeviceDetection(); |
| 121 | |
| 122 | // Presenting VR variables |
| 123 | if (showVRVars) |
| 124 | { |
| 125 | debugUIManager.SetActive(true); |
| 126 | UpdateVariable(); |
| 127 | UpdateStrings(); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | debugUIManager.SetActive(false); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// <summary> |
| 136 | /// Initialize isInited value on OnDestroy |
| 137 | /// </summary> |
| 138 | void OnDestroy() |
| 139 | { |
| 140 | isInited = false; |
| 141 | } |
| 142 | #endregion |
| 143 | |
| 144 | #region Private Functions |
| 145 | /// <summary> |
| 146 | /// Initialize UI GameObjects |
| 147 | /// </summary> |
| 148 | void InitUIComponents() |
| 149 | { |
| 150 | float posY = 0.0f; |
| 151 | int fontSize = 20; |
| 152 | |
| 153 | debugUIObject = new GameObject(); |
| 154 | debugUIObject.name = "DebugInfo"; |
| 155 | debugUIObject.transform.parent = GameObject.Find("DebugUIManager").transform; |
| 156 | debugUIObject.transform.localPosition = new Vector3(0.0f, 100.0f, 0.0f); |
| 157 | debugUIObject.transform.localEulerAngles = Vector3.zero; |
| 158 | debugUIObject.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); |
| 159 | |
| 160 | // Print out for FPS |
| 161 | if (!string.IsNullOrEmpty(strFPS)) |
| 162 | { |
| 163 | fps = VariableObjectManager(fps, "FPS", posY -= offsetY, strFPS, fontSize); |
| 164 | } |
| 165 | |
| 166 | // Print out for IPD |
| 167 | if (!string.IsNullOrEmpty(strIPD)) |
| 168 | { |
| 169 | ipd = VariableObjectManager(ipd, "IPD", posY -= offsetY, strIPD, fontSize); |
| 170 | } |
| 171 | |
| 172 | // Print out for FOV |
| 173 | if (!string.IsNullOrEmpty(strFOV)) |
| 174 | { |
| 175 | fov = VariableObjectManager(fov, "FOV", posY -= offsetY, strFOV, fontSize); |
| 176 | } |
| 177 | |
| 178 | // Print out for Height |
| 179 | if (!string.IsNullOrEmpty(strHeight)) |
| 180 | { |
| 181 | height = VariableObjectManager(height, "Height", posY -= offsetY, strHeight, fontSize); |
| 182 | } |
| 183 | |
| 184 | // Print out for Depth |
| 185 | if (!string.IsNullOrEmpty(strDepth)) |
| 186 | { |
| 187 | depth = VariableObjectManager(depth, "Depth", posY -= offsetY, strDepth, fontSize); |
| 188 | } |
| 189 | |
| 190 | // Print out for Resoulution of Eye Texture |
| 191 | if (!string.IsNullOrEmpty(strResolutionEyeTexture)) |
| 192 | { |
| 193 | resolutionEyeTexture = VariableObjectManager(resolutionEyeTexture, "Resolution", posY -= offsetY, strResolutionEyeTexture, fontSize); |
| 194 | } |
| 195 | |
| 196 | // Print out for Latency |
| 197 | if (!string.IsNullOrEmpty(strLatencies)) |
| 198 | { |
| 199 | latencies = VariableObjectManager(latencies, "Latency", posY -= offsetY, strLatencies, 17); |
| 200 | posY = 0.0f; |
| 201 | } |
| 202 | |
| 203 | initUIComponent = false; |
| 204 | isInited = true; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | /// <summary> |
| 209 | /// Update VR Variables |
| 210 | /// </summary> |
| 211 | void UpdateVariable() |
| 212 | { |
| 213 | UpdateIPD(); |
| 214 | UpdateEyeHeightOffset(); |
| 215 | UpdateEyeDepthOffset(); |
| 216 | UpdateFOV(); |
| 217 | UpdateResolutionEyeTexture(); |
| 218 | UpdateLatencyValues(); |
| 219 | UpdateFPS(); |
| 220 | } |
| 221 | |
| 222 | /// <summary> |
| 223 | /// Update Strings |
| 224 | /// </summary> |
| 225 | void UpdateStrings() |
| 226 | { |
| 227 | if (debugUIObject == null) |
| 228 | return; |
| 229 | |
| 230 | if (!string.IsNullOrEmpty(strFPS)) |
| 231 | fps.GetComponentInChildren<Text>().text = strFPS; |
| 232 | if (!string.IsNullOrEmpty(strIPD)) |
| 233 | ipd.GetComponentInChildren<Text>().text = strIPD; |
| 234 | if (!string.IsNullOrEmpty(strFOV)) |
| 235 | fov.GetComponentInChildren<Text>().text = strFOV; |
| 236 | if (!string.IsNullOrEmpty(strResolutionEyeTexture)) |
| 237 | resolutionEyeTexture.GetComponentInChildren<Text>().text = strResolutionEyeTexture; |
| 238 | if (!string.IsNullOrEmpty(strLatencies)) |
| 239 | { |
| 240 | latencies.GetComponentInChildren<Text>().text = strLatencies; |
| 241 | latencies.GetComponentInChildren<Text>().fontSize = 14; |
| 242 | } |
| 243 | if (!string.IsNullOrEmpty(strHeight)) |
| 244 | height.GetComponentInChildren<Text>().text = strHeight; |
| 245 | if (!string.IsNullOrEmpty(strDepth)) |
| 246 | depth.GetComponentInChildren<Text>().text = strDepth; |
| 247 | } |
| 248 | |
| 249 | /// <summary> |
| 250 | /// It's for rift present GUI |
| 251 | /// </summary> |
| 252 | void RiftPresentGUI(GameObject guiMainOBj) |
| 253 | { |
| 254 | riftPresent = ComponentComposition(riftPresent); |
| 255 | riftPresent.transform.SetParent(guiMainOBj.transform); |
| 256 | riftPresent.name = "RiftPresent"; |
| 257 | RectTransform rectTransform = riftPresent.GetComponent<RectTransform>(); |
| 258 | rectTransform.localPosition = new Vector3(0.0f, 0.0f, 0.0f); |
| 259 | rectTransform.localScale = new Vector3(1.0f, 1.0f, 1.0f); |
| 260 | rectTransform.localEulerAngles = Vector3.zero; |
| 261 | |
| 262 | Text text = riftPresent.GetComponentInChildren<Text>(); |
| 263 | text.text = strRiftPresent; |
| 264 | text.fontSize = 20; |
| 265 | } |
| 266 | |
| 267 | /// <summary> |
| 268 | /// Updates the device detection. |
| 269 | /// </summary> |
| 270 | void UpdateDeviceDetection() |
| 271 | { |
| 272 | if (riftPresentTimeout >= 0.0f) |
| 273 | { |
| 274 | riftPresentTimeout -= Time.deltaTime; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /// <summary> |
| 279 | /// Object Manager for Variables |
| 280 | /// </summary> |
| 281 | /// <returns> gameobject for each Variable </returns> |
| 282 | GameObject VariableObjectManager(GameObject gameObject, string name, float posY, string str, int fontSize) |
| 283 | { |
| 284 | gameObject = ComponentComposition(gameObject); |
| 285 | gameObject.name = name; |
| 286 | gameObject.transform.SetParent(debugUIObject.transform); |
| 287 | |
| 288 | RectTransform rectTransform = gameObject.GetComponent<RectTransform>(); |
| 289 | rectTransform.localPosition = new Vector3(0.0f, posY -= offsetY, 0.0f); |
| 290 | |
| 291 | Text text = gameObject.GetComponentInChildren<Text>(); |
| 292 | text.text = str; |
| 293 | text.fontSize = fontSize; |
| 294 | gameObject.transform.localEulerAngles = Vector3.zero; |
| 295 | |
| 296 | rectTransform.localScale = new Vector3(1.0f, 1.0f, 1.0f); |
| 297 | |
| 298 | return gameObject; |
| 299 | } |
| 300 | |
| 301 | /// <summary> |
| 302 | /// Component composition |
| 303 | /// </summary> |
| 304 | /// <returns> Composed gameobject. </returns> |
| 305 | GameObject ComponentComposition(GameObject GO) |
| 306 | { |
| 307 | GO = new GameObject(); |
| 308 | GO.AddComponent<RectTransform>(); |
| 309 | GO.AddComponent<CanvasRenderer>(); |
| 310 | GO.AddComponent<Image>(); |
| 311 | GO.GetComponent<RectTransform>().sizeDelta = new Vector2(350f, 50f); |
| 312 | GO.GetComponent<Image>().color = new Color(7f / 255f, 45f / 255f, 71f / 255f, 200f / 255f); |
| 313 | |
| 314 | texts = new GameObject(); |
| 315 | texts.AddComponent<RectTransform>(); |
| 316 | texts.AddComponent<CanvasRenderer>(); |
| 317 | texts.AddComponent<Text>(); |
| 318 | texts.GetComponent<RectTransform>().sizeDelta = new Vector2(350f, 50f); |
| 319 | texts.GetComponent<Text>().font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font; |
| 320 | texts.GetComponent<Text>().alignment = TextAnchor.MiddleCenter; |
| 321 | |
| 322 | texts.transform.SetParent(GO.transform); |
| 323 | texts.name = "TextBox"; |
| 324 | |
| 325 | return GO; |
| 326 | } |
| 327 | #endregion |
| 328 | |
| 329 | #region Debugging variables handler |
| 330 | /// <summary> |
| 331 | /// Updates the IPD. |
| 332 | /// </summary> |
| 333 | void UpdateIPD() |
| 334 | { |
| 335 | strIPD = System.String.Format("IPD (mm): {0:F4}", OVRManager.profile.ipd * 1000.0f); |
| 336 | } |
| 337 | |
| 338 | /// <summary> |
| 339 | /// Updates the eye height offset. |
| 340 | /// </summary> |
| 341 | void UpdateEyeHeightOffset() |
| 342 | { |
| 343 | float eyeHeight = OVRManager.profile.eyeHeight; |
| 344 | strHeight = System.String.Format("Eye Height (m): {0:F3}", eyeHeight); |
| 345 | } |
| 346 | |
| 347 | /// <summary> |
| 348 | /// Updates the eye depth offset. |
| 349 | /// </summary> |
| 350 | void UpdateEyeDepthOffset() |
| 351 | { |
| 352 | float eyeDepth = OVRManager.profile.eyeDepth; |
| 353 | strDepth = System.String.Format("Eye Depth (m): {0:F3}", eyeDepth); |
| 354 | } |
| 355 | |
| 356 | /// <summary> |
| 357 | /// Updates the FOV. |
| 358 | /// </summary> |
| 359 | void UpdateFOV() |
| 360 | { |
| 361 | #if UNITY_2017_2_OR_NEWER |
| 362 | OVRDisplay.EyeRenderDesc eyeDesc = OVRManager.display.GetEyeRenderDesc(UnityEngine.XR.XRNode.LeftEye); |
| 363 | #else |
| 364 | OVRDisplay.EyeRenderDesc eyeDesc = OVRManager.display.GetEyeRenderDesc(UnityEngine.VR.VRNode.LeftEye); |
| 365 | #endif |
| 366 | strFOV = System.String.Format("FOV (deg): {0:F3}", eyeDesc.fov.y); |
| 367 | } |
| 368 | |
| 369 | /// <summary> |
| 370 | /// Updates resolution of eye texture |
| 371 | /// </summary> |
| 372 | void UpdateResolutionEyeTexture() |
| 373 | { |
| 374 | #if UNITY_2017_2_OR_NEWER |
| 375 | OVRDisplay.EyeRenderDesc leftEyeDesc = OVRManager.display.GetEyeRenderDesc(UnityEngine.XR.XRNode.LeftEye); |
| 376 | OVRDisplay.EyeRenderDesc rightEyeDesc = OVRManager.display.GetEyeRenderDesc(UnityEngine.XR.XRNode.RightEye); |
| 377 | |
| 378 | float scale = UnityEngine.XR.XRSettings.renderViewportScale; |
| 379 | #else |
| 380 | OVRDisplay.EyeRenderDesc leftEyeDesc = OVRManager.display.GetEyeRenderDesc(UnityEngine.VR.VRNode.LeftEye); |
| 381 | OVRDisplay.EyeRenderDesc rightEyeDesc = OVRManager.display.GetEyeRenderDesc(UnityEngine.VR.VRNode.RightEye); |
| 382 | |
| 383 | float scale = UnityEngine.VR.VRSettings.renderViewportScale; |
| 384 | #endif |
| 385 | float w = (int)(scale * (float)(leftEyeDesc.resolution.x + rightEyeDesc.resolution.x)); |
| 386 | float h = (int)(scale * (float)Mathf.Max(leftEyeDesc.resolution.y, rightEyeDesc.resolution.y)); |
| 387 | |
| 388 | strResolutionEyeTexture = System.String.Format("Resolution : {0} x {1}", w, h); |
| 389 | } |
| 390 | |
| 391 | /// <summary> |
| 392 | /// Updates latency values |
| 393 | /// </summary> |
| 394 | void UpdateLatencyValues() |
| 395 | { |
| 396 | #if !UNITY_ANDROID || UNITY_EDITOR |
| 397 | OVRDisplay.LatencyData latency = OVRManager.display.latency; |
| 398 | if (latency.render < 0.000001f && latency.timeWarp < 0.000001f && latency.postPresent < 0.000001f) |
| 399 | strLatencies = System.String.Format("Latency values are not available."); |
| 400 | else |
| 401 | strLatencies = System.String.Format("Render: {0:F3} TimeWarp: {1:F3} Post-Present: {2:F3}\nRender Error: {3:F3} TimeWarp Error: {4:F3}", |
| 402 | latency.render, |
| 403 | latency.timeWarp, |
| 404 | latency.postPresent, |
| 405 | latency.renderError, |
| 406 | latency.timeWarpError); |
| 407 | #endif |
| 408 | } |
| 409 | |
| 410 | /// <summary> |
| 411 | /// Updates the FPS. |
| 412 | /// </summary> |
| 413 | void UpdateFPS() |
| 414 | { |
| 415 | timeLeft -= Time.unscaledDeltaTime; |
| 416 | accum += Time.unscaledDeltaTime; |
| 417 | ++frames; |
| 418 | |
| 419 | // Interval ended - update GUI text and start new interval |
| 420 | if (timeLeft <= 0.0) |
| 421 | { |
| 422 | // display two fractional digits (f2 format) |
| 423 | float fps = frames / accum; |
| 424 | |
| 425 | strFPS = System.String.Format("FPS: {0:F2}", fps); |
| 426 | |
| 427 | timeLeft += updateInterval; |
| 428 | accum = 0.0f; |
| 429 | frames = 0; |
| 430 | } |
| 431 | } |
| 432 | #endregion |
| 433 | } |