首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Libgdx中使用PerspectiveCamera渲染SpriteBatch

在Libgdx中使用PerspectiveCamera渲染SpriteBatch
EN

Stack Overflow用户
提问于 2018-02-23 00:15:03
回答 1查看 225关注 0票数 0

我想使用SpriteBatch为我的3D环境创建一个平面空间。为此,我在create()方法中使用了一组简单的正方形图像(500*500像素)来创建sprite,如下所示。‘model’变量是示例建筑模型的位置:

代码语言:javascript
复制
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平面。'cam‘是PerspectiveCamera的实例。

代码语言:javascript
复制
   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)。投影有问题吗?

EN

回答 1

Stack Overflow用户

发布于 2018-02-23 05:19:52

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48932210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档