♻️ 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
+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