/************************************************************************************ 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. ************************************************************************************/ using UnityEngine; using System.Collections; // required for Coroutines /// /// Fades the screen from black after a new scene is loaded. /// public class OVRScreenFade : MonoBehaviour { /// /// How long it takes to fade. /// public float fadeTime = 2.0f; /// /// The initial screen color. /// public Color fadeColor = new Color(0.01f, 0.01f, 0.01f, 1.0f); /// /// The shader to use when rendering the fade. /// public Shader fadeShader = null; private Material fadeMaterial = null; private bool isFading = false; /// /// Initialize. /// void Awake() { // create the fade material fadeMaterial = (fadeShader != null) ? new Material(fadeShader) : new Material(Shader.Find("Transparent/Diffuse")); } /// /// Starts the fade in /// void OnEnable() { StartCoroutine(FadeIn()); } /// /// Starts a fade in when a new level is loaded /// void OnLevelWasLoaded(int level) { StartCoroutine(FadeIn()); } /// /// Cleans up the fade material /// void OnDestroy() { if (fadeMaterial != null) { Destroy(fadeMaterial); } } /// /// Fades alpha from 1.0 to 0.0 /// IEnumerator FadeIn() { float elapsedTime = 0.0f; Color color = fadeMaterial.color = fadeColor; isFading = true; while (elapsedTime < fadeTime) { yield return new WaitForEndOfFrame(); elapsedTime += Time.deltaTime; color.a = 1.0f - Mathf.Clamp01(elapsedTime / fadeTime); fadeMaterial.color = color; } isFading = false; } /// /// Renders the fade overlay when attached to a camera object /// void OnPostRender() { if (isFading) { fadeMaterial.SetPass(0); GL.PushMatrix(); GL.LoadOrtho(); GL.Color(fadeMaterial.color); GL.Begin(GL.QUADS); GL.Vertex3(0f, 0f, -12f); GL.Vertex3(0f, 1f, -12f); GL.Vertex3(1f, 1f, -12f); GL.Vertex3(1f, 0f, -12f); GL.End(); GL.PopMatrix(); } } }