You will make radial blur. Motion blur and gaussian blur will be covered in part 2.
Multiple pixels
Radial blur
We will multiply the textures coordinates by numbers less than 1 to make the textures slightly smaller. Then we'll average the values returned at those coordinates. Your code should look like this:
vec4 Color = (texture2D( gm_BaseTexture, v_vTexcoord )
+ texture2D( gm_BaseTexture, v_vTexcoord*0.99+0.5*0.01 )
+ texture2D( gm_BaseTexture, v_vTexcoord*0.98+0.5*0.02 )
+ texture2D( gm_BaseTexture, v_vTexcoord*0.97+0.5*0.03 )
+ texture2D( gm_BaseTexture, v_vTexcoord*0.96+0.5*0.04 )
+ texture2D( gm_BaseTexture, v_vTexcoord*0.95+0.5*0.05 )
+ texture2D( gm_BaseTexture, v_vTexcoord*0.94+0.5*0.06 )
+ texture2D( gm_BaseTexture, v_vTexcoord*0.93+0.5*0.07 )
+ texture2D( gm_BaseTexture, v_vTexcoord*0.92+0.5*0.08 )
+ texture2D( gm_BaseTexture, v_vTexcoord*0.91+0.5*0.09 )) * 0.1;
gl_FragColor = Color * v_vColour;
This code may look confusing, but what it does is rather simple. It gets the average of the different coordinates and centers them with the "+0.5" part. The shader looks like this:
integers
So if we set a float to 0 it would cause an error, but if we set and integer to 0, it would work as long as you don't mix it with floats. The only way to mix the different variable types is to use their functions (int() converts floats to ints and float() does the opposite). Here is an example:
int i = 4;
float j = 0.5+float(i);
That code would set i to 4 and j to 4.5. These are used in "for statements" which are particularly useful in blur shaders.
Constants
const int Number = 48;
That sets the integer "Number" to 48 as an unchanging constant.
For statement in shaders
So platforms do not run loops well, so keep that in mind when making games. When you use them, you must start with defining a variable (most often an integer), then you check if the variable is less then the number of times we want it to repeat (for our example) , and lastly we add to our variable. Rather than using commas to separate the parts, we use semi-colons.
An example would look like this:
for(int i = 0;i < 16;i++)
{
//code that you want to repeat
}
Now what that does is it sets variable i to 0 (an integer), then checks if i is less than 16. If it is, it will add one to i (i++) and will repeat until i is equal to or greater than 16.
Finishing the radial blur
vec4 Color;
float v;
for( float i=0.0;i<1.0;i+=1.0/float(Quality) )
{
v = 0.9+i*0.1;//convert "i" to the 0.9 to 1 range
Color += texture2D( gm_BaseTexture, v_vTexcoord*v+0.5-0.5*v);
}
Color /= float(Quality);
gl_FragColor = Color * v_vColour;
This code defines the vector "Color" and the float "v" (they are set to 0). Then the for statement repeats the code the number of times defined by "Quality". The final result of color is averaged by dividing the total by "Quality". Your full code should look something like this:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
const int Quality = 16;
void main()
{
vec4 Color;
float v;
for( float i=0.0;i<1.0;i+=1.0/float(Quality) )
{
v = 0.9+i*0.1;//convert "i" to the 0.9 to 1 range
Color += texture2D( gm_BaseTexture, v_vTexcoord*v+0.5-0.5*v);
}
Color /= float(Quality);
gl_FragColor = Color * v_vColour;
}
Quality can be changed to any value that suits your purpose. For smaller textures, use smaller numbers and for larger textures, use larger numbers.
DOWNLOAD EXAMPLE
Please share if these help you out!