From 87b9f5eab87d6e8b92d95333139270ff3aaaca2c Mon Sep 17 00:00:00 2001 From: Simon Schneegans Date: Sun, 9 Feb 2025 20:26:41 +0100 Subject: [PATCH] :recycle: Make blur a common method --- resources/shaders/aura-glow.frag | 33 +------------------------------- resources/shaders/common.glsl | 29 ++++++++++++++++++++++++++++ resources/shaders/focus.frag | 33 +------------------------------- 3 files changed, 31 insertions(+), 64 deletions(-) diff --git a/resources/shaders/aura-glow.frag b/resources/shaders/aura-glow.frag index e8f01f3..2302592 100644 --- a/resources/shaders/aura-glow.frag +++ b/resources/shaders/aura-glow.frag @@ -56,23 +56,6 @@ uniform float uEdgeSize; // soft <--> hard uniform float uEdgeHardness; -// A simple blur function -vec4 blur(vec2 uv, float radius, float samples) { - vec4 color = vec4(0.0); - - const float tau = 6.28318530718; - const float directions = 15.0; - - for (float d = 0.0; d < tau; d += tau / directions) { - for (float s = 0.0; s < 1.0; s += 1.0 / samples) { - vec2 offset = vec2(cos(d), sin(d)) * radius * (1.0 - s) / uSize; - color += getInputColor(uv + offset); - } - } - - return color / samples / directions; -} - void main() { // This gradually dissolves from [1..0] from the outside to the center. We @@ -146,26 +129,12 @@ void main() { // calculate this before saturating it float b = (color.r + color.g + color.b) / 3.0; - oColor = blur(iTexCoord.st, b * uBlur * mask1, 7.0); + oColor = getBlurredInputColor(iTexCoord.st, b * uBlur * mask1, 7.0); } // apply masks and colors oColor.a *= mask0; - // i was doing this to try to adjust for light mode - // i don;t like the way these make the effect look - // ...maybe a toggle for it later - /* - float oColorPercent = (oColor.r + oColor.g + oColor.b)/3.0; - oColor.r = mix(oColor.r , 1.0 - oColor.r, mask1 * oColorPercent); - oColor.g = mix(oColor.g , 1.0 - oColor.g, mask1 * oColorPercent); - oColor.b = mix(oColor.b , 1.0 - oColor.b, mask1 * oColorPercent); - // --or - oColor.r = mix(oColor.r , 0.0, mask1 * oColorPercent); - oColor.g = mix(oColor.g , 0.0, mask1 * oColorPercent); - oColor.b = mix(oColor.b , 0.0, mask1 * oColorPercent); - */ - oColor += mask1 * vec4(color.rgb, 1.0); oColor.a *= oColorAlpha; diff --git a/resources/shaders/common.glsl b/resources/shaders/common.glsl index 8364d7b..8f112b4 100644 --- a/resources/shaders/common.glsl +++ b/resources/shaders/common.glsl @@ -153,6 +153,35 @@ void setOutputColor(vec4 outColor) { cogl_color_out = outColor; } #endif // ------------------------------------------------------------------------------- +// A simple blur function +vec4 getBlurredInputColor(vec2 uv, float radius, float samples) { + // Initialize the color accumulator to zero. + vec4 color = vec4(0.0); + + // Define a constant for 2 * PI (tau), which represents a full circle in radians. + const float tau = 6.28318530718; + + // Number of directions for sampling around the circle. + const float directions = 15.0; + + // Outer loop iterates over multiple directions evenly spaced around a circle. + for (float d = 0.0; d < tau; d += tau / directions) { + // Inner loop samples along each direction, with decreasing intensity. + for (float s = 0.0; s < 1.0; s += 1.0 / samples) { + // Calculate the offset for this sample based on direction, radius, and step. + // The (1.0 - s) term ensures more sampling occurs closer to the center. + vec2 offset = vec2(cos(d), sin(d)) * radius * (1.0 - s) / uSize; + + // Add the sampled color at the offset position to the accumulator. + color += getInputColor(uv + offset); + } + } + + // Normalize the accumulated color by dividing by the total number of samples + // and directions to ensure the result is averaged. + return color / samples / directions; +} + // ----------------------------------------------------------------- compositing operators // The Shell.GLSLEffect uses straight alpha blending. This helper method allows diff --git a/resources/shaders/focus.frag b/resources/shaders/focus.frag index 887987f..d8da2c4 100644 --- a/resources/shaders/focus.frag +++ b/resources/shaders/focus.frag @@ -30,37 +30,6 @@ // vec4 getInputColor(vec2 coords) // void setOutputColor(vec4 outColor) -// A simple blur function -vec4 blur(vec2 uv, float radius, float samples) { - // Initialize the color accumulator to zero. - vec4 color = vec4(0.0); - - // Define a constant for 2 * PI (tau), which represents a full circle in radians. - const float tau = 6.28318530718; - - // Number of directions for sampling around the circle. - const float directions = 15.0; - - // Outer loop iterates over multiple directions evenly spaced around a circle. - for (float d = 0.0; d < tau; d += tau / directions) { - // Inner loop samples along each direction, with decreasing intensity. - for (float s = 0.0; s < 1.0; s += 1.0 / samples) { - // Calculate the offset for this sample based on direction, radius, and step. - // The (1.0 - s) term ensures more sampling occurs closer to the center. - vec2 offset = vec2(cos(d), sin(d)) * radius * (1.0 - s) / uSize; - - // Add the sampled color at the offset position to the accumulator. - color += getInputColor(uv + offset); - } - } - - // Normalize the accumulated color by dividing by the total number of samples - // and directions to ensure the result is averaged. - return color / samples / directions; -} - - - // The width of the fading effect is loaded from the settings. uniform float uBlurAmount; uniform float uBlurQuality; @@ -83,7 +52,7 @@ void main() { // Apply the calculated blur effect to the texture at the current texture coordinates. // The blur function uses the blur amount and quality (uBlurQuality) for sampling. - vec4 texColor = blur(iTexCoord.st, blurAmount, uBlurQuality); + vec4 texColor = getBlurredInputColor(iTexCoord.st, blurAmount, uBlurQuality); // Calculate the alpha value for the transition using eased progress. // This determines how transparent the final color will appear.