using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace OculusSampleFramework { public class OVROverlaySample : MonoBehaviour { private bool inMenu; private const string ovrOverlayID = "OVROverlayID"; private const string applicationID = "ApplicationID"; private const string noneID = "NoneID"; private Toggle applicationRadioButton; private Toggle noneRadioButton; [Header("App vs Compositor Comparison Settings")] public GameObject mainCamera; public GameObject uiCamera; public GameObject uiGeoParent; public GameObject worldspaceGeoParent; public OVROverlay cameraRenderOverlay; public OVROverlay renderingLabelOverlay; public Texture applicationLabelTexture; public Texture compositorLabelTexture; [Header("Level Loading Sim Settings")] public GameObject prefabForLevelLoadSim; public OVROverlay cubemapOverlay; public OVROverlay loadingTextQuadOverlay; public float distanceFromCamToLoadText; public float cubeSpawnRadius; public float heightBetweenItems; public int numObjectsPerLevel; public int numLevels; public int numLoopsTrigger = 500000000; private List spawnedCubes = new List(); private void Start() { DebugUIBuilder.instance.AddLabel("OVROverlay Sample"); DebugUIBuilder.instance.AddDivider(); DebugUIBuilder.instance.AddLabel("Level Loading Example"); DebugUIBuilder.instance.AddButton("Simulate Level Load", TriggerLoad); DebugUIBuilder.instance.AddButton("Destroy Cubes", TriggerUnload); DebugUIBuilder.instance.AddDivider(); DebugUIBuilder.instance.AddLabel("OVROverlay vs. Application Render Comparison"); DebugUIBuilder.instance.AddRadio("OVROverlay", "group", delegate(Toggle t) { RadioPressed("OVROverlayID", "group", t); }).GetComponentInChildren(); applicationRadioButton = DebugUIBuilder.instance.AddRadio("Application", "group", delegate(Toggle t) { RadioPressed("ApplicationID", "group", t); }).GetComponentInChildren(); noneRadioButton = DebugUIBuilder.instance.AddRadio("None", "group", delegate(Toggle t) { RadioPressed("NoneID", "group", t); }).GetComponentInChildren(); DebugUIBuilder.instance.Show(); CameraAndRenderTargetSetup(); cameraRenderOverlay.enabled = true; cameraRenderOverlay.currentOverlayShape = OVROverlay.OverlayShape.Quad; spawnedCubes.Capacity = numObjectsPerLevel * numLevels; } private void Update() { if (OVRInput.GetDown(OVRInput.Button.Two) || OVRInput.GetDown(OVRInput.Button.Start)) { if (inMenu) { DebugUIBuilder.instance.Hide(); } else { DebugUIBuilder.instance.Show(); } inMenu = !inMenu; } if (Input.GetKeyDown(KeyCode.A)) { TriggerLoad(); } } private void ActivateWorldGeo() { worldspaceGeoParent.SetActive(true); uiGeoParent.SetActive(false); uiCamera.SetActive(false); cameraRenderOverlay.enabled = false; renderingLabelOverlay.enabled = true; renderingLabelOverlay.textures[0] = applicationLabelTexture; Debug.Log("Switched to ActivateWorldGeo"); } private void ActivateOVROverlay() { worldspaceGeoParent.SetActive(false); uiCamera.SetActive(true); cameraRenderOverlay.enabled = true; uiGeoParent.SetActive(true); renderingLabelOverlay.enabled = true; renderingLabelOverlay.textures[0] = compositorLabelTexture; Debug.Log("Switched to ActivateOVROVerlay"); } private void ActivateNone() { worldspaceGeoParent.SetActive(false); uiCamera.SetActive(false); cameraRenderOverlay.enabled = false; uiGeoParent.SetActive(false); renderingLabelOverlay.enabled = false; Debug.Log("Switched to ActivateNone"); } private void TriggerLoad() { StartCoroutine(WaitforOVROverlay()); } private IEnumerator WaitforOVROverlay() { Transform camTransform = mainCamera.transform; Transform uiTextOverlayTrasnform = loadingTextQuadOverlay.transform; Vector3 newPos = camTransform.position + camTransform.forward * distanceFromCamToLoadText; newPos.y = camTransform.position.y; uiTextOverlayTrasnform.position = newPos; cubemapOverlay.enabled = true; loadingTextQuadOverlay.enabled = true; noneRadioButton.isOn = true; yield return new WaitForSeconds(0.1f); ClearObjects(); SimulateLevelLoad(); cubemapOverlay.enabled = false; loadingTextQuadOverlay.enabled = false; yield return null; } private void TriggerUnload() { ClearObjects(); applicationRadioButton.isOn = true; } private void CameraAndRenderTargetSetup() { float x = cameraRenderOverlay.transform.localScale.x; float y = cameraRenderOverlay.transform.localScale.y; float z = cameraRenderOverlay.transform.localScale.z; float num = 2160f; float num2 = 1200f; float num3 = num * 0.5f; float num4 = num2; float num5 = mainCamera.GetComponent().fieldOfView / 2f; float num6 = 2f * z * Mathf.Tan((float)Math.PI / 180f * num5); float num7 = num4 / num6; float num8 = num7 * x; float num9 = 0f; float num10 = num6 * mainCamera.GetComponent().aspect; float num11 = num3 / num10; num9 = num11 * x; float orthographicSize = y / 2f; float aspect = x / y; uiCamera.GetComponent().orthographicSize = orthographicSize; uiCamera.GetComponent().aspect = aspect; if (uiCamera.GetComponent().targetTexture != null) { uiCamera.GetComponent().targetTexture.Release(); } RenderTexture renderTexture = new RenderTexture((int)num9 * 2, (int)num8 * 2, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); Debug.Log("Created RT of resolution w: " + num9 + " and h: " + num8); renderTexture.hideFlags = HideFlags.DontSave; renderTexture.useMipMap = true; renderTexture.filterMode = FilterMode.Trilinear; renderTexture.anisoLevel = 4; renderTexture.autoGenerateMips = true; uiCamera.GetComponent().targetTexture = renderTexture; cameraRenderOverlay.textures[0] = renderTexture; } private void SimulateLevelLoad() { int num = 0; for (int i = 0; i < numLoopsTrigger; i++) { num++; } Debug.Log("Finished " + num + " Loops"); Vector3 position = mainCamera.transform.position; position.y = 0.5f; for (int j = 0; j < numLevels; j++) { for (int k = 0; k < numObjectsPerLevel; k++) { float f = (float)k * (float)Math.PI * 2f / (float)numObjectsPerLevel; float num2 = ((k % 2 != 0) ? 1f : 1.5f); Vector3 vector = new Vector3(Mathf.Cos(f), 0f, Mathf.Sin(f)) * cubeSpawnRadius * num2; vector.y = (float)j * heightBetweenItems; GameObject gameObject = UnityEngine.Object.Instantiate(prefabForLevelLoadSim, vector + position, Quaternion.identity); Transform transform = gameObject.transform; transform.LookAt(position); Vector3 eulerAngles = transform.rotation.eulerAngles; eulerAngles.x = 0f; transform.rotation = Quaternion.Euler(eulerAngles); spawnedCubes.Add(gameObject); } } } private void ClearObjects() { for (int i = 0; i < spawnedCubes.Count; i++) { UnityEngine.Object.DestroyImmediate(spawnedCubes[i]); } spawnedCubes.Clear(); GC.Collect(); } public void RadioPressed(string radioLabel, string group, Toggle t) { if (string.Compare(radioLabel, "OVROverlayID") == 0) { ActivateOVROverlay(); } else if (string.Compare(radioLabel, "ApplicationID") == 0) { ActivateWorldGeo(); } else if (string.Compare(radioLabel, "NoneID") == 0) { ActivateNone(); } } } }