[英]LIBGdx - I get gray pixels in a sprite when saving as PNG
我一直在嘗試縮小精靈並將其保存到 android 本地商店以備后用,但我嘗試過的一切總是導致精靈周圍出現灰色邊緣。
如您所見,精靈批處理混合功能設置為GL20.GL_ONE
、 GL20.GL_ONE_MINUS_SRC_ALPHA
並且渲染到屏幕看起來很好。 只有在寫入 PNG 時,我才會得到暗邊。 我已經嘗試將 Pixmap 混合設置為無並使用原始圖像的預乘 alpha 版本(這就是為什么有兩個圖像),但是當我查看模擬器的文件系統時,我得到了相同的結果。 這是我的代碼:
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();
}
有幾個問題,我們一起在 Discord 上找到了解決方案,總結一下:
對於第三點,它必須像這樣從 Gimp 重新導出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.