// @name Simple Vignette
// @group Effect
// @param uVignetteAmount "Amount" 0.5 0.0 1.0 0.01
// @param uVignetteRadius "Radius" 0.6 0.0 1.0 0.01
// @param uVignetteSoftness "Softness" 0.4 0.0 1.0 0.01
// @group Color
// @param uVignetteColorR "Color Red" 0.0 0.0 1.0 0.01
// @param uVignetteColorG "Color Green" 0.0 0.0 1.0 0.01
// @param uVignetteColorB "Color Blue" 0.0 0.0 1.0 0.01

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / uResolution.xy;
    vec4 original = texture(uTexture, uv);

    vec2 center = vec2(0.5, 0.5);
    vec2 distVec = uv - center;
    
    // Correct for aspect ratio to make the vignette circular regardless of screen dimensions.
    // We scale the x-component of the distance vector based on aspect ratio.
    // If width > height, x-distances are effectively "compressed" making the circle round.
    distVec.x *= uResolution.x / uResolution.y;
    
    float dist = length(distVec);

    // Define the vignette color from parameters
    vec3 vignetteColor = vec3(uVignetteColorR, uVignetteColorG, uVignetteColorB);

    // Create the vignette mask using smoothstep.
    // smoothstep(edge0, edge1, x) returns 0 if x <= edge0, 1 if x >= edge1.
    // We want the mask to be 1 at the center (low dist) and 0 at the edges (high dist).
    // `uVignetteRadius` defines the inner edge where the vignette starts to fade out.
    // `uVignetteRadius + uVignetteSoftness` defines the outer edge where the vignette is fully applied.
    // By swapping edge0 and edge1, we get a mask that is 1 when dist <= uVignetteRadius
    // and 0 when dist >= uVignetteRadius + uVignetteSoftness.
    float vignetteMask = smoothstep(uVignetteRadius + uVignetteSoftness, uVignetteRadius, dist);

    // Apply the vignette effect: mix the original color with the vignette color based on the mask.
    // When vignetteMask is 1 (center), original.rgb is chosen.
    // When vignetteMask is 0 (edge), vignetteColor is chosen.
    vec3 vignettedColor = mix(vignetteColor, original.rgb, vignetteMask);

    // Blend the vignetted color with the original using uVignetteAmount and global uIntensity.
    // This allows the user to control the overall strength of the effect.
    // If uVignetteAmount * uIntensity is 0, the output is original.rgb.
    // If uVignetteAmount * uIntensity is 1, the output is vignettedColor.
    vec3 finalColor = mix(original.rgb, vignettedColor, uVignetteAmount * uIntensity);
    
    fragColor = vec4(finalColor, original.a);
}