using System;
using System.Collections;
using UnityEngine;

public class OVRScreenFade : MonoBehaviour
{
	private void Awake()
	{
		this.fadeMaterial = new Material(Shader.Find("Oculus/Unlit Transparent Color"));
	}

	private void OnEnable()
	{
		base.StartCoroutine(this.FadeIn());
	}

	private void OnLevelWasLoaded(int level)
	{
		base.StartCoroutine(this.FadeIn());
	}

	private void OnDestroy()
	{
		if (this.fadeMaterial != null)
		{
			UnityEngine.Object.Destroy(this.fadeMaterial);
		}
	}

	private IEnumerator FadeIn()
	{
		float elapsedTime = 0f;
		this.fadeMaterial.color = this.fadeColor;
		Color color = this.fadeColor;
		this.isFading = true;
		while (elapsedTime < this.fadeTime)
		{
			yield return this.fadeInstruction;
			elapsedTime += Time.deltaTime;
			color.a = 1f - Mathf.Clamp01(elapsedTime / this.fadeTime);
			this.fadeMaterial.color = color;
		}
		this.isFading = false;
		yield break;
	}

	private void OnPostRender()
	{
		if (this.isFading)
		{
			this.fadeMaterial.SetPass(0);
			GL.PushMatrix();
			GL.LoadOrtho();
			GL.Color(this.fadeMaterial.color);
			GL.Begin(7);
			GL.Vertex3(0f, 0f, -12f);
			GL.Vertex3(0f, 1f, -12f);
			GL.Vertex3(1f, 1f, -12f);
			GL.Vertex3(1f, 0f, -12f);
			GL.End();
			GL.PopMatrix();
		}
	}

	public float fadeTime = 2f;

	public Color fadeColor = new Color(0.01f, 0.01f, 0.01f, 1f);

	private Material fadeMaterial;

	private bool isFading;

	private YieldInstruction fadeInstruction = new WaitForEndOfFrame();
}
