简体   繁体   English

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

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

I'm using libgdx scene2d to render 2d actors.我正在使用 libgdx scene2d 来渲染 2d 演员。 Some of these actors originally included scene2d Label actors for rendering static text.其中一些actors最初包括scene2d Labelactors,用于渲染static文本。 The Labels work fine but drawing ~20 of them on the screen at once drops the frame rate by 10-15 frames, resulting in noticeably poor rendering while dragging.标签工作正常,但一次在屏幕上绘制约 20 个标签会使帧速率下降 10-15 帧,导致在拖动时渲染明显不佳。

I'm attempting to avoid the Labels by pre-drawing the text to textures, and rendering the textures as scene2d Image actors.我试图通过将文本预绘制到纹理并将纹理渲染为场景 2d 图像演员来避免标签。 I'm creating the texture using the code below:我正在使用以下代码创建纹理:

    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);

I assumed, and have read, that textures are often faster.我假设并读过,纹理通常更快。 However when I replaced the Labels with the texture, it had the same, if not worse, affect on the frame rate.然而,当我用纹理替换标签时,它对帧速率的影响是相同的,如果不是更糟的话。 Oddly, I'm not experiencing this when adding textures from a file, which makes me think I'm doing something wrong in my code.奇怪的是,从文件中添加纹理时我没有遇到这种情况,这让我觉得我的代码做错了。 Is there a different way I should be pre-rendering these pieces of text?我应该以不同的方式预渲染这些文本吗?

I have not tested this, but I think you can enable culling for the whole Stage by setting its root view to use a cullingArea matching the world width and height of the viewport.我没有对此进行测试,但我认为您可以通过将其根视图设置为使用与视口的世界宽度和高度匹配的cullingArea来启用整个舞台的剔除。 I would do this in resize after updating the Stage Viewport just in case the update affects the world width and height of the viewport.我会在更新舞台视口后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())
    );
}

It will only be able to cull Actors that have their x, y, width, and height set properly.它只能剔除已正确设置 x、y、宽度和高度的 Actor。 This is true I think of anything in the scene2d UI package, but for your own custom Actors you will need to do it yourself.这是真的,我想到了scene2d UI package中的任何东西,但是对于您自己的自定义Actor,您需要自己做。

If a child of your root view is a Group that encompasses more than the screen and many actors, you might want to cull its children, too, such that even if the group as a whole is not culled by the root view, the group can still cull a portion of its own children.如果您的根视图的子视图是一个包含多个屏幕和许多演员的组,您可能也想要剔除它的子视图,这样即使整个组没有被根视图剔除,该组也可以仍然剔除一部分自己的孩子。 To figure out the culling rectangle for this, I think you would intersect a rectangle of the group's size with the viewport's world size rectangle offset by -x and -y of the group's position (since the culling rectangle is relative to the position of the group it's set on).为了弄清楚这个剔除矩形,我认为你会将组大小的矩形与组的 position 的 -x 和 -y 偏移的视口的世界大小矩形相交(因为剔除矩形是相对于组的 position它已设置)。

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

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