Is there a way to start and stop a shader?.

I want to use this shader to sweep once over my texture and then stop. Does anybody know how to do that?

void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
    vec2 r = fragCoord.xy / iResolution.xy;
    r.y = 1.0 - r.y;

    vec4 tex = texture2D(iChannel0,r.xy);
    tex += vec4((sin((r.y)+r.x + iGlobalTime*3.)) + tex)/2.;

    fragColor = tex;

No, that's not how shaders work.

If you want to use a shader to manipulate a texture, you need to set up a render-to-texture operation. Create a result texture, bind it as the render target, bind your input texture as a regular input texture, and bind the shader you want to execute "once."

Render a fullscreen quad mapped with the input texture using the desired shader, and the result will be in the output texture which you can use in subsequent draw operations of real geometry.

Note, however, if you're trying to use this technique to animate the textures on otherwise regular geometry, it is not the most efficient way to do that; better would be to adapt the shaders used for the actual geometry to also include the effect you are trying to animate directly into the texture, as it would require far less resource management and state shuffling, which can be expensive and/or tedious.

  • \$\begingroup\$ What if i just Pass a time from 0sec to XSec (Animation end) instead of the iGlobalTine above \$\endgroup\$ – jeromintus Jul 21 '15 at 23:54
  • \$\begingroup\$ You will need to decide on a fixed value of the "time" variable for each render call, yes. \$\endgroup\$ – user1430 Jul 22 '15 at 3:17
  • \$\begingroup\$ Ans There are no Performance Problems? Should i remove the Shaders program After the Animation? And add it every time when i need the animation \$\endgroup\$ – jeromintus Jul 22 '15 at 5:10

no, you cannot stop shaders. That would break the render pipeline and as a result you would break rendering.
Doing such a sweep is simply setting the uniforms to the needed values from your application (for ex. incrementing the texture position values on each iteration). Inside your application, you know the size of the underlying object and can stop using that shader program once the sweep is done.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.