简体   繁体   中英

Transparency in texture not working in libgdx

I'm trying to draw a sprite in libgdx that has a transparent background to it, but the transparent background is filled in with white instead of showing what has already been rendered at that location.

透明度没有显示出来

The sprite (with the hat) is 64 by 64 with transparency around the edge and on the right. There should be two tiles with a 'C!' behind him but it's just filled in with white.

This is the code I am using to render the image.

public void draw(SpriteBatch sb, float parentAlpha){
    sb.enableBlending();
    sb.draw(texture, getX() + 32, getY());
}

If you're going to enable blending, you also need to set the blend function with setBlendFunction . This defines exactly how you want the blending to work. Presumably, you want the classic GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA blending.

All the sprites are supposed to be blended? Maybe disabling the blend after you already drawn could solve your problem.

Maybe something like that:

public void draw(SpriteBatch sb, float parentAlpha){
   sb.enableBlending();
   sb.draw(texture, getX() + 32, getY());
   sb.disableBlending();
}

If this doesn't work you could try activating the blend in the GL options with something like that:

    Gdx.gl.glEnable(GL10.GL_BLEND);
    Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
            ...
            Your code Here
            ...
    Gdx.gl.glDisable(GL10.GL_BLEND);

Hope it helps :)

First, make sure your PNG has transparency information in it.

Second, make sure you're drawing things in the right order (you have to draw the background boxes before the sprite).

I managed to fix the problem by changing my application to OpenGL 2.0 instead of 1.0. Still not sure what the problem was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM