简体   繁体   English

LIBGdx - 保存为 PNG 时,精灵中出现灰色像素

[英]LIBGdx - I get gray pixels in a sprite when saving as PNG

I've been trying to scale down a sprite and save it to the android local store for later use, but everything I've tried always results in a grey edge around the sprite.我一直在尝试缩小精灵并将其保存到 android 本地商店以备后用,但我尝试过的一切总是导致精灵周围出现灰色边缘。

Sprites精灵

As you can see, the sprite batch blend function is set to GL20.GL_ONE , GL20.GL_ONE_MINUS_SRC_ALPHA and rendering to the screen look fine.如您所见,精灵批处理混合功能设置为GL20.GL_ONEGL20.GL_ONE_MINUS_SRC_ALPHA并且渲染到屏幕看起来很好。 It's only when the PNG is written that I get the dark edge.只有在写入 PNG 时,我才会得到暗边。 I've tried setting Pixmap blending to none and using a premultiplied alpha version of the original image (that's why there are two images), but when I look in the emulator's file system I get the same result.我已经尝试将 Pixmap 混合设置为无并使用原始图像的预乘 alpha 版本(这就是为什么有两个图像),但是当我查看模拟器的文件系统时,我得到了相同的结果。 Here's my code:这是我的代码:

private AssetManager assetManager = new AssetManager();
private TextureLoader.TextureParameter textureParameter = new TextureLoader.TextureParameter();
private SpriteBatch spriteBatch;
private FrameBuffer frameBuffer;

@Override
public void create()
{
    Matrix4 matrix4 = new Matrix4();
    this.spriteBatch = new SpriteBatch();
    this.spriteBatch.setProjectionMatrix(matrix4.setToOrtho2D(0, 0, 96, 48));
    this.spriteBatch.setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);
    this.frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, 96, 48, false);

    this.textureParameter.genMipMaps = true;
    this.assetManager.load("sprite.png", Texture.class, textureParameter);
    this.assetManager.load("sprite_pre_multiplied.png", Texture.class, textureParameter);
    this.assetManager.finishLoading();
    Texture texture = this.assetManager.get("sprite.png");
    Texture texture_pre = this.assetManager.get("sprite_pre_multiplied.png");
    texture.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.Linear);
    texture_pre.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.Linear);

    this.frameBuffer.begin();
    this.spriteBatch.begin();
    this.spriteBatch.draw(texture, 0, 0, 48, 48, 0, 0, 132, 132, false, false);
    this.spriteBatch.draw(texture_pre, 48, 0, 48, 48, 0, 0, 132, 132, false, false);
    this.spriteBatch.end();
    Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, 96, 48);
    pixmap.setBlending(Pixmap.Blending.None);
    this.frameBuffer.end();
    PixmapIO.writePNG(Gdx.files.local("sprites.png"), pixmap);
    this.spriteBatch.setProjectionMatrix(matrix4.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
}

@Override
public void render()
{
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    this.spriteBatch.begin();
    this.spriteBatch.draw(this.frameBuffer.getColorBufferTexture(), (Gdx.graphics.getWidth() - 96)/2f, Gdx.graphics.getHeight()/2f, 96, 48, 0, 0, 96, 48, false, false);
    this.spriteBatch.end();
}

There was several issues and we found the solution together on Discord, so to sum things up to others :有几个问题,我们一起在 Discord 上找到了解决方案,总结一下:

  1. Blending function that preserve alpha : spriteBatch.setBlendFunctionSeparate(GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO)保留 alpha 的混合函数: spriteBatch.setBlendFunctionSeparate(GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO)
  2. Clearing framebuffer before drawing : glClearColor(0,0,0,0);绘制前清除帧缓冲区:glClearColor(0,0,0,0); glClear(GL20.GL_COLOR_BUFFER_BIT) glClear(GL20.GL_COLOR_BUFFER_BIT)
  3. The Original PNG file has black RGB on transparent and half-transparent pixels instead of edges sprite color.原始 PNG 文件在透明和半透明像素上具有黑色 RGB,而不是边缘精灵颜色。

For the 3rd point, it has to be re-exported from Gimp like this :对于第三点,它必须像这样从 Gimp 重新导出:

  • add a mask layer with "transfer alpha channel" option添加带有“传输 Alpha 通道”选项的遮罩层
  • fill RGB with the sprite color (fixing edges color)用精灵颜色填充 RGB(固定边缘颜色)
  • export as PNG with "save color values from transparent pixels" option on.使用“从透明像素保存颜色值”选项导出为 PNG。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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