gpt4 book ai didi

java - Libgdx - 帧缓冲区的纹理渲染到屏幕时的不同大小

转载 作者:行者123 更新时间:2023-12-02 13:20:50 25 4
gpt4 key购买 nike

由于我的设计,我想摆脱 NinePatch 对象,但我用它来使用帧缓冲区以所需的大小初始化纹理,然后将该纹理应用到我的自定义对象

我对 LibGDX 和 GL 很陌生。

问题是当我绘制 FBO 纹理来屏幕时图像太小,不知道为什么

我从这里获取了所有这些的骨架:https://stackoverflow.com/a/7632680/401529

我的 RenderToTexture 类是:

public class RenderToTexture {
private float fbScaler = 1f;
private boolean fbEnabled = true;
private FrameBuffer fb = null;
private TextureRegion fbReg = null;


[... more constructors ... ]

/**
*
* @param scale
*/
public RenderToTexture(int width, int height, float scale, boolean hasDepth) {
if(width == -1){
width = (int) (Gdx.graphics.getDisplayMode().width * scale);
}
if(height == -1){
height = (int) (Gdx.graphics.getDisplayMode().height * scale);
}

fb = new FrameBuffer(
Pixmap.Format.RGBA8888,
(int)(width * fbScaler),
(int)(height * fbScaler),
hasDepth
);

fbReg = new TextureRegion(fb.getColorBufferTexture());
fbReg.flip(false,false);
}

public void begin(){
if(fbEnabled) {
fb.begin();
Gdx.gl.glClearColor(0, 0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
}
public TextureRegion end(){
if(fb != null)
{
fb.end();
return fbReg;
// batch.draw(fboRegion, 0, 0);
}
return null;
}

}

还有我的 util 类:

public class ImageUtils {
public static TextureRegion ninePatchToTextureRegion(NinePatch patch, int width, int height){
return ninePatchToTextureRegion(new NinePatchDrawable(patch), width, height);
}

public static TextureRegion ninePatchToTextureRegion(NinePatchDrawable patch, int width, int height){
RenderToTexture r2t = new RenderToTexture(width, height, 1, false);
SpriteBatch sb = new SpriteBatch();
r2t.begin();
sb.begin();
patch.draw(sb, 0, 0, width, height);
sb.end();
sb.dispose();

return r2t.end();
}

}

然后我在创建 Sprite 时调用这个 util 类:

NinePatchDrawable ninepatchtest = new NinePatchDrawable(
new NinePatch(new Texture(Gdx.files.internal("img/ui/btn-retro-blue.png")), 5, 5, 5, 5)
);
TextureRegion result = ImageUtils.ninePatchToTextureRegion(ninepatchtest, 200, 100);
sprite = new Sprite(result);

当前结果(左)和模拟所需结果的大小(右)

screenshot

编辑:相机尺寸是显示模式尺寸,粘在屏幕上,没有缩放,没有移动。

编辑:修复了@Tenfour04建议的缺失处置

如果这种方式不是最好的方式,我愿意接受新的替代方案

最佳答案

好吧,我找到了问题的“一半”解决方案:

正如我在此示例中看到的那样 LibGDX - Drawing to a FrameBuffer does not work我错过了 SpriteBatch 对象的 setProjectionMatrix,所以现在渲染到纹理类 begin() 方法是:

public SpriteBatch begin(){
SpriteBatch batch = null;

if(fbEnabled) {
fb.begin();
Gdx.gl.glClearColor(0, 0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Matrix4 m = new Matrix4();
m.setToOrtho2D(0, 0, fb.getWidth(), fb.getHeight());

batch = new SpriteBatch();
batch.setProjectionMatrix(m);
}

return batch;
}

现在 renderToTexture begin() 返回 spritebatch,所以:

public static TextureRegion ninePatchToTextureRegion(NinePatchDrawable patch, int width, int height){
RenderToTexture r2t = new RenderToTexture(width, height, 1, false);
SpriteBatch sb = r2t.begin();
sb.begin();
patch.draw(sb, 0, 0, width, height);
sb.end();
sb.dispose();
return r2t.end();
}

这解决了从 FBO 绘制的纹理的大小,但是 ninepatch 在帧缓冲区之外绘制正常,并使用帧缓冲区进行拉伸(stretch)...

关于java - Libgdx - 帧缓冲区的纹理渲染到屏幕时的不同大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43575763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com