• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Shader pass in variables in for loops

T

TaylorBramble

Guest
Hi all,
I'm working on shaders, specifically for a simple blur effect.
I am passing in a variable u_Blur to the shader, so that if I get hit or something, the screen blurs for a bit and gradually goes back down.
here's the code in my fragment:

varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float u_Blur;

void main()
{
vec4 Color;
float v;
for( float i=0.0;i<1.0;i+=1.0/ u_Blur )
{
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(u_Blur);
gl_FragColor = Color * v_vColour;
}

based off that guy Xor's tutorials.

Problem is, if I pass in the u_Blur value from my shader controller object, it says the for loop never finishes. To me, that doesn't make sense. I've got the blur_amount variable set to 16, so it should just do 16 iterations, I would think.
I know I'm passing it in correctly, since if i just use the number 16 in place of u_Blur, except for the line "Color /= float(u_Blur); it all works, blur and the color is affected just fine, I just obviously can't change the blur anymore.
Anybody see something wrong here?
New to shaders, so it might just be some bad syntax or something, sorry x.x
 

obscene

Member
Shaders will not let you use a variable for a loop, it has to be hardcoded. No way around it. Not sure why.

I tried to do something like this once to give the player a "quality" setting on a shader... impossible. What I had to do (maybe this can work for your purpose) was make 3 different versions of the shader (low, medium, high quality) and let the player select which shader gets applied.
 
I ran into a similar issue, what I read is the (?compiler) needs to be able to determine in advance that the loop will terminate in a finite length of time, otherwise (?if I recall), you could lock up someone's gpu.
 
T

TaylorBramble

Guest
Ah excellent, thank you so much. A good thing to keep in mind :)
I just used a few if statements in the shader and things are looking pretty good
 
Top