texture gTexture < string textureState = "0,Texture"; >;
float wetness = 0.0;
float rainTime = 0.0;

sampler TextureSampler = sampler_state
{
    Texture = <gTexture>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    AddressU = Wrap;
    AddressV = Wrap;
};

float4 WetPixel(float2 uv : TEXCOORD0, float4 diffuse : COLOR0) : COLOR0
{
    float4 base = tex2D(TextureSampler, uv) * diffuse;
    float luminance = dot(base.rgb, float3(0.299, 0.587, 0.114));

    // Wet asphalt gets darker, slightly desaturated and gains moving micro highlights.
    float3 darkWet = lerp(base.rgb, luminance.xxx, 0.13) * 0.66;
    float grain = sin((uv.x * 211.0 + uv.y * 137.0) + rainTime * 1.7);
    grain *= sin((uv.x * 83.0 - uv.y * 193.0) - rainTime * 1.15);
    float highlight = pow(saturate(luminance * 1.18), 7.0) * 0.14;
    highlight += saturate(grain - 0.82) * 0.035;
    float3 result = lerp(base.rgb, darkWet + highlight.xxx, saturate(wetness));

    return float4(result, base.a);
}

technique RealisticWetRoad
{
    pass P0
    {
        PixelShader = compile ps_2_0 WetPixel();
    }
}

technique fallback
{
    pass P0 { }
}
