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

// Token: 0x0200000A RID: 10
[ExecuteInEditMode]
public class ShadowCasterBaker : MonoBehaviour
{
	// Token: 0x06000080 RID: 128 RVA: 0x0000BFBB File Offset: 0x0000A1BB
	private void OnEnable()
	{
		this.UpdateResources(false);
	}

	// Token: 0x06000081 RID: 129 RVA: 0x0000BFC4 File Offset: 0x0000A1C4
	private void OnValidate()
	{
		this.UpdateResources(false);
	}

	// Token: 0x06000082 RID: 130 RVA: 0x0000BFCD File Offset: 0x0000A1CD
	private void OnDisable()
	{
		if (this.cam)
		{
			this.cam.enabled = false;
		}
	}

	// Token: 0x06000083 RID: 131 RVA: 0x0000BFE8 File Offset: 0x0000A1E8
	public void UpdateResources(bool always)
	{
		if (this.cam == null)
		{
			this.cam = base.GetComponent<Camera>();
			this.cam.depth = -1000f;
		}
		if (this.depthTarget == null || always)
		{
			this.targetSize = this.targetSizePerspective;
			if (this.mainCam && this.mainCam.orthographic)
			{
				this.targetSize = this.targetSizeOrthographic;
			}
			int num = this.targetSize;
			if (this.depthTarget)
			{
				this.depthTarget.Release();
			}
			if (this.shadowSaveTexture)
			{
				this.shadowSaveTexture.Release();
			}
			this.depthTarget = new RenderTexture(num, num, 16, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear);
			this.shadowSaveTexture = new RenderTexture(num, num, 16, RenderTextureFormat.RGFloat);
			this.depthTarget.wrapMode = TextureWrapMode.Clamp;
			this.depthTarget.filterMode = FilterMode.Bilinear;
			this.depthTarget.autoGenerateMips = false;
			this.depthTarget.useMipMap = false;
			this.cam.targetTexture = this.depthTarget;
		}
	}

	// Token: 0x06000084 RID: 132 RVA: 0x0000C100 File Offset: 0x0000A300
	private Bounds GetChildRendererBounds(GameObject go)
	{
		if (this.mainCam)
		{
			this.planes = GeometryUtility.CalculateFrustumPlanes(this.mainCam);
		}
		if (this.mainCam2)
		{
			this.planes2 = GeometryUtility.CalculateFrustumPlanes(this.mainCam2);
		}
		if (this.mainCam3)
		{
			this.planes3 = GeometryUtility.CalculateFrustumPlanes(this.mainCam3);
		}
		if (this.mainCam4)
		{
			this.planes4 = GeometryUtility.CalculateFrustumPlanes(this.mainCam4);
		}
		if (!(go != null))
		{
			return default(Bounds);
		}
		MeshRenderer[] componentsInChildren = go.GetComponentsInChildren<MeshRenderer>();
		this.inBounds.Clear();
		Bounds bounds = default(Bounds);
		int num = componentsInChildren.Length;
		for (int i = 0; i < num; i++)
		{
			MeshRenderer meshRenderer = componentsInChildren[i];
			bounds = meshRenderer.bounds;
			if (meshRenderer && meshRenderer.gameObject.activeSelf && (!this.checkShadowCastingMode || (this.checkShadowCastingMode && meshRenderer.shadowCastingMode == ShadowCastingMode.On)))
			{
				if (this.TestPlanesAABBInternalFastBounds(bounds.min, bounds.max))
				{
					this.inBounds.Add(componentsInChildren[i]);
				}
				else if (this.mainCam2)
				{
					if (this.TestPlanesAABBInternalFastBounds2(bounds.min, bounds.max))
					{
						this.inBounds.Add(componentsInChildren[i]);
					}
					else if (this.mainCam3)
					{
						if (this.TestPlanesAABBInternalFastBounds3(bounds.min, bounds.max))
						{
							this.inBounds.Add(componentsInChildren[i]);
						}
						else if (this.mainCam4 && this.TestPlanesAABBInternalFastBounds4(bounds.min, bounds.max))
						{
							this.inBounds.Add(componentsInChildren[i]);
						}
					}
				}
			}
		}
		if (this.inBounds.Count > 0)
		{
			Bounds bounds2 = this.inBounds[0].bounds;
			if (this.inBounds.Count > 1)
			{
				num = this.inBounds.Count;
				int j = 1;
				int num2 = num;
				while (j < num2)
				{
					bounds2.Encapsulate(this.inBounds[j].bounds);
					j++;
				}
			}
			return bounds2;
		}
		return default(Bounds);
	}

	// Token: 0x06000085 RID: 133 RVA: 0x0000C354 File Offset: 0x0000A554
	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: 0x06000086 RID: 134 RVA: 0x0000C4A0 File Offset: 0x0000A6A0
	public bool TestPlanesAABBInternalFastBounds2(Vector3 boundsMin, Vector3 boundsMax)
	{
		for (int i = 0; i < this.planes2.Length; i++)
		{
			Vector3 normal = this.planes2[i].normal;
			float distance = this.planes2[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: 0x06000087 RID: 135 RVA: 0x0000C5EC File Offset: 0x0000A7EC
	public bool TestPlanesAABBInternalFastBounds3(Vector3 boundsMin, Vector3 boundsMax)
	{
		for (int i = 0; i < this.planes3.Length; i++)
		{
			Vector3 normal = this.planes3[i].normal;
			float distance = this.planes3[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: 0x06000088 RID: 136 RVA: 0x0000C738 File Offset: 0x0000A938
	public bool TestPlanesAABBInternalFastBounds4(Vector3 boundsMin, Vector3 boundsMax)
	{
		for (int i = 0; i < this.planes4.Length; i++)
		{
			Vector3 normal = this.planes4[i].normal;
			float distance = this.planes4[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: 0x06000089 RID: 137 RVA: 0x0000C882 File Offset: 0x0000AA82
	private void Update()
	{
		if (this.folder)
		{
			this.UpdateStuff();
		}
	}

	// Token: 0x0600008A RID: 138 RVA: 0x0000C898 File Offset: 0x0000AA98
	public void UpdateStuff()
	{
		this.bounds = default(Bounds);
		Vector3 vector = this.bounds.center;
		if (this.mainCam)
		{
			vector = this.mainCam.gameObject.GetComponent<CameraParams>().center;
		}
		base.gameObject.transform.position = vector;
		base.gameObject.transform.position = base.gameObject.transform.position + base.gameObject.transform.forward * -1f * this.cameraDistance + base.gameObject.transform.right * -5f;
		float num = this.bounds.extents.y;
		if (this.bounds.extents.x > num)
		{
			num = this.bounds.extents.x;
		}
		if (this.bounds.extents.z > num)
		{
			num = this.bounds.extents.z;
		}
		this.cam.orthographicSize = this.minSize;
		if (this.cam.orthographicSize < this.minSize)
		{
			this.cam.orthographicSize = this.minSize;
		}
	}

	// Token: 0x0600008B RID: 139 RVA: 0x0000C9E8 File Offset: 0x0000ABE8
	private void OnDrawGizmos()
	{
		if (this.folder)
		{
			Gizmos.DrawWireCube(this.bounds.center, this.bounds.size);
		}
	}

	// Token: 0x0600008C RID: 140 RVA: 0x0000CA14 File Offset: 0x0000AC14
	private void OnRenderImage(RenderTexture source, RenderTexture dest)
	{
		if (this.saveShadowMaterial == null)
		{
			this.saveShadowMaterial = new Material(Shader.Find("Hidden/SaveShadows"));
		}
		Graphics.Blit(source, this.shadowSaveTexture, this.saveShadowMaterial, 0);
		Shader.SetGlobalTexture("_BakedShadowCharacterTex", this.shadowSaveTexture);
		Graphics.Blit(source, dest);
	}

	// Token: 0x0600008D RID: 141 RVA: 0x0000CA70 File Offset: 0x0000AC70
	private void OnPostRender()
	{
		Matrix4x4 matrix4x = new Matrix4x4
		{
			m00 = 0.5f,
			m01 = 0f,
			m02 = 0f,
			m03 = 0.5f,
			m10 = 0f,
			m11 = 0.5f,
			m12 = 0f,
			m13 = 0.5f,
			m20 = 0f,
			m21 = 0f,
			m22 = 0.5f,
			m23 = 0.5f,
			m30 = 0f,
			m31 = 0f,
			m32 = 0f,
			m33 = 1f
		};
		Matrix4x4 worldToCameraMatrix = this.cam.worldToCameraMatrix;
		Matrix4x4 projectionMatrix = this.cam.projectionMatrix;
		Matrix4x4 matrix4x2 = matrix4x * projectionMatrix * worldToCameraMatrix;
		this.shadowMatrix = matrix4x2;
		Shader.SetGlobalMatrix("_BakedShadowMatrix", matrix4x2);
	}

	// Token: 0x040001CD RID: 461
	public int targetSizePerspective = 4096;

	// Token: 0x040001CE RID: 462
	public int targetSizeOrthographic = 2048;

	// Token: 0x040001CF RID: 463
	public int targetSize = 4096;

	// Token: 0x040001D0 RID: 464
	public float padding = 1f;

	// Token: 0x040001D1 RID: 465
	public float minSize = 54f;

	// Token: 0x040001D2 RID: 466
	public float groundPoint;

	// Token: 0x040001D3 RID: 467
	public Transform mainLightTr;

	// Token: 0x040001D4 RID: 468
	public float shadowBias = 0.005f;

	// Token: 0x040001D5 RID: 469
	public float cameraDistance = 50f;

	// Token: 0x040001D6 RID: 470
	public Camera cam;

	// Token: 0x040001D7 RID: 471
	public Camera mainCam;

	// Token: 0x040001D8 RID: 472
	public Camera mainCam2;

	// Token: 0x040001D9 RID: 473
	public Camera mainCam3;

	// Token: 0x040001DA RID: 474
	public Camera mainCam4;

	// Token: 0x040001DB RID: 475
	public RenderTexture depthTarget;

	// Token: 0x040001DC RID: 476
	public Plane[] planes = new Plane[0];

	// Token: 0x040001DD RID: 477
	public Plane[] planes2 = new Plane[0];

	// Token: 0x040001DE RID: 478
	public Plane[] planes3 = new Plane[0];

	// Token: 0x040001DF RID: 479
	public Plane[] planes4 = new Plane[0];

	// Token: 0x040001E0 RID: 480
	public RenderTexture shadowSaveTexture;

	// Token: 0x040001E1 RID: 481
	public bool interior;

	// Token: 0x040001E2 RID: 482
	public bool checkShadowCastingMode;

	// Token: 0x040001E3 RID: 483
	public bool update;

	// Token: 0x040001E4 RID: 484
	public GameObject folder;

	// Token: 0x040001E5 RID: 485
	private Bounds bounds;

	// Token: 0x040001E6 RID: 486
	public List<MeshRenderer> inBounds = new List<MeshRenderer>();

	// Token: 0x040001E7 RID: 487
	public Matrix4x4 shadowMatrix;

	// Token: 0x040001E8 RID: 488
	private Material saveShadowMaterial;

	// Token: 0x040001E9 RID: 489
	public Texture2D encodedTextureTest;
}
