Search code examples
javalibgdxlwjgl

Draw transparent texture over ShapeRenderer


When I draw a texture with transparency(in file) over ShapeRenderer any shape isn't being updating. When I set batch.setColor(1f, 1f, 1f, 0.5f) result is almost the same: I see stuck shapes with 50% transparency and also see the same animated shapes underneath. I've tried to use Gdx.gl.glEnable(GL20.GL_BLEND) but it didn't help.

shape.begin(ShapeRenderer.ShapeType.Filled);
       shape.setColor(0f / 255f, 7f / 255f, 32f / 255f, 1f);
       shape.rect(0,0, width, height);
       for(Star star : stars)  {
            star.render(shape);
            star.update(dx, dy, delta);
        }
shape.end();

batch.begin();
batch.draw(overlay, 0, 0, width, height);
app.batch.end();

render method inside the Star class:

public void render(ShapeRenderer shape) {
            r = (position.z / max_depth);
            g = (position.z / max_depth);
            b = (position.z / max_depth);
            a = 1.f;                

            if(r < 0) r = 0;
            if(g < 7f / 255f) g = 7f / 255f;
            if (b < 32f / 255f) b = 32f / 255f

            float radius = (position.z / max_depth) * maxRadius;
            if(radius < 1) radius = 1;

            shape.setColor(r, g, b, a);
            shape.circle(position.x, position.y, radius);
}

Solution

  • You need to set BlendFunction to batch according to your desired blend output. By default it is enabled with GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA

    so try to change to :

    batch.setBlendFunction(GL20.GL_SRC_ALPHA,GL20.GL_DST_COLOR); 
    

    May be this not fit your requirement so choose appropriate one, according to your requirement.