Shader "PleasureArcade/ProcessorShader"
{
    Properties
    {
		_Threshold ("Threshold", Range(0, 1)) = 0.5
		_Range ("Range", Range(0, 1)) = 0.1
		_Differential ("Differential", Float) = 0.0005
		_FlattenNormals ("Flatten Normals", Float) = .03

		_Color ("Color", Color) = (1,1,1,1)
		_ReflectionLod ("Reflection LOD", Float) = 0
		_ReflectionStrength ("Reflection Strength", Range(0,1)) = 0.5

		// Opaque (paint-like, alpha blend) vs. Transparent (ink-like, multiply blend)
		_Opacity ("Opacity", Range(0,1)) = 1
		_LightDesaturation ("Light Desaturation", Range(0,1)) = 0
		_Alpha ("Alpha", Range(0,1)) = 1
		_CenterAlpha ("Center Alpha", Range(0,1)) = 0
		_CenterAlphaSpread ("Center Alpha Spread", Range(0,1)) = 0

		_StencilLayer ("Stencil Layer", Int) = 69
    }
    SubShader
    {
		GrabPass { "_SplortGrab" }

        Tags { "RenderType"="Transparent" "Queue"="Transparent+12" "LightMode"="ForwardBase" "VRCFallback"="Hidden"}

		Cull Off
		ZTest Always
        LOD 200

        Pass
        {
			Stencil
            {
                Ref [_StencilLayer]
                Comp Equal
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
			#include "UnityLightingCommon.cginc"

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

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

			sampler2D _SplortGrab;
			half4 _SplortGrab_ST;	// for stereoscopic adjustments
			fixed4 _Color;
			half _ReflectionLod;
			fixed _ReflectionStrength;

			fixed _Threshold;
			fixed _Range;
			float _Differential;
			fixed _FlattenNormals;
			fixed _Opacity;
			fixed _Alpha;
			fixed _CenterAlpha;
			fixed _CenterAlphaSpread;
			fixed _LightDesaturation;

			// Move quad verts into screen-space
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = float4(v.uv.x * 2 - 1, 1 - v.uv.y * 2, 0, 1);
				o.uv = ComputeGrabScreenPos(o.vertex);
                return o;
            }

			bool IsMirrorClone()
			{
				return unity_CameraProjection[2][0] != 0.f || unity_CameraProjection[2][1] != 0.f;
			}

            fixed4 frag (v2f i) : SV_Target
            {
				clip(IsMirrorClone() ? -1 : 1);

				// 3 tap normals
				float n = tex2D(_SplortGrab, UnityStereoScreenSpaceUVAdjust(i.uv + float2(0, _Differential), _SplortGrab_ST)).a;
				float e = tex2D(_SplortGrab, UnityStereoScreenSpaceUVAdjust(i.uv + float2(_Differential, 0), _SplortGrab_ST)).a;
				float h = tex2D(_SplortGrab, UnityStereoScreenSpaceUVAdjust(i.uv, _SplortGrab_ST)).a;

				clip(h - (_Threshold - _Range));

				// h = tex2D(_SplortGrab, UnityStereoScreenSpaceUVAdjust(i.uv, _SplortGrab_ST)).a;
				// return saturate(fixed4(h * .1, h * .01, h * 0.001, 1));

				// Normal
				float3 dx = float3(_Differential, 0, (h - e) * _FlattenNormals);
				float3 dy = float3(0, _Differential, (h - n) * _FlattenNormals);
				float3 normal = normalize(mul(unity_CameraToWorld, cross(dy, dx)));

                //return fixed4(frac(normal.xyz), 1);

				// Smooth-clipped alpha
				fixed a = smoothstep(_Threshold - _Range * 0.5, _Threshold + _Range * 0.5, h);

				// Center alpha
				fixed centerAlpha = lerp(1, smoothstep(1 - (_CenterAlphaSpread - _Threshold + _Range), _Threshold - _Range, h), _CenterAlpha);
				a *= centerAlpha * centerAlpha;

				// Overall alpha
				a *= _Alpha;

				// Grab background so we can do proper alpha + additive blending
				fixed3 bg = tex2D(_SplortGrab, UnityStereoScreenSpaceUVAdjust(i.uv, _SplortGrab_ST)).rgb;

				// Base liquid coloration
				fixed3 liquidColor = lerp(_Color.rgb * bg, _Color.rgb, _Opacity);

				// Reflections
				half4 skyData = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, normal, _ReflectionLod);
                half3 skyColor = DecodeHDR (skyData, unity_SpecCube0_HDR);

				// Direct (N.L) & GI lighting
				fixed lightIntensity = (_LightColor0.r + _LightColor0.g + _LightColor0.b) * 0.3333;
				fixed4 light = lerp(_LightColor0, fixed4(lightIntensity, lightIntensity, lightIntensity, 1), _LightDesaturation);
				light += fixed4(ShadeSH9(half4(normal, 1)), 0);

				fixed3 col = lerp(bg, liquidColor * light, a) + skyColor * _ReflectionStrength;

				return fixed4(col, 1);
            }
            ENDCG
        }
    }
}
