using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Rendering;

// Token: 0x02000006 RID: 6
public class MenuCameraCrossFade : MonoBehaviour
{
	// Token: 0x0600003F RID: 63 RVA: 0x00006A14 File Offset: 0x00004C14
	private void Awake()
	{
		this.cam = base.gameObject.GetComponent<Camera>();
		this.fading = false;
	}

	// Token: 0x06000040 RID: 64 RVA: 0x00006A30 File Offset: 0x00004C30
	private void CreateTextures()
	{
		int width = this.cam.targetTexture.width;
		int height = this.cam.targetTexture.height;
		if (this.renderTextureCopy != null)
		{
			this.renderTextureCopy.Release();
		}
		if (this.renderTextureNew != null)
		{
			this.renderTextureNew.Release();
		}
		this.renderTextureCopy = new RenderTexture(width, height, 16, RenderTextureFormat.ARGB32);
		this.renderTextureNew = new RenderTexture(width, height, 16, RenderTextureFormat.ARGB32);
	}

	// Token: 0x06000041 RID: 65 RVA: 0x00006AB4 File Offset: 0x00004CB4
	public void StartCrossFade()
	{
		base.enabled = true;
		this.crossfadeValue = 0f;
		if (!base.gameObject.activeInHierarchy)
		{
			return;
		}
		if (this.crossfadeCoroutine == null)
		{
			this.crossfadeCoroutine = this.CrossFade(this.crossfadeLength);
			base.StartCoroutine(this.crossfadeCoroutine);
			this.startCoroutineTime = Time.realtimeSinceStartup;
		}
	}

	// Token: 0x06000042 RID: 66 RVA: 0x00006B14 File Offset: 0x00004D14
	public void StartCrossFadeFast()
	{
		base.enabled = true;
		this.crossfadeValue = 0f;
		if (!base.gameObject.activeInHierarchy)
		{
			return;
		}
		if (this.crossfadeCoroutine == null)
		{
			this.crossfadeCoroutine = this.CrossFade(0.0001f);
			base.StartCoroutine(this.crossfadeCoroutine);
			this.startCoroutineTime = Time.realtimeSinceStartup;
		}
	}

	// Token: 0x06000043 RID: 67 RVA: 0x00006B72 File Offset: 0x00004D72
	private IEnumerator CrossFade(float len)
	{
		float startValue = this.crossfadeValue;
		float endValue = 1f;
		float percentage = 0f;
		float startTime = Time.realtimeSinceStartup;
		if (this.disableWhenDone)
		{
			while (this.renderTextureCopy == null)
			{
				yield return null;
			}
		}
		this.fading = true;
		if (!this.crossfadeMaterial)
		{
			this.crossfadeMaterial = new Material(Links.x.crossfadeShader);
		}
		while (percentage < 1f)
		{
			percentage = (Time.realtimeSinceStartup - startTime) / len;
			this.crossfadeValue = Mathf.Lerp(startValue, endValue, percentage);
			if (len < 0.01f)
			{
				this.crossfadeValue = endValue;
			}
			if (this.crossfadeValue < 0f)
			{
				this.crossfadeMaterial.SetFloat("_CrossFade", 0f);
			}
			else
			{
				this.crossfadeMaterial.SetFloat("_CrossFade", this.crossfadeValue);
			}
			yield return null;
		}
		this.fading = false;
		this.crossfadeCoroutine = null;
		this.startCoroutineTime = 0f;
		this.EndCrossFade();
		yield break;
	}

	// Token: 0x06000044 RID: 68 RVA: 0x00006B88 File Offset: 0x00004D88
	private void OnDisable()
	{
		if (this.crossfadeCoroutine != null)
		{
			base.StopCoroutine(this.crossfadeCoroutine);
			this.crossfadeCoroutine = null;
		}
		if (this.commandBuffer != null)
		{
			this.cam.RemoveCommandBuffer(CameraEvent.AfterImageEffects, this.commandBuffer);
		}
		this.commandBuffer = null;
		if (this.renderTextureCopy != null)
		{
			this.renderTextureCopy.Release();
		}
		if (this.renderTextureNew != null)
		{
			this.renderTextureNew.Release();
		}
		this.startCoroutineTime = 0f;
		this.fading = false;
	}

	// Token: 0x06000045 RID: 69 RVA: 0x00006C16 File Offset: 0x00004E16
	public void EndCrossFade()
	{
		this.startCoroutineTime = 0f;
		if (this.disableWhenDone)
		{
			if (this.crossfadeCoroutine != null)
			{
				base.StopCoroutine(this.crossfadeCoroutine);
				this.crossfadeCoroutine = null;
			}
			base.enabled = false;
		}
	}

	// Token: 0x06000046 RID: 70 RVA: 0x00006C50 File Offset: 0x00004E50
	public void OnPreRender()
	{
		if (this.commandBuffer == null)
		{
			this.commandBuffer = new CommandBuffer();
			this.cam.AddCommandBuffer(CameraEvent.AfterImageEffects, this.commandBuffer);
		}
		if (this.startCoroutineTime > 0f && Time.realtimeSinceStartup > this.startCoroutineTime + this.crossfadeLength * 3f)
		{
			if (this.crossfadeCoroutine != null)
			{
				base.StopCoroutine(this.crossfadeCoroutine);
				this.crossfadeCoroutine = null;
			}
			this.fading = false;
		}
		if (this.cam.targetTexture)
		{
			if (!this.crossfadeMaterial)
			{
				this.crossfadeMaterial = new Material(Links.x.crossfadeShader);
			}
			if (!this.renderTextureCopy)
			{
				this.CreateTextures();
			}
			this.commandBuffer.Clear();
			if (!this.fading)
			{
				this.commandBuffer.Blit(this.cam.targetTexture, this.renderTextureCopy);
				return;
			}
			this.commandBuffer.Blit(this.cam.targetTexture, this.renderTextureNew);
			this.crossfadeMaterial.SetTexture("_LastFrame", this.renderTextureCopy);
			this.crossfadeMaterial.SetTexture("_NewFrame", this.renderTextureNew);
			RenderTargetIdentifier renderTargetIdentifier = new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);
			this.commandBuffer.SetRenderTarget(renderTargetIdentifier);
			this.commandBuffer.Blit(this.renderTextureCopy, renderTargetIdentifier, this.crossfadeMaterial, 0);
		}
	}

	// Token: 0x04000130 RID: 304
	private Camera cam;

	// Token: 0x04000131 RID: 305
	private Material crossfadeMaterial;

	// Token: 0x04000132 RID: 306
	public bool disableWhenDone;

	// Token: 0x04000133 RID: 307
	public float crossfadeLength = 0.25f;

	// Token: 0x04000134 RID: 308
	private IEnumerator crossfadeCoroutine;

	// Token: 0x04000135 RID: 309
	public RenderTexture renderTextureCopy;

	// Token: 0x04000136 RID: 310
	public RenderTexture renderTextureNew;

	// Token: 0x04000137 RID: 311
	public bool fading;

	// Token: 0x04000138 RID: 312
	private CommandBuffer commandBuffer;

	// Token: 0x04000139 RID: 313
	public float crossfadeValue;

	// Token: 0x0400013A RID: 314
	private float startCoroutineTime;
}
