Jump to content
  • Advertisement
Sign in to follow this  

Shader application on click?

This topic is 2809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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

} else {

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;



if(button press)
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!