using System;
using UnityEngine;

namespace LOS
{
	// Token: 0x02000102 RID: 258
	[RequireComponent(typeof(Camera))]
	public class LineOfSightCone : MonoBehaviour
	{
		// Token: 0x17000027 RID: 39
		// (get) Token: 0x060015D6 RID: 5590 RVA: 0x0019C10A File Offset: 0x0019A30A
		public static Plane[] CameraFrustumPlanes
		{
			get
			{
				return LineOfSightCone.m_CameraFrustumPlanes;
			}
		}

		// Token: 0x060015D7 RID: 5591 RVA: 0x0019C111 File Offset: 0x0019A311
		private void Awake()
		{
			this.m_Camera = base.GetComponent<Camera>();
			this.CreateTextures();
		}

		// Token: 0x060015D8 RID: 5592 RVA: 0x0019C125 File Offset: 0x0019A325
		private void OnEnable()
		{
			LOSHelper.ExtractFrustumPlanes(LineOfSightCone.m_CameraFrustumPlanes, this.m_Camera);
		}

		// Token: 0x060015D9 RID: 5593 RVA: 0x0019C138 File Offset: 0x0019A338
		private void CreateTextures()
		{
			this.width = Screen.width;
			this.height = Screen.height;
			if (this.textures[0] != null)
			{
				this.textures[0].Release();
			}
			this.textures = new RenderTexture[1];
			bool flag = false;
			RenderTexture renderTexture = new RenderTexture(this.texSize, this.texSize, 16, RenderTextureFormat.RGHalf);
			renderTexture.name = "rt0";
			this.textures[0] = renderTexture;
			renderTexture.useMipMap = flag;
			renderTexture.filterMode = FilterMode.Trilinear;
		}

		// Token: 0x060015DA RID: 5594 RVA: 0x0019C1BE File Offset: 0x0019A3BE
		private void OnDisable()
		{
		}

		// Token: 0x060015DB RID: 5595 RVA: 0x0019C1C0 File Offset: 0x0019A3C0
		private void OnPreRender()
		{
			if (this.m_Camera == null)
			{
				return;
			}
			if (this.textures[0] == null)
			{
				this.CreateTextures();
			}
			if (base.transform.hasChanged)
			{
				LOSHelper.ExtractFrustumPlanes(LineOfSightCone.m_CameraFrustumPlanes, this.m_Camera);
				base.transform.hasChanged = false;
			}
		}

		// Token: 0x060015DC RID: 5596 RVA: 0x0019C21C File Offset: 0x0019A41C
		[ImageEffectOpaque]
		private void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			if (!this.m_Camera)
			{
				return;
			}
			Matrix4x4 matrix4x;
			Matrix4x4 matrix4x2;
			LOSHelper.CalculateViewVectors(this.m_Camera, out matrix4x, out matrix4x2);
			Materials.Mask.SetMatrix(ShaderID.FrustumRays, matrix4x);
			Materials.Mask.SetMatrix(ShaderID.FrustumOrigins, matrix4x2);
			Materials.Mask.SetMatrix(ShaderID.WorldToCameraMatrix, this.m_Camera.worldToCameraMatrix);
			Material skybox = RenderSettings.skybox;
			RenderSettings.skybox = Materials.SkyBox;
			this.RenderSourceToMask(this.caster, 0, this.caster.gameObject.transform.position);
			RenderSettings.skybox = skybox;
			if (!this.combineMaterial)
			{
				this.combineMaterial = new Material(this.coneShader);
			}
			if (this.caster)
			{
				this.combineMaterial.SetFloat("cameraLengthCone", this.caster.SourceCamera.farClipPlane);
				this.combineMaterial.SetFloat("minVarianceCone", this.caster.MinVariance);
				this.combineMaterial.SetFloat("backfaceFade", this.caster.BackfacesFade);
			}
			Shader.SetGlobalTexture("_SourceDepthTexCone", this.textures[0]);
			Graphics.Blit(source, destination, this.combineMaterial, 0);
		}

		// Token: 0x060015DD RID: 5597 RVA: 0x0019C358 File Offset: 0x0019A558
		private void RenderSourceToMask(LOSSource losSource, int num, Vector3 center)
		{
			Camera sourceCamera = losSource.SourceCamera;
			Materials.SkyBox.SetVector(ShaderID.FarPlane, new Vector4(sourceCamera.farClipPlane, sourceCamera.farClipPlane, sourceCamera.farClipPlane, sourceCamera.farClipPlane));
			sourceCamera.targetTexture = this.textures[num];
			sourceCamera.RenderWithShader(Shaders.DepthCone, null);
			Shader.SetGlobalVector("_SourcePosCone", center);
			Shader.SetGlobalVector("_LosPosCone", losSource.gameObject.transform.position + losSource.gameObject.transform.forward);
			Shader.SetGlobalMatrix("_SourceWorldProjCone", sourceCamera.projectionMatrix * sourceCamera.worldToCameraMatrix);
			Shader.SetGlobalMatrix("_Source_CamToWorldCone", sourceCamera.cameraToWorldMatrix);
			Shader.SetGlobalTexture("_SourceDepthTexCone", this.textures[num]);
		}

		// Token: 0x0400260A RID: 9738
		public int texSize = 1024;

		// Token: 0x0400260B RID: 9739
		public static Plane[] m_CameraFrustumPlanes = new Plane[6];

		// Token: 0x0400260C RID: 9740
		public LOSSource caster;

		// Token: 0x0400260D RID: 9741
		public LOSSource caster2;

		// Token: 0x0400260E RID: 9742
		private Camera m_Camera;

		// Token: 0x0400260F RID: 9743
		public RenderTexture[] textures = new RenderTexture[1];

		// Token: 0x04002610 RID: 9744
		private Material combineMaterial;

		// Token: 0x04002611 RID: 9745
		private int width;

		// Token: 0x04002612 RID: 9746
		private int height;

		// Token: 0x04002613 RID: 9747
		public Shader coneShader;
	}
}
