To solve this the libGDX SpriteBatch can me modified to add another component to the coordinate in the vertex array.
- Copy the source code of SpriteBatch to a new class.
- Copy the source code of Sprite to a new class.
- In the sprite class increase the field VERTEX_SIZE by 1.
- Add the z component to methods like
- In the SpriteBatch class add Z1-Z4 to the list of constants obtained in Batch.
Should look like this:
static public final int X1 = 0;
static public final int Y1 = 1;
static public final int Z1 = 2;
static public final int C1 = 3;
static public final int U1 = 4;
In the constructor change the vertex attributes to 3
new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
and in the line above the
size * 10 to a
size * 10, because of the four added vertex values.
Now use world coordinates for your sprites and pass the projection view matrix to the batch, so it gets uploaded to the graphic card. Now world and screen space coordinates are available in the fragment shader.
Beware that the depth buffer is disabled when using the spriteBatch. You can enable it by removing the
Source code can be found here. Maybe I forgot to mention something.