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

// Token: 0x02000009 RID: 9
public class ShadowCaster : MonoBehaviour
{
	// Token: 0x06000078 RID: 120 RVA: 0x0000B76F File Offset: 0x0000996F
	private void OnEnable()
	{
		this.UpdateResources();
	}

	// Token: 0x06000079 RID: 121 RVA: 0x0000B777 File Offset: 0x00009977
	private void OnValidate()
	{
		this.UpdateResources();
	}

	// Token: 0x0600007A RID: 122 RVA: 0x0000B780 File Offset: 0x00009980
	private void UpdateResources()
	{
		if (this.cam == null)
		{
			this.cam = base.GetComponent<Camera>();
			this.cam.depth = -10f;
		}
		if (this.depthTarget == null || this.depthTarget.width != this.targetSize)
		{
			int num = Mathf.Max(this.targetSize, 16);
			if (this.depthTarget != null)
			{
				this.depthTarget.Release();
			}
			this.depthTarget = new RenderTexture(num, num, 16, RenderTextureFormat.Depth, RenderTextureReadWrite.Default);
			this.depthTarget.wrapMode = TextureWrapMode.Clamp;
			this.depthTarget.filterMode = FilterMode.Bilinear;
			this.depthTarget.autoGenerateMips = false;
			this.depthTarget.useMipMap = false;
			this.cam.targetTexture = this.depthTarget;
		}
	}

	// Token: 0x0600007B RID: 123 RVA: 0x0000B854 File Offset: 0x00009A54
	public void OnPreRender()
	{
		if (this.mainCam)
		{
			this.planes = GeometryUtility.CalculateFrustumPlanes(this.mainCam);
			QualitySettings.shadows = ShadowQuality.Disable;
			this.cam.RenderWithShader(this.shader, "RenderType");
			QualitySettings.shadows = ShadowQuality.All;
			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;
			Shader.SetGlobalMatrix("_ShadowMatrix", matrix4x2);
			Shader.SetGlobalTexture("_ShadowCharacterTex", this.depthTarget);
			Shader.SetGlobalFloat("_ShadowBias", this.shadowBias);
		}
	}

	// Token: 0x0600007C RID: 124 RVA: 0x0000B9C0 File Offset: 0x00009BC0
	private void Update()
	{
		if (this.party == null)
		{
			this.party = Links.x.party;
			this.npc = Links.x.npcs;
			this.deads = Links.x.deads;
		}
		if (this.party != null && !Links.x.characterSheet.open)
		{
			this.GetBounds();
		}
	}

	// Token: 0x0600007D RID: 125 RVA: 0x0000BA28 File Offset: 0x00009C28
	public Bounds GetBounds()
	{
		Vector3 vector = Vector3.zero;
		if (Links.x.rtsCamera)
		{
			if (Links.x.rtsCamera.followCharacter && Links.x.worldCamera.orthographic)
			{
				vector = Links.x.rtsCamera.cameraTargetTr.position;
			}
			else if (!Links.x.worldCamera.orthographic && Links.x.rtsCamera.followCharacter && !Links.x.rtsCamera.puzzleTarget && !Links.x.combat.inRally && !Links.x.combat.inPocketAttack && !Links.x.rtsCamera.joyTarget && !Links.x.combat.pocketTarget)
			{
				vector = Links.x.rtsCamera.followCharacter.tr.position;
			}
		}
		if (Records.x.dialogue)
		{
			vector = Vector3.zero;
		}
		this.positions.Clear();
		if (vector == Vector3.zero)
		{
			int num = this.party.Count;
			Bounds bounds = default(Bounds);
			new Vector3(3f, 4f, 3f);
			for (int i = 0; i < num; i++)
			{
				Character character = this.party[i];
				if (character && character.party && this.TestPlanesAABBInternalFast(character.tr.position, 4f))
				{
					bounds.center = character.currentPosition;
					this.positions.Add(bounds);
				}
			}
			num = this.npc.Count;
			for (int j = 0; j < num; j++)
			{
				Character character = this.npc[j];
				if (character && character.visible == 1 && !character.dead && this.TestPlanesAABBInternalFast(character.tr.position, 4f))
				{
					bounds.center = character.currentPosition;
					this.positions.Add(bounds);
				}
			}
		}
		if (this.positions.Count > 0 || vector != Vector3.zero)
		{
			Bounds bounds2 = default(Bounds);
			if (vector == Vector3.zero)
			{
				bounds2 = this.positions[0];
				if (this.positions.Count > 1)
				{
					int k = 1;
					int count = this.positions.Count;
					while (k < count)
					{
						bounds2.Encapsulate(this.positions[k]);
						k++;
					}
				}
				vector = bounds2.center;
			}
			base.gameObject.transform.position = vector;
			if (!this.mainCam.orthographic)
			{
				base.gameObject.transform.position += base.gameObject.transform.forward * -60f;
			}
			else
			{
				base.gameObject.transform.position += base.gameObject.transform.forward * -60f;
			}
			float num2 = bounds2.extents.y;
			if (bounds2.extents.x > num2)
			{
				num2 = bounds2.extents.x;
			}
			if (bounds2.extents.z > num2)
			{
				num2 = bounds2.extents.z;
			}
			this.cam.orthographicSize = num2 + 10f;
			if (this.cam.orthographicSize < 40f)
			{
				this.cam.orthographicSize = 40f;
			}
			if (this.cam.orthographicSize > 100f)
			{
				this.cam.orthographicSize = 100f;
			}
			return bounds2;
		}
		return default(Bounds);
	}

	// Token: 0x0600007E RID: 126 RVA: 0x0000BE24 File Offset: 0x0000A024
	public bool TestPlanesAABBInternalFast(Vector3 pt, float radius)
	{
		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 = pt.x - radius;
				vector2.x = pt.x + radius;
			}
			else
			{
				vector.x = pt.x + radius;
				vector2.x = pt.x - radius;
			}
			if (normal.y < 0f)
			{
				vector.y = pt.y - radius;
				vector2.y = pt.y + radius;
			}
			else
			{
				vector.y = pt.y + radius;
				vector2.y = pt.y - radius;
			}
			if (normal.z < 0f)
			{
				vector.z = pt.z - radius;
				vector2.z = pt.z + radius;
			}
			else
			{
				vector.z = pt.z + radius;
				vector2.z = pt.z - radius;
			}
			if (normal.x * vector.x + normal.y * vector.y + normal.z * vector.z + distance < 0f)
			{
				return false;
			}
		}
		return true;
	}

	// Token: 0x040001C1 RID: 449
	public int targetSize = 512;

	// Token: 0x040001C2 RID: 450
	public float shadowBias = 0.005f;

	// Token: 0x040001C3 RID: 451
	public Camera cam;

	// Token: 0x040001C4 RID: 452
	public Camera mainCam;

	// Token: 0x040001C5 RID: 453
	private RenderTexture depthTarget;

	// Token: 0x040001C6 RID: 454
	private Plane[] planes = new Plane[0];

	// Token: 0x040001C7 RID: 455
	private List<Character> party;

	// Token: 0x040001C8 RID: 456
	private List<Character> npc;

	// Token: 0x040001C9 RID: 457
	private List<Renderer> fleshRenderers;

	// Token: 0x040001CA RID: 458
	public List<Bounds> positions = new List<Bounds>();

	// Token: 0x040001CB RID: 459
	private List<Character> deads;

	// Token: 0x040001CC RID: 460
	public Shader shader;
}
