繁体   English   中英

Libgdx 将 Bitmap 字体渲染到像素图纹理导致渲染极慢

[英]Libgdx Rendering Bitmap font to pixmap texture causes extremely slow rendering

我正在使用 libgdx scene2d 来渲染 2d 演员。 其中一些actors最初包括scene2d Labelactors,用于渲染static文本。 标签工作正常,但一次在屏幕上绘制约 20 个标签会使帧速率下降 10-15 帧,导致在拖动时渲染明显不佳。

我试图通过将文本预绘制到纹理并将纹理渲染为场景 2d 图像演员来避免标签。 我正在使用以下代码创建纹理:

    BitmapFont font =  manager.get(baseNameFont,BitmapFont.class);
    GlyphLayout gl = new GlyphLayout(font,"Test Text");

    int textWidth = (int)gl.width;
    int textHeight = (int)gl.height;
    LOGGER.info("textHeight: {}",textHeight);
    //int width = Gdx.graphics.getWidth();
    int width = textWidth;
    //int height = 500;
    int height = textHeight;

    SpriteBatch spriteBatch = new SpriteBatch();

    FrameBuffer m_fbo = new FrameBuffer(Pixmap.Format.RGB565, width,height, false);
    m_fbo.begin();
    Gdx.gl.glClearColor(1f,1f,1f,0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Matrix4 normalProjection = new Matrix4()

            .setToOrtho2D(0, 0, width,  height);
    spriteBatch.setProjectionMatrix(normalProjection);

    spriteBatch.begin();

    font.draw(spriteBatch,gl,0,height);


    spriteBatch.end();//finish write to buffer

    Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, (int) width, (int) height);//write frame buffer to Pixmap

    m_fbo.end();

    m_fbo.dispose();
    m_fbo = null;
    spriteBatch.dispose();

    Texture texture = new Texture(pm);
    textTexture = new TextureRegion(texture);
    textTexture.flip(false,true);
    manager.add(texture);

我假设并读过,纹理通常更快。 然而,当我用纹理替换标签时,它对帧速率的影响是相同的,如果不是更糟的话。 奇怪的是,从文件中添加纹理时我没有遇到这种情况,这让我觉得我的代码做错了。 我应该以不同的方式预渲染这些文本吗?

我没有对此进行测试,但我认为您可以通过将其根视图设置为使用与视口的世界宽度和高度匹配的cullingArea来启用整个舞台的剔除。 我会在更新舞台视口后resize ,以防更新影响视口的世界宽度和高度。

@Override
public void resize(int width, int height) {
    //...
    stage.getViewport().update(width, height, true);
    stage.getRoot().setCullingArea(
        new Rectangle(0f, 0f, stage.getViewport().getWorldWidth(), stage.getViewport().getWorldHeight())
    );
}

它只能剔除已正确设置 x、y、宽度和高度的 Actor。 这是真的,我想到了scene2d UI package中的任何东西,但是对于您自己的自定义Actor,您需要自己做。

如果您的根视图的子视图是一个包含多个屏幕和许多演员的组,您可能也想要剔除它的子视图,这样即使整个组没有被根视图剔除,该组也可以仍然剔除一部分自己的孩子。 为了弄清楚这个剔除矩形,我认为你会将组大小的矩形与组的 position 的 -x 和 -y 偏移的视口的世界大小矩形相交(因为剔除矩形是相对于组的 position它已设置)。

暂无
暂无

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

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