Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Stencil Buffer - Highlight Grids

Discussion in 'General Graphics' started by Talthilas, Jan 17, 2017.

  1. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    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:
    1. How do I only write the white gridlines to the stencil buffer, and not the black infill sections?
    2. Is the stencil buffer approach the correct way to approach this problem?
    3. Can anyone suggest a way to achieve what I'm trying to do?
    Thanks guys.

    Aaron.
    SK001 - Stencil Buffer Grid Highlight.png SK002 - GalacticCiv Grid.png
     
  2. faren

    faren

    Joined:
    Sep 10, 2015
    Posts:
    60
    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).
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    Alpha test, aka alpha clip, should work with stencils.
     
  4. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    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.

    SK004 Highlight Grid Success.PNG
     
  5. I_Cudlitz

    I_Cudlitz

    Joined:
    Oct 22, 2016
    Posts:
    85
    Great solution! Good to hear that it's working well, and your end result looks pretty great.
     
    Talthilas likes this.
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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)
     
    Talthilas likes this.