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

namespace cakeslice
{
	// Token: 0x02000113 RID: 275
	[DisallowMultipleComponent]
	[RequireComponent(typeof(Camera))]
	public class OutlineEffect : MonoBehaviour
	{
		// Token: 0x1700005D RID: 93
		// (get) Token: 0x0600167A RID: 5754 RVA: 0x0019E65A File Offset: 0x0019C85A
		// (set) Token: 0x0600167B RID: 5755 RVA: 0x0019E661 File Offset: 0x0019C861
		public static OutlineEffect Instance { get; private set; }

		// Token: 0x0600167C RID: 5756 RVA: 0x0019E669 File Offset: 0x0019C869
		private Material GetMaterialFromID(int ID)
		{
			if (ID == 0)
			{
				return this.outline1Material;
			}
			if (ID == 1)
			{
				return this.outline2Material;
			}
			return this.outline3Material;
		}

		// Token: 0x0600167D RID: 5757 RVA: 0x0019E688 File Offset: 0x0019C888
		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: 0x0600167E RID: 5758 RVA: 0x0019E6FD File Offset: 0x0019C8FD
		private void Awake()
		{
			if (OutlineEffect.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;
			}
			OutlineEffect.Instance = this;
		}

		// Token: 0x0600167F RID: 5759 RVA: 0x0019E73C File Offset: 0x0019C93C
		private void Start()
		{
			this.CreateMaterialsIfNeeded();
			this.UpdateMaterialsPublicProperties();
			if (!Records.x.editor)
			{
				this.turnOffOutlines = false;
			}
			if (this.sourceCamera == null)
			{
				this.sourceCamera = base.GetComponent<Camera>();
				if (this.sourceCamera == null)
				{
					this.sourceCamera = Camera.main;
				}
			}
			this.hovers = Links.x.cameraEffects.hovers;
			this.hoverTypes = Links.x.cameraEffects.hoverTypes;
			this.bubbles = Links.x.cameraEffects.bubbles;
			this.bubbleTypes = Links.x.cameraEffects.bubbleTypes;
			if (this.renderTexture != null)
			{
				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.R8);
			this.renderTexture.antiAliasing = this.qualitySettings;
			if (QualitySettings.antiAliasing == 0)
			{
				this.qualitySettings = 0;
			}
			this.commandBuffer = new CommandBuffer();
			this.sourceCamera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, this.commandBuffer);
		}

		// Token: 0x06001680 RID: 5760 RVA: 0x0019E8A0 File Offset: 0x0019CAA0
		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 != null)
				{
					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.UpdateMaterialsPublicProperties();
			this.commandBuffer.Clear();
			RenderTargetIdentifier renderTargetIdentifier = new RenderTargetIdentifier(this.renderTexture);
			this.commandBuffer.SetRenderTarget(renderTargetIdentifier, BuiltinRenderTextureType.CameraTarget);
			this.commandBuffer.ClearRenderTarget(false, true, Color.clear);
			foreach (Outline outline in this.outlines)
			{
				2112;
				if (outline != null && outline.gameObject.activeSelf && outline.gameObject.activeInHierarchy)
				{
					bool flag = true;
					if (outline.Renderer && !outline.Renderer.enabled)
					{
						flag = false;
					}
					if (outline.SkinnedMeshRenderer && !outline.SkinnedMeshRenderer.enabled)
					{
						flag = false;
					}
					if (flag)
					{
						for (int i = 0; i < outline.SharedMaterials.Length; i++)
						{
							Material material = null;
							if (outline.gameObject.layer == 11)
							{
								this.commandBuffer.SetGlobalFloat("useZCutoff", 0f);
							}
							else
							{
								this.commandBuffer.SetGlobalFloat("useZCutoff", 1f);
							}
							if (outline.SharedMaterials[i].mainTexture != null && outline.SharedMaterials[i])
							{
								foreach (Material material2 in this.materialBuffer)
								{
									if (material2.mainTexture == outline.SharedMaterials[i].mainTexture && material2.color == this.GetMaterialFromID(outline.color).color)
									{
										material = material2;
									}
								}
								if (material == null)
								{
									material = new Material(this.GetMaterialFromID(outline.color));
									material.mainTexture = outline.SharedMaterials[i].mainTexture;
									this.materialBuffer.Add(material);
								}
							}
							else
							{
								material = this.GetMaterialFromID(outline.color);
							}
							if (this.backfaceCulling)
							{
								material.SetInt("_Culling", 2);
							}
							else
							{
								material.SetInt("_Culling", 0);
							}
							this.commandBuffer.DrawRenderer(outline.Renderer, material, i, 0);
						}
					}
				}
			}
			if (Links.x.cameraEffects.HoverCount() <= 0 && Links.x.cameraEffects.BubbleCount() <= 0)
			{
				int count = this.outlines.Count;
			}
		}

		// Token: 0x06001681 RID: 5761 RVA: 0x0019EC68 File Offset: 0x0019CE68
		private void OnEnable()
		{
			if (this.autoEnableOutlines)
			{
				foreach (Outline outline in Object.FindObjectsByType<Outline>(FindObjectsSortMode.None))
				{
					outline.enabled = false;
					outline.enabled = true;
				}
			}
		}

		// Token: 0x06001682 RID: 5762 RVA: 0x0019ECA2 File Offset: 0x0019CEA2
		private void OnDestroy()
		{
			if (this.renderTexture != null)
			{
				this.renderTexture.Release();
			}
			this.DestroyMaterials();
		}

		// Token: 0x06001683 RID: 5763 RVA: 0x0019ECC4 File Offset: 0x0019CEC4
		private void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			if ((Links.x.cameraEffects.HoverCount() > 0 || Links.x.cameraEffects.BubbleCount() > 0 || this.outlines.Count > 0) && !this.turnOffOutlines)
			{
				this.outlineShaderMaterial.SetTexture("_FinalOutlineSource", this.renderTexture);
				this.outlineShaderMaterial.SetFloat("UseBlur", 0f);
				Graphics.Blit(source, destination, this.outlineShaderMaterial, 1);
				return;
			}
			Graphics.Blit(source, destination);
		}

		// Token: 0x06001684 RID: 5764 RVA: 0x0019ED4C File Offset: 0x0019CF4C
		private void CreateMaterialsIfNeeded()
		{
			if (this.outlineShader == null)
			{
				this.outlineShader = Resources.Load<Shader>("OutlineShader");
			}
			if (this.outlineBufferShader == null)
			{
				this.outlineBufferShader = Resources.Load<Shader>("OutlineBufferShader");
			}
			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: 0x06001685 RID: 5765 RVA: 0x0019EE94 File Offset: 0x0019D094
		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: 0x06001686 RID: 5766 RVA: 0x0019EF58 File Offset: 0x0019D158
		public void UpdateMaterialsPublicProperties()
		{
			if (this.outlineShaderMaterial)
			{
				float num = 1f;
				if (this.scaleWithScreenSize)
				{
					num = (float)Screen.height / this.scale;
				}
				if (this.scaleWithScreenSize && num < 1f)
				{
					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
				{
					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: 0x06001687 RID: 5767 RVA: 0x0019F1D1 File Offset: 0x0019D3D1
		public void AddOutline(Outline outline)
		{
			this.outlines.Add(outline);
		}

		// Token: 0x06001688 RID: 5768 RVA: 0x0019F1E0 File Offset: 0x0019D3E0
		public void RemoveOutline(Outline outline)
		{
			this.outlines.Remove(outline);
		}

		// Token: 0x04002675 RID: 9845
		private readonly LinkedSet<Outline> outlines = new LinkedSet<Outline>();

		// Token: 0x04002676 RID: 9846
		public bool useBlur;

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

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

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

		// Token: 0x0400267A RID: 9850
		public Color lineColor0 = Color.red;

		// Token: 0x0400267B RID: 9851
		public Color lineColor1 = Color.green;

		// Token: 0x0400267C RID: 9852
		public Color lineColor2 = Color.blue;

		// Token: 0x0400267D RID: 9853
		public bool additiveRendering;

		// Token: 0x0400267E RID: 9854
		public bool backfaceCulling = true;

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

		// Token: 0x04002680 RID: 9856
		public bool addLinesBetweenColors;

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

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

		// Token: 0x04002683 RID: 9859
		public bool flipY;

		// Token: 0x04002684 RID: 9860
		public Camera sourceCamera;

		// Token: 0x04002685 RID: 9861
		public bool autoEnableOutlines = true;

		// Token: 0x04002686 RID: 9862
		public bool turnOffOutlines;

		// Token: 0x04002687 RID: 9863
		[HideInInspector]
		public Camera outlineCamera;

		// Token: 0x04002688 RID: 9864
		private Material outline1Material;

		// Token: 0x04002689 RID: 9865
		private Material outline2Material;

		// Token: 0x0400268A RID: 9866
		private Material outline3Material;

		// Token: 0x0400268B RID: 9867
		private Material outlineEraseMaterial;

		// Token: 0x0400268C RID: 9868
		private Shader outlineShader;

		// Token: 0x0400268D RID: 9869
		private Shader outlineBufferShader;

		// Token: 0x0400268E RID: 9870
		[HideInInspector]
		public Material outlineShaderMaterial;

		// Token: 0x0400268F RID: 9871
		[HideInInspector]
		public RenderTexture renderTexture;

		// Token: 0x04002690 RID: 9872
		[HideInInspector]
		private int w;

		// Token: 0x04002691 RID: 9873
		[HideInInspector]
		private int h;

		// Token: 0x04002692 RID: 9874
		private int qualitySettings;

		// Token: 0x04002693 RID: 9875
		public float scale = 360f;

		// Token: 0x04002694 RID: 9876
		private List<Renderer> hovers;

		// Token: 0x04002695 RID: 9877
		private List<int> hoverTypes;

		// Token: 0x04002696 RID: 9878
		private List<Renderer> bubbles;

		// Token: 0x04002697 RID: 9879
		private List<int> bubbleTypes;

		// Token: 0x04002698 RID: 9880
		private Material[] mats;

		// Token: 0x04002699 RID: 9881
		public BlurOptimized blur;

		// Token: 0x0400269A RID: 9882
		private CommandBuffer commandBuffer;

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

		// Token: 0x0400269C RID: 9884
		private bool RenderTheNextFrame;
	}
}
