0

I have to draw a Texture with some opacity. I can't use a Sprite, because I'm distorting the Texture via SpriteBatch's draw(Texture texture, float[] spriteVertices, int offset, int count).

Setting the color of the SpriteBatch right before drawing, like this

    Color color = batch.getColor();

    float oldAlpha = color.a;
    color.a = 0.3f;

    batch.setColor(color);
    batch.draw(img, vertices, 0, vertices.length);

    color.a = oldAlpha;
    batch.setColor(color);

does not work. It seems like the only possible way to do this is to use a Sprite, but I need the Texture for the above method.

JohnBrighton
  • 161
  • 6
  • that should work – Daahrien Aug 17 '17 at 12:03
  • 1
    I don't think so. When you submit vertices directly, they are copied verbatim, so they include color data and the sprite batch's current color is ignored. To answer the question, look at the source code of Sprite to see how it applies color to the four vertices in its float array. Do the same thing to your float array before passing to it to draw(). – Tenfour04 Aug 17 '17 at 12:10
  • @Tenfour04 You are right. Seems like the only way to get this to work is to specify the opacity via the vertices. I'd like to mark your response as solution! In other words: in order to set the Texture opacity, the color passed by the vertices must contain the proper opacity level (no matter what color it is, since the Texture will be used; however, its opacity level will be applied to the Texture). – JohnBrighton Aug 17 '17 at 14:58
  • 1
    Maybe you should go custom [Shader](https://github.com/libgdx/libgdx/wiki/Shaders), if the default shader is not working on your scenario. – Tokenyet Aug 17 '17 at 16:21
  • You may have to enable blending before rendering texture with alpha channel using `Gdx.gl.glEnable(Gdx.gl20.GL_BLEND)` then set the desired blend functions ie `batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA)`, see this question for more info https://stackoverflow.com/questions/25347456/how-to-do-blending-in-libgdx – Alexandre GUIDET Aug 20 '17 at 21:34

0 Answers0