0
\$\begingroup\$

I am trying to use postprocessing to create a gaussian blur, however, no blur is to be seen, what am I doing wrong?

I have included the full code at the bottom of this post in a google docs link, but here are the most important bits and pieces.

Part of the xna draw method:

RenderTarget2D renderTarget;
renderTarget = new RenderTarget2D(this.device, screenWidth, screenHeight, true, pp.BackBufferFormat, pp.DepthStencilFormat);
this.device.SetRenderTarget(renderTarget);
this.device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DeepSkyBlue, 1.0f, 0);
Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);

// Set the effect parameters (the 'simple' technique creates a red lambertian shaded model, like is shown on the image below)
effect.CurrentTechnique = effect.Techniques["Simple"];
effect.Parameters["TextureWidth"].SetValue(screenWidth);
device.DepthStencilState = DepthStencilState.Default;
device.BlendState = BlendState.Opaque;

// Draw the model
mesh.Draw();

// Render our scene
this.device.SetRenderTarget(null);

effect.CurrentTechnique = effect.Techniques["GaussianBlur"];

spriteBatch.Begin(0, BlendState.Opaque, null, null, null, effect);
spriteBatch.Draw((Texture2D)renderTarget, screenRectangle, Color.White);
spriteBatch.End();

The pixelshader:

float Pixels[13] =
{
   -6,
   -5,
   -4,
   -3,
   -2,
   -1,
    0,
    1,
    2,
    3,
    4,
    5,
    6,
};

float BlurWeights[13] =
{
   0.002216,
   0.008764,
   0.026995,
   0.064759,
   0.120985,
   0.176033,
   0.199471,
   0.176033,
   0.120985,
   0.064759,
   0.026995,
   0.008764,
   0.002216,
};


float4 GaussianPixelShader(float2 TextureCoordinate : TEXCOORD0) : COLOR
{
    // Pixel width
    float pixelWidth = 1/TextureWidth;

    float4 color = {0, 0, 0, 1};

    float2 blur;
    blur.y = TextureCoordinate.y;

    for (int i = 0; i < 13; i++) 
    {
        blur.x = TextureCoordinate.x + Pixels[i] * pixelWidth;
        color += tex2D(TextureSampler, blur.xy) * BlurWeights[i];
    }  

    return color;
}

What I have further found out by testing

The code in the GaussianPixelShader does get executed (if I set it to just return a black color it gives an entire solid black screen) but just doesn't seem to work/do the blurring.

Images

This is how currently my model looks (I cant see any blurring but maybe the blurring is just too light): image.

Full Code

Link to the shader code and the link to the xna/c# code.

\$\endgroup\$
3
\$\begingroup\$

It looks like TextureWidth is an integer. The line:

    float pixelWidth = 1/TextureWidth;

will be calculated using integer arithmetic, and so long as TextureWidth > 1, the result will be zero. This means the line:

    blur.x = TextureCoordinate.x + Pixels[i] * pixelWidth;

will be equivalent to:

    blur.x = TextureCoordinate.x;

and you will end up with a weighted sum of the same pixel.

You can fix this by making sure that pixelWidth is calculated using floating point arithmetic:

    float pixelWidth = 1.0/(float)TextureWidth;
\$\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.