// @name Sine Wave Scanlines
// @group Primary Wave
// @param uWaveAmplitude "Wave Amplitude" 0.02 0.0 0.1 0.002
// @param uWaveFrequency "Wave Frequency" 20.0 5.0 100.0 1.0
// @param uWaveSpeed "Wave Speed" 2.0 0.0 10.0 0.25
// @param uWaveDirection "Wave Direction" 0.0 0.0 1.0 1.0
// @group Secondary Wave
// @param uWave2Amplitude "Second Amplitude" 0.0 0.0 0.05 0.002
// @param uWave2Frequency "Second Frequency" 50.0 10.0 150.0 5.0
// @param uWave2Speed "Second Speed" 5.0 0.0 15.0 0.5
// @group Modulation
// @param uModFrequency "Mod Frequency" 3.0 0.5 10.0 0.5
// @param uModDepth "Mod Depth" 0.0 0.0 1.0 0.05
// @param uModSpeed "Mod Speed" 0.5 0.0 3.0 0.1
// @group Scanlines
// @param uScanlineCount "Scanline Count" 200.0 50.0 500.0 10.0
// @param uScanlineDarkness "Scanline Darkness" 0.2 0.0 1.0 0.05
// @param uScanlineSharpness "Scanline Sharpness" 0.5 0.0 1.0 0.05
// @group Distortion
// @param uBarrelAmount "Barrel Distort" 0.0 -0.5 0.5 0.02
// @param uTwistAmount "Twist" 0.0 0.0 1.0 0.05
// @param uTwistSpeed "Twist Speed" 1.0 0.0 5.0 0.25
// @group Color
// @param uRGBPhase "RGB Phase Offset" 0.0 0.0 1.0 0.05
// @param uColorBleed "Color Bleed" 0.0 0.0 0.02 0.001
// @param uSaturationWave "Saturation Wave" 0.0 0.0 0.5 0.02
// @group Glow
// @param uGlowAmount "Glow Amount" 0.0 0.0 1.0 0.05
// @param uGlowSpread "Glow Spread" 3.0 1.0 10.0 0.5

vec3 rgb2hsv(vec3 c) {
    vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

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 mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / uResolution.xy;
    
    // === BARREL DISTORTION ===
    if (abs(uBarrelAmount) > 0.01) {
        vec2 center = uv - 0.5;
        float dist = dot(center, center);
        uv = uv + center * dist * uBarrelAmount * uIntensity;
    }
    
    // === WAVE DISPLACEMENT ===
    float t = uTime * uIntensity;
    
    // Modulation envelope
    float modEnvelope = 1.0;
    if (uModDepth > 0.0) {
        float modWave = sin(uv.y * uModFrequency * 6.28 + t * uModSpeed);
        modEnvelope = 1.0 - uModDepth * (0.5 + 0.5 * modWave);
    }
    
    // Primary wave
    float wave1;
    if (uWaveDirection < 0.5) {
        // Horizontal displacement based on Y
        wave1 = sin(uv.y * uWaveFrequency + t * uWaveSpeed) * uWaveAmplitude * modEnvelope;
    } else {
        // Vertical displacement based on X
        wave1 = sin(uv.x * uWaveFrequency + t * uWaveSpeed) * uWaveAmplitude * modEnvelope;
    }
    
    // Secondary wave (perpendicular)
    float wave2 = 0.0;
    if (uWave2Amplitude > 0.0) {
        if (uWaveDirection < 0.5) {
            wave2 = sin(uv.x * uWave2Frequency + t * uWave2Speed) * uWave2Amplitude;
        } else {
            wave2 = sin(uv.y * uWave2Frequency + t * uWave2Speed) * uWave2Amplitude;
        }
    }
    
    // Twist
    vec2 displacement;
    if (uTwistAmount > 0.0) {
        vec2 center = uv - 0.5;
        float angle = atan(center.y, center.x);
        float dist = length(center);
        float twist = sin(dist * 20.0 - t * uTwistSpeed) * uTwistAmount * 0.1;
        angle += twist * uIntensity;
        center = vec2(cos(angle), sin(angle)) * dist;
        uv = center + 0.5;
    }
    
    if (uWaveDirection < 0.5) {
        displacement = vec2(wave1, wave2) * uIntensity;
    } else {
        displacement = vec2(wave2, wave1) * uIntensity;
    }
    
    vec2 distortedUV = uv + displacement;
    
    // === COLOR SAMPLING ===
    vec3 col;
    
    // RGB phase offset and color bleed
    if (uRGBPhase > 0.0 || uColorBleed > 0.0) {
        float phaseOffset = uRGBPhase * 0.02 * uIntensity;
        float bleed = uColorBleed * uIntensity;
        
        vec2 rOffset = vec2(phaseOffset + bleed, 0.0);
        vec2 bOffset = vec2(-phaseOffset - bleed, 0.0);
        
        col.r = texture(uTexture, clamp(distortedUV + rOffset, 0.0, 1.0)).r;
        col.g = texture(uTexture, clamp(distortedUV, 0.0, 1.0)).g;
        col.b = texture(uTexture, clamp(distortedUV + bOffset, 0.0, 1.0)).b;
    } else {
        col = texture(uTexture, clamp(distortedUV, 0.0, 1.0)).rgb;
    }
    
    // === SCANLINES ===
    float scanY = fragCoord.y;
    float scanline = sin(scanY * 3.14159 * uScanlineCount / uResolution.y) * 0.5 + 0.5;
    scanline = pow(scanline, mix(0.5, 4.0, uScanlineSharpness));
    scanline = mix(1.0, scanline, uScanlineDarkness * uIntensity);
    
    col *= scanline;
    
    // === SATURATION WAVE ===
    if (uSaturationWave > 0.0) {
        float satWave = sin(distortedUV.y * 30.0 + t * 3.0) * uSaturationWave;
        vec3 hsv = rgb2hsv(col);
        hsv.y = clamp(hsv.y + satWave * uIntensity, 0.0, 1.0);
        col = hsv2rgb(hsv);
    }
    
    // === GLOW ===
    if (uGlowAmount > 0.0) {
        vec3 glow = vec3(0.0);
        float totalWeight = 0.0;
        
        for (float i = 1.0; i <= 10.0; i++) {
            if (i > uGlowSpread) break;
            
            float weight = 1.0 / i;
            vec2 offset = vec2(0.0, i / uResolution.y);
            
            glow += texture(uTexture, clamp(distortedUV + offset, 0.0, 1.0)).rgb * weight;
            glow += texture(uTexture, clamp(distortedUV - offset, 0.0, 1.0)).rgb * weight;
            totalWeight += weight * 2.0;
        }
        
        glow /= totalWeight;
        
        // Add glow where scanlines create dark areas
        float glowMask = 1.0 - scanline;
        col += glow * glowMask * uGlowAmount * uIntensity;
    }
    
    fragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
}

