Sign in to follow this  
Wardyahh

Shader application on click?

Recommended Posts

Hello,

i'm trying to find out whether or not you can have a shader run only when a button is pressed.

For instance, say i have a pixel shader that darkens the colour slightly in a radius of 2 around the point at which a mouse is clicked.

i apply this shader to a 2d square of 3x3 units.

is it possible to have this effect apply itself only when pressing the mouse button.

so i click in the centre of the square and it slightly darkens a 2 unit radius around it, however if i hold the button the pixels in the 2 unit circle gradually get darker as the shader is applied over and over again.

i know how to pull the mouse coords and program the shader to darken a radius, but i'm not sure if ths can be done for a defined period or if its just a staright "applied or not".

perhaps i'm too tired to notice the obvious.

if anyone knows how to do something like i've described please let me know.

thanks in advance

Share this post


Link to post
Share on other sites
I don't really get your 'defined period' statement. The shader runs each frame which is likely to be much much shorter than the length of time you're holding the mousebutton.

In that case each frame you just have

if(mouseDown){
RenderStuffWithShader();
} else {
RenderStuffWithoutShader();
}



Shaders don't function over time and it doesn't get applied 'over and over again', as you said. Each scene you everything from scratch. If you want to darken more the longer you hold the mouse down, than you just keep track of some value and tell the shader how much to darken the image when you draw.

Share this post


Link to post
Share on other sites
i think he was talking about the more time you hold the button the darkener it should be.

AFAIK, shaders don't have any kind of state, so you can't do the way you were thinking. But what is the problem with incrementing a program variable, and pass it as a uniform or constant to the shader?


darkness = 0
for each frame:
if push button:
button_pushed = true
if release button:
button_pushed = false
darkness = 0

if button_pushed:
increment darkness

Share this post


Link to post
Share on other sites
I would do this one of two ways:
1) pass a uniform bool to the shader indicating that the effect should be applied
2) have two techniques; one with the effect and one without

For either of these two ways, you need to pass a uniform variable that holds the time since the button was pressed:


static int time = 0;

shader->SetInt("time",time);

time++;

if(button press)
shader->SetBool("doEffect",true);
else
{
shader->SetBool("doEffect",false);
time = 0;
}


//draw scene here

Share this post


Link to post
Share on other sites
Thanks for all the replies.

I suspected that shaders do not store state, i have placed all the variables in a containers class to be passed to the shader as suggested.

i do want the effect to be applied to the shader every frame, however i only want it to get darker when the mouse button is pressed.

i believe i can do this using class variables to store the value of the darkening.


Thanks for the help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this