Isometric Sprites on a Hex Cell Terrain

I’m trying to draw a set of isometric, sprite-based buildings across a 3D hex terrain. There are various hex cells constructed which can vary In elevation. I would like for the sprite buildings to sit on (some of) the cells ,which are placed during runtime, and for them to be drawn such that the terrain of the cell, in which the sprite sits, to not obscure the building. That is, the sprite intersects the terrain and appears to sit in its centre (hex). However, I would also like for cells, which are closer to the camera and higher elevation, to obscure the building as one might expect.

I’ve tried a number of potential solutions, but I’ve found some issues with my tests and I’m not so hot on HLSL so hoping for a few pointers.

  1. I tried to set ZTest to Always. This solves the problem of drawing the parts of the building sprite which cut through the seating hex cell on top. However, of course, this means the sprite is not obscured by higher/ closer cells since it’s always drawn on top with this setting.

  2. I tried to assign sorting orders of sprites and terrain hex cells based upon their elevations. As an aside, I have used an implementation of Catlike codings hex map for the cell based terrain. However, I have made some modifications such that cells are meshed together, based upon elevation, and in chunks (for efficiency) so that I can set the sorting order for that mesh.

Solution 2) seemed like it was going to work. However, I ran into some problems using a custom shader which did some strange things on some elevations of sprite, whereby the skybox would show through on parts of the transparent (cutout) at particular areas where there is a large difference between elevtions. There can be a number of varying elevations, ranging from 0 → 6 which are equally spaced in y.

This problem has stumped me and I’m not sure how I can solve it and I wonder if I’m over thinking it, in the meantime, I’ll look over some docs on unity shaders, but would be awesome if I could get some help!

Danke!

I’d suggest you first decide if you want to use Z-buffering or sort all your rendered stuff by yourself.

If you go for sotring yourself - just disable z-buffering and go for it, but the drawback is that you will have to draw everything (not just buildings and terrain, but also all the actors and effects) sorted by the terrain cell it belongs to.
This means more coding mess and constant switching of materials as you draw.
This would also mean you have to draw everything manually, including shadows if you use them, because that’s the only way of controlling the draw order.

So I guess you can tell, where I’m going with this - don’t sort and find a way to just use Z-buffering.

Rendering your terrain will be straightforward.
Then, on top of your terrain cell, draw an inversed hexagon box (or whatever shape you use for your terrain cells), so that you can only see the inside faces, not the outside ones.
Imagine this as your hexagonal bounding box for a building, but turned inside-out :slight_smile:

Then just project your flat building image onto the walls of that cell using custom shader.

If you’re using URP, you should easily be able to build a shader graph and use “Screen Position” node