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. Dismiss Notice

Fog of War with URP

Discussion in 'Scripting' started by Zalosath, Apr 27, 2020.

  1. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Hi there,

    I've been working for the past couple of days on implementing fog of war into my game. I want it to be Dynamic Fog of War, such that, unexplored areas are completely black, areas that have been explored but have no vision anymore would be a gray colour revealing the map below (only shows what they saw at the time) and of course, true vision, where the player is currently standing.

    I managed to get something working but it's really not ideal.

    So at present state the gray colour is not implemented, I.e. the map will not stay revealed.

    It looks like this so far.
    https://gyazo.com/aba015537dee44d822787ac12abe968f
    (gif showing what I mean)

    This method works as a result of using sprite masks to clear a texture "fog" where the reveal node is present.

    This has multiple issues; namely, if a unit is set to auto attack they will still be able to attack units that are not yet revealed. As I have no way of knowing if a unit is "revealed".

    I found this great blog post that is very descriptive but I simply cannot understand the main bulk of it. It's not quite descriptive enough to actually get a working prototype.
    Here's the blog post:
    https://blog.gemserk.com/2018/08/27/implementing-fog-of-war-for-rts-games-in-unity-1-2/

    I also tried using shader graphs but for the life of me, shaders are not my thing and got absolutely no where with it.

    And of course there's the tutorials that use Clear Flags, which was removed in the URP update.

    FYI, this is in the scripting section as I imagine it includes some scripting, which is my strong suit in comparison to shaders.

    If anyone has any potential solutions to my issue, then please let me know.
    Thanks.
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,

    I think you need to separate two issues, data and view.

    And what is the look you are after? That tutorial has blobby, somewhat natural looking fog, but your test has sharp, paper cutout look? Is that what you want to do?

    Anyway, you could just store visibility data as a matrix like that article's fog system had. Just store some sort of coordinate grid of data, where you store either on/off or higher fidelity visiblity data. Then, when you have the data and you can alter it correcly, you can start thinking about how to render it.

    Rendering to texture, if you have that very sharp look, means that you would need a quite high resolution. One way to get fog of war is that you could maybe instantiate "fog" objects over your world (but that wouldn't work ok if you want to carve it out with a round shape) Or you could do it like they did it - render black and white blob objects to the screen and then capture that with a camera to a rendertexture, and then use that texture as an overlay, maybe as an UI element or a post effect...

    Anyway, check some tutorial how to render camera to texture, it's not that hard. You would just need to configure that camera to cull away the objects that aren't needed for the fog render (i.e. everything else but the fog masking blobs) and then render that to a RenderTexture asset. You can do that all via UI and without code...
     
  3. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    I tried to make the fog natural looking but sprite masks don't allow for transparency.

    As we speak I'm making the matrix, I've made a 100x100 float matrix that holds opacity values and this works fine programmatically. I'm unsure how to turn the render texture into something that will display the fog.

    "render black and white blob objects to the screen and then capture that with a camera to a rendertexture", I don't understand how I would then turn this into fog? A white blob would be an area of revelation I imagine but then how would I make this area transparent so I can see through it.

    Thanks.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    You would use a generated black and white "mask" as an alpha, so that you can fade in and out a texture or just a flat color.
     
  5. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    By generated you mean one that follows the matrix I created?

    How would I then make the soft transition like the blog post shows?

    EDIT: With the matrix I was able to generate this:


    Highlighted in the bottom left is 1 point in the matrix.
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well - use soft textures in your blob objects you capture with your fog camera. Instead of on-off, have some gradients?
     
  7. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    How do I render to a render texture if I'm using the camera as Overlay? Unity doesn't let me assign render textures unless its a Base camera.
     
  8. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Okay I managed to get something working.
    https://gyazo.com/13db704ea6081ceb7f17f5416d04e7cc

    How would I move the fog downwards so you can't see through the side of it?
     
  9. miroslaw

    miroslaw

    Joined:
    Nov 15, 2012
    Posts:
    70
    This may help? https://andrewhungblog.wordpress.com/2018/06/23/implementing-fog-of-war-in-unity/

    I've implemented this and it works fantastic. Though, I'm trying to figure out how to get higher resolution for of war - sometimes the revealed mapping texture doesn't follow the bounds of objects perfectly, so there will be "shadowy" parts around the object that should not contain fog.

    I'm still trying to understand how it works so i've been playing around with the settings.

    My DarkShroud is 450x450 and my Shroud is 1200x1200 so not sure if that has something to do with it... I plan on having big worlds.
     
  10. faolad

    faolad

    Joined:
    Jan 27, 2013
    Posts:
    118
    Is this method working for the URP half baked system?
     
  11. Nurallaerel

    Nurallaerel

    Joined:
    May 6, 2015
    Posts:
    13
    Do you know any solution for URP? The link provides great solution for fog of war but since Projectors are not working with this pipeline, I'm trying to find anything.
     
    Surd23 and Amulo like this.
  12. Surd23

    Surd23

    Joined:
    May 9, 2021
    Posts:
    1
    2022 - answer is still open!
    Can anyone help with fog of war at URP?
    I realized my fog with that tutorial: https://andrewhungblog.wordpress.com/2018/06/23/implementing-fog-of-war-in-unity/
    And fog looks great! There is a black and semi-black area, all is well: fog sample.png
    But it does not work with URP, because Projectors components are not working with this pipeline!
    I was trying to make fog through URP Decal system, but I am working with shaders only couple months, and don't reached well result. Plus if Main camera go through the Decal box all fog is disappeared, when Projector component have no this problem.
    Please help! I want work with URP and make new effects and shaders, but I can't make fog with URP by myself. Googling don't give any useful answer...
     
    Nurallaerel likes this.