Stencil Buffer - Highlight Grids

Hi,

I’m using a texture to draw grid lines for my strategy game. The material that uses the texture is placed on a large quad/plane object that spans the entire playable map. What I would like to do, is when a colored quad (or similar) moves over the grid, I would like the quads color to be draw to the gridlines that overlap the quad. See the attached image to explain what I’m trying to achieve (I’ve labelled it ‘final result’). I’ve also attached a screenshot from the game Galactic Civilization 3 which is what I’m ultimately trying to achieve with this effect.

I have been trying to use a shader with a stencil buffer to do this (in Shader Forge asset). I can’t seem to get it working (I’m not sure I’m even approaching this the right way). I basically want to only draw the gridlines to the stencill buffer, but the shader seems to write both the white gridlines and the black infill sections, so when the quad moves over, everything gets colored within the quads overlapping area.

So my questions are:

  • How do I only write the white gridlines to the stencil buffer, and not the black infill sections?
  • Is the stencil buffer approach the correct way to approach this problem?
  • Can anyone suggest a way to achieve what I’m trying to do?

Thanks guys.

Aaron.

2924594--216099--SK002 - GalacticCiv Grid.png

Hello,
The problem comes from that even if the black is transparent it’s still written in the buffer. You need to use a discard when alpha is inferior to a certain value to avoid writing the “black” part of the grid in the buffer (the alpha test may work but I have never tested it).

Alpha test, aka alpha clip, should work with stencils.

Thanks guys.

Well I went down a different road. I was getting the above method to work with limited success. The lines ended up looking horrible though (aliased and jagged). I decided to just use one big alpha blended grid texture for the main map grids, and then have a separate grid mesh for each colored region. Then I just recalculate the UV’s of the region mesh dynamically to ensure they always line up with the map grids, and its working very well.

Here’s the end result.

2926731--216326--SK004 Highlight Grid Success.PNG

Great solution! Good to hear that it’s working well, and your end result looks pretty great.

1 Like

you don’t need to align anything if it’s all in world space uv coordinates - might simplify things (pass x/z for instance as uv multiplied by whatever looks good)

1 Like