#version 330 core
// Stage 1h plate-model Slide / Push: RGBA sampling, no YUV/orient/fit.
// Effect math (translation per side + Slide vs Push semantics)
// ported verbatim from transition_slide.fs.
//
// Alpha contract: assumes plates carry alpha=1 inside the clip's dst
// rect and alpha=0 outside (the standard ClipPlateRenderer output --
// drawYuvOverlay writes opaque pixels into the cleared plate). The
// final `mix(aRgba, bRgba, bValid)` therefore preserves the legacy
// "B overlays A as it slides in" semantic without darkening at the
// premul/non-premul seam.

out vec4 FragColor;
in  vec2 TexCoord;

uniform sampler2D u_planeA;
uniform sampler2D u_planeB;

uniform vec2  u_direction;
uniform float u_progress;
uniform int   u_pushMode;

float uvInRange(vec2 uv) {
    return step(0.0, uv.x) * step(uv.x, 1.0)
         * step(0.0, uv.y) * step(uv.y, 1.0);
}

vec4 samplePlate(sampler2D plate, vec2 uv) {
    if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
        return vec4(0.0);
    }
    return texture(plate, vec2(uv.x, 1.0 - uv.y));
}

void main() {
    vec2 uv = TexCoord;
    vec2 uvDir = vec2(u_direction.x, -u_direction.y);

    vec2 uvA = (u_pushMode != 0) ? (uv - u_progress * uvDir) : uv;
    vec2 uvB = uv + (1.0 - u_progress) * uvDir;

    float aValid = uvInRange(uvA);
    float bValid = uvInRange(uvB);

    vec4 aRgba = samplePlate(u_planeA, uvA);
    vec4 bRgba = samplePlate(u_planeB, uvB);

    vec4 rgba;
    if (u_pushMode != 0) {
        rgba = aValid * aRgba + (1.0 - aValid) * bValid * bRgba;
    } else {
        rgba = mix(aRgba, bRgba, bValid);
    }
    FragColor = rgba;
}
