/*
 * Custom shader for Minecraft clouds
 * Acts like "UnlitTwoTexture" from Source
 * Fades with fog
 */
Shader "Custom/Minecraft/Clouds"
{
    Properties
    {
        _MainTex("Main Texture", 2D) = "white" {}
        _MainTexScrollX("Main Texture Scroll X", Float) = 0.001
        _MainTexScrollY("Main Texture Scroll Y", Float) = 0.001

        _NoiseTex("Noise Texture", 2D) = "white" {}
        _NoiseTexScrollX("Noise Texture Scroll X", Float) = 0.001
        _NoiseTexScrollY("Noise Texture Scroll Y", Float) = 0.001

        _Color("Tint Color", Color) = (0.37, 0.37, 0.37, 1.0)
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Blend One One  // Additive blending
        ZWrite Off
        Cull Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv1 : TEXCOORD0;
                float2 uv2 : TEXCOORD1;
                float4 vertex : SV_POSITION;
                float3 viewPos : TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            half _MainTexScrollX;
            half _MainTexScrollY;

            sampler2D _NoiseTex;
            float4 _NoiseTex_ST;
            half _NoiseTexScrollX;
            half _NoiseTexScrollY;

            float4 _Color;

            // Fog globals
            uniform float unity_FogStart;
            uniform float unity_FogEnd;
            uniform float unity_FogDensity;

            v2f vert (appdata v)
            {
                v2f o;
                float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
                float4 viewPos = mul(UNITY_MATRIX_V, worldPos);
                o.viewPos = viewPos.xyz;
                o.vertex = UnityObjectToClipPos(v.vertex);
                
                // Textures scrolling
                o.uv1 = TRANSFORM_TEX(v.uv, _MainTex) + float2(_MainTexScrollX, _MainTexScrollY) * _Time.y;
                o.uv2 = TRANSFORM_TEX(v.uv, _NoiseTex) + float2(_NoiseTexScrollX, _NoiseTexScrollY) * _Time.y;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 tex1 = tex2D(_MainTex, i.uv1);
                fixed4 tex2 = tex2D(_NoiseTex, i.uv2);
                fixed4 col = tex1 * tex2 * _Color;

                // Calculate fog factor
                float depth = abs(i.viewPos.z);
                float fogFactor = 1.0;
                #if defined(FOG_LINEAR)
                    fogFactor = saturate((unity_FogEnd - depth) / (unity_FogEnd - unity_FogStart));
                #elif defined(FOG_EXP)
                    fogFactor = exp2(-unity_FogDensity * depth);
                #elif defined(FOG_EXP2)
                    fogFactor = exp2(-unity_FogDensity * unity_FogDensity * depth * depth);
                #endif

                // col.rgb = lerp(fixed4(0,0,0,0).rgb, col.rgb, fogFactor);
                
                col.rgb *= fogFactor; // Fade to black

                return col;
            }

            ENDCG
        }
    }

    FallBack "Particles/Standard Unlit"
}
