Shader "Hidden/BPR/ApplyDepthCutoff"
{
	Properties
	{
		_DepthTex ("Depth Texture", 2D) = "white" {}

		_StartDist ("Depth Min", Float) = 0
		_EndDist ("Depth Max", Float) = 100

		//_ClearColor ("Clear Color", Color) = (0,0,0,0)
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		ZTest Off
		ZWrite On
		ColorMask 0

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			struct frag_out
			{
				//float4 color : COLOR;
				float depth : DEPTH;
			};

			sampler2D _DepthTex;
			float4 _DepthTex_ST;
			float _MaxDist;
			//float4 _ClearColor;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _DepthTex);
				return o;
			}
			inline float LinearEyeDepthToOutDepth(float z)
			{
				return (1 - _ZBufferParams.w * z) / (_ZBufferParams.z * z);
			}
			frag_out frag (v2f i)
			{
				frag_out o;
				//o.color = _ClearColor;

				fixed4 col = tex2D(_DepthTex, i.uv);
				float dist = col.r * _MaxDist;
				o.depth = LinearEyeDepthToOutDepth(dist);

				return o;
			}
			ENDCG
		}
	}
}
