// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

Shader "Hidden/TerrainEngine/Splatmap/WCDiffuse-BaseGen"
{
    Properties
    {
        [HideInInspector] _DstBlend("DstBlend", Float) = 0.0        
        _ColorMap("ColorMap (RGB)", 2D) = "white"
        _OffsetSize("Offset / Size", Vector) = (0,0,0,0)
    }    
    
    SubShader
    {
        Tags
        {
            "Name" = "_MainTex"
            "Format" = "RGBA32"
            "Size" = "1"
        }
        Pass
        {
            ZTest Always Cull Off ZWrite Off
            Blend One [_DstBlend]
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0

            #include "UnityCG.cginc"

            sampler2D _Control;
            sampler2D _Splat0;
            sampler2D _Splat1;
            sampler2D _Splat2;
            sampler2D _Splat3;
            sampler2D _ColorMap;

            float4 _Control_TexelSize;
            float4 _Splat0_ST;
            float4 _Splat1_ST;
            float4 _Splat2_ST;
            float4 _Splat3_ST;
            float4 _OffsetSize;

            struct appdata_t {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f {
                float4 vertex : SV_POSITION;
                float2 texcoord0 : TEXCOORD0;
                float2 texcoord1 : TEXCOORD1;
                float2 texcoord2 : TEXCOORD2;
                float2 texcoord3 : TEXCOORD3;
                float2 texcoord4 : TEXCOORD4;
            };

            v2f vert(appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                // adjust splatUVs so the edges of the terrain tile lie on pixel centers
                float2 controlUV = (v.texcoord * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
                o.texcoord0 = controlUV;
                o.texcoord1 = TRANSFORM_TEX(v.texcoord, _Splat0);
                o.texcoord2 = TRANSFORM_TEX(v.texcoord, _Splat1);
                o.texcoord3 = TRANSFORM_TEX(v.texcoord, _Splat2);
                o.texcoord4 = TRANSFORM_TEX(v.texcoord, _Splat3);
                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                i.texcoord0 = i.texcoord0;

                float4 alpha = tex2D(_Control, (i.texcoord0 - _OffsetSize.xy) / _OffsetSize.zw);
                float4 splat0 = tex2D(_Splat0, i.texcoord1);
                float4 splat1 = tex2D(_Splat1, i.texcoord2);
                float4 splat2 = tex2D(_Splat2, i.texcoord3);
                float4 splat3 = tex2D(_Splat3, i.texcoord4);
                return (alpha.x * splat0 + alpha.y * splat1 + alpha.z * splat2 + alpha.w * splat3) * float4(tex2D(_ColorMap, i.texcoord0.xy).rgb,1);
            }
            ENDCG
        }
    }
    Fallback Off
}
