| 1 | /************************************************************************************ |
| 2 | |
| 3 | Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 4 | |
| 5 | See SampleFramework license.txt for license terms. Unless required by applicable law |
| 6 | or agreed to in writing, the sample code is provided “AS IS” WITHOUT WARRANTIES OR |
| 7 | CONDITIONS OF ANY KIND, either express or implied. See the license for specific |
| 8 | language governing permissions and limitations under the license. |
| 9 | |
| 10 | ************************************************************************************/ |
| 11 | using System.Collections; |
| 12 | using System.Collections.Generic; |
| 13 | using UnityEngine; |
| 14 | using UnityEngine.UI; |
| 15 | using System; |
| 16 | |
| 17 | namespace OculusSampleFramework |
| 18 | { |
| 19 | |
| 20 | /// <summary> |
| 21 | /// The rendering methods swappable via radio buttons |
| 22 | /// </summary> |
| 23 | public enum EUiDisplayType |
| 24 | { |
| 25 | EUDT_WorldGeoQuad, |
| 26 | EUDT_OverlayQuad, |
| 27 | EUDT_None, |
| 28 | EUDT_MaxDislayTypes |
| 29 | } |
| 30 | |
| 31 | /// <summary> |
| 32 | /// Usage: demonstrate how to use overlay layers for a paneled UI system |
| 33 | /// On Mobile, we support both Cylinder layer and Quad layer |
| 34 | /// Press any button: it will cycle [world geometry Quad]->[overlay layer Quad]->[world geometry cylinder]->[overlay layer cylinder] |
| 35 | /// On PC, only Quad layer is supported |
| 36 | /// Press any button: it will cycle [world geometry Quad]->[overlay layer Quad] |
| 37 | /// |
| 38 | /// You should be able to observe sharper and less aliased image when switch from world geometry to overlay layer. |
| 39 | /// |
| 40 | /// </summary> |
| 41 | public class OVROverlaySample : MonoBehaviour |
| 42 | { |
| 43 | |
| 44 | bool inMenu; |
| 45 | |
| 46 | /// <summary> |
| 47 | /// The string identifiers for DebugUI radio buttons |
| 48 | /// </summary> |
| 49 | const string ovrOverlayID = "OVROverlayID"; |
| 50 | const string applicationID = "ApplicationID"; |
| 51 | const string noneID = "NoneID"; |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Toggle references |
| 55 | /// </summary> |
| 56 | Toggle applicationRadioButton; |
| 57 | Toggle noneRadioButton; |
| 58 | |
| 59 | [Header("App vs Compositor Comparison Settings")] |
| 60 | /// <summary> |
| 61 | /// The main camera used to calculate reprojected OVROverlay quad |
| 62 | /// </summary> |
| 63 | public GameObject mainCamera; |
| 64 | |
| 65 | /// <summary> |
| 66 | /// The camera used to render UI panels |
| 67 | /// </summary> |
| 68 | public GameObject uiCamera; |
| 69 | |
| 70 | /// <summary> |
| 71 | /// The parents of grouped UI panels |
| 72 | /// </summary> |
| 73 | public GameObject uiGeoParent; |
| 74 | public GameObject worldspaceGeoParent; |
| 75 | |
| 76 | /// <summary> |
| 77 | /// The OVROverlay component to pass the uiCamera rendered RT to |
| 78 | /// </summary> |
| 79 | public OVROverlay cameraRenderOverlay; |
| 80 | |
| 81 | /// <summary> |
| 82 | /// The OVROverlay component displaying which rendering mode is active |
| 83 | /// </summary> |
| 84 | public OVROverlay renderingLabelOverlay; |
| 85 | |
| 86 | /// <summary> |
| 87 | /// The quad textures to indicate the active rendering method |
| 88 | /// </summary> |
| 89 | public Texture applicationLabelTexture; |
| 90 | public Texture compositorLabelTexture; |
| 91 | |
| 92 | /// <summary> |
| 93 | /// The resources & settings needed for the level loading simulation demo |
| 94 | /// </summary> |
| 95 | [Header("Level Loading Sim Settings")] |
| 96 | public GameObject prefabForLevelLoadSim; |
| 97 | public OVROverlay cubemapOverlay; |
| 98 | public OVROverlay loadingTextQuadOverlay; |
| 99 | public float distanceFromCamToLoadText; |
| 100 | public float cubeSpawnRadius; |
| 101 | public float heightBetweenItems; |
| 102 | public int numObjectsPerLevel; |
| 103 | public int numLevels; |
| 104 | public int numLoopsTrigger = 500000000; |
| 105 | List<GameObject> spawnedCubes = new List<GameObject>(); |
| 106 | |
| 107 | #region MonoBehaviour handler |
| 108 | |
| 109 | void Start() |
| 110 | { |
| 111 | DebugUIBuilder.instance.AddLabel("OVROverlay Sample"); |
| 112 | DebugUIBuilder.instance.AddDivider(); |
| 113 | DebugUIBuilder.instance.AddLabel("Level Loading Example"); |
| 114 | DebugUIBuilder.instance.AddButton("Simulate Level Load", TriggerLoad); |
| 115 | DebugUIBuilder.instance.AddButton("Destroy Cubes", TriggerUnload); |
| 116 | DebugUIBuilder.instance.AddDivider(); |
| 117 | DebugUIBuilder.instance.AddLabel("OVROverlay vs. Application Render Comparison"); |
| 118 | DebugUIBuilder.instance.AddRadio("OVROverlay", "group", delegate (Toggle t) { RadioPressed(ovrOverlayID, "group", t); }).GetComponentInChildren<Toggle>(); |
| 119 | applicationRadioButton = DebugUIBuilder.instance.AddRadio("Application", "group", delegate (Toggle t) { RadioPressed(applicationID, "group", t); }).GetComponentInChildren<Toggle>(); |
| 120 | noneRadioButton = DebugUIBuilder.instance.AddRadio("None", "group", delegate (Toggle t) { RadioPressed(noneID, "group", t); }).GetComponentInChildren<Toggle>(); |
| 121 | |
| 122 | DebugUIBuilder.instance.Show(); |
| 123 | |
| 124 | // Start with Overlay Quad |
| 125 | CameraAndRenderTargetSetup(); |
| 126 | cameraRenderOverlay.enabled = true; |
| 127 | cameraRenderOverlay.currentOverlayShape = OVROverlay.OverlayShape.Quad; |
| 128 | spawnedCubes.Capacity = numObjectsPerLevel * numLevels; |
| 129 | } |
| 130 | |
| 131 | void Update() |
| 132 | { |
| 133 | // Switch ui display types |
| 134 | if (OVRInput.GetDown(OVRInput.Button.Two) || OVRInput.GetDown(OVRInput.Button.Start)) |
| 135 | { |
| 136 | if (inMenu) DebugUIBuilder.instance.Hide(); |
| 137 | else DebugUIBuilder.instance.Show(); |
| 138 | inMenu = !inMenu; |
| 139 | } |
| 140 | |
| 141 | // Trigger loading simulator via keyboard |
| 142 | if (Input.GetKeyDown(KeyCode.A)) |
| 143 | { |
| 144 | TriggerLoad(); |
| 145 | } |
| 146 | } |
| 147 | #endregion |
| 148 | |
| 149 | #region Private Functions |
| 150 | |
| 151 | /// <summary> |
| 152 | /// Usage: Activate the world geometry and deactivate OVROverlay display |
| 153 | /// </summary> |
| 154 | void ActivateWorldGeo() |
| 155 | { |
| 156 | worldspaceGeoParent.SetActive(true); |
| 157 | uiGeoParent.SetActive(false); |
| 158 | uiCamera.SetActive(false); |
| 159 | cameraRenderOverlay.enabled = false; |
| 160 | renderingLabelOverlay.enabled = true; |
| 161 | renderingLabelOverlay.textures[0] = applicationLabelTexture; |
| 162 | Debug.Log("Switched to ActivateWorldGeo"); |
| 163 | } |
| 164 | |
| 165 | /// <summary> |
| 166 | /// Usage: Activate OVROverlay display and deactivate the world geometry |
| 167 | /// </summary> |
| 168 | void ActivateOVROverlay() |
| 169 | { |
| 170 | worldspaceGeoParent.SetActive(false); |
| 171 | uiCamera.SetActive(true); |
| 172 | cameraRenderOverlay.enabled = true; |
| 173 | uiGeoParent.SetActive(true); |
| 174 | renderingLabelOverlay.enabled = true; |
| 175 | renderingLabelOverlay.textures[0] = compositorLabelTexture; |
| 176 | Debug.Log("Switched to ActivateOVROVerlay"); |
| 177 | } |
| 178 | |
| 179 | /// <summary> |
| 180 | /// Usage: Deactivate both world geometry and OVROverlay display |
| 181 | /// </summary> |
| 182 | void ActivateNone() |
| 183 | { |
| 184 | worldspaceGeoParent.SetActive(false); |
| 185 | uiCamera.SetActive(false); |
| 186 | cameraRenderOverlay.enabled = false; |
| 187 | uiGeoParent.SetActive(false); |
| 188 | renderingLabelOverlay.enabled = false; |
| 189 | Debug.Log("Switched to ActivateNone"); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /// <summary> |
| 194 | /// This function is to simulate a level load event in Unity |
| 195 | /// The idea is to enable a cubemap overlay right before any action that will stall the main thread |
| 196 | /// This cubemap overlay can be combined with other OVROverlay objects, such as animated textures to indicate "Loading..." |
| 197 | /// </summary> |
| 198 | void TriggerLoad() |
| 199 | { |
| 200 | StartCoroutine(WaitforOVROverlay()); |
| 201 | } |
| 202 | |
| 203 | IEnumerator WaitforOVROverlay() |
| 204 | { |
| 205 | Transform camTransform = mainCamera.transform; |
| 206 | Transform uiTextOverlayTrasnform = loadingTextQuadOverlay.transform; |
| 207 | Vector3 newPos = camTransform.position + camTransform.forward * distanceFromCamToLoadText; |
| 208 | newPos.y = camTransform.position.y; |
| 209 | uiTextOverlayTrasnform.position = newPos; |
| 210 | cubemapOverlay.enabled = true; |
| 211 | loadingTextQuadOverlay.enabled = true; |
| 212 | noneRadioButton.isOn = true; |
| 213 | yield return new WaitForSeconds(0.1f); |
| 214 | ClearObjects(); |
| 215 | SimulateLevelLoad(); |
| 216 | cubemapOverlay.enabled = false; |
| 217 | loadingTextQuadOverlay.enabled = false; |
| 218 | yield return null; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /// <summary> |
| 223 | /// Usage: Destroy all loaded resources and switch back to world geometry rendering mode. |
| 224 | /// </summary> |
| 225 | void TriggerUnload() |
| 226 | { |
| 227 | ClearObjects(); |
| 228 | applicationRadioButton.isOn = true; |
| 229 | } |
| 230 | |
| 231 | /// <summary> |
| 232 | /// Usage: Recreate UI render target according overlay type and overlay size |
| 233 | /// </summary> |
| 234 | void CameraAndRenderTargetSetup() |
| 235 | { |
| 236 | float overlayWidth = cameraRenderOverlay.transform.localScale.x; |
| 237 | float overlayHeight = cameraRenderOverlay.transform.localScale.y; |
| 238 | float overlayRadius = cameraRenderOverlay.transform.localScale.z; |
| 239 | |
| 240 | #if UNITY_ANDROID |
| 241 | // Gear VR display panel resolution |
| 242 | float hmdPanelResWidth = 2560; |
| 243 | float hmdPanelResHeight = 1440; |
| 244 | #else |
| 245 | // Rift display panel resolution |
| 246 | float hmdPanelResWidth = 2160; |
| 247 | float hmdPanelResHeight = 1200; |
| 248 | #endif |
| 249 | |
| 250 | float singleEyeScreenPhysicalResX = hmdPanelResWidth * 0.5f; |
| 251 | float singleEyeScreenPhysicalResY = hmdPanelResHeight; |
| 252 | |
| 253 | // Calculate RT Height |
| 254 | // screenSizeYInWorld : how much world unity the full screen can cover at overlayQuad's location vertically |
| 255 | // pixelDensityY: pixels / world unit ( meter ) |
| 256 | |
| 257 | float halfFovY = mainCamera.GetComponent<Camera>().fieldOfView / 2; |
| 258 | float screenSizeYInWorld = 2 * overlayRadius * Mathf.Tan(Mathf.Deg2Rad * halfFovY); |
| 259 | float pixelDensityYPerWorldUnit = singleEyeScreenPhysicalResY / screenSizeYInWorld; |
| 260 | float renderTargetHeight = pixelDensityYPerWorldUnit * overlayWidth; |
| 261 | |
| 262 | // Calculate RT width |
| 263 | float renderTargetWidth = 0.0f; |
| 264 | |
| 265 | // screenSizeXInWorld : how much world unity the full screen can cover at overlayQuad's location horizontally |
| 266 | // pixelDensityY: pixels / world unit ( meter ) |
| 267 | |
| 268 | float screenSizeXInWorld = screenSizeYInWorld * mainCamera.GetComponent<Camera>().aspect; |
| 269 | float pixelDensityXPerWorldUnit = singleEyeScreenPhysicalResX / screenSizeXInWorld; |
| 270 | renderTargetWidth = pixelDensityXPerWorldUnit * overlayWidth; |
| 271 | |
| 272 | // Compute the orthographic size for the camera |
| 273 | float orthographicSize = overlayHeight / 2.0f; |
| 274 | float orthoCameraAspect = overlayWidth / overlayHeight; |
| 275 | uiCamera.GetComponent<Camera>().orthographicSize = orthographicSize; |
| 276 | uiCamera.GetComponent<Camera>().aspect = orthoCameraAspect; |
| 277 | |
| 278 | if (uiCamera.GetComponent<Camera>().targetTexture != null) |
| 279 | uiCamera.GetComponent<Camera>().targetTexture.Release(); |
| 280 | |
| 281 | RenderTexture overlayRT = new RenderTexture( |
| 282 | (int)renderTargetWidth * 2, |
| 283 | (int)renderTargetHeight * 2, |
| 284 | 0, |
| 285 | RenderTextureFormat.ARGB32, |
| 286 | RenderTextureReadWrite.sRGB); |
| 287 | Debug.Log("Created RT of resolution w: " + renderTargetWidth + " and h: " + renderTargetHeight); |
| 288 | |
| 289 | overlayRT.hideFlags = HideFlags.DontSave; |
| 290 | overlayRT.useMipMap = true; |
| 291 | overlayRT.filterMode = FilterMode.Trilinear; |
| 292 | overlayRT.anisoLevel = 4; |
| 293 | #if UNITY_5_5_OR_NEWER |
| 294 | overlayRT.autoGenerateMips = true; |
| 295 | #else |
| 296 | overlayRT.generateMips = true; |
| 297 | #endif |
| 298 | uiCamera.GetComponent<Camera>().targetTexture = overlayRT; |
| 299 | |
| 300 | cameraRenderOverlay.textures[0] = overlayRT; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /// <summary> |
| 305 | /// Usage: block main thread with an empty for loop and generate a bunch of cubes around the player. |
| 306 | /// </summary> |
| 307 | void SimulateLevelLoad() |
| 308 | { |
| 309 | int numToPrint = 0; |
| 310 | for (int p = 0; p < numLoopsTrigger; p++) |
| 311 | { |
| 312 | numToPrint++; |
| 313 | } |
| 314 | Debug.Log("Finished " + numToPrint + " Loops"); |
| 315 | Vector3 playerPos = mainCamera.transform.position; |
| 316 | playerPos.y = 0.5f; |
| 317 | // Generate a bunch of blocks, "blocking" the mainthread ;) |
| 318 | for (int j = 0; j < numLevels; j++) |
| 319 | { |
| 320 | for (var i = 0; i < numObjectsPerLevel; i++) |
| 321 | { |
| 322 | var angle = i * Mathf.PI * 2 / numObjectsPerLevel; |
| 323 | float stagger = (i % 2 == 0) ? 1.5f : 1.0f; |
| 324 | var pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * cubeSpawnRadius * stagger; |
| 325 | pos.y = j * heightBetweenItems; |
| 326 | var newInst = Instantiate(prefabForLevelLoadSim, pos + playerPos, Quaternion.identity); |
| 327 | var newObjTransform = newInst.transform; |
| 328 | newObjTransform.LookAt(playerPos); |
| 329 | Vector3 newAngle = newObjTransform.rotation.eulerAngles; |
| 330 | newAngle.x = 0.0f; |
| 331 | newObjTransform.rotation = Quaternion.Euler(newAngle); |
| 332 | spawnedCubes.Add(newInst); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /// <summary> |
| 339 | /// Usage: destroy all created cubes and garbage collect. |
| 340 | /// </summary> |
| 341 | void ClearObjects() |
| 342 | { |
| 343 | for (int i = 0; i < spawnedCubes.Count; i++) |
| 344 | { |
| 345 | DestroyImmediate(spawnedCubes[i]); |
| 346 | } |
| 347 | spawnedCubes.Clear(); |
| 348 | GC.Collect(); |
| 349 | } |
| 350 | #endregion |
| 351 | |
| 352 | #region Debug UI Handlers |
| 353 | |
| 354 | /// <summary> |
| 355 | /// Usage: radio button handler. |
| 356 | /// </summary> |
| 357 | public void RadioPressed(string radioLabel, string group, Toggle t) |
| 358 | { |
| 359 | if (string.Compare(radioLabel, ovrOverlayID) == 0) |
| 360 | { |
| 361 | ActivateOVROverlay(); |
| 362 | } |
| 363 | else if (string.Compare(radioLabel, applicationID) == 0) |
| 364 | { |
| 365 | ActivateWorldGeo(); |
| 366 | } |
| 367 | else if (string.Compare(radioLabel, noneID) == 0) |
| 368 | { |
| 369 | ActivateNone(); |
| 370 | } |
| 371 | } |
| 372 | #endregion |
| 373 | } |
| 374 | } |