Code Snippet: Radial Blur shader

This short code snipped will show how to make a simple radial blur as a post effect pass. The effect is cool and the code is preeeetty simple.

If you are in XNA, you can use it in spritebatch (make a shader with a pixel shader only, set it and call the classic spritebatch to draw the image) or you can use in DirectX, Opengl (minor changes)

sampler TextureSampler : register(s0);

float2 Center = { 0.5, 0.5 }; ///center of the screen (could be any place)
float BlurStart = 1.0f; /// blur offset
float BlurWidth = -0.1; ///how big it should be
int nsamples = 10;

float4 PS_RadialBlur(float2 UV	: TEXCOORD0 ) : COLOR
    UV -= Center;
    float4 c = 0;
    for(int i=0; i <nsamples; i++) {
    	float scale = BlurStart + BlurWidth*(i/(float) (nsamples-1));
    	c += tex2D(TextureSampler, UV * scale + Center );
   	c /= nsamples;
    return c;

Experiment changing the nsamples parameter. You can also put it as a uniform variable and force the compiler to unroll the for loop =P

Enjoy !


  1. #1 by on 28 de maio de 2017 - 10:34 am

    Post writing is also a fun, if you be acquainted with afterward you can write if not it is complicated to write.|

1 301 302 303
(não será publicado)