using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Rendering;

namespace LOS
{
	// Token: 0x02000110 RID: 272
	[RequireComponent(typeof(Camera))]
	public class PointLightShadows : MonoBehaviour
	{
		// Token: 0x17000057 RID: 87
		// (get) Token: 0x06001655 RID: 5717 RVA: 0x0019DCAF File Offset: 0x0019BEAF
		public static Plane[] CameraFrustumPlanes
		{
			get
			{
				return PointLightShadows.m_CameraFrustumPlanes;
			}
		}

		// Token: 0x06001656 RID: 5718 RVA: 0x0019DCB6 File Offset: 0x0019BEB6
		private void Awake()
		{
			this.m_Camera = base.GetComponent<Camera>();
			this.pointLightObject.SetActive(false);
		}

		// Token: 0x06001657 RID: 5719 RVA: 0x0019DCD0 File Offset: 0x0019BED0
		private void OnEnable()
		{
			LOSHelper.ExtractFrustumPlanes(PointLightShadows.m_CameraFrustumPlanes, this.m_Camera);
		}

		// Token: 0x06001658 RID: 5720 RVA: 0x0019DCE2 File Offset: 0x0019BEE2
		private void OnDisable()
		{
			Materials.DestroyResources();
			Shaders.DestroyResources();
			if (this.buffer != null)
			{
				this.m_Camera.RemoveCommandBuffer(CameraEvent.AfterDepthTexture, this.buffer);
			}
		}

		// Token: 0x06001659 RID: 5721 RVA: 0x0019DD08 File Offset: 0x0019BF08
		private void CreateCombineTexture()
		{
			this.width = Screen.width;
			this.height = Screen.height;
			if (this.combinedLights)
			{
				this.combinedLights.Release();
			}
			this.combinedLights = new RenderTexture(this.width, this.height, 16, RenderTextureFormat.R8);
			if (this.combinedLights2)
			{
				this.combinedLights2.Release();
			}
			this.combinedLights2 = new RenderTexture(this.width, this.height, 16, RenderTextureFormat.R8);
		}

		// Token: 0x0600165A RID: 5722 RVA: 0x0019DD91 File Offset: 0x0019BF91
		private float GetCamDistance(int id)
		{
			if (id < this.cameraDistance.Count)
			{
				return this.cameraDistance[id];
			}
			return 0f;
		}

		// Token: 0x0600165B RID: 5723 RVA: 0x0019DDB3 File Offset: 0x0019BFB3
		private Vector3 GetCamPosition(int id)
		{
			if (id < this.cameraPosition.Count)
			{
				return this.cameraPosition[id];
			}
			return Vector3.zero;
		}

		// Token: 0x0600165C RID: 5724 RVA: 0x0019DDD8 File Offset: 0x0019BFD8
		public void Buffer()
		{
			if (this.buffer == null)
			{
				this.buffer = new CommandBuffer();
				this.buffer.name = "Quad Shadows";
				this.m_Camera.AddCommandBuffer(CameraEvent.AfterDepthTexture, this.buffer);
			}
			this.buffer.Clear();
			if (Links.x && Links.x.cameraEffects)
			{
				if (this.quadW != Screen.width || this.quadH != Screen.height || !this.quadShadowTexture)
				{
					if (this.quadShadowTexture)
					{
						this.quadShadowTexture.Release();
					}
					if (this.quadShadowTextureFinal)
					{
						this.quadShadowTextureFinal.Release();
					}
					this.quadW = Screen.width;
					this.quadH = Screen.height;
					this.quadShadowTexture = new RenderTexture(this.quadW, this.quadH, 16, RenderTextureFormat.R8);
				}
				RenderTargetIdentifier renderTargetIdentifier = new RenderTargetIdentifier(this.quadShadowTexture);
				this.buffer.SetRenderTarget(renderTargetIdentifier);
				this.buffer.ClearRenderTarget(true, true, Color.clear);
				if (!this.switchingScenes)
				{
					for (int i = 0; i < this.quadsToRender.Count; i++)
					{
						if (this.quadsToRender[i])
						{
							if (!Links.x.cameraEffects.indoors)
							{
								this.buffer.DrawRenderer(this.quadsToRender[i], this.quadMats[i], 0, 0);
							}
							else
							{
								this.buffer.DrawRenderer(this.quadsToRender[i], this.quadMats[i], 0, 0);
							}
						}
					}
				}
				this.buffer.SetGlobalTexture("_QuadShadows", this.quadShadowTexture);
			}
		}

		// Token: 0x0600165D RID: 5725 RVA: 0x0019DFAC File Offset: 0x0019C1AC
		public void SwitchingScenes()
		{
			Shader.SetGlobalTexture("_RealtimeShadowTex", Links.x.emptyTexture);
			this.switchingScenes = true;
			for (int i = 0; i < this.quadMats.Count; i++)
			{
				Object.Destroy(this.quadMats[i]);
			}
			this.quadMats.Clear();
			this.quadsToRender.Clear();
		}

		// Token: 0x0600165E RID: 5726 RVA: 0x0019E011 File Offset: 0x0019C211
		public void DoneSwitchingScenes()
		{
			this.switchingScenes = false;
		}

		// Token: 0x0600165F RID: 5727 RVA: 0x0019E01A File Offset: 0x0019C21A
		private void OnPreRender()
		{
			if (this.width != Screen.width || this.height != Screen.height)
			{
				this.CreateCombineTexture();
			}
			this.Buffer();
		}

		// Token: 0x06001660 RID: 5728 RVA: 0x0019E044 File Offset: 0x0019C244
		private void Update()
		{
			if (Links.x && Links.x.diorama)
			{
				if (this.quads == null)
				{
					this.quads = Links.x.quads;
				}
				if (this.quads != null && !this.switchingScenes)
				{
					this.quadsToRender.Clear();
					int num = 0;
					int count = this.quads.Count;
					this.planes = GeometryUtility.CalculateFrustumPlanes(Links.x.worldCamera);
					for (int i = 0; i < count; i++)
					{
						if (this.quads[i].isOn)
						{
							MeshRenderer art = this.quads[i].art;
							if (art && art.enabled && this.TestPlanesAABBInternalFastBounds(this.quads[i].art.bounds.min, this.quads[i].art.bounds.max))
							{
								this.quadsToRender.Add(art);
								if (this.quadMats.Count < this.quadsToRender.Count)
								{
									Material material = new Material(this.quadShader);
									this.quadMats.Add(material);
								}
								else
								{
									Material material2 = this.quadMats[num];
								}
								num++;
							}
						}
					}
				}
			}
		}

		// Token: 0x06001661 RID: 5729 RVA: 0x0019E1B9 File Offset: 0x0019C3B9
		public void SetupNewInterior(InteriorLights l)
		{
		}

		// Token: 0x06001662 RID: 5730 RVA: 0x0019E1BC File Offset: 0x0019C3BC
		private void RenderSourceToMask(LOSSource losSource, int num, Vector3 center)
		{
			Camera camera = losSource.SourceCamera;
			if (!camera)
			{
				camera = losSource.gameObject.GetComponent<Camera>();
				losSource.SetCamera(camera);
			}
			int num2 = this.currentLightIndex * 6;
			if (num + num2 < this.savedTextures.Count)
			{
				Materials.SkyBox.SetVector(ShaderID.FarPlane, new Vector4(camera.farClipPlane, camera.farClipPlane, camera.farClipPlane, camera.farClipPlane));
				camera.targetTexture = this.savedTextures[num + num2];
				FieldInfo field = typeof(Canvas).GetField("willRenderCanvases", BindingFlags.Static | BindingFlags.NonPublic);
				object value = field.GetValue(null);
				field.SetValue(null, null);
				camera.RenderWithShader(Shaders.Depth, null);
				field.SetValue(null, value);
				this.sourcePositions[num + num2] = losSource.gameObject.transform.position + losSource.gameObject.transform.forward;
				this.projectionMatrix[num + num2] = camera.projectionMatrix * camera.worldToCameraMatrix;
			}
		}

		// Token: 0x06001663 RID: 5731 RVA: 0x0019E2D4 File Offset: 0x0019C4D4
		public bool TestPlanesAABBInternalFastBounds(Vector3 boundsMin, Vector3 boundsMax)
		{
			for (int i = 0; i < this.planes.Length; i++)
			{
				Vector3 normal = this.planes[i].normal;
				float distance = this.planes[i].distance;
				Vector3 vector;
				Vector3 vector2;
				if (normal.x < 0f)
				{
					vector.x = boundsMin.x;
					vector2.x = boundsMax.x;
				}
				else
				{
					vector.x = boundsMax.x;
					vector2.x = boundsMin.x;
				}
				if (normal.y < 0f)
				{
					vector.y = boundsMin.y;
					vector2.y = boundsMax.y;
				}
				else
				{
					vector.y = boundsMax.y;
					vector2.y = boundsMin.y;
				}
				if (normal.z < 0f)
				{
					vector.z = boundsMin.z;
					vector2.z = boundsMax.z;
				}
				else
				{
					vector.z = boundsMax.z;
					vector2.z = boundsMin.z;
				}
				if (normal.x * vector.x + normal.y * vector.y + normal.z * vector.z + distance < 0f)
				{
					return false;
				}
			}
			return true;
		}

		// Token: 0x0400264A RID: 9802
		public int texSize = 512;

		// Token: 0x0400264B RID: 9803
		private static Plane[] m_CameraFrustumPlanes = new Plane[6];

		// Token: 0x0400264C RID: 9804
		public List<LOSSource> casters = new List<LOSSource>();

		// Token: 0x0400264D RID: 9805
		private Camera m_Camera;

		// Token: 0x0400264E RID: 9806
		public List<float> cameraDistance = new List<float>();

		// Token: 0x0400264F RID: 9807
		public List<Vector3> cameraPosition = new List<Vector3>();

		// Token: 0x04002650 RID: 9808
		public List<Matrix4x4> projectionMatrix = new List<Matrix4x4>();

		// Token: 0x04002651 RID: 9809
		public List<RenderTexture> savedTextures = new List<RenderTexture>();

		// Token: 0x04002652 RID: 9810
		public List<Vector3> sourcePositions = new List<Vector3>();

		// Token: 0x04002653 RID: 9811
		private CommandBuffer buffer;

		// Token: 0x04002654 RID: 9812
		public GameObject pointLightObject;

		// Token: 0x04002655 RID: 9813
		public Light currentLight;

		// Token: 0x04002656 RID: 9814
		private int width;

		// Token: 0x04002657 RID: 9815
		private int height;

		// Token: 0x04002658 RID: 9816
		private int quadW;

		// Token: 0x04002659 RID: 9817
		private int quadH;

		// Token: 0x0400265A RID: 9818
		public RenderTexture combinedLights;

		// Token: 0x0400265B RID: 9819
		public RenderTexture combinedLights2;

		// Token: 0x0400265C RID: 9820
		public RenderTexture quadShadowTexture;

		// Token: 0x0400265D RID: 9821
		public RenderTexture quadShadowTextureFinal;

		// Token: 0x0400265E RID: 9822
		public Material combineMaterial;

		// Token: 0x0400265F RID: 9823
		public int currentLightIndex;

		// Token: 0x04002660 RID: 9824
		public int savedLight1;

		// Token: 0x04002661 RID: 9825
		public int savedLight2;

		// Token: 0x04002662 RID: 9826
		public int savedLight3;

		// Token: 0x04002663 RID: 9827
		public InteriorLights currentInteriorLight;

		// Token: 0x04002664 RID: 9828
		private List<Light> lights;

		// Token: 0x04002665 RID: 9829
		public bool baking;

		// Token: 0x04002666 RID: 9830
		private List<Culling> quads;

		// Token: 0x04002667 RID: 9831
		public List<MeshRenderer> quadsToRender = new List<MeshRenderer>();

		// Token: 0x04002668 RID: 9832
		private Plane[] planes = new Plane[0];

		// Token: 0x04002669 RID: 9833
		public List<Material> quadMats = new List<Material>();

		// Token: 0x0400266A RID: 9834
		public Shader quadShader;

		// Token: 0x0400266B RID: 9835
		public bool switchingScenes;
	}
}
