#include "/lib/all_the_libs.glsl"
varying vec2 texcoord;
varying vec2 BloomTilePos;
#include "/global/post/bloom.glsl"

/* DRAWBUFFERS:0 */


void main() {
    vec4 Color = texture2D(colortex0, texcoord);
    
    #ifdef BLOOM
    // Base bloom
    vec3 FinalBloom = blur3x3(colortex1, BloomTilePos).rgb / 3.0;

    // --- Bright-pass mask from original scene ---
    float brightness = get_luminance(Color.rgb); // <-- use Color, not blurred bloom
    float mask = smoothstep(0.7, 1.0, brightness);

    // Lens blade streaks
    vec3 streaks = vec3(0.0);
    int N = 6;                // Number of blades
    float streakLength = 0.3; // Distance in screen-space
    float streakIntensity = 0.4;

    if(mask > 0.0) {
        for(int i = 0; i < N; i++) {
            float angle = float(i) * 3.14159 / float(N);
            vec2 dir = vec2(cos(angle), sin(angle));

            // sample along blade direction from current pixel
            for(float t = 0.0; t < streakLength; t += 0.01) {
                vec2 sampleUV = texcoord + dir * t;
                vec3 sample = texture2D(colortex1, sampleUV).rgb;

                float fade = (streakLength - t) / streakLength;
                streaks += sample * fade;
            }
        }
        streaks *= streakIntensity * brightness;
    }

    // Combine bloom + streaks
    float BloomFactor = get_luminance(FinalBloom) * BLOOM_CURVE + (1.0 - BLOOM_CURVE);
    BloomFactor += 0.2 * (rainStrength) * isOutdoorsSmooth;

    BloomFactor /= 1.667; // Reminder: remove this next major version

    Color.rgb = mix(Color.rgb+streaks, FinalBloom, BloomFactor * BLOOM_STRENGTH);
    #endif

    gl_FragData[0] = Color;
}
