【问题标题】:How does libgdx draw text in a 3D perspective?libgdx 如何在 3D 透视图中绘制文本?
【发布时间】:2019-10-24 10:53:57
【问题描述】:

我想在我的 guiCam(不受任何旋转或变换影响的第二个摄像头)中渲染固定文本以进行调试。

我认为正在渲染的文本在我的角度来看要么大要么小,但我似乎无法在官方 libgdx 文档中找到文本是如何绘制的以及我可以如何操作大小。

创建:

guiCam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        guiCam.position.set(1f, 1f, 20f);
        guiCam.lookAt(0, 0, 0);
        guiCam.near = 1f;
        guiCam.far = 300f;
        guiCam.update();

batch = new SpriteBatch();
        batch.setProjectionMatrix(guiCam.combined);

        font = new BitmapFont();
        font.setColor(Color.RED);

渲染:

font.draw(batch, "debug line here", 200, 200);

有人可以向我解释一下 libgdx 默认字体是如何绘制的,或者是否有更好的解决方案来显示固定文本?

我已经尝试过使用 OrthoGraphic 相机而不是专用相机,但由于我的主要应用程序是 3D,因此 OrthoGraphic 相机不起作用。

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    Found this for you on stackexchange.

    lookx2给出的答案:

    你可以直接使用另一个SpriteBatch而不设置投影矩阵来绘制HUD,

    camera.update();
    spriteBatch.setProjectionMatrix(camera.combined);
    spriteBatch.begin();
    aButton.draw(spriteBatch, 1F);
    playerShip.draw(spriteBatch, 1F);
    spriteBatch.end();
    
    hudBatch.begin();
    //Draw using hudBatch
    hudBatch.end();
    

    如果您想在屏幕上呈现文本,那么您需要通过绘制到 HUD 来实现

    【讨论】:

    • 感谢您的快速响应,我使用两个相机的问题是我使用的是 3D 相机而不是 2D OrthoGraphic 相机。我正在为字体使用单独的批次,并将该批次的投影矩阵设置为 guiCam。
    【解决方案2】:

    我认为它甚至不能通过混合 2d 和 3d 批次来工作。您应该将 Stage 和 StringBuilder 与单透视相机一起使用。

    例如,在create()函数中初始化如下

    ...
    font = new BitmapFont();
    //design the font
    label = new Label("<text>", new Label.LabelStyle(font, Color.BLACK)); 
    // set position
    label.setPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    stage = new Stage();
    stage.addActor(label);
    ...
    

    然后在render()函数中使用StringBuilder

    ...
    StringBuilder builder = new StringBuilder();
    //Change text at runtime    
    builder.append("<text>");
    label.setText(builder);
    stage.draw();
    ...
    

    帮助我使用它的例子来自https://sudonull.com/post/91327-The-simplest-3D-game-on-libGDX-for-Android-in-200-lines-of-code

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 2013-12-26
      • 1970-01-01
      • 2013-01-19
      • 2013-01-18
      • 2014-04-27
      • 1970-01-01
      • 2012-09-10
      • 2017-11-29
      相关资源
      最近更新 更多