Tasharen Fog of War

Discussion in 'Assets and Asset Store' started by ArenMook, Aug 8, 2012.

  1. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    [​IMG]

    NOTE: This package requires Unity Pro.

    Ever wanted high-quality smooth fog of war for your strategy game? How about a line-of-sight revealing system for a heist game like Monaco? Well, now you can have it in 3 simple steps:
    1. Drag drop the Fog of War prefab into your scene. You can find this prefab in Assets/Fog of War/FoW folder.
    2. Add FOWEffect script to your Main Camera. Make sure it references the "Image Effects/Fog of War" shader.
    3. Add FOWRevealer script to one or more game objects in your scene. It's best to add it to a game object located at around the unit's "eye level" in order for the fog of war to be accurate.
    4. Running on a mobile device or don't have Pro? You can adapt your object and environment shaders to eliminate the need for an image effect, making it perfectly suitable for mobile devices: http://forum.unity3d.com/threads/146697-Tasharen-Fog-of-War?p=1022831&viewfull=1#post1022831

    What makes it great?
    • It's fast. If you already render to depth (for example using Tasharen Water), then you likely won't even notice a performance drop. In fact, with proper use of FOWRenderers hiding renderers hidden by fog you can actually increase performance.
    • All line-of-sight calculations are done on a separate thread, so they don't affect your game's performance.
    • It's smooth -- and level of smoothness also doesn't affect performance as it's done on another thread.
    • You can specify how often it updates and how quickly it fades in and out, and the two work independently. Updating only twice per second will still give you perfectly smooth fading results.
    • You can easily check to see if a point is visible, explored, or not.
    • You can optimize the worker thread's performance even further by specifying that certain line-of-sight checks should only be performed once (stationary object).
    • You get a script that can automatically hide all renderers under the game object that's hidden from view (FOWRenderers).

    Play the Demo: http://www.tasharen.com/windward/fow/
     
    Last edited: Nov 19, 2012
  2. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
  3. shiva.js

    shiva.js

    Member

    Joined:
    Oct 5, 2011
    Messages:
    13
    Great plugin. I had it integrated and working well within an hour.
    For anyone interested in this plugin for mobile devices, I opted to modify my environment shaders to lookup the fog textures instead of using the provided image effect (there are issues with sampling depth here anyway, though workarounds exist if you need to persist for whatever reason).

    Top quality, highly recommended!
     
  4. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    That's actually pretty clever. It's true, if you can modify the environment / unit shaders to read the fog texture, you get fog of war without the image effect, and basically completely free as far as performance cost goes.
     
  5. ImogenPoot

    ImogenPoot

    New Member

    Joined:
    Jul 2, 2012
    Messages:
    214
    Your assets kind of makes one wanna create a strategy game instead of a shooter-like one ^^. This is definitely on the radar as well as the water, when I finished my current project I am definitely going to make a strategy game.
     
  6. illumina

    illumina

    New Member

    Joined:
    Aug 13, 2012
    Messages:
    4
    I'm finding a bizarre issue where running in the editor by pressing "play" can execute without having a compiled shader (I think). It seems to just cause the whole screen to fade in/out instead of jus the area on the map that is revealed.

    Have you seen this happening? In particular I have TWO unity editor open when playing (networked game). Not sure if this happens with one editor open only. Building the game to run in a player seems to fix the problem for a little while. Not sure what I'm doing that breaks it again...
     
  7. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    Nope, can't say I've ever seen that. Is it one of Unity 4's issues?
     
  8. illumina

    illumina

    New Member

    Joined:
    Aug 13, 2012
    Messages:
    4
    Nope, Unity 3.5.5. I saw it happening with the example scene as well. Is there some kind of step that needs to happen in order to make sure the shaders are always compiled before running in preview from the editor?
     
  9. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    Unity's bottom right corner has a spinner when something is compiling, although I'm not sure if it works properly for shaders.
     
  10. illumina

    illumina

    New Member

    Joined:
    Aug 13, 2012
    Messages:
    4
    Looks like it has to do with running two unity instances. It seems that unity occasionally deletes the temp files (including compiled shaders) and gets confused across instances. Bummer.

    Anyway, the fog of war system is awesome. Trying to figure out the nuances of how it works... mostly so far so good. Of course documentation would be nice :)

    Thanks for putting this together. NGUI is awesome too.
     
  11. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    I'll make a video when I have some time. Unfortunately I'm stupidly busy until October.
     
  12. illumina

    illumina

    New Member

    Joined:
    Aug 13, 2012
    Messages:
    4
    Found a bug, do you have some kind of tool you use for reporting them?

    Basically, in FOWSystem.RevealIntoCache I have a situation where my cachedBuffer isn't square. Looks like it's due to having everything at funky floating point locations in the world. Getting a hard crash expecting to have 7 rows as opposed to 6.
     
  13. AdamWaters

    AdamWaters

    New Member

    Joined:
    Aug 30, 2011
    Messages:
    92
    ArenMook,

    Does this fog work with multiple players/cameras? I'm not sure if multiplayer was built into your game or not.

    Thanks in advance
     
  14. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    @illumina: not square? It's created as "size * size", which is always square. What do you mean by funky floating point locations?

    @AdamWaters: The fog is drawn as an image effect, so any camera with that effect on it will have the fog.
     
  15. radarpq

    radarpq

    New Member

    Joined:
    Mar 18, 2011
    Messages:
    4

    Hi @ArenMook @shiva.js ,

    I bought this plugin this morning, the integration time is great , but I got trouble for mobile devices.
    The image effect shader is really a frame killer and I can't follow your solution in the quote above.

    Could you explain the solution in detail , please?
     
  16. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    Just pasting my email response, for reference:
     
  17. shiva.js

    shiva.js

    Member

    Joined:
    Oct 5, 2011
    Messages:
    13
    Hope this helps...


    add the following to your environment shader/s:

    Code (csharp):
    1. sampler2D _FogTex0;
    2. sampler2D _FogTex1;
    3.  
    4. uniform float4 _Params;
    5. uniform half4 _Unexplored;
    6. uniform half4 _Explored;
    still in your shader, modify your vertex to fragment struct to include fog coords, kinda like:

    Code (csharp):
    1. struct v2f_surf
    2. {
    3.   float4 pos : SV_POSITION;
    4.   float2 uv0 : TEXCOORD0;
    5.   float2 lmap : TEXCOORD1;
    6.   float2 fog : TEXCOORD2; // this is the new line to add
    7. };
    add the following to your vertex function:

    Code (csharp):
    1. float4 worldPos = mul (_Object2World, v.vertex);
    2. o.fog.xy = worldPos.xz * _Params.z + _Params.xy;
    add the following to your fragment function:

    Code (csharp):
    1. half4 fog = lerp(tex2D(_FogTex0, IN.fog), tex2D(_FogTex1, IN.fog), _Params.w);
    2. c = lerp(lerp(c * _Unexplored, c * _Explored, fog.g), c, fog.r); // where c is the fixed4 that you will return

    now, in FOWEffect.cs, replace the function with this one (note it's now a regular MonoBehaviour using OnPreRender):

    Code (csharp):
    1. {
    2.     if (mFog == null)
    3.     {
    4.         mFog = FOWSystem.instance;
    5.         if (mFog == null) mFog = FindObjectOfType(typeof(FOWSystem)) as FOWSystem;
    6.     }
    7.  
    8.     if (mFog == null || !mFog.enabled)
    9.     {
    10.         enabled = false;
    11.         return;
    12.     }
    13.  
    14.     float invScale = 1f / mFog.worldSize;
    15.     Transform t = mFog.transform;
    16.     float x = t.position.x - mFog.worldSize * 0.5f;
    17.     float z = t.position.z - mFog.worldSize * 0.5f;
    18.     Vector4 p = new Vector4(-x * invScale, -z * invScale, invScale, mFog.blendFactor);
    19.  
    20.     Shader.SetGlobalColor("_Unexplored", unexploredColor);
    21.     Shader.SetGlobalColor("_Explored", exploredColor);
    22.     Shader.SetGlobalVector("_Params", p);
    23.     Shader.SetGlobalTexture("_FogTex0", mFog.texture0);
    24.     Shader.SetGlobalTexture("_FogTex1", mFog.texture1);
    25. }
    Let me know how it goes :)
     
  18. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    Thank you for sharing :D
     
  19. radarpq

    radarpq

    New Member

    Joined:
    Mar 18, 2011
    Messages:
    4
    Thank you, @shiva.js!

    I've make the plugin work on the ShadowGun Sample with your solution.
    Running great on the mobile device.

    [​IMG]
     
  20. hy.cn

    hy.cn

    New Member

    Joined:
    Nov 19, 2012
    Messages:
    1
    The demo looks great. I have a question here, can I use the FOW texture for minimap? Or the FOW texture is only for camera view?
     
  21. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    Sure you can. The Fog of War creates a texture -- you can use this texture anywhere. Just look at what I did with it in Windward: http://www.tasharen.com/windward/
     
  22. frarees

    frarees

    Member

    Joined:
    Jun 8, 2012
    Messages:
    21
    Had no success with the mobile approach @shiva.js posted. I've written a shader, and did what he says, but nothing happened (also, no errors or warnings). Any help on this topic? An attached demo project also would help, if someone has already tested it. Thanks!
     
  23. jkrassman

    jkrassman

    New Member

    Joined:
    Dec 18, 2012
    Messages:
    20
    Hi, nice stuff!

    One question, but I am unsure how to explain it - but I will give it a go...

    Is it possible to let places that has been in sight before, still to be visible. So when the player do progress in the game world, it is getting more and more visible?

    Joakim
     
  24. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    Yup, just set the explored color to white.
     
  25. jkrassman

    jkrassman

    New Member

    Joined:
    Dec 18, 2012
    Messages:
    20
    Sounds great!

    Is there any documentation within the package or any videos around?

    Joakim
     
  26. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    It's all in code for now -- well-commented as expected. You reminded me that I should make a video for it though...
     
  27. jkrassman

    jkrassman

    New Member

    Joined:
    Dec 18, 2012
    Messages:
    20
    And maybe a sample that has the modification for mobile versions? :)
     
  28. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    Maybe!
     
  29. jkrassman

    jkrassman

    New Member

    Joined:
    Dec 18, 2012
    Messages:
    20
    Ok, since you almost promised that - I will buy it instantly, and flood this thread with questions!

    Joakim
     
  30. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
  31. jkrassman

    jkrassman

    New Member

    Joined:
    Dec 18, 2012
    Messages:
    20
    Cant get past your verification page when registering to the forum....how hard can it be? :)
     
  32. jkrassman

    jkrassman

    New Member

    Joined:
    Dec 18, 2012
    Messages:
    20
    Okey, so I did change the explored color to white and got what I wanted. So far so good!

    Next step when I try to follow the small tutorial on the previous page to make it mobile.

    Where can I find these? I guess that I have to open up some compiled shader?

    Joakim
     
  33. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    Download the built-in shaders: http://unity3d.com/unity/download/archive

    Open up whichever shader you are using (for example "Diffuse"), and create a version of it that will sample the fog of war texture inside. That said, it's an intermediate difficulty task.
     
  34. jkrassman

    jkrassman

    New Member

    Joined:
    Dec 18, 2012
    Messages:
    20
    Ok, and I have to do that for all my shaders I am using in the game? Or is it for some shader that the FOW uses?

    Joakim
     
  35. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    For whatever you want affected by the fog of war.

    Keep in mind the Fog of War works perfectly fine as a post-process image effect (desktop, non-mobile), so only do this if you need fast fog of war on mobiles.
     
  36. Hephaestus

    Hephaestus

    New Member

    Joined:
    Jan 7, 2012
    Messages:
    1
    ArenMook - Any chance that you've created that example mobile shader? I just bought Tasharen FOW today to play with, and as I'm a complete shader noob, whipping up a version based on the basic Diffuse shader is proving to be well beyond me at the moment.
     
  37. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    I am at GDC right now, but I might be able to whip up a quick one after.
     
  38. NnelGdaBandit

    NnelGdaBandit

    Member

    Joined:
    Dec 21, 2012
    Messages:
    10
    This looks great, I'm currently looking for a FOW system and this looks to be what I need. Though I'll probably hold out until the mobile shader is included in the package. Looks sweet though!
     
  39. Jashengmato

    Jashengmato

    Member

    Joined:
    Mar 23, 2013
    Messages:
    6
    Thanks a lot! @shiva.js
    For those who is not good at shader scripting, here's a basic shader compatible with FOW system to get start with!

    Code (csharp):
    1.  
    2. // Unlit shader. Simplest possible textured shader.
    3. // - no lighting
    4. // - no lightmap support
    5. // - no per-material color
    6.  
    7. Shader "Jash/TextureWithFr0g"
    8. {
    9. Properties {
    10.     //_Color ("Main Color", Color) = (1,1,1,0.5)
    11.     _MainTex ("Texture", 2D) = "white" { }
    12. }
    13. SubShader {
    14.     Pass {
    15.  
    16. CGPROGRAM
    17. #pragma vertex vert
    18. #pragma fragment frag
    19.  
    20. #include "UnityCG.cginc"
    21.  
    22. float4 _Color;
    23. sampler2D _MainTex;
    24.  
    25. //begin regin FOG_OF_WAR
    26. sampler2D _FogTex0;
    27. sampler2D _FogTex1;
    28. uniform float4 _Params;
    29. uniform half4 _Unexplored;
    30. uniform half4 _Explored;
    31. //end regin
    32.  
    33. struct v2f {
    34.     float4  pos : SV_POSITION;
    35.     float2  uv : TEXCOORD0;
    36.    
    37.     float2 fog : TEXCOORD2; // this is the new line to add
    38. };
    39.  
    40. float4 _MainTex_ST;
    41.  
    42. v2f vert (appdata_base v)
    43. {
    44.     v2f o;
    45.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    46.     o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
    47.    
    48.     float4 worldPos = mul (_Object2World, v.vertex);
    49.     o.fog.xy = worldPos.xz * _Params.z + _Params.xy;
    50.    
    51.     return o;
    52. }
    53.  
    54. half4 frag (v2f i) : COLOR
    55. {
    56.     half4 c = tex2D (_MainTex, i.uv);
    57.    
    58.     half4 fog = lerp(tex2D(_FogTex0, i.fog), tex2D(_FogTex1, i.fog), _Params.w);
    59.     c= lerp(lerp(c * _Unexplored, c * _Explored, fog.g), c, fog.r); // where c is the fixed4 that you will return
    60.    
    61.     return c;// * _Color;
    62. }
    63. ENDCG
    64.  
    65.     }
    66. }
    67. Fallback "VertexLit"
    68. }
    69.  
     
  40. GCat

    GCat

    Member

    Joined:
    Jul 31, 2012
    Messages:
    86
    the mobile shader seems not to work now
     
  41. cristian_ritter

    cristian_ritter

    New Member

    Joined:
    Aug 11, 2011
    Messages:
    37
    How to convert this shader of surface to vertex and fragment method?

    Code (csharp):
    1.  
    2. Shader "Nature/Terrain/Bumped Specular" {
    3. Properties {
    4.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    5.     _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    6.  
    7.     // set by terrain engine
    8.     [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {}
    9.     [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
    10.     [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
    11.     [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
    12.     [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
    13.     [HideInInspector] _Normal3 ("Normal 3 (A)", 2D) = "bump" {}
    14.     [HideInInspector] _Normal2 ("Normal 2 (B)", 2D) = "bump" {}
    15.     [HideInInspector] _Normal1 ("Normal 1 (G)", 2D) = "bump" {}
    16.     [HideInInspector] _Normal0 ("Normal 0 (R)", 2D) = "bump" {}
    17.     // used in fallback on old cards  base map
    18.     [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {}
    19.     [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
    20. }
    21.    
    22. SubShader {
    23.     Tags {
    24.         "SplatCount" = "4"
    25.         "Queue" = "Geometry-100"
    26.         "RenderType" = "Opaque"
    27.     }
    28. CGPROGRAM
    29. #pragma surface surf BlinnPhong vertex:vert
    30. #pragma target 3.0
    31.  
    32. void vert (inout appdata_full v)
    33. {
    34.     v.tangent.xyz = cross(v.normal, float3(0,0,1));
    35.     v.tangent.w = -1;
    36. }
    37.  
    38. struct Input {
    39.     float2 uv_Control : TEXCOORD0;
    40.     float2 uv_Splat0 : TEXCOORD1;
    41.     float2 uv_Splat1 : TEXCOORD2;
    42.     float2 uv_Splat2 : TEXCOORD3;
    43.     float2 uv_Splat3 : TEXCOORD4;
    44. };
    45.  
    46. sampler2D _Control;
    47. sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    48. sampler2D _Normal0,_Normal1,_Normal2,_Normal3;
    49. half _Shininess;
    50.  
    51. void surf (Input IN, inout SurfaceOutput o) {
    52.     fixed4 splat_control = tex2D (_Control, IN.uv_Control);
    53.     fixed4 col;
    54.     col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0);
    55.     col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1);
    56.     col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2);
    57.     col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3);
    58.     o.Albedo = col.rgb;
    59.  
    60.     fixed4 nrm;
    61.     nrm  = splat_control.r * tex2D (_Normal0, IN.uv_Splat0);
    62.     nrm += splat_control.g * tex2D (_Normal1, IN.uv_Splat1);
    63.     nrm += splat_control.b * tex2D (_Normal2, IN.uv_Splat2);
    64.     nrm += splat_control.a * tex2D (_Normal3, IN.uv_Splat3);
    65.     // Sum of our four splat weights might not sum up to 1, in
    66.     // case of more than 4 total splat maps. Need to lerp towards
    67.     // "flat normal" in that case.
    68.     fixed splatSum = dot(splat_control, fixed4(1,1,1,1));
    69.     fixed4 flatNormal = fixed4(0.5,0.5,1,0.5); // this is "flat normal" in both DXT5nm and xyz*2-1 cases
    70.     nrm = lerp(flatNormal, nrm, splatSum);
    71.     o.Normal = UnpackNormal(nrm);
    72.  
    73.     o.Gloss = col.a * splatSum;
    74.     o.Specular = _Shininess;
    75.  
    76.     o.Alpha = 0.0;
    77. }
    78. ENDCG  
    79. }
    80.  
    81. Dependency "AddPassShader" = "Hidden/Nature/Terrain/Bumped Specular AddPass"
    82. Dependency "BaseMapShader" = "Specular"
    83.  
    84. Fallback "Nature/Terrain/Diffuse"
    85. }
    86.  
     
    Last edited: Jun 3, 2013
  42. pudd1nG

    pudd1nG

    New Member

    Joined:
    Feb 21, 2012
    Messages:
    22
    Hi ArenMook,

    I've just bought the package and I'm wondering if you have any idea to why my player is revealing through objects? It seems no matter what I do it only works on terrain :s

    Image example attached :)

    Cheers for all your work!

    $VKaDG3Z.jpg
     
  43. ArenMook

    ArenMook

    Member

    Joined:
    Oct 20, 2010
    Messages:
    1,902
    Your walls need to be set up so that they are on the layer that fog of war actually hits, otherwise the fog simply won't know that the walls are there. You also have to make sure that your min/max range for the FOWRevealer is set up correctly. Everything will always be visible within the minimum distance, whether it's blocked by something or not (so it should be set to something small -- generally the "melee" range).

    I also advise asking questions here -- http://www.tasharen.com/forum/index.php?board=6.0 -- as I don't check Unity forums that often.
     
  44. Will Chen

    Will Chen

    Member

    Joined:
    May 6, 2013
    Messages:
    7
    Hello every one !

    If you need to run this project on Mobile .I have another way.
    And it's very simple , you don't need even change the shader.
    I used two days to find the problem,wish it to help you! ^ ^

    FIRST STEP: (In FOWSystem.cs)

    Try to changed the
    function into
    SECOND STEP: (In FOWEffect.cs)

    try to add this code in Function OnRenderImage()


    That's ok ,you can try to run it on mobile now. :)

    I think the error for mobile is caused by the conflicts of Update() and OnRenderImage()
     
    Last edited: Jul 4, 2013
  45. Will Chen

    Will Chen

    Member

    Joined:
    May 6, 2013
    Messages:
    7
    Does anyone know how to promote the project's performance ? when I run this fog of system in Ipad Mini, It just 44fps for average.

    I think it is caused by OnRenderImage(Graphic.bilt() function) and the cg in FOW shader. I tried to make it better, but no effect . Anyone help?
     
    Last edited: Jul 10, 2013
  46. Chaosgod_Espér

    Chaosgod_Espér

    Member

    Joined:
    Oct 25, 2012
    Messages:
    221
    I´ve a question too.

    I´m creating a DUngeon Keeper like Game - means a RTS. So there are up to 115 Objects that can reveal the FoW. And the revealed parts need to stay revealed.
    Is that possible and (more important) fast with Your FoW?
     
  47. Velo222

    Velo222

    Member

    Joined:
    Apr 29, 2012
    Messages:
    589
    Does this asset require Unity pro?
     
  48. MikeFas

    MikeFas

    New Member

    Joined:
    Apr 14, 2014
    Messages:
    1
    Does this work on 4.3? I cannot get it to work on a 2D plane with the x and y axis.
    Please can you advise on which files to change to make this work on x/y.
    Urgent.

    --
    Mikey Fas
     
  49. capitalJmedia

    capitalJmedia

    Member

    Joined:
    Feb 10, 2011
    Messages:
    195
    Hi, has anyone had luck getting FOW to work on iOS using the shader based approach? The Image Effect works fine, although a bit processor intensive, so I'm trying to see if I can use the shader approach but when I switch the project over to iOS the FOW doesn't show up, even in the demo scene.

    I've tried in a brand new project as well and switching to iOS platform seems to disable the fog itself, but objects still appear/disappear as if the fog was there.
     
  50. aanimation

    aanimation

    Member

    Joined:
    Oct 23, 2012
    Messages:
    40
    hi ArenMook, is package can use as DarkMap like LoL game?