using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.XR;
using UnityStandardAssets.ImageEffects;

namespace cakeslice
{
	// Token: 0x02000115 RID: 277
	[DisallowMultipleComponent]
	[RequireComponent(typeof(Camera))]
	public class OutlineEffectGlow : MonoBehaviour
	{
		// Token: 0x1700005F RID: 95
		// (get) Token: 0x06001693 RID: 5779 RVA: 0x0019F38C File Offset: 0x0019D58C
		// (set) Token: 0x06001694 RID: 5780 RVA: 0x0019F393 File Offset: 0x0019D593
		public static OutlineEffectGlow Instance { get; private set; }

		// Token: 0x06001695 RID: 5781 RVA: 0x0019F39B File Offset: 0x0019D59B
		private Material GetMaterialFromID(int ID)
		{
			if (ID == 0)
			{
				return this.outline1Material;
			}
			if (ID == 1)
			{
				return this.outline2Material;
			}
			return this.outline3Material;
		}

		// Token: 0x06001696 RID: 5782 RVA: 0x0019F3B8 File Offset: 0x0019D5B8
		private Material CreateMaterial(Color emissionColor)
		{
			Material material = new Material(this.outlineBufferShader);
			material.SetColor("_Color", emissionColor);
			material.SetInt("_SrcBlend", 5);
			material.SetInt("_DstBlend", 10);
			material.SetInt("_ZWrite", 0);
			material.DisableKeyword("_ALPHATEST_ON");
			material.EnableKeyword("_ALPHABLEND_ON");
			material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
			material.renderQueue = 3000;
			return material;
		}

		// Token: 0x06001697 RID: 5783 RVA: 0x0019F42D File Offset: 0x0019D62D
		private void Awake()
		{
			if (OutlineEffectGlow.Instance != null)
			{
				Object.Destroy(this);
				throw new Exception("you can only have one outline camera in the scene");
			}
			if (this.blur)
			{
				this.blur.enabled = false;
			}
			OutlineEffectGlow.Instance = this;
		}

		// Token: 0x06001698 RID: 5784 RVA: 0x0019F46C File Offset: 0x0019D66C
		private void Start()
		{
			this.CreateMaterialsIfNeeded();
			this.UpdateMaterialsPublicProperties();
			if (this.sourceCamera == null)
			{
				this.sourceCamera = base.GetComponent<Camera>();
				if (this.sourceCamera == null)
				{
					this.sourceCamera = Camera.main;
				}
			}
			if (this.outlineCamera == null)
			{
				foreach (Camera camera in base.GetComponentsInChildren<Camera>())
				{
					if (camera.name == "Outline Camera")
					{
						this.outlineCamera = camera;
						camera.enabled = false;
						break;
					}
				}
				if (this.outlineCamera == null)
				{
					this.outlineCamera = new GameObject("Outline Camera")
					{
						transform = 
						{
							parent = this.sourceCamera.transform
						}
					}.AddComponent<Camera>();
					this.outlineCamera.enabled = false;
				}
			}
			this.qualitySettings = 1;
			if (QualitySettings.antiAliasing == 2)
			{
				this.qualitySettings = 2;
			}
			if (QualitySettings.antiAliasing == 4)
			{
				this.qualitySettings = 4;
			}
			if (QualitySettings.antiAliasing == 8)
			{
				this.qualitySettings = 8;
			}
			this.renderTexture = new RenderTexture(this.sourceCamera.pixelWidth, this.sourceCamera.pixelHeight, 16, RenderTextureFormat.Default);
			this.renderTexture.antiAliasing = this.qualitySettings;
			if (QualitySettings.antiAliasing == 0)
			{
				this.qualitySettings = 0;
			}
			this.UpdateOutlineCameraFromSource();
			this.commandBuffer = new CommandBuffer();
			this.sourceCamera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, this.commandBuffer);
		}

		// Token: 0x06001699 RID: 5785 RVA: 0x0019F5E8 File Offset: 0x0019D7E8
		public void OnPreRender()
		{
			if (this.commandBuffer == null)
			{
				return;
			}
			this.CreateMaterialsIfNeeded();
			if (this.renderTexture == null || this.renderTexture.width != this.sourceCamera.pixelWidth || this.renderTexture.height != this.sourceCamera.pixelHeight || this.qualitySettings != QualitySettings.antiAliasing)
			{
				if (this.renderTexture)
				{
					this.renderTexture.Release();
				}
				this.qualitySettings = 1;
				if (QualitySettings.antiAliasing == 2)
				{
					this.qualitySettings = 2;
				}
				if (QualitySettings.antiAliasing == 4)
				{
					this.qualitySettings = 4;
				}
				if (QualitySettings.antiAliasing == 8)
				{
					this.qualitySettings = 8;
				}
				this.renderTexture = new RenderTexture(this.sourceCamera.pixelWidth, this.sourceCamera.pixelHeight, 16, RenderTextureFormat.Default);
				this.renderTexture.antiAliasing = this.qualitySettings;
				if (QualitySettings.antiAliasing == 0)
				{
					this.qualitySettings = 0;
				}
				this.outlineCamera.targetTexture = this.renderTexture;
			}
			this.UpdateMaterialsPublicProperties();
			this.UpdateOutlineCameraFromSource();
			this.commandBuffer.Clear();
			RenderTargetIdentifier renderTargetIdentifier = new RenderTargetIdentifier(this.renderTexture);
			this.commandBuffer.SetRenderTarget(renderTargetIdentifier, BuiltinRenderTextureType.CameraTarget);
			this.commandBuffer.ClearRenderTarget(false, true, Color.clear);
			foreach (OutlineGlow outlineGlow in this.outlines)
			{
				if (outlineGlow.gameObject.activeSelf && outlineGlow.gameObject.activeInHierarchy)
				{
					bool flag = true;
					if (outlineGlow.Renderer && !outlineGlow.Renderer.enabled)
					{
						flag = false;
					}
					if (outlineGlow.SkinnedMeshRenderer && !outlineGlow.SkinnedMeshRenderer.enabled)
					{
						flag = false;
					}
					if (flag)
					{
						LayerMask layerMask = 2112;
						if (outlineGlow != null && layerMask == (layerMask | (1 << outlineGlow.gameObject.layer)))
						{
							for (int i = 0; i < outlineGlow.SharedMaterials.Length; i++)
							{
								Material material = null;
								if (outlineGlow.SharedMaterials[i].mainTexture != null && outlineGlow.SharedMaterials[i])
								{
									foreach (Material material2 in this.materialBuffer)
									{
										if (material2.mainTexture == outlineGlow.SharedMaterials[i].mainTexture)
										{
											if (outlineGlow.eraseRenderer && material2.color == this.outlineEraseMaterial.color)
											{
												material = material2;
											}
											else if (material2.color == this.GetMaterialFromID(outlineGlow.color).color)
											{
												material = material2;
											}
										}
									}
									if (material == null)
									{
										if (outlineGlow.eraseRenderer)
										{
											material = new Material(this.outlineEraseMaterial);
										}
										else
										{
											material = new Material(this.GetMaterialFromID(outlineGlow.color));
										}
										material.mainTexture = outlineGlow.SharedMaterials[i].mainTexture;
										this.materialBuffer.Add(material);
									}
								}
								else if (outlineGlow.eraseRenderer)
								{
									material = this.outlineEraseMaterial;
								}
								else
								{
									material = this.GetMaterialFromID(outlineGlow.color);
								}
								if (this.backfaceCulling)
								{
									material.SetInt("_Culling", 2);
								}
								else
								{
									material.SetInt("_Culling", 0);
								}
								this.commandBuffer.DrawRenderer(outlineGlow.Renderer, material, 0, 0);
								MeshFilter meshFilter = outlineGlow.MeshFilter;
								if (meshFilter && meshFilter.sharedMesh != null)
								{
									for (int j = 1; j < meshFilter.sharedMesh.subMeshCount; j++)
									{
										this.commandBuffer.DrawRenderer(outlineGlow.Renderer, material, j, 0);
									}
								}
								SkinnedMeshRenderer skinnedMeshRenderer = outlineGlow.SkinnedMeshRenderer;
								if (skinnedMeshRenderer && skinnedMeshRenderer.sharedMesh != null)
								{
									for (int k = 1; k < skinnedMeshRenderer.sharedMesh.subMeshCount; k++)
									{
										this.commandBuffer.DrawRenderer(outlineGlow.Renderer, material, k, 0);
									}
								}
							}
						}
					}
				}
			}
			this.frameCount++;
			if (this.frameCount > 5000)
			{
				for (int l = 0; l < this.materialBuffer.Count; l++)
				{
					if (this.materialBuffer[l])
					{
						Object.Destroy(this.materialBuffer[l]);
					}
				}
				this.frameCount = 0;
				this.materialBuffer.Clear();
			}
		}

		// Token: 0x0600169A RID: 5786 RVA: 0x0019FADC File Offset: 0x0019DCDC
		private void OnEnable()
		{
			if (this.autoEnableOutlines)
			{
				foreach (OutlineGlow outlineGlow in Object.FindObjectsByType<OutlineGlow>(FindObjectsSortMode.None))
				{
					outlineGlow.enabled = false;
					outlineGlow.enabled = true;
				}
			}
		}

		// Token: 0x0600169B RID: 5787 RVA: 0x0019FB16 File Offset: 0x0019DD16
		private void OnDestroy()
		{
			if (this.renderTexture != null)
			{
				this.renderTexture.Release();
			}
			this.DestroyMaterials();
		}

		// Token: 0x0600169C RID: 5788 RVA: 0x0019FB38 File Offset: 0x0019DD38
		private void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			if (this.outlines.Count > 0)
			{
				this.outlineShaderMaterial.SetTexture("_FinalOutlineSource", this.renderTexture);
				if (this.useBlur && (this.w != Screen.width || this.h != Screen.height || this.blurred == null))
				{
					if (this.blurred != null)
					{
						this.blurred.Release();
					}
					if (this.readyToBlur != null)
					{
						this.readyToBlur.Release();
					}
					this.w = Screen.width;
					this.h = Screen.height;
					this.blurred = new RenderTexture(this.w, this.h, 0, RenderTextureFormat.ARGB32);
					this.readyToBlur = new RenderTexture(this.w, this.h, 0, RenderTextureFormat.ARGB32);
				}
				if (this.useBlur)
				{
					this.outlineShaderMaterial.SetFloat("UseBlur", 1f);
					Graphics.Blit(source, this.readyToBlur, this.outlineShaderMaterial, 1);
				}
				else
				{
					this.outlineShaderMaterial.SetFloat("UseBlur", 0f);
					Graphics.Blit(source, destination, this.outlineShaderMaterial, 1);
				}
				if (this.useBlur)
				{
					this.blur.OnRenderImage(this.readyToBlur, this.blurred);
					this.outlineShaderMaterial.SetTexture("_OutlineTex", this.blurred);
					Graphics.Blit(source, destination, this.outlineShaderMaterial, 2);
					return;
				}
			}
			else
			{
				Graphics.Blit(source, destination);
			}
		}

		// Token: 0x0600169D RID: 5789 RVA: 0x0019FCBC File Offset: 0x0019DEBC
		private void CreateMaterialsIfNeeded()
		{
			if (this.outlineShader == null)
			{
				this.outlineShader = Resources.Load<Shader>("OutlineShaderGlow");
			}
			if (this.outlineBufferShader == null)
			{
				this.outlineBufferShader = Resources.Load<Shader>("OutlineBufferShaderGlow");
			}
			if (this.outlineShaderMaterial == null)
			{
				this.outlineShaderMaterial = new Material(this.outlineShader);
				this.outlineShaderMaterial.hideFlags = HideFlags.HideAndDontSave;
				this.UpdateMaterialsPublicProperties();
			}
			if (this.outlineEraseMaterial == null)
			{
				this.outlineEraseMaterial = this.CreateMaterial(new Color(0f, 0f, 0f, 0f));
			}
			if (this.outline1Material == null)
			{
				this.outline1Material = this.CreateMaterial(new Color(1f, 0f, 0f, 0f));
			}
			if (this.outline2Material == null)
			{
				this.outline2Material = this.CreateMaterial(new Color(0f, 1f, 0f, 0f));
			}
			if (this.outline3Material == null)
			{
				this.outline3Material = this.CreateMaterial(new Color(0f, 0f, 1f, 0f));
			}
		}

		// Token: 0x0600169E RID: 5790 RVA: 0x0019FE04 File Offset: 0x0019E004
		private void DestroyMaterials()
		{
			foreach (Material material in this.materialBuffer)
			{
				Object.DestroyImmediate(material);
			}
			this.materialBuffer.Clear();
			Object.DestroyImmediate(this.outlineShaderMaterial);
			Object.DestroyImmediate(this.outlineEraseMaterial);
			Object.DestroyImmediate(this.outline1Material);
			Object.DestroyImmediate(this.outline2Material);
			Object.DestroyImmediate(this.outline3Material);
			this.outlineShader = null;
			this.outlineBufferShader = null;
			this.outlineShaderMaterial = null;
			this.outlineEraseMaterial = null;
			this.outline1Material = null;
			this.outline2Material = null;
			this.outline3Material = null;
		}

		// Token: 0x0600169F RID: 5791 RVA: 0x0019FEC8 File Offset: 0x0019E0C8
		public void UpdateMaterialsPublicProperties()
		{
			if (this.outlineShaderMaterial)
			{
				float num = 1f;
				if (this.scaleWithScreenSize)
				{
					num = (float)Screen.height / 360f;
				}
				if (this.scaleWithScreenSize && num < 1f)
				{
					if (XRSettings.isDeviceActive && this.sourceCamera.stereoTargetEye != StereoTargetEyeMask.None)
					{
						this.outlineShaderMaterial.SetFloat("_LineThicknessX", 0.001f * (1f / (float)XRSettings.eyeTextureWidth) * 1000f);
						this.outlineShaderMaterial.SetFloat("_LineThicknessY", 0.001f * (1f / (float)XRSettings.eyeTextureHeight) * 1000f);
					}
					else
					{
						this.outlineShaderMaterial.SetFloat("_LineThicknessX", 0.001f * (1f / (float)Screen.width) * 1000f * this.lineThickness);
						this.outlineShaderMaterial.SetFloat("_LineThicknessY", 0.001f * (1f / (float)Screen.height) * 1000f * this.lineThickness);
					}
				}
				else if (XRSettings.isDeviceActive && this.sourceCamera.stereoTargetEye != StereoTargetEyeMask.None)
				{
					this.outlineShaderMaterial.SetFloat("_LineThicknessX", num * (this.lineThickness / 1000f) * (1f / (float)XRSettings.eyeTextureWidth) * 1000f);
					this.outlineShaderMaterial.SetFloat("_LineThicknessY", num * (this.lineThickness / 1000f) * (1f / (float)XRSettings.eyeTextureHeight) * 1000f);
				}
				else
				{
					this.outlineShaderMaterial.SetFloat("_LineThicknessX", num * (this.lineThickness / 1000f) * (1f / (float)Screen.width) * 1000f);
					this.outlineShaderMaterial.SetFloat("_LineThicknessY", num * (this.lineThickness / 1000f) * (1f / (float)Screen.height) * 1000f);
				}
				this.outlineShaderMaterial.SetFloat("_LineIntensity", this.lineIntensity);
				this.outlineShaderMaterial.SetFloat("_FillAmount", this.fillAmount);
				this.outlineShaderMaterial.SetColor("_LineColor1", this.lineColor0 * this.lineColor0);
				this.outlineShaderMaterial.SetColor("_LineColor2", this.lineColor1 * this.lineColor1);
				this.outlineShaderMaterial.SetColor("_LineColor3", this.lineColor2 * this.lineColor2);
				Links.x.hoverMaterial1.SetColor("_Color", Color.red);
				Links.x.hoverMaterial2.SetColor("_Color", Color.green);
				Links.x.hoverMaterial3.SetColor("_Color", Color.blue);
				if (this.flipY)
				{
					this.outlineShaderMaterial.SetInt("_FlipY", 1);
				}
				else
				{
					this.outlineShaderMaterial.SetInt("_FlipY", 0);
				}
				if (!this.additiveRendering)
				{
					this.outlineShaderMaterial.SetInt("_Dark", 1);
				}
				else
				{
					this.outlineShaderMaterial.SetInt("_Dark", 0);
				}
				if (this.cornerOutlines)
				{
					this.outlineShaderMaterial.SetInt("_CornerOutlines", 1);
				}
				else
				{
					this.outlineShaderMaterial.SetInt("_CornerOutlines", 0);
				}
				Shader.SetGlobalFloat("_OutlineAlphaCutoff", this.alphaCutoff);
			}
		}

		// Token: 0x060016A0 RID: 5792 RVA: 0x001A022C File Offset: 0x0019E42C
		private void UpdateOutlineCameraFromSource()
		{
			this.outlineCamera.CopyFrom(this.sourceCamera);
			this.outlineCamera.renderingPath = RenderingPath.Forward;
			this.outlineCamera.backgroundColor = new Color(0f, 0f, 0f, 0f);
			this.outlineCamera.clearFlags = CameraClearFlags.Color;
			this.outlineCamera.rect = new Rect(0f, 0f, 1f, 1f);
			this.outlineCamera.cullingMask = 0;
			this.outlineCamera.targetTexture = this.renderTexture;
			this.outlineCamera.enabled = false;
			this.outlineCamera.allowHDR = false;
		}

		// Token: 0x060016A1 RID: 5793 RVA: 0x001A02DF File Offset: 0x0019E4DF
		public void AddOutline(OutlineGlow outline)
		{
			this.outlines.Add(outline);
		}

		// Token: 0x060016A2 RID: 5794 RVA: 0x001A02EE File Offset: 0x0019E4EE
		public void RemoveOutline(OutlineGlow outline)
		{
			this.outlines.Remove(outline);
		}

		// Token: 0x040026A0 RID: 9888
		private readonly LinkedSetGlow<OutlineGlow> outlines = new LinkedSetGlow<OutlineGlow>();

		// Token: 0x040026A1 RID: 9889
		public bool useBlur;

		// Token: 0x040026A2 RID: 9890
		[Range(0.0001f, 6f)]
		public float lineThickness = 1.25f;

		// Token: 0x040026A3 RID: 9891
		[Range(0f, 10f)]
		public float lineIntensity = 0.5f;

		// Token: 0x040026A4 RID: 9892
		[Range(0f, 1f)]
		public float fillAmount = 0.2f;

		// Token: 0x040026A5 RID: 9893
		public Color lineColor0 = Color.red;

		// Token: 0x040026A6 RID: 9894
		public Color lineColor1 = Color.green;

		// Token: 0x040026A7 RID: 9895
		public Color lineColor2 = Color.blue;

		// Token: 0x040026A8 RID: 9896
		public bool additiveRendering;

		// Token: 0x040026A9 RID: 9897
		public bool backfaceCulling = true;

		// Token: 0x040026AA RID: 9898
		private int qualitySettings;

		// Token: 0x040026AB RID: 9899
		[Header("These settings can affect performance!")]
		public bool cornerOutlines;

		// Token: 0x040026AC RID: 9900
		public bool addLinesBetweenColors;

		// Token: 0x040026AD RID: 9901
		[Header("Advanced settings")]
		public bool scaleWithScreenSize = true;

		// Token: 0x040026AE RID: 9902
		[Range(0.1f, 0.9f)]
		public float alphaCutoff = 0.5f;

		// Token: 0x040026AF RID: 9903
		public bool flipY;

		// Token: 0x040026B0 RID: 9904
		public Camera sourceCamera;

		// Token: 0x040026B1 RID: 9905
		public bool autoEnableOutlines = true;

		// Token: 0x040026B2 RID: 9906
		[HideInInspector]
		public Camera outlineCamera;

		// Token: 0x040026B3 RID: 9907
		private Material outline1Material;

		// Token: 0x040026B4 RID: 9908
		private Material outline2Material;

		// Token: 0x040026B5 RID: 9909
		private Material outline3Material;

		// Token: 0x040026B6 RID: 9910
		private Material outlineEraseMaterial;

		// Token: 0x040026B7 RID: 9911
		private Shader outlineShader;

		// Token: 0x040026B8 RID: 9912
		private Shader outlineBufferShader;

		// Token: 0x040026B9 RID: 9913
		[HideInInspector]
		public Material outlineShaderMaterial;

		// Token: 0x040026BA RID: 9914
		[HideInInspector]
		public RenderTexture renderTexture;

		// Token: 0x040026BB RID: 9915
		[HideInInspector]
		public RenderTexture extraRenderTexture;

		// Token: 0x040026BC RID: 9916
		[HideInInspector]
		public RenderTexture blurred;

		// Token: 0x040026BD RID: 9917
		[HideInInspector]
		public RenderTexture blurred1;

		// Token: 0x040026BE RID: 9918
		[HideInInspector]
		public RenderTexture readyToBlur;

		// Token: 0x040026BF RID: 9919
		private int w;

		// Token: 0x040026C0 RID: 9920
		private int h;

		// Token: 0x040026C1 RID: 9921
		public BlurOptimized blur;

		// Token: 0x040026C2 RID: 9922
		private int frameCount;

		// Token: 0x040026C3 RID: 9923
		private CommandBuffer commandBuffer;

		// Token: 0x040026C4 RID: 9924
		private List<Material> materialBuffer = new List<Material>();

		// Token: 0x040026C5 RID: 9925
		private bool RenderTheNextFrame;
	}
}
