using UnityEngine;

[ExecuteInEditMode]
public class Class3dPostImageEffect : MonoBehaviour
{
	public int BLOOM_DIVIDER = 4;

	public float BLOOM_WIDTHMOD = 0.5f;

	private const int PASS_FASTBLOOM_DOWNSAMPLE = 1;

	private const int PASS_FASTBLOOM_VERTICALBLUR = 2;

	private const int PASS_FASTBLOOM_HORIZONTALBLUR = 3;

	private const int PASS_POSTDIFFUSIONBLOOM_VERTICALGAUSS = 0;

	private const int PASS_POSTDIFFUSIONBLOOM_HORIZONGAUSS = 1;

	private const int PASS_POSTDIFFUSIONBLOOM_BLOOM = 2;

	private const int PASS_POSTDIFFUSIONBLOOM_OVERLAY1 = 3;

	private const int PASS_POSTDIFFUSIONBLOOM_OVERLAY2 = 5;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_APPLYBG = 0;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_APPLYBGDEBUG = 1;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_COC2ALPHA = 2;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_DOWNSAMPLE = 3;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_FOGBLOOM = 4;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_BLOOMCOLOR = 5;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_VERTICALGAUSS = 6;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_HORIZONGAUSS = 7;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_BLOOM = 8;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_COCBG_RICH = 9;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_COCBGFG = 10;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_OVERLAY1 = 11;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_OVERLAY1_INVERSE = 12;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_OVERLAY2 = 13;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_OVERLAY2_INVERSE = 14;

	private const int PASS_POSTDIFFUSIONDOFBLOOM_COCFG = 15;

	private const float DOF_HEIGHT_BASE_SIZE_HORIZONTAL = 0.0034722222f;

	private const float DOF_HEIGHT_BASE_SIZE_VERTICAL = 0.0010986328f;

	private const float DIVIDE_SCREEN = 1f;

	private const float DOFONEOVERBASESIZE = 0.001953125f;

	private const float DOFBASESIZE = 512f;

	private float _dofWidthOverHeight = 1.25f;

	private float _dofHeightBaseSize = 0.0024414062f;

	private int _rezworkWidth;

	private int _rezworkHeight;

	[SerializeField]
	public DofDiffusionBloomOverlayParam _param = new DofDiffusionBloomOverlayParam();

	[SerializeField]
	private Material _postDiffusionBloomMaterial;

	[SerializeField]
	private Material _postDiffusionDofBloomMaterial;

	[SerializeField]
	private Material _fastBloomMaterial;

	[Header("Antialiasing")]
	public bool EnableAntialiasing = true;

	public float edgeThresholdMin = 0.05f;

	public float edgeThreshold = 0.2f;

	public float edgeshilhouette = 4f;

	private Material antialiasingMaterial;

	public void Initialize()
	{
		_postDiffusionDofBloomMaterial = (_postDiffusionBloomMaterial = new Material(Shader.Find("Class3D/ImageEffects/Rich/PostDiffusionDofBloom_Rich")));
		_fastBloomMaterial = new Material(Shader.Find("Class3D/ImageEffects/FastBloom"));
		Shader shader = Shader.Find("Hidden/FXAA III (Console)");
		if (shader != null && shader.isSupported)
		{
			antialiasingMaterial = new Material(shader);
		}
		_param.TargetCamera = GetComponent<Camera>();
	}

	public void Destroy()
	{
		if (_fastBloomMaterial != null)
		{
			Object.Destroy(_fastBloomMaterial);
			_fastBloomMaterial = null;
		}
		if (_postDiffusionBloomMaterial != null)
		{
			Object.Destroy(_postDiffusionBloomMaterial);
			_postDiffusionBloomMaterial = null;
		}
		if (_postDiffusionDofBloomMaterial != null)
		{
			Object.Destroy(_postDiffusionDofBloomMaterial);
			_postDiffusionDofBloomMaterial = null;
		}
		if (antialiasingMaterial != null)
		{
			Object.Destroy(antialiasingMaterial);
			antialiasingMaterial = null;
		}
	}

	private static float GetLowResolutionDividerBasedOnQuality(float baseDivider)
	{
		return baseDivider * 0.5f;
	}

	private RenderTexture CreateBloomTexture(RenderTexture source, RenderTexture downSample)
	{
		RenderTexture renderTexture = null;
		int width = source.width / BLOOM_DIVIDER;
		int height = source.height / BLOOM_DIVIDER;
		if (_param.BloomBlurSize > 0f)
		{
			Vector4 zero = Vector4.zero;
			bool flag = false;
			RenderTexture renderTexture2 = downSample;
			if (renderTexture2 == null)
			{
				zero.z = 0f;
				zero.w = 1f;
				_fastBloomMaterial.SetVector("_Parameter", zero);
				renderTexture2 = RenderTexture.GetTemporary(width, height, 0);
				renderTexture2.filterMode = FilterMode.Bilinear;
				Graphics.Blit(source, renderTexture2, _fastBloomMaterial, 1);
				flag = true;
			}
			float num = 1f * (float)source.width / (1f * (float)source.height);
			float bloomBlurSize = _param.BloomBlurSize;
			float num2 = 0.001953125f;
			zero.x = bloomBlurSize / num * num2;
			zero.y = bloomBlurSize * num2;
			zero.z = _param.BloomThreshold;
			zero.w = _param.BloomIntensity;
			_fastBloomMaterial.SetVector("_Parameter", zero);
			RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0);
			temporary.filterMode = FilterMode.Bilinear;
			Graphics.Blit(renderTexture2, temporary, _fastBloomMaterial, 2);
			renderTexture = temporary;
			temporary = RenderTexture.GetTemporary(width, height, 0);
			temporary.filterMode = FilterMode.Bilinear;
			Graphics.Blit(renderTexture, temporary, _fastBloomMaterial, 3);
			renderTexture.DiscardContents();
			RenderTexture.ReleaseTemporary(renderTexture);
			if (flag)
			{
				renderTexture2.DiscardContents();
				RenderTexture.ReleaseTemporary(renderTexture2);
			}
			return temporary;
		}
		Vector4 zero2 = Vector4.zero;
		zero2.z = _param.BloomThreshold;
		zero2.w = _param.BloomIntensity;
		_fastBloomMaterial.SetVector("_Parameter", zero2);
		RenderTexture temporary2 = RenderTexture.GetTemporary(width, height, 0);
		temporary2.filterMode = FilterMode.Bilinear;
		Graphics.Blit(source, temporary2, _fastBloomMaterial, 1);
		return temporary2;
	}

	private RenderTexture OnRenderImageDiffusionDofBloom(RenderTexture source, RenderTexture destination)
	{
		PrepareDofParam(source, _postDiffusionDofBloomMaterial);
		int num = 2;
		RenderTexture temporary = RenderTexture.GetTemporary(source.width, source.height, 0);
		RenderTexture temporary2 = RenderTexture.GetTemporary(_rezworkWidth, _rezworkHeight, 0);
		RenderTexture temporary3 = RenderTexture.GetTemporary(_rezworkWidth, _rezworkHeight, 0);
		RenderTexture temporary4 = RenderTexture.GetTemporary(_rezworkWidth, _rezworkHeight, 0);
		num = 9;
		if (_param.DofQualityType == DepthBlurAndBloom.DofQuality.BackgroundAndForeground)
		{
			_postDiffusionDofBloomMaterial.SetFloat("_dofForegroundSize", _param.DofForegroundSize);
			num = 10;
		}
		Graphics.Blit(source, temporary, _postDiffusionDofBloomMaterial, num);
		Graphics.Blit(temporary, temporary4, _postDiffusionDofBloomMaterial, 3);
		BlurBlt(temporary4, temporary2, _param.DofMaxBlurSpread);
		DiffusionFilterProcess(temporary2, temporary3);
		_postDiffusionDofBloomMaterial.SetTexture("_TapLowBackground", temporary3);
		RenderTexture renderTexture = null;
		if (_param.IsEnableBloom)
		{
			renderTexture = CreateBloomTexture(temporary, temporary4);
			_postDiffusionDofBloomMaterial.SetTexture("_Bloom", renderTexture);
			_postDiffusionDofBloomMaterial.SetFloat("_BloomIsScreenBlend", (_param.BloomBlendMode == DofDiffusionBloomOverlayParam.BloomScreenBlendMode.Screen) ? 1f : 0f);
			_param.ScreenOverlay.PostFilmBlit(temporary, destination, _postDiffusionDofBloomMaterial, 8, 11, 13);
		}
		else
		{
			_postDiffusionDofBloomMaterial.SetFloat("_BloomIsScreenBlend", 0f);
			_param.ScreenOverlay.PostFilmBlit(temporary, destination, _postDiffusionDofBloomMaterial, -1, 11, 13);
		}
		if (renderTexture != null)
		{
			renderTexture.DiscardContents();
			RenderTexture.ReleaseTemporary(renderTexture);
			renderTexture = null;
		}
		if (temporary != source)
		{
			temporary.DiscardContents();
			RenderTexture.ReleaseTemporary(temporary);
			temporary = null;
		}
		if (temporary2 != null)
		{
			temporary2.DiscardContents();
			RenderTexture.ReleaseTemporary(temporary2);
			temporary2 = null;
		}
		if (temporary3 != null)
		{
			temporary3.DiscardContents();
			RenderTexture.ReleaseTemporary(temporary3);
			temporary3 = null;
		}
		if (temporary4 != null)
		{
			temporary4.DiscardContents();
			RenderTexture.ReleaseTemporary(temporary4);
			temporary4 = null;
		}
		return destination;
	}

	private void PrepareDofParam(RenderTexture source, Material mtrl)
	{
		source.filterMode = FilterMode.Bilinear;
		source.wrapMode = TextureWrapMode.Clamp;
		Camera targetCamera = _param.TargetCamera;
		float num = targetCamera.farClipPlane - targetCamera.nearClipPlane;
		float num2 = 0.1f;
		Vector4 zero = Vector4.zero;
		switch (_param.DofFocalType)
		{
		case DepthBlurAndBloom.DofFocalType.Transform:
			num2 = ((!(_param.DofFocalTransfrom != null)) ? FocalDistance01(_param.DofFocalPoint) : (targetCamera.WorldToViewportPoint(_param.DofFocalTransfrom.position).z / num));
			break;
		case DepthBlurAndBloom.DofFocalType.Position:
			num2 = targetCamera.WorldToViewportPoint(_param.DofFocalPosition).z / num;
			break;
		case DepthBlurAndBloom.DofFocalType.Point:
			num2 = FocalDistance01(_param.DofFocalPoint);
			break;
		}
		if (num2 < 0f)
		{
			num2 = 0f;
		}
		if (_param.DofSmoothness < 0.1f)
		{
			_param.DofSmoothness = 0.1f;
		}
		zero.x = 1f / (float)source.width;
		zero.y = 1f / (float)source.height;
		mtrl.SetVector("_InvRenderTargetSize", zero);
		float num3 = num2 * _param.DofSmoothness;
		float num4 = num3;
		_dofWidthOverHeight = (float)source.width / (float)source.height;
		_dofHeightBaseSize = ((source.width > source.height) ? 0.0034722222f : 0.0010986328f);
		float num5 = 1E-06f;
		zero.x = ((num3 < num5) ? 0f : (1f / num3));
		zero.y = ((num4 < num5) ? 0f : (1f / num4));
		zero.z = _param.DofFocalSize / num * 0.5f + num2;
		mtrl.SetVector("_CurveParams", zero);
		mtrl.SetFloat("_bloomDofWeight", _param.BloomDofWeight);
		float lowResolutionDividerBasedOnQuality = GetLowResolutionDividerBasedOnQuality(1f);
		_rezworkWidth = (int)((float)source.width * lowResolutionDividerBasedOnQuality);
		_rezworkHeight = (int)((float)source.height * lowResolutionDividerBasedOnQuality);
	}

	private float FocalDistance01(float worldDist)
	{
		return _param.TargetCamera.WorldToViewportPoint((worldDist - _param.TargetCamera.nearClipPlane) * _param.TargetCamera.transform.forward + _param.TargetCamera.transform.position).z / (_param.TargetCamera.farClipPlane - _param.TargetCamera.nearClipPlane);
	}

	private void DiffusionFilterProcess(RenderTexture source, RenderTexture destination)
	{
		float lowResolutionDividerBasedOnQuality = GetLowResolutionDividerBasedOnQuality(1f);
		RenderTexture temporary = RenderTexture.GetTemporary(_rezworkWidth, _rezworkHeight, 0);
		Vector4 zero = Vector4.zero;
		zero.x = _param.DiffusionBlurSize * lowResolutionDividerBasedOnQuality / (float)_rezworkWidth;
		zero.y = _param.DiffusionBlurSize * lowResolutionDividerBasedOnQuality / (float)_rezworkHeight;
		_postDiffusionDofBloomMaterial.SetVector("_PixelSize", zero);
		zero.x = _param.DiffusionBright;
		zero.y = _param.DiffusionSaturation;
		zero.z = _param.DiffusionContrast;
		zero.w = _param.DiffusionThreshold;
		_postDiffusionDofBloomMaterial.SetVector("_ColorParam", zero);
		_postDiffusionDofBloomMaterial.mainTexture = source;
		Graphics.Blit(null, temporary, _postDiffusionDofBloomMaterial, 6);
		_postDiffusionDofBloomMaterial.mainTexture = temporary;
		Graphics.Blit(null, destination, _postDiffusionDofBloomMaterial, 7);
		_postDiffusionDofBloomMaterial.mainTexture = null;
		if (temporary != null)
		{
			temporary.DiscardContents();
			RenderTexture.ReleaseTemporary(temporary);
			temporary = null;
		}
	}

	private void BlurBlt(RenderTexture from, RenderTexture to, float spread)
	{
	}
}
