使用PerspectiveCamera在Libgdx中渲染SpriteBatch

时间:2018-02-22 16:15:03

标签: libgdx sprite

我想在我的3D环境中使用SpriteBatch创建一个平面空间。为此,我在create()方法中使用了一组简单的方形图像(500 * 500像素)来创建精灵,如下所示。 '坐标'变量是样本构建模型的位置:

texture = new Texture("tile.png");
matrix.setToRotation(new Vector3(1, 0, 0), 90);

  for (int z = 0; z < 10; z++) {
     for (int x = 0; x < 10; x++) {
         sprites[x][z] = new Sprite(texture);
         sprites[x][z].setPosition(coordinate.getEast() + 10 * x, coordinate.getNorth() + 10 * z);
         sprites[x][z].setSize(10,10);
       }
   }
spriteBatch = new SpriteBatch();

然后在render()部分中,我使用此片段将spriteBatch转换并投影到已放置3D模型的x-z平面。 &#39; cam&#39;是PerspectiveCamera的实例。

   if (loading && assetManager.update()) {
        loadingModel();
    }

    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    Gdx.gl.glClearColor(0.2f, 0.5f, 0.5f, 1);

    /* rendering 3D models */
    for (MyModel g : instances) {

        g.renderModel(cam);
    }
    /* rendering sprites */
    spriteBatch.setProjectionMatrix(cam.combined);
    spriteBatch.setTransformMatrix(matrix);
    spriteBatch.begin();
       for (int z = 0; z < 10; z++) {
          for (int x = 0; x < 10; x++) {
            sprites[x][z].draw(spriteBatch);
           }
        }
    spriteBatch.end();

问题:当相机放在地上时,我的精灵表面显示正常(图1)。但是当摄像机的高度增加时,表面看起来也已经升高并且覆盖了地面上的一些建筑物(图2)。投影有什么问题吗?

figure 1 figure 2

1 个答案:

答案 0 :(得分:0)

SpriteBatch不使用深度书写,因此在绘制3D模型后必须使用SpriteBatch进行绘制。

或者,如果您的精灵是完全不透明的,您可以在Gdx.gl.glDepthMask(true);之后立即致电spriteBatch.begin()来手动启用深度书写。