HLSL Effects: Blur

Another very simple effect is blurring the image. To do this, you grab the color of each pixel in the texture and add to it the colors from the pixels adjacent to the target pixel. To try this, replace the pixel shader in your game with the following code:

float4 PixelShader(VertexOut input) : COLOR0
{
    float4 Color;
    Color =  tex2D(ColoredTextureSampler, input.textureCoordinates.xy);
    Color += tex2D(ColoredTextureSampler, input.textureCoordinates.xy + (0.01));
    Color += tex2D(ColoredTextureSampler, input.textureCoordinates.xy − (0.01));
    Color = Color / 3;

    return Color;
}

This effect will require pixel shader 1.4 or higher, so change the line in your technique that defines the pixel shader to:

PixelShader = compile ps_1_4 PixelShader(  );

Tip

Wouldn't you always want to use the latest and greatest pixel shader? Why even bother with pixel shader 1.1?

In fact, you should always use the lowest-version pixel shader that you can, depending on the functions that you are going to implement. The reason? Graphics cards only support certain pixel shader and vertex shader versions. If you don't need to use version 1.4, but you tell HLSL to use that version anyway, some users might not be able to run your game because their graphics cards might only support pixel shader 1.1 (even though they ought to be able to run your game because you're not actually using any of the functions in pixel shader 1.4).

The result will be a blurred image, as shown in Figure 13-8.

Figure 13-8. Blurred ...

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.