Libgdx纹理图像透明渲染

8
我使用纹理绘制了两张图片,但背景图片变成了黑色。源图片是png格式,具有透明度。我该如何解决这个问题?
我该如何呈现带有透明度的原始图像?
2个回答

32

试一下这个:

            spriteBatch.begin();
            //background
            seaTexture = new Texture(px);
            Color c = spriteBatch.getColor();
            spriteBatch.setColor(c.r, c.g, c.b, 1f); //set alpha to 1
            spriteBatch.draw(seaTexture, 0, 0, 480, 320);
            //foreground
            c = spriteBatch.getColor();
            spriteBatch.setColor(c.r, c.g, c.b, .3f);//set alpha to 0.3
            spriteBatch.draw(blockTexture, 50, 100, 120, 120);

            spriteBatch.end();

1
我需要先使用这行代码 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 清除屏幕,然后才能在 spriteBatch.begin() 前看到透明效果,具体请参考这里的说明 - rockhammer
3
当然,你需要这样做。我只展示了代码中最重要的部分。 - Nolesh

2

如果之前禁用了混合,请尝试使用spritebatch.enableBlending()。不过默认情况下应该是启用的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接