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. Fdudka

    Fdudka

    Joined:
    Jul 27, 2015
    Posts:
    15
    Hi! I have a new question, it's about the FogOfWarSecondary ! I'm working on a network game so I have two players "at least" that each have a FogOfWar Component attach to their cameras.

    As usual, I disable many components on the non-local player including the FogOfWar, camera, etc.

    my problem comes in with the mini map that is using the the FogOfWarSecondary. I cannot get the mini map to work. It looks like its completely foggy on one side. Strangely enough, it's on the host side that it's not working. Both teams are set to 0 on their side and non-local units are set to team 1 (both on client and server).

    I read in the manual :
    FogOfWarSecondary has no values to change in the inspector. It must be on a camera gameobject,
    and will reuse the fog from the primary FogOfWar camera in the scene.

    Can you explain what is the meaning of "primary"?
    Do i have to manually set the "right "camera to the FogOfWarSecondary to a static hidden FoW component that i don't see?

    in my case i got many FogofWar Components since i have many instance of players. Just some of them are turn off.

    Thanks
     
  2. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    The FogOfWar component is considered the "primary" fog of war, and the FogOfWarSecondary will mimic it (ie the secondary displays anything that the primary displays). The FogOfWarSecondary will only copy the FogOfWar that has the same Team ID as it has. So, I guess the first thing to test would be if the primary FogOfWar is displaying the correct fog and has the right team ID. If it is, then the secondary should be fine.

    Btw, I'm not sure what you mean by "It looks like its completely foggy on one side".
     
  3. Fdudka

    Fdudka

    Joined:
    Jul 27, 2015
    Posts:
    15
    Hi, Thank you for helping me out!

    It looks like its completely foggy on one side :
    That wasn't clear from me, sorry.. on one of the computers (int my case, the host).




    If there are multiple primary FogofWar in the scene, how does the FogOfWarSecondary decides which FogOfWar it will copy?

    - The FogOfWarSecondary is part of the Scene since it's use for the mini-map. It cannot be a children of the player since the player moves around and not the camera for the mini-map.

    - However, each player must have their own FogOfWar attach to the camera that sits as a Children of the player himself.

    I fixed the issue adding a new function.

    public void SetNewCurrent() {
    current = GameManager.localPlayer.GetPlayerCam ().GetComponent<FogOfWar> ();
    Reinitialize();
    }

    The "current" value is static. The FogOfWarSecondary was using the "current" of the latest player joining the scene.

    void OnRenderImage(RenderTexture source, RenderTexture destination) {
    FogOfWar.current.RenderFog(source, destination, _camera, _transform);
    }
     
  4. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    Your images seem to be down :(

    Both FogOfWar and FogOfWarSecondary components have a Team value on it. These need to be the same for the FogOfWarSecondary to copy it (that is how it knows which one to copy).

    I think you might have an old version of FogOfWar? Have you tried updating to the latest?
     
  5. curiousbrandon

    curiousbrandon

    Joined:
    Sep 22, 2017
    Posts:
    6
    Great asset, thanks! Works straight out of the box.

    One quick question: I want to mask the previously explored area after the unit has left the area. What's the best way to do this? Essentially, instead of showing part of the fog, I want the area to go back to have the full fog. Is there a place we can change the settings for this?

    Thanks again!
     
  6. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    On the Fog Of War component there is a Partial Fog Amount slider that will do this. Put it all the way up to one and it should do what you want.
     
    curiousbrandon likes this.
  7. MadMimic

    MadMimic

    Joined:
    Jan 23, 2013
    Posts:
    8
    Hello I have one question. We are making a 2d RTS top down game and we'll need a FOW system. Every tutorial I've found used Terrain to set the fog of war, but in our case we are using Quads, ands Sprites to render the ground. My question is, does this plugin work in our setting?

    Thanks,
     
  8. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    Fog of War does not require any type of terrain to be in the scene. To block line of sight, it requires colliders (2D or 3D) to be put on walls, but other than that, there are no restrictions on how you implement your scenes.
     
  9. SiliconAvatar

    SiliconAvatar

    Joined:
    Mar 23, 2014
    Posts:
    48
    Hi. I bought this a while back and finally got around to using it in earnest.

    Have you thought about adding a flood fill style fog of war to the mix?

    Click the link and scroll down to the visibility section for a read.

    http://kittylambda.com/index.php?p=essay_ultima_vi

    I use this asset in conjunction with Super Tilemap Editor. I got your FOW asset to work with it for the most part.

    I'd like to see some more options for the lighting. There are many resources out there for various types of lighting and levels of permissiveness in roguelike lighting out there, for example. It'd be neat to be able to choose between a few options. Other expansion opportunities lie in colored tile lighting, flickering firelight, rotating lights (lighthouse style), etc. This asset could expand in many ways. I hope you keep it going.

    Thanks.
     
  10. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    FogOfWar wasn't initially created with lighting in mind which is why there aren't too many features for it, but I'd like to explore it more and see if it will work with the package. I have put flood filling on my to-do list as it doesn't seem too difficult to pull off.

    Flickering and rotating lights I would leave up to the user to developer (a script to rotate the transfotm should be easy enough). I'll have to think about colored lights though. Could be tricky.

    Thanks for the suggestions!
     
    SiliconAvatar likes this.
  11. SiliconAvatar

    SiliconAvatar

    Joined:
    Mar 23, 2014
    Posts:
    48
    Cool. Thanks.

    I have a question -

    I'm creating a 2D game (think Ultima or Roguelike) and I'm wondering if there is a way to make an NPC not affect the fog at all. I don't want it to add to the player's field of vision or block the player's field of vision. It has to have a collider so I can raycast to it for other purposes. I tried a lot of different team settings, messed with the line of sight mask, etc. and never got it working that way.

    All I was able to figure out was to give it a 3D collider. I can still raycast to that and your FOW isn't script affected by it in my project.
     
  12. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    I'm not sure I understand. Can you just not have a FogOfWarUnit script on the NPC and then just put it on another layer so FoW doesn't detect it?
     
  13. SiliconAvatar

    SiliconAvatar

    Joined:
    Mar 23, 2014
    Posts:
    48
    Yes, I got it - thank you. Another script was causing confusion. It's all sorted now.
     
  14. ch0kk0

    ch0kk0

    Joined:
    Jan 20, 2016
    Posts:
    36
    anyone using this with Aquas ?

    Building a RTS type scenario with terrain/islands and Aquas for the water.

    Fog of war follows the terrain under the water which looks a bit weird. I am wanting it to sit flat on surface of water instead.
     
  15. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    Maybe you could just clear all of the fog where the water is? The FogOfWar component has a SetFog() method that you can use to do this.
     
  16. LoveApril

    LoveApril

    Joined:
    Mar 11, 2018
    Posts:
    4
    hi.I have a simple question. fog of war can be assigned 150 units or 200 units?
     
  17. Zaeran

    Zaeran

    Joined:
    Dec 14, 2012
    Posts:
    11
    Hey there!

    I'm wondering if there's a simple way to paint reveal the fog of war, instead of using line of sight. I realise I could put down a bunch of invisible units, but that's definitely not an ideal solution.

    Would it be best to just directly assign the revealed pixels based on the brush location?
     
  18. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    There are no limits to the number of units you can have, but more units will have more of an impact on performance.

    You have access to the individual pixels of the fog texture by using the FogOfWar.fogValues. This is a byte array where 0 is unfogged and 255 is fully fogged. Changing this will give you full flexibility over the fog. But using FogOfWarUnits will yield better performance as it can be multithreaded.
     
  19. Zaeran

    Zaeran

    Joined:
    Dec 14, 2012
    Posts:
    11
    Thanks for the suggestions!

    It turns out there was a much simpler solution. Because we don't need the fog to re-cover unless a user paints it back, be just set the Partial Fog Amount value to zero, and move one unit around the map to act as a brush.
     
  20. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    Awesome! Glad you got it working!
     
  21. Appminis

    Appminis

    Joined:
    Apr 12, 2014
    Posts:
    141
    Love the asset - one question:

    For an RTS, how to make the fog of war a collision area? For example, you shouldn't be able to place a building in an area that is covered in fog of war.

    Thanks!
     
  22. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    The FogOfWar component has a bunch of functions in it such as IsInFog() and GetFogValue() that you can use to achieve this. There is also VisibilityOfArea() which does a rectangle. These functions are super fast, so feel free to do many checks per frame!
     
  23. Appminis

    Appminis

    Joined:
    Apr 12, 2014
    Posts:
    141
    Great, thanks!
     
  24. Appminis

    Appminis

    Joined:
    Apr 12, 2014
    Posts:
    141
    Another question - the fog of war is obscuring my uGUI panels. How do we exclude that?
     
  25. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    I'm not sure about uGUI, but Unity's canvas UI will render on top of the fog.
    I'm guessing that uGUI is putting the UI in world space. If this is the case, you should be able to put another camera in the scene that renders after your main camera (ie give the new camera a higher depth value), then get uGUI to render to that camera instead.
     
  26. Appminis

    Appminis

    Joined:
    Apr 12, 2014
    Posts:
    141
    I mean Unity UI when I say uGui :)

    So my Unity GUI canvas is being blocked:


    Here are my Canvas settings:


    Master canvas (parent):
     
  27. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    Oops. That's embarrassing:confused:

    I think in screen space - camera, Unity will render the UI before image effects. This results in the fog being over the top (because FogOfWar is implemented as an image effect). If you don't want to switch your canvas to Screen Space - Overlay, they you will need to use another camera that just renders the UI. This is easy to setup:
    • Make a new camera as a child object of your main camera
    • Change this camera's depth value to be higher than the main camera
    • Point your canvas to the new camera
     
  28. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    148
    Hey,

    Just I want to know if this package is good enough for android devices or not ? because I've tested FogOfWarTestScene on my PC by Unity 2018.1 (My graphic card is Nvidia GTX 970). FPS is not really good. Between 24 and 33. (And I know it i definitely not good for android devices with this FPS on this kind of PC)

    This is Fog Of War script:

    upload_2018-5-5_12-59-59.png

    Also by these settings I see jagged edges, I know I can increase map resolution but it breaks FPS
     
    Last edited: May 5, 2018
  29. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    For Android, you probably want to tick the multithreaded button at the bottom of the FogOfWar component. This will definitely improve performance on larger maps and might make it easier to reduce those jaggies too.

    The next update will include fading fog in and out which will greatly reduce the jaggies in most situations.
     
    blitzvb likes this.
  30. Appminis

    Appminis

    Joined:
    Apr 12, 2014
    Posts:
    141
    Thanks!
     
  31. 3meish

    3meish

    Joined:
    Sep 20, 2015
    Posts:
    4
    Hello, i have a question: Can i achieve something like this?

    Just show the environment element but not the hidden player like DoTA2 FOW?
    Thanks.
     

    Attached Files:

  32. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    Yes! There is a HideInFog component that you can put on your units. This will hide the Renderer when they move out of the fog.
     
  33. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    148
    It doesn't really help.

    I've changed my settings on PC and it is good on it but on my android device (Honor 8 Lite) it is around 2 or 3 FPS even by checking multithread checkbox.

    This is the setting:

    upload_2018-5-21_19-31-25.png

    upload_2018-5-21_19-31-47.png

    Even I've changed setting to 128 x 128 map resolution and disabled blur by changing Blur Amount and Blur Iterations to 0 but doesn't help that much. Maybe by these settings around 20 FPS
     
  34. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    You are definitely trying all the right things. Unfortunately, I don't have the phone you are testing on, but I have tested on android phones that are much less and get much better performance than you are reporting, which makes me think that this phone is an oddity (which happens more often than I'd like with android...).

    Is there any chance you could profile on the device and email me the results?
     
  35. LoveApril

    LoveApril

    Joined:
    Mar 11, 2018
    Posts:
    4
    Hey, I've been using assets for a while. It's great, but I have a simple question. I have 100 units. The frame count is low. I try to adjust the low setting. 128x128 map resolution and disabled blur by changing Blur Amount and Blur Iterations to 0 and multithreaded, but the effect is still not good. And it was tested on my computer. I read the document.
    You say: Why does the fog appear to update at a slow frame rate? You can easily change the update frequency in the FogOfWar component. Updating the fog every frame for every unit can have a massive performance impact. To combat this, units update the Fog individually. After a set amount of time, the fog is rendered and then the loop starts again.
    But I do not know where to adjust, is it in the fog of war update? Sorry, my main language is not English. If I can get help, thank you very much!
     
  36. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    It's the Max Milliseconds Per Frame at the very bottom of the FogOfWar component. You should set this to be lower if you want better performance. I'll have to make this more obvious in the documentation. Thanks for pointing this out!
     
  37. LoveApril

    LoveApril

    Joined:
    Mar 11, 2018
    Posts:
    4
    I am very touched by your quick reply. Thank you very much! The problem has been solved very well. I have the last question. I set multithreading to 8 on my computer. What happens if I pack the game and run the game on another computer with only 4 threads? Will it automatically become 4 threads? What I mean is if I have multi-threaded set to 8, but the computer only has 4 threads what happens, thank you
     
  38. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    I have left managing the thread count up to developers, as different projects may require a different amount of threads (for example, you may want to use other threads for something else, and don't want FoW to hog it all).

    If you do want to control it automatically at runtime, you can get your system's processor count from System.Environment.ProcessorCount. You can easily change the threads used by FoW by changing the FogOfWar.threads variable. It should take affect on the FogOfWar's next Update() call.
     
  39. douglassophies

    douglassophies

    Joined:
    Jun 17, 2012
    Posts:
    96
    Hey, i was trying to find out about culling. Am i right in saying that there is no culling feature for this? How easy would it be to add one? I guess frustum culling will do most of the work but i think this would help often.
     
  40. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    What is it that you're trying to cull? Culling units that are hidden by fog? There is a HideInFog component that will do exactly this for you.
     
  41. douglassophies

    douglassophies

    Joined:
    Jun 17, 2012
    Posts:
    96
    Thanks for the reply. HideInFog works well for animated objects like animals that should be hidden in the partial fog (vs the absolute black of unexplored areas) but its a nuisance to put on every single object in the game. There is no way to default tell things not to render if they are hidden by the black absolute fog?
     
  42. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    It wouldn't be too hard to write a script that attached a HideInFog to every renderer in the scene.

    The reason why I haven't implemented this is because it is quite game-specific. Quite a lot of devs want to have some objects that appear over the fog while other objects don't. Some devs also do some funky things with world-space UI that can cause it to be rendered under the fog, which is not usually what they want. So the problem is a bit trickier than it seems. But I'm happy to take any ideas to help solve the problem.
     
  43. douglassophies

    douglassophies

    Joined:
    Jun 17, 2012
    Posts:
    96
    Ok thanks for your replies. With frustrum culling, it should only render what is under the fog when i move the camera to look at a patch of fog and in that case, it wont be rendering anything else so the amount of rendering should be relatively constant with only the occasional minimal gains. At least that is my prediction. Will leave this topic alone for now unless i realise i missed something.
    Thanks again!
     
  44. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    FoW does not override unity's frustum culling. Sorry if I'm not understanding you correctly :confused:
     
  45. douglassophies

    douglassophies

    Joined:
    Jun 17, 2012
    Posts:
    96
    Ah yes, i don't think i wrote that very clearly... I am essentially saying it maybe doesn't matter much in terms of performance gains. I have to be able to render every "cameras view worth" of map space anyway so if there is a highly demanding view, i might see gains by having some of it not rendered due to FoW but when the player clears the FoW, i have to render it anyway. So either way, i have to make sure the view is renderable at a good FPS. There will be bits just off camera that could be saved by not rendering for FoW, but most of that will be not rendered due to frustum culling. If i am right about this, its not worth my (or your) time to develop a FoW culling further.
     
  46. mkgame

    mkgame

    Joined:
    Feb 24, 2014
    Posts:
    583
    Unity 5.6.6f2

    Hi, I bought your asset and I cannot use it. The Performance is terrible, even with threads. I got in my RTS game about +40ms more frame time (75% of the frame is used by automatic update). Alone the FoW script on the camera kills the performance. Any idea, did you test it on Unity 5.6.X ?
     
  47. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    Hi mkgame,

    What device and unity version are you testing on, and how many threads have you been testing with? Also, what map resolution do you have FoW set to, and how many units do you have in the scene? Are the performance issues also happening with the sample scene?
     
  48. mkgame

    mkgame

    Joined:
    Feb 24, 2014
    Posts:
    583
    Unity 5.6.6f2

    Hi, the first part of the load in the profiler is the load in your first example. The last high load part is because of the deep profiling. Right side in the inspector the settings in FoW is visible. The rest of the load burns on the UI part of your script, but that is not important.

    FoW_PerformanceIssue.jpg

    Edit: Code is not well optimized. I had to optimize this 40+ ms for my 2000x2000 terrain. But the fading loop kills at the end the frame rate by looping 4000000 times. This part is then not threaded. To fix this is difficult. I would recommend to pre-calculate the faded area and the visible area in the thread and then paint it in the update method, if the calculations have been finished. However, the better solution would be to update units by the distance they moved and then just the units which are really moved. Something like update(Vector3 oldPos, Transform unit)...

    And try to avoid such big performance mistakes (got about 10ms less CPU time):

    Code (CSharp):
    1.  public override void GetValues(byte[] outvalues)
    2.         {
    3.             //for (int i = 0; i < _values.Length; ++i)
    4.             //    outvalues[i] = _values[i];
    5.  
    6.             Array.Copy (_values, outvalues, _values.Length);
    7.         }
     

    Attached Files:

    Last edited: Sep 2, 2018
  49. mkgame

    mkgame

    Joined:
    Feb 24, 2014
    Posts:
    583
    There is a bit more. In UFoW the shader on your mesh renderer (on all visible objects in your scene) must be changed or the shader you use must be patched. I made my own shader with Shader Forge and that is not so simple to patch. I hope that I understood that correctly. Not sure what happens, if I don't change the shader on the mesh renderers. Maybe I lost a feature, or it will not work, not sure, I don't have it yet.

    I hope I can fix the performance issue in this asset, because the result is okay. However, I would like an animated cloud texture, not the single colorized shadow, but it is still okay.
     
  50. stu_pidd_cow

    stu_pidd_cow

    Joined:
    Aug 4, 2014
    Posts:
    116
    The latest build (which will be released sometime soon hopefully) will change the way fading works and may have some performance boost. If you pop me an email, I can send you a unitypackage to test it. Single threading should never be used on large resolution map.
    I will definitely put in Array.Copy(), thanks for pointing this out!

    I have always wanted to give more flexibility with the shader rendering to allow for features like this, but I am yet to come up with a simple way to do it. I will definitely consider any ideas you have :)