// @name Golden Glow
// @group Bloom
// @param uBloomSize "Bloom Size" 3.0 1.0 10.0 0.5
// @param uBloomIntensity "Bloom Intensity" 0.6 0.0 2.0 0.05
// @param uGoldenBloomThreshold "Bloom Threshold" 0.3 0.0 1.0 0.05
// @group Color
// @param uGoldAmount "Gold Tint" 0.15 0.0 0.5 0.01
// @param uWarmth "Warmth" 0.85 0.5 1.0 0.01
// @param uGoldenChroma "RGB Split" 0.0 0.0 0.01 0.001
// @group Vignette
// @param uVignetteSize "Vignette Size" 0.8 0.3 1.5 0.05
// @param uVignetteFeather "Vignette Feather" 0.5 0.1 1.0 0.05
// @param uGoldenVignette "Vignette Strength" 0.5 0.0 1.5 0.05

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / uResolution.xy;
    vec4 col = texture(uTexture, uv);
    
    // Chromatic aberration
    if (uGoldenChroma > 0.0) {
        float aberr = uGoldenChroma * uIntensity;
        col.r = texture(uTexture, uv + vec2(aberr, 0.0)).r;
        col.b = texture(uTexture, uv - vec2(aberr, 0.0)).b;
    }
    
    // Sample surrounding pixels for bloom
    float bloomSize = uBloomSize / uResolution.x;
    vec4 bloom = vec4(0.0);
    float total = 0.0;
    
    for(float x = -2.0; x <= 2.0; x += 1.0) {
        for(float y = -2.0; y <= 2.0; y += 1.0) {
            float weight = 1.0 / (1.0 + length(vec2(x, y)));
            bloom += texture(uTexture, uv + vec2(x, y) * bloomSize) * weight;
            total += weight;
        }
    }
    bloom /= total;
    
    // Extract bright parts
    float brightness = dot(bloom.rgb, vec3(0.299, 0.587, 0.114));
    vec3 bloomCol = bloom.rgb * smoothstep(uGoldenBloomThreshold, 0.8, brightness);
    
    // Tint bloom golden
    vec3 goldTint = vec3(1.0, uWarmth, 0.5);
    bloomCol *= goldTint;
    
    // Add bloom to original
    col.rgb += bloomCol * uBloomIntensity * uIntensity;
    
    // Golden color grade
    col.rgb = mix(col.rgb, col.rgb * goldTint, uGoldAmount * uIntensity);
    
    // Warm vignette
    vec2 vigUV = uv - 0.5;
    float vig = 1.0 - dot(vigUV, vigUV) * uVignetteSize;
    vig = smoothstep(0.0, uVignetteFeather, vig);
    col.rgb *= mix(1.0, vig, uGoldenVignette * uIntensity);
    
    fragColor = col;
}
