//
// GPS Squash - Masked Section Shader
// Amostra cor de uma seção da textura de blur (coordenadas de tela)
// e aplica a mascara alfa de outra textura (ex: keycap PNG)
//

texture gBlurTexture;
texture gMaskTexture;

// Retângulo da seção no espaço da tela normalizado (0-1)
// x, y = posição superior-esquerda, z, w = largura, altura
float4 gSectionRect = float4(0, 0, 1, 1);

sampler BlurSampler = sampler_state
{
    Texture = (gBlurTexture);
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

sampler MaskSampler = sampler_state
{
    Texture = (gMaskTexture);
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

float4 MaskedSectionPS(float2 texCoord : TEXCOORD0, float4 diffuse : COLOR0) : COLOR0
{
    // Mapear UV local (0-1) para coordenada de tela na textura de blur
    float2 blurUV = float2(
        gSectionRect.x + texCoord.x * gSectionRect.z,
        gSectionRect.y + texCoord.y * gSectionRect.w
    );

    float4 blurColor = tex2D(BlurSampler, blurUV);
    float maskAlpha = tex2D(MaskSampler, texCoord).a;

    // Cor do blur, alfa da mascara * diffuse
    return float4(blurColor.rgb * diffuse.rgb, maskAlpha * diffuse.a);
}

technique masked_section
{
    pass P0
    {
        PixelShader = compile ps_2_0 MaskedSectionPS();
    }
}
