Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Fog of War

Discussion in 'Assets and Asset Store' started by stu_pidd_cow, Oct 11, 2016.

  1. andympage101

    andympage101

    Joined:
    Jun 7, 2013
    Posts:
    12
    Hiya, I have had this asset for a while and now I am using it on my latest project on unity 2018.2.6f1. Currently I am trying to make the visuals as pixel perfect as I can with unity's new 2d pixel perfect package, however when I apply the Fog of War on the camera, unfortunately on build everything becomes very blurry even if all the settings are configured to point filtering and the blur params are at 0. If i disable the fog of war and build again it looks fine.

    I imagine it is something to do with the render order but I cannot seem to get it to work no matter what order I load the scripts and what render queue I try to set to the fog of war. Does anyone have any ideas?
     
  2. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    Just to clarify, you're saying it's not saving the texture filter you selected when doing a build? Does is appear fine in the editor? What platform/device are you building to?
    Script execution order should be irrelevant for this.
     
  3. andympage101

    andympage101

    Joined:
    Jun 7, 2013
    Posts:
    12
    Hi apologies for the lack of information and thanks for the quick reply
    I am just using the PC, Mac and Linux Standalone at the moment.
    It did look fine on the editor but I think because of the settings on the pixel perfect script it stretched the render image across the screen on build making it blurry.

    After having spent a bit more time on it I have found the solution, in the FogOfWar.cs CustomGraphicsBlit function, at the start I added:

    Code (CSharp):
    1.             if (dest != null)
    2.                 dest.filterMode = FilterMode.Point;
    Really simple and it works great so far
     
  4. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    Ah I see now. I might need to put in a toggle to let you do this from the UI. Thanks for pointing this out!
     
  5. mkgame

    mkgame

    Joined:
    Feb 24, 2014
    Posts:
    583
    Hi, I fixed the code for the circle revealer (that is what I need), I have now 1.6ms (not just for FoW, for your scene) load at all for 100 units on 2000x2000 map with a texture resolution of 1000x1000. I made a state machine in a coroutine with Fade, Reveal, Paint 2d texture. Fade is now in a thread, works fine.
    The other threads have about 1-5% load, not sure, Unity also uses all the cores. If you want the code, I can give it to you, maybe you can use it. But you should have the same results, if you fade in thread and use Array.Copy(). Then in a coroutine: fade -> wait until all threads processed -> reveal for all units -> wait until all threads processed -> paint on 2d texture.

    For me it works fine now!
     
  6. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    Sounds pretty awesome! I'd be happy to check out your changes and put them in. Feel free to pop me an email (you can find it in the user guide).
     
  7. -Singularity-

    -Singularity-

    Joined:
    Jul 27, 2014
    Posts:
    117
    hi, is there a good way to use this for line of sight only?

    IE, no full fog, only partial. That way I could have a small map following the player with no need for chunk manager (which seems to cause a screen flicker when transitioning, even on a powerful machine)

    my setup is top down open world, with only the player needing vision.
     
  8. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    There is a bug with the chunk manager in the current release, but it will be fixed in the next release.
    I'm not sure what you mean by partial fog.
     
  9. -Singularity-

    -Singularity-

    Joined:
    Jul 27, 2014
    Posts:
    117
    Great, that's good news.

    Essentially, id like to use this for line of sight only. So everything starts with partial fog and no full fog at all, ie, 50% fog over the entire map, reducing to 0% where the player can see.

    Is the best way just to use the set all function to 0?
     
  10. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    If you want it to be partially visible, you can change the alpha value on the Fog Color.
     
  11. -Singularity-

    -Singularity-

    Joined:
    Jul 27, 2014
    Posts:
    117
    cheers - that was playing havoc with other image effects previously but I've managed to fix it.

    I'm thinking about opportunity for efficiency here though. If we only ever need line of sight, and don't need to remember where we have been, can we have a small map following the player that only just covers the visible screen?

    This would remove the need for chunk manager and likely be much faster given the small map size?
     
  12. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    Hmmmm it's not a bad idea. I'll look into implementing this. Thanks for the idea!
     
  13. Paradoks

    Paradoks

    Joined:
    Oct 13, 2009
    Posts:
    418
    Hello, does your fow work with lightweight render pipeline ?
    Because it seems that blitting dont work on it.
     
  14. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    At the moment FoW only supports the legacy render pipeline, but I intend on adding LWRP and HDRP as well as custom render pipelines in the next few releases.
     
  15. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
    This is great! Had it up and running in just a few minutes, and the "Clear Fog" setting appears to be doing exactly what I need (show the skybox where the fog is). However, when I enable it, my frame rate drops massively. The render thread goes from taking almost no time at all (0.8 ms) to 14 ms, while the CPU goes from 10 to 40 ms per frame.

    Changing the settings doesn't seem to make a difference. I've attached screenshots of the settings and stats window.

    Is this just always a very heavy weight mode, or is there anything that can be done to improve performance? A quick run of the profiler shows most time is spent in FogOfWar.OnRenderImage() and Gfx.ReadbackImage.

    blackfog2.png blackfogsettings2.png clearfog2.png clearfogsettings2.png
     
    Last edited: Nov 27, 2018
  16. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
  17. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
    Oh, another (smaller) issue: when I pan the camera in the game view so that I can see underneath my "floor", the fog texture becomes visible underneath. I'm not sure how to explain, so here's a picture:

    under-image.png

    I've colored the fog blue to make it more obvious. I may be able to prevent the camera from going anywhere where this shows up, but given the view angle, centering the bottom of the map on the screen would cause this to show.
     
  18. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
    (But to quote my review - brilliant asset, good documentation, almost zero effort to get working... very pleased!)
     
  19. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    I just looked into this and you're right! For some reason, I missed this. I'll look into a fix for this. Thanks for pointing it out.

    This is intended, but you should be able to tweak it by changing the Outside Fog Strength slider on the FogOfWar component.
     
  20. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
    > This is intended, but you should be able to tweak it by changing the Outside Fog Strength slider on the FogOfWar component.

    I don't think that's it - the screenshot is with the slider at zero. If I turn it up to one, of course the skybox disappears and everything turns blue - except where the unfogged holes are.

    Here's another scerenshot with the outside fog strength set to .5:

    bluefog.png

    The white text/lines I've drawn on top afterwards, not part of the scene.

    I'm not sure where the bottom blue square, a sort of "after image" of the fog texture, is coming from.
     
  21. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
    > Way out of my depth here on graphics programming, but is there any way to use the rendered skybox without pulling it out of the GPU?

    Could it be as easy as this? In
    RenderClearFog()
    , just remove everything relating to
    skyboximage
    and use
    skyboxcamera.targetTexture
    directly:

    clearfogmat.SetTexture(_ids.skyboxTex, skyboxcamera.targetTexture);
     
  22. Neurolog

    Neurolog

    Joined:
    Oct 19, 2018
    Posts:
    5
    Just got this- very nice asset. I'm making a paintable fog of war and have managed to remove the fog with click and drag using a revealer prefab nicely.

    Now I'm looking for a way to turn the fog on and off at runtime. So far, I've made a public camera variable, added the fog of war component, and set up this code to execute on button press:
    "bool presszero = Input.GetButtonDown("Number0");
    if (presszero)
    {

    if (Camera1.GetComponent(FogOfWar).enabled == true)
    {
    Camera1.GetComponent(FogOfWar).enabled = false;
    }


    else
    {
    Camera1.GetComponent(FogOfWar).enabled = true;
    }"
    The button press and debugging executes nicely, and Camera1 has been set to main camera in the editor. However, Unity can't seem to find fog of war when I try to enable it. Do you know if I am referring to it wrong? Thanks!
     
  23. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    Actually I think disabling the Fog Far Plane toggle on the Fog Of War component should fix this!
     
  24. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    What do you mean by "Unity can't seem to find fog of war"? Using Camera1.GetComponent<FogOfWar>() should still work find.
     
  25. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
    > Actually I think disabling the Fog Far Plane toggle on the Fog Of War component should fix this!

    Did some testing, and it's definitely the fog on the far plane: if I move the camera far plane the image moves along. However, toggling the setting makes no difference.

    I think I found the cause: outside the fog area, rawdpth is apparently always zero, so
    fog *= step(rawdpth, 0.999);
    doesn't take effect. Adding
    if (rawdpth == 0) fog = 0;
    seems to work, but results in slight artifacts at the edge of the floor.
     
  26. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    It seems like your depth buffer is flipped (which can happen on some render devices), but it looks like there is no code to cater for it. I just found that there is a UNITY_REVERSED_Z macro that can detect this. There is a line with the following macro in the shader:

    Code (CSharp):
    1. #if UNITY_VERSION >= 550 && !SHADER_API_GLES && !SHADER_API_GLES3 && !SHADER_API_D3D
    Try replacing it with:

    Code (CSharp):
    1. #if UNITY_REVERSED_Z
    or

    Code (CSharp):
    1. #if !UNITY_REVERSED_Z
    I think that should fix it properly.
     
  27. Neurolog

    Neurolog

    Joined:
    Oct 19, 2018
    Posts:
    5
    I think the problem for me is that I don't have the scripts themselves inside of the scrips folder, but rather the "importer" scripts. When I call the FogOfWar script, no such script is detected. So I can call the importer scripts from my code with this method but not the scripts themselves- for example the FogOfWarUnitImporter can be found, but not FogOfWarUnit. However, I have no problem adding the true scripts to a game object using the GUI (attaching FogOfWarUnit to a gameobject works fine in the . Do you think this is a problem with my import or is it intended that only the importer scripts display in the scripts folder?
     
  28. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
    > UNITY_REVERSED_Z

    No dice, unfortunately.

    I moved it out of the
    #ifdef CAMERA_ORTHOGRAPHIC
    block (since I'm using a perspective camera) and
    #if !UNITY_REVERSED_Z 
    has no effect (so I guess it is reversed) while
    #if UNITY_REVERSED_Z
    just makes all fog disappear.
     
  29. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    Sorry, I don't think I understand what you mean with these importer scripts. Is it something that transforms other scripts?
     
  30. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    Yes, having the wrong UNITY_REVERSED_Z would certainly make the fog disappear, but I'm not why it wouldn't work. What platform graphics API(ie D3D11, GL, etc) are you testing with?
     
  31. CodePocalypse

    CodePocalypse

    Joined:
    Mar 9, 2018
    Posts:
    5
    Hi stu_pidd_cow,

    Great asset...I am also have the same problem as Kaboom. FOW is being projected onto the far clipping plane.

    Specs:
    U 2017.3.1f1
    Win 10 OS
    NVIDIA GeForce GTX 570
    DX11 (I believe)

    As you can see in the image, the FOW shows perfectly on the table but underneath the table is a duplicate of the FOW above being projected onto the far clipping plane.

    FOWFarClip.jpg
     
  32. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    Hmmm this is odd. I'm trying to replicate it with the sample scene. This is with Fog Far Plane on (and background set to red):
    upload_2018-12-6_9-21-57.png

    And this is off (with the background set to red):
    upload_2018-12-6_9-22-47.png

    This is what the expected behavior is for me. Do you get something different when doing it with the sample scene?
     
  33. CodePocalypse

    CodePocalypse

    Joined:
    Mar 9, 2018
    Posts:
    5
    I checked the sample scene and it does work as your above example. I honestly can't remember when that projection showed up. We've been deving hard on this focusing on mechanics, when I first saw it I just ignored it thinking it was a minor issue that I could fix with a script setting or something. But trying a bunch of settings didn't work. So I looked up your forum and when I saw other people with the same issue and just assumed it was the shader and not MY fabulous code...oh no, can't possibly be that. :rolleyes:

    Full disclosure though...I did do some modifications to your FOW shader. I added luminence to make it globally semi transparent in order to see certain world items like terrain and houses and yet still fog out the enemy game items when in the fog. But the modification is simple and I stitched it in with care so as not to mess up your shader calcs (I believe).

    I also have a second camera with the clear flags set to "depth only" and culling certain layers because I didn't want certain items hidden under the fog - things like artillery targeting gfx, etc (which would show grey when targeting within the fog). It's possible this second camera is somehow interfering with the shader calcs. Or the outline script asset I am using (which also has a weird camera setup) may be interfering? However, when I turn off the extra clear flag camera the projection remains...

    [Good News]
    So...as I was typing this...I just tried Kabooms idea above and it is working now without artifacts - have no idea why - but adding the if(rawdpth == 0) fog = 0; to the FOW shader seems to have done the trick. o_O

    Code (CSharp):
    1. #ifndef FOGFARPLANE
    2.      fog *= step(rawdpth, 0.999); // don't show fog on the far plane
    3.      if(rawdpth == 0) fog = 0;
    4. #endif
     
  34. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    I'll have to look into why this is fixing it, but I'll definitely put it in the next release. Thanks for pointing this out.
     
  35. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    v1.15 has just been uploaded to the asset store for approval. This has been a huge update, but hopefully it was worth the wait!
     
    jushiip likes this.
  36. -Singularity-

    -Singularity-

    Joined:
    Jul 27, 2014
    Posts:
    117
    Oooooo, what does 1.15 include?
     
  37. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    1,475
    I am currently experimenting with this asset and have few questions :

    1. I am trying to use chunk manager, but I am not sure what the size of map should be according to the player's visibility length. If I set the player's length to be 10 unit. What is the map size I need to set it to? This becomes even more confusing say when you have a top down game situation where player's vision length is 10 but when he is on one side of the screen so the camera can see more than 10 units. Should I be setting the max of both the camera can see from top to down and from left to right? Right now, I have around 36 Unity unit length going from my left to right within the camera, so should I be setting the map size to be 36 or 38 (just to be sure?) I have tried this but it looks like chunk swap happens at the time when I can see the area not covered by the new chuck. I had to make the map size much larger like 70 in order for the swapping is covered without camera seeing the uncovered areas. There also seems like one frame bug glitch issue when swapping to another chunk. A flash of one frame that make all the fog disappear (flickers)

    2. Performance question. If I have say 256x256 map, but its size is say 100, does it make any difference if I set the size to be smaller? Setting size to be smaller will give me more detailed fog, but I wonder if the performance also depends on the number of fog unit that are processed within the fog of war unit's visible area too?

    3. Minimap question. I can see pretty straight forward of utilizing the fog texture into minimap. But when you use chunk manager, how do you suggest the best way to tackling this problem?

    4. Line of sight : I am guessing that ray casting is used for this? Just how many castings are done? What are the factors?

    5. Is there way for me to implement my own custom fog shader? Single color or a textured fog is fine, but I would like to try and build something more interesting, such as animated cloud or smoke like.

    It's a lot of questions but thanks for taking time to answering them. !
     
  38. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    • Added an array size check when setting FogOfWar.fogValues
    • Fixed flickering when switching chunks with the chunk manager in multithreaded mode
    • Fixed flickering when performing a manual update in multithreaded mode
    • Made tree materials more diffuse in sample scenes
    • Turned of UI by default in smaple scenes
    • Fade speed now affects fade in and out speeds
    • Added boolean to stop unit information from being updated
    • Changed manual updates to a updateAutomatically boolean value
    • Changed texture shapes to always use the Alpha8 texture format to save memory usage
    • Made it so Partial Fog Amount can be changed without destructively affecting the fog map
    • Added VR support
    • Fixed a bunch of bugs with shape offset
    • Added Reinitialize button to FogOfWar component when playing in the editor
    • Added rectangular box and texture shapes
    • Merged box and texture unit shapes
    • Added pixel perfect toggle for screen texture
    • Changed filter mode to point filtering toggle
    • Fixed major performance issue with clear fog
    • Changed fade speed to fade duration (in seconds)
    • Made texture shapes not cause errors with multithreading
    • Fixed some weird behaviour that can happen when not fogging the far plane
     
  39. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    The size of the chunk manager should be the size that your camera can see. For example, if your camera can see 100x100 in world space, the map size should be 100x100. Also, the flickering issue with chunking should be fixed in the latest update (v1.15).

    The map size has no/little impact on performance. It's the resolution that will have the biggest impact (less it better). The number of units, and how large their vision radius is will also have an impact, but multithreading should help a lot with the number of units.

    Minimaps with the chunk manager can be tricky. Rather then getting the fog texture directly, you might want to implement the minimap as a separate camera with a FogOfWarSecondary script attached to it. All you do is have the camera look at a different "minimap" layer mask so that it only renders minimap stuff.

    At the moment, there are 256 raycasts per unit (none are cast if line of sight is turned off). You can modify this if you want in the FogOfWarUnit script at line 256 (in v1.15). I will need to make this value public so that it can be easily changed...

    There is currently a fogColorTexture on the FogOfWar component which will let you set the color. But there will be lots of new customization features in the next update ;)
     
  40. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    1,475
    For the line of sight ray cast, If I set the angle to be say 60, then will it cast less rays? Or is it a constant fixed number no matter how narrow the sight angle is? If it is a constant number of rays casted 360 degrees around then could this not be better optimized?

    In this video, author uses the min max edge detection stuff to narrow down the ray cast hit. And only casts within the angle of view.
     
  41. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    It is a fixed constant, but you are right, it should definitely be optimised. I'll work on this optimisation for the next release.

    Video?
     
  42. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    1,475
  43. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
    Thanks for the update! Some issues I hit after updating:
    • my fog busting units are no longer fog busting if fadeDuration is zero (which I think I need, because I have instant fog busting scans which only exist for a single frame). Should I use manual updates instead? I added an
      else _fadeAmount = 1;
      after the fadeDuration check;
    • I'm getting "OnRenderImage() possibly didn't write anything to the destination texture!" warnings in the log, but I'm not sure if there are any adverse effects

    The `rawdepth == 0` test does result in artifacting around the edges of fogged areas (but checking "fog far plane" now works for me).
     
  44. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    A fade duration of 0 would mean that it never fades in. You probably want this to be really high. But I can definitely make it so zero is instant.
    I haven't seen this one before. Which version of Unity are you using?
    I added the rawdepth == 0 to the latest release. You're saying that it causes artifacts now?
     
  45. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    152
    Hey just upgraded to latest version (1.15) but there is a compiler error on
    FogOfWarEditor.cs line 12. There is no `Reinitialize()` in FogOfWar.cs

     
  46. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    112
    Hmmm that's odd. You should see the Reinitialize() method on line 190 in the FogOfWar.cs script. Is it possible that this file didn't update for some reason?
     
  47. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    152
    You're right. I missed it at the time but it was because I had my own script with the same name. So I wrapped your editor script in your FoW namespace.
     
  48. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    28
    Sorry for the late reply, I was out of town for the weekend.

    That is what the documentation says it does :)

    It's the same as before (it was CodePocalypse who didn't get the artifacts I believe).

    I'll check my Unity version for the OnRenderImage message!
     
  49. -Singularity-

    -Singularity-

    Joined:
    Jul 27, 2014
    Posts:
    117
    Hi, getting an error when building to windows on U 2017.3

    Shader error in 'Hidden/FogOfWar': undeclared identifier '_CameraWS' at line 161 (on d3d11)

    Compiling Vertex program with CAMERA_ORTHOGRAPHIC PLANE_XY TEXTUREFOG CLEARFOG
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_RGBM_ENCODING

    getting the same error for Vulkan

    Shader error in 'Hidden/FogOfWar': undeclared identifier '_CameraWS' at line 161 (on vulkan)
    Compiling Vertex program with CAMERA_ORTHOGRAPHIC PLANE_XY TEXTUREFOG
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_RGBM_ENCODING

    Any idea?

    edit - also happens in a clean project
     
    Last edited: Dec 20, 2018
  50. crltools

    crltools

    Joined:
    Jun 2, 2014
    Posts:
    1
    So I've been having trouble building to android.
    First, FogOfWarShader.shader had two subsequent "undeclared identifier" errors while attempting to compile. I fixed them by:

    1. since
    _CameraWS 
    was the first identifier in question, I commented out lines 161-166 (I'm not using TEXTUREFOG so my assumption was that this would be ok).

    2. then, the same complaint arose about
    fogcolor
    on line 168. So I commented that out and pasted it to line 178 (just above the
    sceneColor 
    declaration).

    After this, the project worked normally in the editor and I was able to build to an android phone BUT, on the phone it appears the be rendering the z-axis flipped. I wasn't able to ascertain if this is related to the "
    UNITY_REVERSED_Z
    macro" issue mentioned above as I'm almost completely inexperienced writing shaders.

    Suggestions?
     
    Last edited: Dec 20, 2018
    DoritoDog likes this.