2

I'm trying to achieve the effect as shown in (B) of the following image. If I set alpha on the Sprite Batch I end up with the effect as shown in (C). Can anyone please tell me how I can get the desired effect as in (B).

enter image description here

Please note i'm not trying to mask the sprites. I'm trying to set a global alpha that will affect all sprites drawn after the global alpha has been set. An example of usage would be a dialog that fades in or out.

MSD
  • 137
  • 9
  • The only way I know of to do this would be to draw all of them at full opacity to a FrameBuffer and then draw the FrameBuffer's texture to the screen at partial transparency. – Tenfour04 Mar 28 '16 at 13:30
  • @Tenfour04: "*draw all of them at full opacity*" Just to make sure we're on the same page, you still need to *write* the alpha. You're just not using the alpha for *blending*. – Nicol Bolas Mar 28 '16 at 14:47
  • I mean that you draw them normally with blending and alpha, but don't reduce their opacity in the first pass by using a vertex color with lowered alpha. – Tenfour04 Mar 28 '16 at 15:38
  • did you try with the blending mode Gdx.gl.glEnable(GL20.GL_BLEND); Gdx.gl20.glBlendFuncSeparate(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA,GL20.GL_ONE, GL20.GL_ONE); ? – Hllink Mar 28 '16 at 19:40

1 Answers1

1

What you're asking for is non-trivial. Blending works on a per-triangle basis, after all, and two separate primitives are two separate primitives. One will be blended after the other, with no connection between them.

The only general way to achieve what you want is to render the sum total of these objects to a separate framebuffer, writing the alpha values of each object, but without blending. Note that the separate framebuffer should have an alpha component, and it should have been cleared to 0 before you started writing.

Once you have that separate framebuffer finished, you can then render it as a single quad, using blending (with some added alpha, as you desire).

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Hi, I've tried using pixmaps rendered to textures. This works as desired but has a very high overhead in terms of memory and time. Would a framebuffer be an improvement? – MSD Mar 29 '16 at 07:59