#version 440

layout(location = 0) in vec2 qt_TexCoord0;
layout(location = 0) out vec4 fragColor;

layout(std140, binding = 0) uniform buf {
    mat4 qt_Matrix;
    float qt_Opacity;
    float time;
    float aberration; // 0.0 to 1.0, controlled by Audio Amplitude
};

float rand(vec2 co) {
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

void main() {
    // 1. Calculate Distortion Offset based on Amplitude (aberration)
    // We warp the UVs slightly away from center
    vec2 uv = qt_TexCoord0;
    vec2 dist = uv - 0.5;
    
    // 2. Chromatic Aberration
    // Red Channel shifts OUT
    // Blue Channel shifts IN
    float strength = aberration * 0.02; // Max shift 2% of texture size
    
    vec2 rUV = uv + (dist * strength);
    vec2 bUV = uv - (dist * strength);
    
    // Sample texture? We don't have a texture input (source is empty Item), we are generating visuals.
    // Wait, ShaderEffect usually works on sourceItem. 
    // Here we are generating NOISE on top of a gradient.
    // So we apply Aberration to the NOISE function? 
    // Or do we want to aberrate the pixels UNDERNEATH?
    // ShaderEffect with no source property renders purely procedural content.
    
    // Let's create layered procedural noise with channel offsets
    float nR = rand(rUV + vec2(time * 0.01, 0.0));
    float nG = rand(uv  + vec2(time * 0.01, 0.0)); // Green is anchor
    float nB = rand(bUV + vec2(time * 0.01, 0.0));
    
    // Also modulate alpha by aberration - higher volume = more intense grain?
    // Or maybe just pure glitch.
    
    vec4 grainColor = vec4(nR, nG, nB, 1.0);
    
    // Mix it with opacity
    fragColor = grainColor * qt_Opacity;
}
