#version 330 core

in vec2 vUV;

uniform sampler2D uTexA;
uniform sampler2D uTexB;
uniform float uTime;
uniform float uAlpha;
uniform float uPosition;
uniform float uAmplitude;
uniform float uSpeed;
uniform float uFeather;
uniform float uAngle;
uniform float uWaveFrequency;
uniform int uMode;
uniform int uInvert;

out vec4 FragColor;

const float PI = 3.14159265359;

float splitDistance(vec2 uv)
{
    float split = uPosition + uAmplitude * sin(uTime * uSpeed);

    if (uMode == 1)
    {
        return uv.y - split;
    }

    if (uMode == 2)
    {
        vec2 dir = vec2(cos(uAngle), sin(uAngle));
        float projected = dot(uv - vec2(0.5), dir) + 0.5;
        return projected - split;
    }

    if (uMode == 3)
    {
        float wave = sin((uv.y * uWaveFrequency + uTime * uSpeed) * PI * 2.0) * uAmplitude;
        return uv.x + wave - uPosition;
    }

    return uv.x - split;
}

void main()
{
    vec4 a = texture(uTexA, vUV);
    vec4 b = texture(uTexB, vUV);

    float feather = max(uFeather, 0.0001);
    float mixValue = smoothstep(-feather, feather, splitDistance(vUV));
    if (uInvert != 0)
    {
        mixValue = 1.0 - mixValue;
    }

    vec4 color = mix(a, b, mixValue);
    color.a *= uAlpha;
    FragColor = color;
}
