Using ShaderProgram in libgdx to create an overlay for an in-game menu

Currently, creating a game and trying to make an overlay to cover the screen when you press the "menu" button - what I think should be quite common / simple, but still having problems with its implementation.

My current setting:

  • TiledMapRenderer: display TMX tiles (background / map)
  • SpriteBatch: for various assets (e.g. player image)
  • Stage: hold menu buttons
  • ShaderProgram: using GLSL to create an overlay / shader effect
  • SpriteBatch and map configured to use ShaderProgram

As suggested by many people, for performance I use only one SpriteBatch - therefore the same batch of sprites is used by both different assets and a menu item.

The goal of the shader is to add a dark / translucent overlay to the gray screen so that the menu is easier to read when it is open.

The main problem I am facing is that since the assets and menus have the same SpriteBatch, they also use the same shader ... so when I turn on the shading effect everything (background and menu buttons) greyed out.

How can I use only one SpriteBatch, but apply Shader only to the background (and keep the menu buttons normal / asymmetrical)?

+6
source share
2 answers
  • you can use: setShader (ShaderProgram shader)

  • you can add some logic to your shader to enable the overlay (uniformity, read some variable attributes, etc.), but this is not recommended because every line you put there is executed thousands of times per second.

  • [Recommended] you could draw a real overlay. You have a small gray texture (8x8px) with transparency and draw it after the background and in front of the menu. That would be on screen coordinates, just like a menu, right? therefore, it will not add any complexity (create a sprite large enough).

  • [Optional] when your game pauses when you press the menu, you can copy your screen to FBO by pressing the menu button. Then you can post-process the image (darken, blur) and use it as the background for the menu. This would allow for more pleasant effects, and in fact would be more effective, since the pattern of the hole in the hole becomes "cached" inside fbo (it is faster to draw a single texture fbo than to ask the hole scene again).

+9
source

After a discussion on the libgdx forums, it turned out that the solution was to set the power in the shader and then clear the package (i.e. spriteBatch.flush() ) before drawing the next one .

So rude...

 spriteBatch.start(); //draw stuff spriteBatch.flush(); // set shader strength/variables // draw more stuff (is now affected by shader) spriteBatch.end(); 
+2
source

All Articles