#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;
};

// Smooth noise function
float noise(vec2 p) {
    return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}

// Smooth interpolation
float smoothNoise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    f = f * f * (3.0 - 2.0 * f); // Smoothstep
    
    float a = noise(i);
    float b = noise(i + vec2(1.0, 0.0));
    float c = noise(i + vec2(0.0, 1.0));
    float d = noise(i + vec2(1.0, 1.0));
    
    return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}

// Fractal Brownian Motion
float fbm(vec2 p) {
    float value = 0.0;
    float amplitude = 0.5;
    for (int i = 0; i < 4; i++) {
        value += amplitude * smoothNoise(p);
        p *= 2.0;
        amplitude *= 0.5;
    }
    return value;
}

// HSV to RGB conversion
vec3 hsv2rgb(vec3 c) {
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

void main() {
    vec2 uv = qt_TexCoord0;
    float t = time * 0.05;
    
    // Multiple moving blobs
    vec2 p1 = uv * 3.0 + vec2(t * 0.3, t * 0.2);
    vec2 p2 = uv * 2.5 + vec2(-t * 0.4, t * 0.3);
    vec2 p3 = uv * 4.0 + vec2(t * 0.2, -t * 0.25);
    
    float blob1 = fbm(p1);
    float blob2 = fbm(p2);
    float blob3 = fbm(p3);
    
    // Combine blobs
    float combined = (blob1 + blob2 + blob3) / 3.0;
    
    // Live hue shifting - slowly cycle through hues over time
    float hueShift = time * 0.02; // Slow hue rotation
    
    // Create colors using HSV with shifting hue
    vec3 color1 = hsv2rgb(vec3(fract(0.75 + hueShift), 0.6, 0.35)); // Purple range
    vec3 color2 = hsv2rgb(vec3(fract(0.85 + hueShift), 0.7, 0.35)); // Magenta range
    vec3 color3 = hsv2rgb(vec3(fract(0.55 + hueShift), 0.7, 0.35)); // Blue range
    vec3 color4 = hsv2rgb(vec3(fract(0.08 + hueShift), 0.7, 0.35)); // Orange range
    vec3 color5 = hsv2rgb(vec3(fract(0.50 + hueShift), 0.6, 0.30)); // Teal range
    
    // Mix colors based on blob values for visible multi-color effect
    vec3 color = mix(color1, color2, blob1);
    color = mix(color, color3, blob2);
    color = mix(color, color4, blob3);
    color = mix(color, color5, combined);
    
    // Moderate brightness for visible but dark background accent
    float brightness = 0.25 + combined * 0.25;
    color *= brightness;
    
    fragColor = vec4(color, qt_Opacity);
}
