0
votes

I want to create a flat space using SpriteBatch for my 3D environment. For this, I've used a set of simple square images (500*500 pixel) to create the sprite as follows in create() method. The 'coordinate' variable is the location of sample building 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();

Then in the render() part I used this snippet to transform and project the spriteBatch to the x-z plane where 3D models have been located. The 'cam' is the instance of 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();

Problem: When the camera is placed on the ground, my sprite surface displays fine (figure 1). But when the height of camera increases, it seems the surface has been elevated too and covers some of the buildings on the ground (figure 2). Is there something wrong with projection?

figure 1 figure 2

1

1 Answers

0
votes

SpriteBatch doesn't use depth writing, so you must draw with the SpriteBatch after you have drawn the 3D models.

Alternatively, if your sprites are totally opaque, you can manually enable depth writing by calling Gdx.gl.glDepthMask(true); right after spriteBatch.begin().