♻️ Make blur a common method

This commit is contained in:
Simon Schneegans
2025-02-09 20:26:41 +01:00
parent 96e79b3265
commit 87b9f5eab8
3 changed files with 31 additions and 64 deletions
+1 -32
View File
@@ -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;
+29
View File
@@ -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
+1 -32
View File
@@ -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.