using System;
using UnityEngine;

namespace LOS
{
	// Token: 0x0200010D RID: 269
	[ExecuteInEditMode]
	[RequireComponent(typeof(Camera))]
	[AddComponentMenu("Line of Sight/LOS Source")]
	public class LOSSource : MonoBehaviour, ILOSSource
	{
		// Token: 0x17000046 RID: 70
		// (get) Token: 0x0600162C RID: 5676 RVA: 0x0019D4E8 File Offset: 0x0019B6E8
		// (set) Token: 0x0600162D RID: 5677 RVA: 0x0019D4F0 File Offset: 0x0019B6F0
		public Color MaskColor
		{
			get
			{
				return this.m_MaskColor;
			}
			set
			{
				this.m_MaskColor = value;
			}
		}

		// Token: 0x17000047 RID: 71
		// (get) Token: 0x0600162E RID: 5678 RVA: 0x0019D4F9 File Offset: 0x0019B6F9
		// (set) Token: 0x0600162F RID: 5679 RVA: 0x0019D501 File Offset: 0x0019B701
		public float MaskIntensity
		{
			get
			{
				return this.m_MaskIntensity;
			}
			set
			{
				this.m_MaskIntensity = value;
			}
		}

		// Token: 0x17000048 RID: 72
		// (get) Token: 0x06001630 RID: 5680 RVA: 0x0019D50A File Offset: 0x0019B70A
		// (set) Token: 0x06001631 RID: 5681 RVA: 0x0019D512 File Offset: 0x0019B712
		public bool MaskInvert
		{
			get
			{
				return this.m_MaskInvert;
			}
			set
			{
				this.m_MaskInvert = value;
			}
		}

		// Token: 0x17000049 RID: 73
		// (get) Token: 0x06001632 RID: 5682 RVA: 0x0019D51B File Offset: 0x0019B71B
		// (set) Token: 0x06001633 RID: 5683 RVA: 0x0019D523 File Offset: 0x0019B723
		public int RenderTargetWidth
		{
			get
			{
				return this.m_RenderTargetWidth;
			}
			set
			{
				this.m_RenderTargetWidth = Mathf.Clamp(value, 1, 4096);
			}
		}

		// Token: 0x1700004A RID: 74
		// (get) Token: 0x06001634 RID: 5684 RVA: 0x0019D537 File Offset: 0x0019B737
		// (set) Token: 0x06001635 RID: 5685 RVA: 0x0019D53F File Offset: 0x0019B73F
		public int RenderTargetHeight
		{
			get
			{
				return this.m_RenderTargetHeight;
			}
			set
			{
				this.m_RenderTargetHeight = Mathf.Clamp(value, 1, 4096);
			}
		}

		// Token: 0x1700004B RID: 75
		// (get) Token: 0x06001636 RID: 5686 RVA: 0x0019D553 File Offset: 0x0019B753
		// (set) Token: 0x06001637 RID: 5687 RVA: 0x0019D55B File Offset: 0x0019B75B
		public float DistanceFade
		{
			get
			{
				return this.m_DistanceFade;
			}
			set
			{
				this.m_DistanceFade = Mathf.Clamp01(value);
			}
		}

		// Token: 0x1700004C RID: 76
		// (get) Token: 0x06001638 RID: 5688 RVA: 0x0019D569 File Offset: 0x0019B769
		// (set) Token: 0x06001639 RID: 5689 RVA: 0x0019D571 File Offset: 0x0019B771
		public float EdgeFade
		{
			get
			{
				return this.m_EdgeFade;
			}
			set
			{
				this.m_EdgeFade = Mathf.Clamp01(value);
			}
		}

		// Token: 0x1700004D RID: 77
		// (get) Token: 0x0600163A RID: 5690 RVA: 0x0019D57F File Offset: 0x0019B77F
		// (set) Token: 0x0600163B RID: 5691 RVA: 0x0019D587 File Offset: 0x0019B787
		public bool ExcludeBackfaces
		{
			get
			{
				return this.m_ExcludeBackfaces;
			}
			set
			{
				this.m_ExcludeBackfaces = value;
			}
		}

		// Token: 0x1700004E RID: 78
		// (get) Token: 0x0600163C RID: 5692 RVA: 0x0019D590 File Offset: 0x0019B790
		// (set) Token: 0x0600163D RID: 5693 RVA: 0x0019D598 File Offset: 0x0019B798
		public float BackfacesFade
		{
			get
			{
				return this.m_BackfacesFade;
			}
			set
			{
				this.m_BackfacesFade = value;
			}
		}

		// Token: 0x1700004F RID: 79
		// (get) Token: 0x0600163E RID: 5694 RVA: 0x0019D5A1 File Offset: 0x0019B7A1
		// (set) Token: 0x0600163F RID: 5695 RVA: 0x0019D5A9 File Offset: 0x0019B7A9
		public float MinVariance
		{
			get
			{
				return this.m_MinVariance;
			}
			set
			{
				this.m_MinVariance = Mathf.Clamp01(value);
			}
		}

		// Token: 0x17000050 RID: 80
		// (get) Token: 0x06001640 RID: 5696 RVA: 0x0019D5B7 File Offset: 0x0019B7B7
		// (set) Token: 0x06001641 RID: 5697 RVA: 0x0019D5BF File Offset: 0x0019B7BF
		public PixelOperation OutOfBoundArea
		{
			get
			{
				return this.m_OutOfBoundArea;
			}
			set
			{
				this.m_OutOfBoundArea = value;
			}
		}

		// Token: 0x17000051 RID: 81
		// (get) Token: 0x06001642 RID: 5698 RVA: 0x0019D5C8 File Offset: 0x0019B7C8
		public Camera SourceCamera
		{
			get
			{
				return this.m_Camera;
			}
		}

		// Token: 0x17000052 RID: 82
		// (get) Token: 0x06001643 RID: 5699 RVA: 0x0019D5D0 File Offset: 0x0019B7D0
		public Bounds CameraBounds
		{
			get
			{
				return this.m_CameraBounds;
			}
		}

		// Token: 0x17000053 RID: 83
		// (get) Token: 0x06001644 RID: 5700 RVA: 0x0019D5D8 File Offset: 0x0019B7D8
		public GameObject GameObject
		{
			get
			{
				return base.gameObject;
			}
		}

		// Token: 0x17000054 RID: 84
		// (get) Token: 0x06001645 RID: 5701 RVA: 0x0019D5E0 File Offset: 0x0019B7E0
		public Plane[] FrustumPlanes
		{
			get
			{
				return this.m_FrustumPlanes;
			}
		}

		// Token: 0x17000055 RID: 85
		// (get) Token: 0x06001646 RID: 5702 RVA: 0x0019D5E8 File Offset: 0x0019B7E8
		public Vector4 SourceInfo
		{
			get
			{
				Vector4 vector = Vector4.zero;
				if (this.m_Camera != null)
				{
					vector = this.m_Camera.transform.position;
					vector.w = this.m_Camera.farClipPlane;
				}
				return vector;
			}
		}

		// Token: 0x17000056 RID: 86
		// (get) Token: 0x06001647 RID: 5703 RVA: 0x0019D632 File Offset: 0x0019B832
		public bool IsVisible
		{
			get
			{
				return this.m_IsVisibile;
			}
		}

		// Token: 0x06001648 RID: 5704 RVA: 0x0019D63A File Offset: 0x0019B83A
		private void Awake()
		{
			this.m_Camera = base.GetComponent<Camera>();
		}

		// Token: 0x06001649 RID: 5705 RVA: 0x0019D648 File Offset: 0x0019B848
		public void SetCamera(Camera cam)
		{
			this.m_Camera = cam;
		}

		// Token: 0x0600164A RID: 5706 RVA: 0x0019D654 File Offset: 0x0019B854
		private void OnEnable()
		{
			LOSManager.Instance.AddLOSSource(this);
			this.m_Camera = base.GetComponent<Camera>();
			base.enabled &= Util.Verify(this.m_Camera != null, "Camera Component missing");
			if (base.enabled)
			{
				LOSHelper.InitSourceCamera(this.m_Camera);
				this.m_CameraBounds = LOSHelper.CalculateSourceBounds(this.m_Camera);
				LOSHelper.ExtractFrustumPlanes(this.m_FrustumPlanes, this.m_Camera);
			}
			this.m_IsVisibile = true;
		}

		// Token: 0x0600164B RID: 5707 RVA: 0x0019D6D7 File Offset: 0x0019B8D7
		private void OnDisable()
		{
			LOSManager.Instance.RemoveLOSSource(this);
		}

		// Token: 0x0600164C RID: 5708 RVA: 0x0019D6E4 File Offset: 0x0019B8E4
		public Matrix4x4 CalculatePerspectiveCorners(Camera currentCamera)
		{
			float farClipPlane = currentCamera.farClipPlane;
			float fieldOfView = currentCamera.fieldOfView;
			float aspect = currentCamera.aspect;
			Matrix4x4 zero = Matrix4x4.zero;
			float num = fieldOfView * 0.5f * 0.017453292f;
			float num2 = Mathf.Tan(num) * farClipPlane;
			float num3 = Mathf.Tan(num) * aspect * farClipPlane;
			Vector3 vector = currentCamera.transform.up * num2;
			Vector3 vector2 = currentCamera.transform.right * num3;
			Vector3 vector3 = currentCamera.transform.forward * farClipPlane;
			Vector3 vector4 = vector3 + vector - vector2;
			Vector3 vector5 = vector3 + vector + vector2;
			Vector3 vector6 = vector3 - vector + vector2;
			Vector3 vector7 = vector3 - vector - vector2;
			zero.SetRow(0, vector4);
			zero.SetRow(1, vector5);
			zero.SetRow(2, vector6);
			zero.SetRow(3, vector7);
			return zero;
		}

		// Token: 0x0600164D RID: 5709 RVA: 0x0019D7E4 File Offset: 0x0019B9E4
		public Bounds CalculateSourceBounds()
		{
			Matrix4x4 matrix4x = this.CalculatePerspectiveCorners(this.m_Camera);
			Vector3 position = this.m_Camera.transform.position;
			Vector3 vector = this.GetFrustumMin(matrix4x);
			vector = Vector3.Min(vector + position, position);
			Vector3 vector2 = this.GetFrustumMax(matrix4x);
			vector2 = Vector3.Max(vector2 + position, position);
			Bounds bounds = default(Bounds);
			bounds.SetMinMax(vector, vector2);
			return bounds;
		}

		// Token: 0x0600164E RID: 5710 RVA: 0x0019D850 File Offset: 0x0019BA50
		private Vector3 GetFrustumMin(Matrix4x4 frustumCorners)
		{
			Vector3 vector = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
			for (int i = 0; i < 4; i++)
			{
				vector = Vector3.Min(vector, frustumCorners.GetRow(i));
			}
			return vector;
		}

		// Token: 0x0600164F RID: 5711 RVA: 0x0019D894 File Offset: 0x0019BA94
		private Vector3 GetFrustumMax(Matrix4x4 frustumCorners)
		{
			Vector3 vector = new Vector3(float.MinValue, float.MinValue, float.MinValue);
			for (int i = 0; i < 4; i++)
			{
				vector = Vector3.Max(vector, frustumCorners.GetRow(i));
			}
			return vector;
		}

		// Token: 0x06001650 RID: 5712 RVA: 0x0019D8D8 File Offset: 0x0019BAD8
		public void ExtractFrustumPlanes(Camera camera)
		{
			Matrix4x4 matrix4x = camera.projectionMatrix * camera.worldToCameraMatrix;
			Vector3 vector = new Vector3(matrix4x[3, 0] + matrix4x[0, 0], matrix4x[3, 1] + matrix4x[0, 1], matrix4x[3, 2] + matrix4x[0, 2]);
			float num = vector.magnitude;
			this.m_FrustumPlanes[0].normal = vector.normalized;
			this.m_FrustumPlanes[0].distance = (matrix4x[3, 3] + matrix4x[0, 3]) / num;
			vector = new Vector3(matrix4x[3, 0] - matrix4x[0, 0], matrix4x[3, 1] - matrix4x[0, 1], matrix4x[3, 2] - matrix4x[0, 2]);
			num = vector.magnitude;
			this.m_FrustumPlanes[1].normal = vector.normalized;
			this.m_FrustumPlanes[1].distance = (matrix4x[3, 3] - matrix4x[0, 3]) / num;
			vector = new Vector3(matrix4x[3, 0] + matrix4x[1, 0], matrix4x[3, 1] + matrix4x[1, 1], matrix4x[3, 2] + matrix4x[1, 2]);
			num = vector.magnitude;
			this.m_FrustumPlanes[2].normal = vector.normalized;
			this.m_FrustumPlanes[2].distance = (matrix4x[3, 3] + matrix4x[1, 3]) / num;
			vector = new Vector3(matrix4x[3, 0] - matrix4x[1, 0], matrix4x[3, 1] - matrix4x[1, 1], matrix4x[3, 2] - matrix4x[1, 2]);
			num = vector.magnitude;
			this.m_FrustumPlanes[3].normal = vector.normalized;
			this.m_FrustumPlanes[3].distance = (matrix4x[3, 3] - matrix4x[1, 3]) / num;
			vector = new Vector3(matrix4x[3, 0] + matrix4x[2, 0], matrix4x[3, 1] + matrix4x[2, 1], matrix4x[3, 2] + matrix4x[2, 2]);
			num = vector.magnitude;
			this.m_FrustumPlanes[4].normal = vector.normalized;
			this.m_FrustumPlanes[4].distance = (matrix4x[3, 3] + matrix4x[2, 3]) / num;
			vector = new Vector3(matrix4x[3, 0] - matrix4x[2, 0], matrix4x[3, 1] - matrix4x[2, 1], matrix4x[3, 2] - matrix4x[2, 2]);
			num = vector.magnitude;
			this.m_FrustumPlanes[5].normal = vector.normalized;
			this.m_FrustumPlanes[5].distance = (matrix4x[3, 3] - matrix4x[2, 3]) / num;
		}

		// Token: 0x04002635 RID: 9781
		private const int MIN_TEXTURE_SIZE = 1;

		// Token: 0x04002636 RID: 9782
		private const int MAX_TEXTURE_SIZE = 4096;

		// Token: 0x04002637 RID: 9783
		[Header("Mask Settings", order = 0)]
		[Tooltip("Color rendered into mask")]
		[SerializeField]
		private Color m_MaskColor = Color.white;

		// Token: 0x04002638 RID: 9784
		[Tooltip("Intensity of color rendered into mask")]
		[Range(0f, 10f)]
		[SerializeField]
		private float m_MaskIntensity = 1f;

		// Token: 0x04002639 RID: 9785
		[Tooltip("Inverts mask")]
		[SerializeField]
		private bool m_MaskInvert;

		// Token: 0x0400263A RID: 9786
		[Header("Render Target Settings", order = 1)]
		[Tooltip("Width of target render texture")]
		[SerializeField]
		private int m_RenderTargetWidth = 512;

		// Token: 0x0400263B RID: 9787
		[Tooltip("Height of target render texture")]
		[SerializeField]
		private int m_RenderTargetHeight = 512;

		// Token: 0x0400263C RID: 9788
		[Header("Fade Settings", order = 2)]
		[Tooltip("Fade over distance amount")]
		[Range(0f, 1f)]
		[SerializeField]
		private float m_DistanceFade = 0.1f;

		// Token: 0x0400263D RID: 9789
		[Tooltip("Edge fade amount")]
		[Range(0f, 1f)]
		[SerializeField]
		private float m_EdgeFade = 0.1f;

		// Token: 0x0400263E RID: 9790
		[Header("Backface Settings", order = 4)]
		[Tooltip("Excludes backfacing faces from mask")]
		[SerializeField]
		private bool m_ExcludeBackfaces = true;

		// Token: 0x0400263F RID: 9791
		[Tooltip("Fades the edge of the backface transition in the mask")]
		[Range(0f, 1f)]
		[SerializeField]
		private float m_BackfacesFade = 0.1f;

		// Token: 0x04002640 RID: 9792
		[Header("Advanced Settings", order = 4)]
		[Tooltip("Reduces artifacts in mask, similar to shadow bias setting")]
		[Range(0f, 1f)]
		[SerializeField]
		private float m_MinVariance = 0.1f;

		// Token: 0x04002641 RID: 9793
		[Tooltip("Sets how pixels above and below the cameras frustum are handled")]
		[SerializeField]
		private PixelOperation m_OutOfBoundArea;

		// Token: 0x04002642 RID: 9794
		private Plane[] m_FrustumPlanes = new Plane[6];

		// Token: 0x04002643 RID: 9795
		private Camera m_Camera;

		// Token: 0x04002644 RID: 9796
		private Bounds m_CameraBounds;

		// Token: 0x04002645 RID: 9797
		private bool m_IsVisibile = true;
	}
}
