using System;
using UnityEngine;

namespace LOS
{
	// Token: 0x02000106 RID: 262
	public static class LOSHelper
	{
		// Token: 0x060015F1 RID: 5617 RVA: 0x0019C6A0 File Offset: 0x0019A8A0
		public static void ExtractFrustumPlanes(Plane[] planes, 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;
			planes[0].normal = vector.normalized;
			planes[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;
			planes[1].normal = vector.normalized;
			planes[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;
			planes[2].normal = vector.normalized;
			planes[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;
			planes[3].normal = vector.normalized;
			planes[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;
			planes[4].normal = vector.normalized;
			planes[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;
			planes[5].normal = vector.normalized;
			planes[5].distance = (matrix4x[3, 3] - matrix4x[2, 3]) / num;
		}

		// Token: 0x060015F2 RID: 5618 RVA: 0x0019C9A8 File Offset: 0x0019ABA8
		public static 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: 0x060015F3 RID: 5619 RVA: 0x0019CAA8 File Offset: 0x0019ACA8
		public static void CalculateViewVectors(Camera currentCamera, out Matrix4x4 rays, out Matrix4x4 origins)
		{
			Matrix4x4 inverse = currentCamera.projectionMatrix.inverse;
			rays = Matrix4x4.zero;
			origins = Matrix4x4.zero;
			for (int i = 0; i < 4; i++)
			{
				Vector4 vector = inverse * LOSHelper.m_NearHomogenousCorners[i];
				Vector4 vector2 = inverse * LOSHelper.m_FarHomogenousCorners[i];
				vector /= vector.w;
				Vector4 vector3 = vector2 / vector2.w - vector;
				vector3 /= vector3.z;
				Vector4 vector4 = vector - vector3 * vector.z;
				vector4.w = 1f;
				vector4 = currentCamera.cameraToWorldMatrix * vector4;
				vector3 *= -1f;
				vector3.w = 0f;
				vector3 = currentCamera.cameraToWorldMatrix * vector3;
				rays.SetRow(i, vector3);
				origins.SetRow(i, vector4);
			}
		}

		// Token: 0x060015F4 RID: 5620 RVA: 0x0019CBAC File Offset: 0x0019ADAC
		public static Bounds CalculateSourceBounds(Camera sourceCamera)
		{
			Matrix4x4 matrix4x = LOSHelper.CalculatePerspectiveCorners(sourceCamera);
			Vector3 position = sourceCamera.transform.position;
			Vector3 vector = LOSHelper.GetFrustumMin(matrix4x);
			vector = Vector3.Min(vector + position, position);
			Vector3 vector2 = LOSHelper.GetFrustumMax(matrix4x);
			vector2 = Vector3.Max(vector2 + position, position);
			Bounds bounds = default(Bounds);
			bounds.SetMinMax(vector, vector2);
			return bounds;
		}

		// Token: 0x060015F5 RID: 5621 RVA: 0x0019CC08 File Offset: 0x0019AE08
		private static 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: 0x060015F6 RID: 5622 RVA: 0x0019CC4C File Offset: 0x0019AE4C
		private static 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: 0x060015F7 RID: 5623 RVA: 0x0019CC90 File Offset: 0x0019AE90
		public static int NearestPowerOfTwo(int value)
		{
			return (int)Mathf.Pow(2f, (float)Mathf.CeilToInt(Mathf.Log((float)value) / Mathf.Log(2f)));
		}

		// Token: 0x060015F8 RID: 5624 RVA: 0x0019CCB5 File Offset: 0x0019AEB5
		public static void InitSourceCamera(Camera sourceCamera)
		{
			sourceCamera.enabled = false;
			sourceCamera.orthographic = false;
			sourceCamera.renderingPath = RenderingPath.VertexLit;
			sourceCamera.clearFlags = CameraClearFlags.Skybox;
			sourceCamera.allowHDR = false;
			sourceCamera.depthTextureMode = DepthTextureMode.None;
		}

		// Token: 0x060015F9 RID: 5625 RVA: 0x0019CCE4 File Offset: 0x0019AEE4
		public static bool CheckBoundsVisibility(LOSSource losSource, Bounds meshBounds, int layerMask)
		{
			Camera sourceCamera = losSource.SourceCamera;
			return losSource.IsVisible && sourceCamera != null && GeometryUtility.TestPlanesAABB(losSource.FrustumPlanes, meshBounds) && LOSHelper.CheckRayCast(sourceCamera, meshBounds, losSource.SourceInfo.w, layerMask);
		}

		// Token: 0x060015FA RID: 5626 RVA: 0x0019CD30 File Offset: 0x0019AF30
		private static bool CheckRayCast(Camera currentCamera, Bounds meshBounds, float maxDistance, int layerMask)
		{
			bool flag = false;
			Vector3 position = currentCamera.transform.position;
			if (LOSHelper.CheckRayConnect(meshBounds.center, position, maxDistance, layerMask) || LOSHelper.CheckRayConnect(meshBounds.max, position, maxDistance, layerMask) || LOSHelper.CheckRayConnect(meshBounds.min, position, maxDistance, layerMask))
			{
				flag = true;
			}
			return flag;
		}

		// Token: 0x060015FB RID: 5627 RVA: 0x0019CD84 File Offset: 0x0019AF84
		private static bool CheckRayConnect(Vector3 origin, Vector3 cameraPosition, float maxDistance, int layerMask)
		{
			bool flag = false;
			Vector3 vector = origin - cameraPosition;
			if (vector.sqrMagnitude < maxDistance * maxDistance)
			{
				Ray ray = new Ray(cameraPosition, vector);
				float num = vector.magnitude - 0.1f;
				num = Mathf.Max(1E-05f, num);
				if (!Physics.Raycast(ray, num, layerMask))
				{
					flag = true;
				}
			}
			return flag;
		}

		// Token: 0x04002618 RID: 9752
		private static readonly Vector4[] m_NearHomogenousCorners = new Vector4[]
		{
			new Vector4(-1f, 1f, -1f, 1f),
			new Vector4(1f, 1f, -1f, 1f),
			new Vector4(1f, -1f, -1f, 1f),
			new Vector4(-1f, -1f, -1f, 1f)
		};

		// Token: 0x04002619 RID: 9753
		private static readonly Vector4[] m_FarHomogenousCorners = new Vector4[]
		{
			new Vector4(-1f, 1f, 1f, 1f),
			new Vector4(1f, 1f, 1f, 1f),
			new Vector4(1f, -1f, 1f, 1f),
			new Vector4(-1f, -1f, 1f, 1f)
		};
	}
}
