Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Fog of war shader, how to handle slightly tilted camera angle?

Discussion in 'Shaders' started by bitinn, May 25, 2020.

  1. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    673
    How might Northgard / Starcraft 2 etc. render their fog of war?

    I am most interested in how they manage to cover the trees and terrains, while also prevent camera line of sight issue due to their camera tilt.

    In particular:

    - If the fog of war is a volume, then towards the bottom of the screen, line of sight may cross the fog volume, causing the fog to appear to cover more area than it actually is.

    - If the fog of war is a plane, then I wonder what trick they use to cover everything above it, just ztest always + creative depth based shading?

    fog-of-war-01.png
    fog-of-war-02.jpg

    (I also found some Starcraft 2 mod that can see the fog of war from ground level, which suggest they might be using fog volume; but given this fog height, how did they avoid line-of-sight issue?)

    fog-of-war-03.jpg
     
  2. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    673
    I figure the best solution is either:

    1. use the separate camera and render using orthogonal mode.

    2. depth projection, basically reconstruct the world space position from depth and then use it to shade, mask out area that's not within the volume. so the fog appear to "stick" to ground.

    I am going with plan 2, I just have a question: if I don't want a screen-space post-process effect, but instead use a volume (a cube) in world, how can I convert the volume's vertex position to view-space vertex position on far plane, so that we can do the depth projection?

    (Pretty sure this is the most efficient way, any thoughts @bgolus ?)
     
  3. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    673
    Actually I am overthinking this, I can either:

    1. use normalized view direction and depth (pixel depth and depth texture) to calculate the world position
    2. use view space position and depth ratio (again, pixel depth and depth texture), which will also give me the same result.

    Recommended reading:

    https://mynameismjp.wordpress.com/2010/09/05/position-from-depth-3/
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,186
    Both games appear to just be using a top down 2D projection for the actual fog of war. Northgard is additionally using some noise to make the edge more interesting. I can't entirely pin down what else they're doing, because some objects poke out of the fog more than they should, and others seem to go dark on their heights when they shouldn't. My best guess is they're using a low resolution static 3D noise to add some additional dynamics to the 2D fog of war mask.
     
unityunity