opengllibgdxspritealphaspritebatch

libgdx how to set alpha on multiple sprites as if it were a single sprite


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.


Solution

  • 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).