[英]LibGDX FrameBuffer to TextureRegion is rendering incorrect sprite
我正在制作一款游戏,要求我动态生成带有文字的卡片。 为了实现这一点,我试图将通用卡背景和一些文本渲染到帧缓冲区,然后从中创建一个TextureRegion用作Sprite。
上面的大型彩色图像是下面函数返回的精灵的样子。 正在渲染的图像实际上是纹理图集正在加载精灵的完整spritesheet。
第二个图像(预期结果)是从像素图到函数末尾生成的PNG屏幕截图的内容。
我可以确认正在使用函数中生成的TextureRegion,因为更改fboRegion.flip()
的参数会影响生成的精灵。
private Sprite generateCard(String text, TextureAtlas atlas){
//Create and configure font
BitmapFont font = new BitmapFont();
font.setColor(Color.RED);
//Create a SpriteBatch (Temporary, need to pass this in later)
SpriteBatch sb = new SpriteBatch();
//Create sprite from texture atlas
Sprite sprite = atlas.createSprite("back", 3);
//Create FrameBuffer
FrameBuffer fbo = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
//Extract Texture from FrameBuffer
TextureRegion fboRegion = new TextureRegion(fbo.getColorBufferTexture());
//Flip the TextureRegion
fboRegion.flip(false, true);
//Begin using the FrameBuffer (disable drawing to the screen?)
fbo.begin();
//Clear the FrameBuffer with transparent black
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
sb.begin();
//Draw card background
sb.draw(sprite, 0,0, sprite.getWidth(), sprite.getHeight());
//Draw card text
font.draw(sb, text, 20, 100);
sb.end();
//Take "Screenshot" of the FrameBuffer
Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
PixmapIO.writePNG(new FileHandle("screenshot.png"), pm);
fbo.end();
sb.dispose();
fbo.dispose();
return new Sprite(fboRegion);
}
如果有人有任何建议我会非常感谢你的帮助。 我觉得有些事情发生在错误的顺序但是我不能为我的生活看到FrameBuffer可以获取它正在渲染的图像或为什么返回的精灵有错误的图像但是将FrameBuffer转储到PNG给出正确的图像。
因此经过一些额外的实验后,我发现上面的代码完全符合预期。 我设法将问题跟踪到其他一些代码,这些代码采用了精灵的尺寸和位置,但渲染了不同的纹理(因此当我更改纹理区域时翻转)。
感谢那些检查了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.