using System;
using UnityEngine;
using UnityEngine.Rendering;

// Token: 0x02000077 RID: 119
[ExecuteInEditMode]
public class NGSSFrustumShadowsSS : MonoBehaviour
{
	// Token: 0x1700001A RID: 26
	// (get) Token: 0x06000B9D RID: 2973 RVA: 0x000E046C File Offset: 0x000DE66C
	private Camera mCamera
	{
		get
		{
			if (this._mCamera == null)
			{
				this._mCamera = base.GetComponent<Camera>();
				if (this._mCamera == null)
				{
					this._mCamera = Camera.main;
				}
			}
			return this._mCamera;
		}
	}

	// Token: 0x06000B9E RID: 2974 RVA: 0x000E04A7 File Offset: 0x000DE6A7
	private void AddCommandBuffers()
	{
	}

	// Token: 0x06000B9F RID: 2975 RVA: 0x000E04A9 File Offset: 0x000DE6A9
	private void RemoveCommandBuffers()
	{
		this.isInitialized = false;
	}

	// Token: 0x06000BA0 RID: 2976 RVA: 0x000E04B2 File Offset: 0x000DE6B2
	public void SetRaySamples(string q)
	{
		this.quality = 0;
		if (q == "Normal")
		{
			this.quality = 1;
		}
		if (q == "High")
		{
			this.quality = 2;
		}
	}

	// Token: 0x06000BA1 RID: 2977 RVA: 0x000E04E4 File Offset: 0x000DE6E4
	private void SetVariables()
	{
		if (!this.mainShadowsLight)
		{
			return;
		}
		if (this.mMaterial == null)
		{
			if (this.frustumShadowsShader == null)
			{
				this.frustumShadowsShader = Shader.Find("Hidden/NGSSFrustumShadows");
			}
			this.mMaterial = new Material(this.frustumShadowsShader);
		}
		if (this.mainShadowsLight)
		{
			Shader.SetGlobalFloat("_ShadowOpacity", 1f - this.mainShadowsLight.shadowStrength);
		}
		if (this.m_Temporal != this.isTemporal)
		{
			base.enabled = false;
			this.isTemporal = this.m_Temporal;
			base.enabled = true;
		}
		this.mMaterial.SetFloat("_TemporalScale", this.m_Temporal ? Mathf.Clamp(this.m_Scale, 0f, 0.99f) : 0f);
		this.mMaterial.SetVector("_Jitter_Offset", this.m_Temporal ? (this.GenerateRandomOffset() * this.m_Jittering) : Vector2.zero);
		this.mMaterial.SetMatrix("WorldToView", this.mCamera.worldToCameraMatrix);
		this.mMaterial.SetVector("LightPos", this.mainShadowsLight.transform.position);
		this.mMaterial.SetVector("LightDir", -this.mCamera.transform.InverseTransformDirection(this.mainShadowsLight.transform.forward));
		this.mMaterial.SetVector("LightDirWorld", -this.mainShadowsLight.transform.forward);
		this.mMaterial.SetFloat("ShadowsEdgeTolerance", this.m_shadowsEdgeBlur * 0.075f);
		if (this.interior)
		{
			if (this._mCamera.orthographic)
			{
				this.mMaterial.SetFloat("ShadowsSoftness", this.m_shadowsBlurPoint);
				this.mMaterial.SetFloat("RayScale", this.m_rayScalePoint);
				this.mMaterial.SetFloat("RayThickness", this.m_rayThicknessPoint);
				if (this.quality == 0)
				{
					this.mMaterial.SetFloat("RaySamples", (float)this.m_raySamplesPoint - 5f);
				}
				if (this.quality >= 1)
				{
					this.mMaterial.SetFloat("RaySamples", (float)this.m_raySamplesPoint);
				}
				this.mMaterial.SetFloat("ShadowsBias", this.m_shadowsBias * 0f);
				if (this.m_dithering)
				{
					this.mMaterial.EnableKeyword("NGSS_USE_DITHERING");
				}
				else
				{
					this.mMaterial.DisableKeyword("NGSS_USE_DITHERING");
				}
			}
			else
			{
				this.mMaterial.SetFloat("ShadowsSoftness", this.m_shadowsBlur);
				this.mMaterial.SetFloat("RayScale", this.m_rayScalePerspective);
				this.mMaterial.SetFloat("RayThickness", this.m_rayThicknessPerspective);
				if (this.quality == 0)
				{
					this.mMaterial.SetFloat("RaySamples", (float)this.m_raySamplesPerspective - 5f);
				}
				if (this.quality >= 1)
				{
					this.mMaterial.SetFloat("RaySamples", (float)this.m_raySamplesPerspective);
				}
				this.mMaterial.SetFloat("ShadowsBias", this.m_shadowsBias * 0.02f);
				this.mMaterial.EnableKeyword("NGSS_USE_DITHERING");
			}
		}
		else
		{
			this.mMaterial.SetFloat("ShadowsSoftness", this.m_shadowsBlur);
			this.mMaterial.SetFloat("RayScale", this.m_rayScale);
			this.mMaterial.SetFloat("RayThickness", this.m_rayThickness);
			if (this.quality == 0)
			{
				this.mMaterial.SetFloat("RaySamples", (float)this.m_raySamples - 5f);
			}
			if (this.quality >= 1)
			{
				this.mMaterial.SetFloat("RaySamples", (float)this.m_raySamples);
			}
			this.mMaterial.SetFloat("ShadowsBias", this.m_shadowsBias * 0f);
			if (this.m_dithering)
			{
				this.mMaterial.EnableKeyword("NGSS_USE_DITHERING");
			}
			else
			{
				this.mMaterial.DisableKeyword("NGSS_USE_DITHERING");
			}
		}
		this.mMaterial.SetFloat("screenHeight", (float)Screen.height / 200f);
		this.mMaterial.SetFloat("screenWidth", (float)Screen.width / 200f);
		this.mMaterial.SetFloat("orthoSize", this.mCamera.orthographicSize);
		this.mMaterial.SetFloat("ShadowsDistanceStart", this.m_shadowsDistanceStart - 10f);
		if (this.farCamera)
		{
			this.mMaterial.SetFloat("farCamera", 1f);
		}
		else
		{
			this.mMaterial.SetFloat("farCamera", 0f);
		}
		if (this.m_deferredBackfaceOptimization && this.mCamera.actualRenderingPath == RenderingPath.DeferredShading)
		{
			this.mMaterial.EnableKeyword("NGSS_DEFERRED_OPTIMIZATION");
			this.mMaterial.SetFloat("BackfaceOpacity", this.m_deferredBackfaceTranslucency);
		}
		else
		{
			this.mMaterial.DisableKeyword("NGSS_DEFERRED_OPTIMIZATION");
		}
		if (this.m_fastBlur)
		{
			this.mMaterial.EnableKeyword("NGSS_FAST_BLUR");
		}
		else
		{
			this.mMaterial.DisableKeyword("NGSS_FAST_BLUR");
		}
		if (this.interior)
		{
			this.mMaterial.EnableKeyword("NGSS_USE_LOCAL_SHADOWS");
		}
		else
		{
			this.mMaterial.DisableKeyword("NGSS_USE_LOCAL_SHADOWS");
		}
		this.mMaterial.SetFloat("RayScreenScale", this.m_rayScreenScale ? 1f : 0f);
	}

	// Token: 0x06000BA2 RID: 2978 RVA: 0x000E0A93 File Offset: 0x000DEC93
	private void Init()
	{
		if (this.isInitialized || this.mainShadowsLight == null)
		{
			return;
		}
		this.SetVariables();
		this.CreateFullscreenQuad(this.mCamera);
		this.AddCommandBuffers();
		this.isInitialized = true;
	}

	// Token: 0x06000BA3 RID: 2979 RVA: 0x000E0ACB File Offset: 0x000DECCB
	private void OnEnable()
	{
		this.Init();
		this.SetVariables();
	}

	// Token: 0x06000BA4 RID: 2980 RVA: 0x000E0ADC File Offset: 0x000DECDC
	private void OnDisable()
	{
		Shader.SetGlobalFloat("NGSS_FRUSTUM_SHADOWS_ENABLED", 0f);
		if (this.isInitialized)
		{
			this.RemoveCommandBuffers();
		}
		if (this.shadowsTexture != null)
		{
			if (this.shadowsTexture)
			{
				this.shadowsTexture.Release();
			}
			if (this.shadowsTempTexture)
			{
				this.shadowsTempTexture.Release();
			}
			if (this.shadowsTempTexture4)
			{
				this.shadowsTempTexture4.Release();
			}
			this.shadowsTexture = null;
		}
		this.SetVariables();
	}

	// Token: 0x06000BA5 RID: 2981 RVA: 0x000E0B69 File Offset: 0x000DED69
	private void OnApplicationQuit()
	{
		if (this.isInitialized)
		{
			this.RemoveCommandBuffers();
		}
	}

	// Token: 0x06000BA6 RID: 2982 RVA: 0x000E0B79 File Offset: 0x000DED79
	private void Awake()
	{
		this.update = true;
		this.originalSamplesDirectional = this.m_raySamples;
		this.originalSamplesPoint = this.m_raySamplesPoint;
	}

	// Token: 0x06000BA7 RID: 2983 RVA: 0x000E0B9C File Offset: 0x000DED9C
	private void OnPreRender()
	{
		this.Init();
		if (!this.isInitialized || !this.mainShadowsLight)
		{
			return;
		}
		this.AddCommandBuffers();
		int pixelWidth = this.mCamera.pixelWidth;
		int pixelHeight = this.mCamera.pixelHeight;
		this.update = false;
		if (this.shadowsTexture != null && (this.shadowsTexture.width != pixelWidth || this.shadowsTexture.height != pixelHeight))
		{
			this.update = true;
		}
		if (this.shadowsTexture == null || this.update)
		{
			if (this.shadowsTexture)
			{
				this.shadowsTexture.Release();
			}
			if (this.shadowsTempTexture)
			{
				this.shadowsTempTexture.Release();
			}
			if (this.shadowsTempTexture4)
			{
				this.shadowsTempTexture4.Release();
			}
			int num = 1;
			this.shadowsTexture = new RenderTexture(pixelWidth / num, pixelHeight / num, 0, RenderTextureFormat.RHalf, RenderTextureReadWrite.Linear);
			this.shadowsTempTexture = new RenderTexture(pixelWidth / num, pixelHeight / num, 0, RenderTextureFormat.RHalf, RenderTextureReadWrite.Linear);
			this.shadowsTempTexture4 = new RenderTexture(pixelWidth / num, pixelHeight / num, 0, RenderTextureFormat.RHalf, RenderTextureReadWrite.Linear);
		}
		if (this.mMaterial == null)
		{
			if (this.frustumShadowsShader == null)
			{
				this.frustumShadowsShader = Shader.Find("Hidden/NGSSFrustumShadows");
			}
			this.mMaterial = new Material(this.frustumShadowsShader);
		}
		this.SetVariables();
	}

	// Token: 0x06000BA8 RID: 2984 RVA: 0x000E0D00 File Offset: 0x000DEF00
	private float GetHaltonValue(int index, int radix)
	{
		float num = 0f;
		float num2 = 1f / (float)radix;
		while (index > 0)
		{
			num += (float)(index % radix) * num2;
			index /= radix;
			num2 /= (float)radix;
		}
		return num;
	}

	// Token: 0x06000BA9 RID: 2985 RVA: 0x000E0D38 File Offset: 0x000DEF38
	private Vector2 GenerateRandomOffset()
	{
		Vector2 vector = new Vector2(this.GetHaltonValue(this.m_SampleIndex & 1023, 2), this.GetHaltonValue(this.m_SampleIndex & 1023, 3));
		int num = this.m_SampleIndex + 1;
		this.m_SampleIndex = num;
		if (num >= 16)
		{
			this.m_SampleIndex = 0;
		}
		float num2 = Mathf.Tan(0.008726646f * this.mCamera.fieldOfView);
		float num3 = num2 * this.mCamera.aspect;
		vector.x *= num3 / (0.5f * (float)this.mCamera.pixelWidth);
		vector.y *= num2 / (0.5f * (float)this.mCamera.pixelHeight);
		return vector;
	}

	// Token: 0x06000BAA RID: 2986 RVA: 0x000E0DF4 File Offset: 0x000DEFF4
	private void InitializeTriangle()
	{
		if (this.fullScreenTriangle)
		{
			return;
		}
		this.fullScreenTriangle = new Mesh
		{
			name = "My Post-Processing Stack Full-Screen Triangle",
			vertices = new Vector3[]
			{
				new Vector3(-1f, -1f, 0f),
				new Vector3(-1f, 3f, 0f),
				new Vector3(3f, -1f, 0f)
			},
			triangles = new int[] { 0, 1, 2 }
		};
		this.fullScreenTriangle.UploadMeshData(true);
	}

	// Token: 0x06000BAB RID: 2987 RVA: 0x000E0EA4 File Offset: 0x000DF0A4
	private void CreateFullscreenQuad(Camera cam)
	{
		Mesh mesh = new Mesh();
		Vector3[] array = new Vector3[]
		{
			cam.ViewportToWorldPoint(new Vector3(0f, 0f, cam.nearClipPlane + 0.01f)),
			cam.ViewportToWorldPoint(new Vector3(1f, 0f, cam.nearClipPlane + 0.01f)),
			cam.ViewportToWorldPoint(new Vector3(0f, 1f, cam.nearClipPlane + 0.01f)),
			cam.ViewportToWorldPoint(new Vector3(1f, 1f, cam.nearClipPlane + 0.01f))
		};
		Vector2[] array2 = new Vector2[]
		{
			new Vector2(0f, 0f),
			new Vector2(1f, 0f),
			new Vector2(0f, 1f),
			new Vector2(1f, 1f)
		};
		int[] array3 = new int[] { 0, 3, 1, 0, 2, 3 };
		mesh.vertices = array;
		mesh.uv = array2;
		mesh.triangles = array3;
		this.fullScreenTriangle = mesh;
	}

	// Token: 0x040011BF RID: 4543
	[Header("REFERENCES")]
	public Light mainShadowsLight;

	// Token: 0x040011C0 RID: 4544
	public Shader frustumShadowsShader;

	// Token: 0x040011C1 RID: 4545
	public bool interior;

	// Token: 0x040011C2 RID: 4546
	[Header("SHADOWS SETTINGS")]
	[Tooltip("Poisson Noise. Randomize samples to remove repeated patterns.")]
	public bool m_dithering;

	// Token: 0x040011C3 RID: 4547
	private bool farCamera;

	// Token: 0x040011C4 RID: 4548
	[Tooltip("If enabled a faster separable blur will be used.\nIf disabled a slower depth aware blur will be used.")]
	private bool m_fastBlur = true;

	// Token: 0x040011C5 RID: 4549
	[Tooltip("If enabled, backfaced lit fragments will be skipped increasing performance. Requires GBuffer normals.")]
	private bool m_deferredBackfaceOptimization;

	// Token: 0x040011C6 RID: 4550
	[Range(0f, 1f)]
	[Tooltip("Set how backfaced lit fragments are shaded. Requires DeferredBackfaceOptimization to be enabled.")]
	private float m_deferredBackfaceTranslucency;

	// Token: 0x040011C7 RID: 4551
	[Tooltip("Tweak this value to remove soft-shadows leaking around edges.")]
	[Range(0.01f, 1f)]
	private float m_shadowsEdgeBlur = 0.25f;

	// Token: 0x040011C8 RID: 4552
	[Tooltip("Overall softness of the shadows.")]
	[Range(0.01f, 1f)]
	private float m_shadowsBlur = 0.5f;

	// Token: 0x040011C9 RID: 4553
	private float m_shadowsBlurPoint = 0.5f;

	// Token: 0x040011CA RID: 4554
	[Tooltip("Tweak this value if your objects display backface shadows.")]
	[Range(0f, 1f)]
	public float m_shadowsBias;

	// Token: 0x040011CB RID: 4555
	[Tooltip("The distance in metters from camera where shadows start to shown.")]
	private float m_shadowsDistanceStart;

	// Token: 0x040011CC RID: 4556
	[Header("RAY SETTINGS")]
	[Tooltip("If enabled the ray length will be scaled at screen space instead of world space. Keep it enabled for an infinite view shadows coverage. Disable it for a ContactShadows like effect. Adjust the Ray Scale property accordingly.")]
	private bool m_rayScreenScale = true;

	// Token: 0x040011CD RID: 4557
	[Header("Orthographic Directional")]
	[Tooltip("Number of samplers between each step. The higher values produces less gaps between shadows but is more costly.")]
	[Range(16f, 128f)]
	public int m_raySamples = 64;

	// Token: 0x040011CE RID: 4558
	[Tooltip("The higher the value, the larger the shadows ray will be.")]
	[Range(0.001f, 1f)]
	public float m_rayScale = 0.25f;

	// Token: 0x040011CF RID: 4559
	[Tooltip("The higher the value, the ticker the shadows will look.")]
	[Range(0f, 1f)]
	public float m_rayThickness = 0.01f;

	// Token: 0x040011D0 RID: 4560
	[Header("Perspective Directional")]
	[Tooltip("Number of samplers between each step. The higher values produces less gaps between shadows but is more costly.")]
	[Range(16f, 128f)]
	public int m_raySamplesPerspective = 64;

	// Token: 0x040011D1 RID: 4561
	[Tooltip("The higher the value, the larger the shadows ray will be.")]
	[Range(0.001f, 10f)]
	public float m_rayScalePerspective = 0.25f;

	// Token: 0x040011D2 RID: 4562
	[Tooltip("The higher the value, the ticker the shadows will look.")]
	[Range(0f, 10f)]
	public float m_rayThicknessPerspective = 0.01f;

	// Token: 0x040011D3 RID: 4563
	[Header("Point")]
	[Tooltip("Number of samplers between each step. The higher values produces less gaps between shadows but is more costly.")]
	[Range(16f, 128f)]
	public int m_raySamplesPoint = 64;

	// Token: 0x040011D4 RID: 4564
	[Tooltip("The higher the value, the larger the shadows ray will be.")]
	[Range(0.001f, 10f)]
	public float m_rayScalePoint = 0.25f;

	// Token: 0x040011D5 RID: 4565
	[Tooltip("The higher the value, the ticker the shadows will look.")]
	[Range(0f, 5f)]
	public float m_rayThicknessPoint = 0.01f;

	// Token: 0x040011D6 RID: 4566
	[Tooltip("Temporal filtering. Improves the shadows aliasing by adding an extra temporal pass. Currently experimental, does not work when the Scene View is open, only in Game View.")]
	private bool m_Temporal;

	// Token: 0x040011D7 RID: 4567
	private bool isTemporal;

	// Token: 0x040011D8 RID: 4568
	[Range(0f, 1f)]
	[Tooltip("Temporal scale in seconds. The bigger the smoother the shadows but produces trail/blur within shadows.")]
	private float m_Scale = 0.75f;

	// Token: 0x040011D9 RID: 4569
	[Tooltip("Improves the temporal filter by shaking the screen space shadows at different frames.")]
	[Range(0f, 0.25f)]
	private float m_Jittering;

	// Token: 0x040011DA RID: 4570
	public RenderTexture shadowsTexture;

	// Token: 0x040011DB RID: 4571
	public RenderTexture shadowsTempTexture;

	// Token: 0x040011DC RID: 4572
	public RenderTexture shadowsTempTexture4;

	// Token: 0x040011DD RID: 4573
	private int mainTexID = Shader.PropertyToID("_MainTex");

	// Token: 0x040011DE RID: 4574
	private int cShadow = Shader.PropertyToID("NGSS_ContactShadowRT");

	// Token: 0x040011DF RID: 4575
	private int cShadow2 = Shader.PropertyToID("NGSS_ContactShadowRT2");

	// Token: 0x040011E0 RID: 4576
	private int dSource = Shader.PropertyToID("NGSS_DepthSourceRT");

	// Token: 0x040011E1 RID: 4577
	private int m_SampleIndex;

	// Token: 0x040011E2 RID: 4578
	private CommandBuffer computeShadowsCB;

	// Token: 0x040011E3 RID: 4579
	private bool isInitialized;

	// Token: 0x040011E4 RID: 4580
	public Material mMaterial;

	// Token: 0x040011E5 RID: 4581
	private bool update;

	// Token: 0x040011E6 RID: 4582
	private int originalSamplesDirectional;

	// Token: 0x040011E7 RID: 4583
	private int originalSamplesPoint;

	// Token: 0x040011E8 RID: 4584
	public int quality;

	// Token: 0x040011E9 RID: 4585
	private Camera _mCamera;

	// Token: 0x040011EA RID: 4586
	private Mesh fullScreenTriangle;
}
