Z position of sprites not being honoured?


I have a scene with some playing cards on a tabletop. When the camera is perpendicular to the table it looks fine. But when I angle the camera the cards with a y above the centre point seem to be rendered under the table. I’ve shown this by putting the tabletop blend mode to “Multiply”.

All the cards have a z which puts them closer to the camera than the tabletop so they should never be obscured by the table top.

What am I missing or is this a bug? (using 1.7)

Just as a comparison… this is with the table top blend mode set to “Alpha” and camera looking straight down at the cards.

How close to each other are the sprites?
Remember that the sorting on Z is done in the space of the view matrix (the camera), and it’s only considering the center point of the sprite.
So if the tabletop sprite is very close to the other sprites, it may suddenly get a sorting index before another sprite, and then it may well start to look strange.
You can probably verify this by inspecting the number of draw calls going up, as it will draw “cards”, “tabletop” and “more cards”.

I would make sure that the tabletop is sufficiently far away in order to avoid any mixups.

That makes sense. So I’m losing z-precision as they tilt away from the camera… But I need the cards to look like they are resting on the table so I had put them close to do that.

The way I solved this in the end was to make a custom render script that renders sprites with material tagged as “card” after “tile”.


local predicates = {
-- ...        	
-- Declare custom predicates.
	card =
		tags = { hash("card") },
		object = nil
	cursor =
		tags = { hash("cursor") },
		object = nil
-- in the update()

render.draw(predicates.tile.object, { frustum = camera.frustum })
render.draw(predicates.card.object, { frustum = camera.frustum })
render.draw(predicates.cursor.object, { frustum = camera.frustum })