#define PAINT_PATTERN 1
#define CUSTOM_PATTERN 1
#include "../../../Render/D3D11RenderDevice/Src/ShaderPS.hlsl"

cbuffer Constants: register(b1)
{
    float left, top, right, bottom;
    float width, height;
}

float slice(float p, float p0, float p1, float max, float scale)
{
    if (p <= p0)
    {
        return p * scale;
    }
    else if (p >= p1)
    {
        return 1.0 - (max - p) * scale;
    }
    else
    {
        float s = (p - p0) / (p1 - p0);
        return lerp(p0 * scale, 1.0 - (max - p1) * scale, s);
    }
}

half4 GetCustomPattern(in In i)
{
    float texWidth, texHeight;
    pattern.GetDimensions(texWidth, texHeight);

    float u = slice(i.uv0.x * width, left, width - right, width, 1.0 / texWidth);
    float v = slice(i.uv0.y * height, top, height - bottom, height, 1.0 / texHeight);

    return pattern.SampleLevel(patternSampler, float2(u, v), 0);
}
