【问题标题】:Using PerspectiveCamera to render SpriteBatch in Libgdx在 Libgdx 中使用 PerspectiveCamera 渲染 SpriteBatch
【发布时间】:2018-02-22 16:15:03
【问题描述】:

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

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() 部分中,我使用这个 sn-p 将 spriteBatch 转换并投影到 3D 模型所在的 x-z 平面。 'cam' 是 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)。投影有问题吗?

【问题讨论】:

    标签: libgdx sprite


    【解决方案1】:

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

    或者,如果您的精灵完全不透明,您可以通过在spriteBatch.begin() 之后调用Gdx.gl.glDepthMask(true); 来手动启用深度写入。

    【讨论】:

    • 我在渲染 3D 模型后绘制了精灵,如您在第二个代码 sn-p 中所见。另外,我应用了“Gdx.gl.glDepthMask”,但没有任何改变。
    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 2014-10-28
    • 2011-11-24
    • 1970-01-01
    • 2014-01-22
    • 2014-01-03
    • 1970-01-01
    • 2015-12-24
    相关资源
    最近更新 更多