0
$\begingroup$

This is dont-know-where-I-downloaded blend file with a Box Blur Filter 2D. It is maybe the only blur filter I found on Internet (there's another but too much simpler). It makes a very good blur effect, with two values: amount and multiplier. The more amount, the more quality of the blur (and more CPU consumption). Multiplier doesn't use more CPU, it only make the blur bigger. It's fantastic... but it has a bug.

When amount is 0, it makes no blur but the image is darken than original. With 8, the brightness appears to be similar... but it gets brighter when the value is higher. This is the code:

/* Simple Box Blur
 * Higher amount = slower
 * multipler property does not affect performance
 */

uniform sampler2D bgl_RenderedTexture;
uniform float bgl_RenderedTextureWidth;
uniform float bgl_RenderedTextureHeight;
uniform int amount;
uniform float multiplier;

vec2 pix = vec2(1.0 / bgl_RenderedTextureWidth, 1.0 / bgl_RenderedTextureHeight) * multiplier;

void main() {
    vec4 sum = vec4(0);

    int i, j;

    for(i = -amount; i <= amount; i++){
        for(j = -amount; j <= amount; j++){
            sum += texture2D(bgl_RenderedTexture, gl_TexCoord[0].st + vec2(float(i), float(j)) * pix);
    }}
    gl_FragColor = sum / pow((amount + 1) * 2, 2);
}

This is the file (also, I would like to know how write this code to thank him/her).

Note: this file works in Blender 2.76 and up.

$\endgroup$
1
$\begingroup$
gl_FragColor = sum / pow((amount + 1) * 2, 2);

This scaling factor isn't correct. Each loop executes amount * 2 + 1 times, not (amount + 1) * 2 times. So it should be:

gl_FragColor = sum / pow(amount * 2 + 1, 2);

Which for amount set to 0 gives a scaling factor of 1.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.