我想使用SpriteBatch为我的3D环境创建一个平面空间。为此,我在create()方法中使用了一组简单的正方形图像(500*500像素)来创建sprite,如下所示。‘model’变量是示例建筑模型的位置:
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的实例。
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)。投影有问题吗?


发布于 2018-02-23 05:19:52
SpriteBatch不使用深度书写,因此您必须在绘制3D模型后使用SpriteBatch进行绘制。
或者,如果您的精灵是完全不透明的,您可以在spriteBatch.begin()之后立即调用Gdx.gl.glDepthMask(true);来手动启用深度写入。
https://stackoverflow.com/questions/48932210
复制相似问题