Tasharen Entertainment Forum

Support => Other Packages => Topic started by: ArenMook on August 07, 2012, 10:16:31 PM

Title: Tasharen Fog of War
Post by: ArenMook on August 07, 2012, 10:16:31 PM
(http://www.tasharen.com/windward/fow/ss1m.jpg) (http://www.tasharen.com/windward/fow/ss1.jpg)

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:

What makes it great?

Play the Demo: http://www.tasharen.com/windward/fow/
Title: Re: Tasharen Fog of War
Post by: frarees on November 07, 2012, 05:00:01 AM
Is this package suitable for mobile platforms?
Title: Re: Tasharen Fog of War
Post by: ArenMook on November 07, 2012, 05:35:37 AM
If you check the thread on the Unity forums, people have used this package with mobiles, however it has to be done by modifying the object shaders instead. The idea is to not have a post-processing effect (image effect), but to have the shader used on your game objects actually sample the fog inside. This way you reduce fillrate, making it suitable for mobile platforms with next to no performance loss over not having the fog enabled.
Title: Re: Tasharen Fog of War
Post by: missyouangled on November 24, 2012, 04:28:18 AM
(http://www.tasharen.com/windward/fow/ss1m.jpg) (http://www.tasharen.com/windward/fow/ss1.jpg)

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:
  • Drag & drop the Fog of War prefab into your scene. You can find this prefab in Assets/Fog of War/FoW folder.
  • Add FOWEffect script to your Main Camera. Make sure it references the "Image Effects/Fog of War" shader.
  • 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.

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/
Title: Re: Tasharen Fog of War
Post by: vgiu on January 21, 2013, 12:07:17 PM
I'd like to know a few things about this:

- I suppose there is a viewing distance for the FOWRevealers, right?
- Is there a way of making it so that higher ground always see lower ground unities (no occlusion from a tall building's "shadow")
- How do we define what can and what cannot be seen by the fog of war? Can any object become an obstacle to sight, or only terrain?
- Is it possible to assert FoW information via scripting? (I'd use this for a server side fow that sends only information on objects that are seen by the clients)
Title: Re: Tasharen Fog of War
Post by: ArenMook on January 21, 2013, 12:36:14 PM
Yup, there is a range on the FOWRevealer.

Higher ground will automatically give you better sight than lower ground. You can actually see that in the demo. You will be able to see over small obstacles.

By default everything can be "seen" (as in, nothing gets flat out hidden) -- but it will be shaded by the FOW's colors (ie: if a unit is hidden in the fog, it will be tinted accordingly). You can also attach the FOWRenderers script to units to automatically turn off the renderers when they are covered by the fog. In most cases you will want to use this script on all your movable units as it doesn't make sense to still be able to see them if they are supposed to be invisible.

You can pull FOW's topology information and do your own checks, I suppose... but if you're going with that route it might make sense to roll your own custom solution. I suggest keeping it simple. Send the info anyway and let the clients sort what can and cannot be seen.
Title: Re: Tasharen Fog of War
Post by: vgiu on January 21, 2013, 12:55:45 PM
What I meant by the third question was whether I can have an object that can block the line of sight other than the terrain itself, if I put lots of buildings they are all taken into account so I'd be able to hide behind walls, or is it optimized to work, as in the demo, only with a terrain object and that is used to determine the fog of war?
Title: Re: Tasharen Fog of War
Post by: ArenMook on January 21, 2013, 03:40:30 PM
Of course you can use objects. Terrain isn't the only blocker. It's up to you which layers will block the line of sight -- just specify them.
Title: Re: Tasharen Fog of War
Post by: vgiu on January 21, 2013, 03:54:51 PM
Great! We just wanted to be sure, since the demo does not show that in action. Thanks for the reply :)
Title: Re: Tasharen Fog of War
Post by: ArenMook on January 21, 2013, 03:57:16 PM
Windward does. ;) If you play it you will notice that towers block line of sight.
Title: Re: Tasharen Fog of War
Post by: samelgod on February 21, 2013, 01:13:57 AM
Hey, Its real good asset,
I'm trying to blend its fow texture with minimap picture
do you have a shader to do that (that will work with UITexture) ?
Title: Re: Tasharen Fog of War
Post by: ArenMook on February 21, 2013, 04:27:23 AM
Yeah, Windward has a minimap and it's affected by the fog, but I never made a package out of that Minimap... although I do get requests to do so every so often.
Title: Re: Tasharen Fog of War
Post by: samelgod on February 21, 2013, 07:10:37 AM
could you post the shader or part of it?

I'm having hard time trying to make only visible/not-visible masking
and try to keep the explored areas not visible
Title: Re: Tasharen Fog of War
Post by: illumina on March 07, 2013, 10:22:10 PM
Hey There,
I am using your fog of war system in an iPad3 game and am finding the performance to be horrendous.

I really like the system and have integrated it fairly deeply into my game, so I'd like to keep using it. I'm seeing a framerate of about 25FPS with the FOW system turned on. If I turn off the shader then I get around 80FPS. The FOWSystem.Update() causes substantial spikes every few seconds (6ms) too in addition.

Unity profiler shows my game is spending significant time(11-30ms) in Device.Present, so I am fairly certain the issue is the shader. I've attached two screenshots of the unity profiler with both Device.Present() and FOWSystem.Update() selected.

I've tried twiddling different configuration options in FOWSystem, but can't seem to find any that reduce perf impact. I'm using a 256 world size and 256 texture size, 0.1 update time, and 0.4 blend time. None of my objects are below Y=0, and i am using some line of sight. All works fine... just slow on device..

Have you tested this on iOS before? Am I doing something stupid?

Thanks!
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 08, 2013, 04:04:26 AM
0.1 update time means you are updating your fog 10 times per second. This is highly unusual... I recommend you drop that to 1 per second, or twice at the most. Let blend time take care of the rest.

In Windward I had it set to 2 updates per second, blend time of 0.6, meaning the fade was slightly slower than the update. You can have the update be even less if your character doesn't move quickly.

For an iOS device I also suggest making custom shaders for objects, rather than using the Image Effect approach. You will significantly improve performance that way.
Title: Re: Tasharen Fog of War
Post by: illumina on March 08, 2013, 04:26:06 AM
Thanks for the prompt response. Two things

1) our game is an action rpg, so the fog needs to be fairly responsive. Kind of like Starcraft. I don't think I can get away with 2 per second, but I'll double check again.

2) could you elaborate a bit on the shader strategy you're suggesting for iOS? Are you suggesting not to use a screen effect but instead use multi texturing and have each objects shader know whether to tint itself & do the blending?
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 08, 2013, 05:03:30 AM
Pretty much. Sample not just the material's main texture, but also the fog's texture at the world coordinates of the pixel.
Title: Re: Tasharen Fog of War
Post by: valsimot on May 10, 2013, 09:54:39 AM
Hi this is a great tool, but I have crazy draw calls issue when I enable FOWEffect script on camera.

This is with no FOW effect
(http://img51.imageshack.us/img51/7671/nofogofwar.png) (http://imageshack.us/photo/my-images/51/nofogofwar.png/)

And this is what happens when I enable it (no revealer on scene, but it's same with or without it)
(http://img706.imageshack.us/img706/342/fogofwar.png) (http://imageshack.us/photo/my-images/706/fogofwar.png/)

There are few hundred unity cubes on the scene and draw calls where always less then 30. Can you tell me why this draw call issue happens with FOW?

Unity 4.1.2. Pro, Win 7.
Title: Re: Tasharen Fog of War
Post by: ArenMook on May 10, 2013, 11:39:21 PM
You see the "saved by batching" part to the right of draw calls? Dynamic batching doesn't work in many situations. You need to use either static batching (mark objects as "static" so they get batched), or just find a way to not have such an obscene number of draw calls to begin with.
Title: blocker
Post by: carfi on May 25, 2013, 05:51:34 AM
In Examples Scenes:Outdoor
I add a cube and my object in the scenes.Like this:
(http://www.cheaton9.com/1.jpg)
They both have collider component.
They both in aaa layer.
(http://www.cheaton9.com/2.jpg)
But they don't block the line.Like this:
(http://www.cheaton9.com/3.jpg)
Where I set error?
I am a newbie.Please teach me how to add the blocker.
Thank you so much!
Title: Re: Tasharen Fog of War
Post by: ArenMook on May 25, 2013, 08:19:53 PM
When do you add them? For this to work you have to add them before hitting Play.
Title: Re: Tasharen Fog of War
Post by: carfi on May 25, 2013, 09:55:10 PM
My unity version is 4.0.1f2 pro
Import the package - open the "Outdoor" scene - add my object - set object layer - set raycast mask - play
Am I set error in other setting?
Title: Re: Tasharen Fog of War
Post by: ArenMook on May 26, 2013, 12:02:22 AM
Nope, that's it. I just tried it myself to double check. Imported the package, added a cube to the scene, scaled it to 10, 10, 1, positioned it, hit Play -- works.

Added a new layer, changed the cube to be on that layer, and added this layer to the FOWSystem, hit Play -- works.

I did not modify anything else. I noticed you modified other properties -- such as Height Range. Your cube is likely taller than 8.6 units, so raycasts won't see it. I also did not modify the texture size -- default is 128, yours is at 56.
Title: Re: Tasharen Fog of War
Post by: carfi on May 26, 2013, 01:54:41 AM
o~Thank you very much!!!!again and again!!!
I download the package again~~~~~it work~!!!!!

Thank you for your answer,I find my Problem!
Title: Re: Tasharen Fog of War
Post by: cristiansr on June 04, 2013, 12:56:40 PM
I downloaded the "Unity Built-in Shaders" to take a look, and applied the code sent by @shiva.js for uses Fog in Mobile.
I attached the code to each object on the scene, but the terrain uses "Nature/Terrain/Diffuse" shader and I need adapt code for this shader...
I made some changes in the code, and now I have this:

  1. Shader "Mobile/Fog of War/Nature/Terrain/Diffuse" {
  2. Properties {
  3.         [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {}
  4.         [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
  5.         [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
  6.         [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
  7.         [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
  8.         // used in fallback on old cards & base map
  9.         [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {}
  10.         [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
  11. }
  12.        
  13. SubShader {
  14.         Tags {
  15.                 "SplatCount" = "4"
  16.                 "Queue" = "Geometry-100"
  17.                 "RenderType" = "Opaque"
  18.         }
  19.         Pass
  20.         {
  21.                 CGPROGRAM
  22. //              #pragma surface surf Lambert
  23.                
  24.                 struct Input {
  25.                         float2 uv_Control : TEXCOORD0;
  26.                         float2 uv_Splat0 : TEXCOORD1;
  27.                         float2 uv_Splat1 : TEXCOORD2;
  28.                         float2 uv_Splat2 : TEXCOORD3;
  29.                         float2 uv_Splat3 : TEXCOORD4;
  30.                 };
  31.                
  32.                 sampler2D _Control;
  33.                 sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
  34.                
  35.                 void surf (Input IN, inout SurfaceOutput o) {
  36.                         fixed4 splat_control = tex2D (_Control, IN.uv_Control);
  37.                         fixed3 col;
  38.                         col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
  39.                         col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
  40.                         col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
  41.                         col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
  42.                         o.Albedo = col;
  43.                         o.Alpha = 0.0;
  44.                 }
  45.                 ENDCG
  46.         }
  47.        
  48.         Pass
  49.         {
  50.                 CGPROGRAM
  51.                 #pragma vertex vert_surf
  52.                 #pragma fragment frag_surf
  53.                
  54.                 #include "UnityCG.cginc"
  55.        
  56.                 struct v2f_surf {
  57.                     float4  pos : SV_POSITION;
  58.                     float2  uv : TEXCOORD0;
  59.                    
  60.                     float2 fog : TEXCOORD2;
  61.                 };
  62.                
  63.                 float4 _Color;
  64.                 sampler2D _MainTex;
  65.                
  66.                 //begin regin FOG_OF_WAR
  67.                 sampler2D _FogTex0;
  68.                 sampler2D _FogTex1;
  69.                 uniform float4 _Params;
  70.                 uniform half4 _Unexplored;
  71.                 uniform half4 _Explored;
  72.                 //end regin
  73.                
  74.                 float4 _MainTex_ST;
  75.                
  76.                 v2f_surf vert_surf (appdata_full v) {
  77.                         v2f_surf o;
  78.                  
  79.                     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
  80.                     o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
  81.                    
  82.                     float4 worldPos = mul (_Object2World, v.vertex);
  83.                     o.fog.xy = worldPos.xz * _Params.z + _Params.xy;
  84.                  
  85.                         return o;
  86.                 }
  87.                
  88.                 half4 frag_surf (v2f_surf IN) : COLOR
  89.                 {
  90.                         half4 c = tex2D (_MainTex, IN.uv);
  91.                        
  92.                         half4 fog = lerp(tex2D(_FogTex0, IN.fog), tex2D(_FogTex1, IN.fog), _Params.w);
  93.                         c= lerp(lerp(c * _Unexplored, c * _Explored, fog.g), c, fog.r);
  94.                  
  95.                         return c * _Color;
  96.                 }
  97.                 ENDCG
  98.         }
  99. }
  100.  
  101. Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Lightmap-AddPass"
  102. Dependency "BaseMapShader" = "Diffuse"
  103.  
  104. Fallback "Mobile/Fog of War/Diffuse"
  105. }
  106.  

as it was before:
  1. Shader "Nature/Terrain/Diffuse" {
  2. Properties {
  3.         [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {}
  4.         [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
  5.         [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
  6.         [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
  7.         [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
  8.         // used in fallback on old cards & base map
  9.         [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {}
  10.         [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
  11. }
  12.        
  13. SubShader {
  14.         Tags {
  15.                 "SplatCount" = "4"
  16.                 "Queue" = "Geometry-100"
  17.                 "RenderType" = "Opaque"
  18.         }
  19. CGPROGRAM
  20. #pragma surface surf Lambert
  21. struct Input {
  22.         float2 uv_Control : TEXCOORD0;
  23.         float2 uv_Splat0 : TEXCOORD1;
  24.         float2 uv_Splat1 : TEXCOORD2;
  25.         float2 uv_Splat2 : TEXCOORD3;
  26.         float2 uv_Splat3 : TEXCOORD4;
  27. };
  28.  
  29. sampler2D _Control;
  30. sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
  31.  
  32. void surf (Input IN, inout SurfaceOutput o) {
  33.         fixed4 splat_control = tex2D (_Control, IN.uv_Control);
  34.         fixed3 col;
  35.         col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
  36.         col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
  37.         col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
  38.         col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
  39.         o.Albedo = col;
  40.         o.Alpha = 0.0;
  41. }
  42. ENDCG  
  43. }
  44.  
  45. Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Lightmap-AddPass"
  46. Dependency "BaseMapShader" = "Diffuse"
  47.  
  48. // Fallback to Diffuse
  49. Fallback "Diffuse"
  50. }
  51.  

This is it, do you have any idea to solve that? (Especially in relation to the part of splats)
Title: Re: Tasharen Fog of War
Post by: ArenMook on June 06, 2013, 12:20:11 PM
What are you trying to do here, and why did you add a second pass? Are you trying to add Fog of War to the terrain shader?
Title: Re: Tasharen Fog of War
Post by: ounick on August 26, 2013, 09:03:22 PM
I currently implementing the system for mobile. 
I plan on turning it on and off cleanly at certain intervals in the game.  Do i need to find all the FOWRevealers and FOWRenderers and disable them?

Also, how do I go about using a texture (like Monaco does with their "foggy blueprint" overlay) to define the "unexplored color" instead of a solid color? Would I need to place an image plane over everything and modify its transparency?

Thanks.
Title: Re: Tasharen Fog of War
Post by: ArenMook on August 27, 2013, 12:46:34 PM
The fog of war system works by changing the color, however you can have the unexplored color be fully transparent (alpha of 0), and have a background image behind it, Monaco-style. This way as you explore the map, it will slowly cover the unexplored texture. I did that with Windward, minus the background texture part.
Title: Re: Tasharen Fog of War
Post by: jchowdown on September 04, 2013, 07:02:09 PM
The idea is to not have a post-processing effect (image effect), but to have the shader used on your game objects actually sample the fog inside.

Hi, I'm a shader newbie so please bear with my questions.

1) Assuming the fog texture has smooth transitions between explored/unexplored areas, do my shaders just sample the fog color at the position of my game object and then just color the pixels with the appropriate color? Can it be done on a per-pixel basis so that on large objects the fog colors at different points blend smoothly across the whole object?

2) How easy is it for game-level code to sample fog at a location?

3) Also, does this FOW package make it easily to have FOW that is cleared permanently (ie fog does not return to areas that are free of revealers)?

Thanks in advance,
Jeff
Title: Re: Tasharen Fog of War
Post by: ArenMook on September 05, 2013, 04:29:09 AM
FOW system has a function to check whether the object at the specified position is obscured by fog or not.

However, assuming you want the transition to be smooth, the sampling should be done in the shader, not in game code. If you're a shader newbie, then you likely won't be able to do it. There was a shader example posted on the Tasharen Fog of War thread on the Unity forums though.

As for #3 -- FOW has 3 states -- unexplored, explored, and visible. If the explored state matches your visible state, the units will remain visible even if you don't have revealers in the area.
Title: Fog move wrongly with orthographic camera
Post by: maesla on September 23, 2013, 06:29:30 AM
Hello.

I'm having problem with the fow. When I move the camera with perspective camera, everything works fine. But if I change to orthographic camera, the fog move with the camera, and it is not anchored to the entities as it should be.

It is very important to fix that. Any idea?

Thanks a lot.
Title: Re: Tasharen Fog of War
Post by: ArenMook on September 23, 2013, 06:45:49 AM
Can't say I've ever tried using it with an orthographic camera, so off the top of my head I can't tell you what's wrong. Can you post a pic of your game and what you're trying to do?
Title: Re: Tasharen Fog of War
Post by: maesla on September 23, 2013, 07:45:45 AM
I have more details.

I have two effects attached to the camera. Fow and the highlighting system (http://forum.unity3d.com/threads/143043-Highlighting-System-Released). When I deactivate the highlighting system, the fow works fine, but when I active it, the fow grows and it move with the camera. All of this is in orthographic camera, in perspective works fine.

Both effects have OnRenderImage and it is there where the problem is. I am going to mail to the highlighting system author aswell to see if we can find a solution between all of us.

Do you need more details? I am going to ask him If I can send you the highlighting system effect code.

Thank you


Edit:
I have exactly the line
  1. public class HighlightingEffect : MonoBehaviour
  2. {
  3. ...
  4. private int layerMask = (1 << HighlightableObject.highlightingLayer);
  5.  
  6. ...
  7.  
  8. void OnPreRender()
  9. {
  10. ...
  11. shaderCamera.cullingMask = layerMask; //If I comment this line, the problem is fixed but the highlighting system do not work
  12. ...
  13. shaderCamera.Render();
  14. }
  15.  
Title: Re: Tasharen Fog of War
Post by: ArenMook on September 23, 2013, 08:16:49 AM
Does he ever change the cullingMask back after drawing with that camera? What is shaderCamera? Make sure the shader camera doesn't draw the fog of war. Fog of war should happen after everything else.
Title: Re: Tasharen Fog of War
Post by: maesla on September 23, 2013, 09:11:56 AM
It is the fog camera and the culling mask is never restored. The weird things is that only happens with orthographic camera. I am going to ask with the author of the other plugin
Title: Re: Tasharen Fog of War
Post by: jchowdown on October 22, 2013, 09:05:46 PM
Hi, I've been using FOW in my game for a few weeks and I've been thinking about some optimizations:

1) my units are stationary most of the time. I was thinking about leaving their LOSChecks value as OnlyOnce while they're standing still, and only set it to EveryUpdate while they're actually moving. Is it safe to set this on my revealer in the main thread? I think this would be a huge optimization.

2) As a test, I took my running game and for one FOWRevealer instance, I set the LOSChecks value to OnlyOnce in Unity's inspector. I immediately got an out of bounds crash here (in RevealIntoCache): http://screencast.com/t/Bt4ivGYfb5
The size of the buffer is 41616 ( or 204*204 as determined by xmax - ymin) but the range given by ymax-ymin is actually 205. The actually array index into r.cachedBuffer is 41709. I believe this is a rounding error in this code: http://screencast.com/t/VnJMUXDFaMjQ.
One artificial way of fixing this is to set ymax be exactly ymin + ( xmax - xmin ). What do you think?

3) In RevealUsingLOS and RevealUsingRadius, there is a two-level nested for() loop that performs a check that x and y are within the bounds of the texture size. Can't those checks be pulled out of the for() loops for optimization? What if we clamped ymin,ymax,xmin,xmax like this? http://screencast.com/t/u5rqTvDm

Thanks in advance,
jeff
Title: Re: Tasharen Fog of War
Post by: ArenMook on October 25, 2013, 01:22:42 AM
1. Seems logical, yes.

2. I don't get this one. There is a check that only executes the code if the sx and sy are within 0 to (textureSize-1) bounds, which means that it won't execute with a dimension of 255. How are you getting to that?

3. Seems fine to me.
Title: Re: Tasharen Fog of War
Post by: Chaosgod_Espér on October 25, 2013, 02:53:38 AM
I´ve some questions too.

1st:)
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?


2nd:)
Is there any way to animate the Edges of the fog, to give it a Fog-Look?
Title: Re: Tasharen Fog of War
Post by: ArenMook on October 26, 2013, 07:57:38 PM
1. To have revealed parts stay revealed just change the "explored" color to match the "visible" color. The more moving revealers you have the slower the performance will be (obviously). Using non-moving revealers will speed it up.

2. Civ 5 style? Sure. FoW generates a texture to you, but how you display it is up to you. Write a shader to display clouds in the areas marked by the fog if you wish.
Title: Re: Tasharen Fog of War
Post by: frarees on October 27, 2013, 01:49:53 PM
I'm trying to use the FoW for a tile based game, so I need it to be really precise. I've modded it a little to allow point filtering mode, and adjusted texture size to fit tile size. All the objects are snapped to a 2 unity units size grid (the revealer too). See attached image to see current result.

However, I don't see the line of sight being coherent. Am I missing some setting? If not, how could I edit the algorithm (probably the RevealUsingLOS method) to improve it and fit?
Title: Re: Tasharen Fog of War
Post by: ArenMook on October 27, 2013, 10:33:31 PM
To be honest I've never tried using FoW with such large pixels. Whether areas are visible or not are determined by performing height difference checks from revealers to surrounding pixels The algorithm is pretty straightforward as you can see by looking at the code -- but since it's not done using raycasts, it may not give you the expected results in your case.
Title: Re: Tasharen Fog of War
Post by: rsharr on December 02, 2013, 05:59:36 PM
Is it possible to have multiple "fog of war" prefabs that work with each other?

I have very large terrain tiles and want to attach the fog of war to each tile. 

I attempted this and the fog of war works on the very first tile but as soon as I get to the second tile and the fog system is called, the entire screen turns in to the "explored" state.

Thanks


--edit

I guess whats happening is under the render image it is finding the first object of type and not updating to the new system. Would I accomplish this through a collider and having it trigger to find the correct fog system?

--edit

nevermind, I have no idea what I am doing as far as editing these scripts to find a solution.

Basics of what I want:

I have a tile system, each tile is large compared to the player.
I have the fog system working on one tile.
I want to be able to walk across one tile to another and activate that specific tile's fog system instead of the previous.

having one system on any given tile works just fine, its just when I get 2 or more systems is when it starts acting wonky. I am guessing because this fog system was never designed for this setup and is getting confused as to which system to render to camera. Is there a solution to this that you can think of? Thanks again.
Title: Re: Tasharen Fog of War
Post by: ArenMook on December 03, 2013, 10:42:12 AM
The fog of war system was designed with only one set of textures to sample in the post-processing shader. Multiple FOW systems mean multiple textures, so it won't work.
Title: Re: Tasharen Fog of War
Post by: rsharr on December 03, 2013, 11:02:26 AM
Can you think of a way that I can flush the texture "cache" and update to the new fog system upon entering an area? I am not concerned with the fog system remembering where I explored and not, as my explored / unexplored colors are the same. Thanks

edit-

Im not asking you to do it, but rather asking if you think its possible with your system
Title: Re: Tasharen Fog of War
Post by: ArenMook on December 03, 2013, 09:07:02 PM
You can enable the correct FOW system and/or set the right textures inside a script attached to the object(s) you are trying to draw. Add a OnWillRenderObject (http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnWillRenderObject.html) function to a script and do it inside. You will also want to create custom shaders that sample the FOW texture inside rather than doing it via the post-process step.
Title: Re: Tasharen Fog of War
Post by: rsharr on December 06, 2013, 09:16:53 AM
did you ever end up getting my email or pm?
Title: Re: Tasharen Fog of War
Post by: ArenMook on December 07, 2013, 12:51:20 AM
Oh, yes I did, sorry. Swamped as usual. Unfortunately I don't have the time right now.
Title: Re: Tasharen Fog of War
Post by: valbery on January 15, 2014, 06:50:02 PM
Hi,

I want buy this product but i must ask a question. Is there a way to make a building only "visible" in the fog of war?

Regards
Title: Re: Tasharen Fog of War
Post by: ArenMook on January 15, 2014, 10:35:14 PM
I am not sure what you mean. Make a building visible only in the fog of war? Fog of war hides things, not makes them visible.
Title: Re: Tasharen Fog of War
Post by: valbery on January 16, 2014, 12:36:27 PM
Hello ArenMook,

We are developing a rts game as Age Of Empires. Our players can make building with mouse click where can they click on Terrain. Bu they must make their building only explored area in the Terrain. Are you support this feature?

Regards
Title: Re: Tasharen Fog of War
Post by: ArenMook on January 17, 2014, 12:50:14 AM
You can query to see if the specified target position is visible using FoW and prevent building if it's not.
Title: Re: Tasharen Fog of War
Post by: plabrie on January 23, 2014, 01:20:58 AM
Hi Aren,

right to business: how do you decide the texture size ?
My world is big enough and initially with the setting by default, it didn't work at all because the x,y position it took were negative and not fitting in the texture, something like this. I had to change to 2048 size, and it "works" but the texture doesn't align with my view, like a dark patch fit in a in an alley, but is offset on top of the building. (I could provide texture tomorrow possibly) But still, how do you pick your texture size ? Do I really need to update it so it fit perfectly ?

also, i would think it would be better to have the details of all the variables expose in the FOW system in the readme ?

Thank you very much.
Title: Re: Tasharen Fog of War
Post by: ArenMook on January 23, 2014, 07:38:05 AM
Decide the texture size? I'm not sure what you mean. FoW will use the dimensions of the texture you specified, stretched over the region of your choice (in all directions, using transform's position as the center).
Title: Re: Tasharen Fog of War
Post by: plabrie on January 23, 2014, 12:04:49 PM
So, the issue was with the y being inverted...

http://www.tasharen.com/forum/index.php?topic=6468.0
So I fixed it with what is written in that post.

Next issue I have encounter now, is when I have the SSAOEffect enabled from Unity with the FogImage Effect. It create artifact that stay on screen. If I disable the SSAO and bring it back, it make the current artifact dissapear and new one appear.
My feeling when I see it: It seems that when the SSAO Effect is enabled, it create the texture that is blended on top, and then doesn't update it again, so when I move, it seems to move that texture on top of my scene.

I attached an image of my issue.

I can have SSAO enabled, then I enabled the FOW, disable it, and the SSAO will have that issue. I disable the SSAO, reenable it, everything work fine. Enable FOW, same issue, etc.
Title: Re: Tasharen Fog of War
Post by: ArenMook on January 24, 2014, 10:48:11 AM
The new FoW version I pushed has two example scripts of how to do FoW without the post-process effect. If Unity's SSAO conflicts with other post process effects, you can adjust your object shaders to sample FoW textures instead using the provided shaders as an example. It's a simple modification, also explained here: http://www.tasharen.com/forum/index.php?topic=7662.msg36228#msg36228
Title: Re: Tasharen Fog of War
Post by: ounick on January 25, 2014, 11:50:43 AM
I am using the fog of war system with the non-postprocess method.  It has been working pretty well.  However, I started lightmapping my levels, and everything no longer reacts to the fog of war shading.  I looked at my shader and the lightmapped pass is incomplete for the fog of war. 
Can anyone please help finish this shader to support the non-postprocess fog of war and lightmapping?

I am shader illiterate and I think my brain is about to enter melt-down.

Thanks, Nick

  1. // Unlit shader. Simplest possible textured shader.
  2. // - SUPPORTS lightmap
  3. // - no lighting
  4. // - no per-material color
  5.  
  6. Shader "Mobile/UnlitFoW(Supports Lightmap)" {
  7.         Properties {
  8.        
  9.                 _MainTex ("Base (RGB)", 2D) = "white" {}
  10.                
  11.         }
  12.        
  13.         SubShader {
  14.                 Tags { "RenderType"="Opaque" }
  15.                 LOD 100
  16.                
  17.                 // Non-lightmapped
  18.                 Pass {
  19.                         Tags { "LightMode" = "Vertex" }
  20.                         Lighting Off
  21.                         CGPROGRAM
  22.                
  23.                         #pragma vertex vert
  24.                
  25.                         #pragma fragment frag
  26.                
  27.                         #include "UnityCG.cginc"
  28.                        
  29.                         float4 _Color;
  30.                        
  31.                         sampler2D _MainTex;
  32.                        
  33.                         sampler2D _FogTex0; //fog texture variable
  34.        
  35.                         sampler2D _FogTex1; //fog texture variable
  36.        
  37.                         uniform float4 _Params; //fog variable
  38.        
  39.                         uniform half4 _Unexplored; //fog variable
  40.        
  41.                         uniform half4 _Explored; //fog variable
  42.                        
  43.                         float4 _MainTex_ST;
  44.                        
  45.                         struct v2f
  46.                         {
  47.                        
  48.                                 float4 pos : SV_POSITION;
  49.                                
  50.                                 float2 uv : TEXCOORD0;
  51.                                
  52.                                 float2 lmap : TEXCOORD1;
  53.                                
  54.                                 float2 fog : TEXCOORD2; // this is a new line
  55.                        
  56.                         };
  57.                        
  58.                         half4 frag (v2f i) : COLOR
  59.                         {
  60.                        
  61.                             half4 c = tex2D (_MainTex, i.uv);
  62.                        
  63.                             half4 fog = lerp(tex2D(_FogTex0, i.fog), tex2D(_FogTex1, i.fog), _Params.w);
  64.                        
  65.                             c = lerp(lerp(c * _Unexplored, c * _Explored, fog.g), c, fog.r); // where c is the fixed4 to return
  66.                        
  67.                             return c;// * _Color;
  68.                        
  69.                         }
  70.                        
  71.                         v2f vert (appdata_base v)
  72.                         {
  73.                        
  74.                             v2f o;
  75.                        
  76.                             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
  77.                        
  78.                             o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
  79.                        
  80.                             float4 worldPos = mul (_Object2World, v.vertex);
  81.                        
  82.                             o.fog.xy = worldPos.xz * _Params.z + _Params.xy;
  83.                        
  84.                             return o;
  85.                
  86.                         }
  87.                         ENDCG
  88.                         //SetTexture [_MainTex] { combine texture }
  89.                 }
  90.                
  91.                 // Lightmapped, encoded as dLDR
  92.                 Pass {
  93.                         Tags { "LightMode" = "VertexLM" }
  94.        
  95.                         Lighting Off
  96.                         BindChannels {
  97.                                 Bind "Vertex", vertex
  98.                                 Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
  99.                                 Bind "texcoord", texcoord1 // main uses 1st uv
  100.                         }
  101.                        
  102.                         SetTexture [unity_Lightmap] {
  103.                                 matrix [unity_LightmapMatrix]
  104.                                 combine texture
  105.                         }
  106.                         SetTexture [_MainTex] {
  107.                                 combine texture * previous DOUBLE, texture * primary
  108.                         }
  109.                 }
  110.                
  111.                 // Lightmapped, encoded as RGBM
  112.                 Pass {
  113.                         Tags { "LightMode" = "VertexLMRGBM" }
  114.                        
  115.                         Lighting Off
  116.                         BindChannels {
  117.                                 Bind "Vertex", vertex
  118.                                 Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
  119.                                 Bind "texcoord", texcoord1 // main uses 1st uv
  120.                         }
  121.                        
  122.                         SetTexture [unity_Lightmap] {
  123.                                 matrix [unity_LightmapMatrix]
  124.                                 combine texture * texture alpha DOUBLE
  125.                         }
  126.                         SetTexture [_MainTex] {
  127.                                 combine texture * previous QUAD, texture * primary
  128.                         }
  129.                 }      
  130.                
  131.         }
  132. }
  133.  
Title: Re: Tasharen Fog of War
Post by: ArenMook on January 25, 2014, 04:11:18 PM
You need to convert this:
  1.             BindChannels {
  2.                 Bind "Vertex", vertex
  3.                 Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
  4.                 Bind "texcoord", texcoord1 // main uses 1st uv
  5.             }
  6.            
  7.             SetTexture [unity_Lightmap] {
  8.                 matrix [unity_LightmapMatrix]
  9.                 combine texture
  10.             }
  11.             SetTexture [_MainTex] {
  12.                 combine texture * previous DOUBLE, texture * primary
  13.             }
...into actual shader code instead, like you have above for the vertex lit shader. I can't give you any pointers here though as I am not familiar with the lightmapping logic Unity uses. The basic idea is to blend the previous result with the light map though -- so you don't need to write a surface shader -- just a vertex and fragment shader combo (in other words same as you have above).
Title: Re: Tasharen Fog of War
Post by: ounick on January 25, 2014, 11:03:57 PM
Thanks.  I will give it a shot.  If any shader pros out there, feel free to chime in  ;)
Title: Re: Tasharen Fog of War
Post by: ounick on January 26, 2014, 12:21:13 AM
For anyone interested, I think I got it.  *updated*
  1. Pass {
  2.                         Tags { "LightMode" = "VertexLM" "LightMode" = "VertexLMRGBM"}
  3.        
  4.                         Lighting Off
  5.                        
  6.                         CGPROGRAM
  7.                
  8.                         #pragma vertex vert
  9.                
  10.                         #pragma fragment frag
  11.                
  12.                         #include "UnityCG.cginc"
  13.                        
  14.                         float4 _Color;
  15.                        
  16.                         sampler2D _MainTex;
  17.                        
  18.                         sampler2D   unity_Lightmap;
  19.                         float4          unity_LightmapST;
  20.                        
  21.                         sampler2D _FogTex0; //fog texture variable
  22.        
  23.                         sampler2D _FogTex1; //fog texture variable
  24.        
  25.                         uniform float4 _Params; //fog variable
  26.        
  27.                         uniform half4 _Unexplored; //fog variable
  28.        
  29.                         uniform half4 _Explored; //fog variable
  30.                        
  31.                         float4 _MainTex_ST;
  32.                        
  33.                         struct v2f
  34.                         {
  35.                        
  36.                                 float4 pos : SV_POSITION;
  37.                                
  38.                                 float2 uv : TEXCOORD0;
  39.                                
  40.                                 float2 lmap : TEXCOORD1;
  41.                                
  42.                                 float2 fog : TEXCOORD2; // this is a new line
  43.                        
  44.                         };
  45.                        
  46.                         struct VertInput
  47.             {
  48.                 float4 vertex   : POSITION;
  49.                 float2 texcoord : TEXCOORD0;
  50.                                 float4 texcoord1: TEXCOORD1;
  51.  
  52.             };
  53.                        
  54.                         half4 frag (v2f i) : COLOR
  55.                         {
  56.                        
  57.                             half4 c = tex2D (_MainTex, i.uv);
  58.                        
  59.                             half4 fog = lerp(tex2D(_FogTex0, i.fog), tex2D(_FogTex1, i.fog), _Params.w);
  60.                        
  61.                             c = lerp(lerp(c * _Unexplored, c * _Explored, fog.g), c, fog.r); // where c is the fixed4 to return
  62.                            
  63.                                 return half4 ( c.xyz * DecodeLightmap ( tex2D ( unity_Lightmap, i.lmap ) ), c.w );
  64.                         }
  65.                        
  66.                         v2f vert (VertInput v)
  67.                         {
  68.                        
  69.                             v2f o;
  70.                        
  71.                             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
  72.                        
  73.                             o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
  74.                            
  75.                             o.lmap      = ( unity_LightmapST.xy * v.texcoord1.xy ) + unity_LightmapST.zw;
  76.                        
  77.                             float4 worldPos = mul (_Object2World, v.vertex);
  78.                        
  79.                             o.fog.xy = worldPos.xz * _Params.z + _Params.xy;
  80.                        
  81.                             return o;
  82.                
  83.                         }
  84.                         ENDCG
  85.                
Title: Re: Tasharen Fog of War
Post by: GrimmGames on March 07, 2014, 08:36:48 AM
Hi.  I have two questions.

1) Is it possible to save and load the fog of war state?  i.e. I don't want the player to have to uncover the same areas twice between play sessions.

2) Is it possible to have dynamic LoS objects like doors or other objects that might be moved. i.e. I want things like doors to block the LoS check locally until they are opened or removed.

Thanks!
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 07, 2014, 11:12:06 AM
1. Yup. FOWSystem GetRevealedBuffer / SetRevealedBuffer:
  1.         /// <summary>
  2.         /// Retrieve the revealed buffer
  3.         /// </summary>
  4.  
  5.         public byte[] GetRevealedBuffer ()
  6.         {
  7.                 int size = textureSize * textureSize;
  8.                 byte[] buff = new byte[size];
  9.                 for (int i = 0; i < size; ++i) buff[i] = mBuffer1[i].g;
  10.                 return buff;
  11.         }
  12.  
  13.         /// <summary>
  14.         /// Reveal the area, given the specified array of bytes.
  15.         /// </summary>
  16.  
  17.         public void SetRevealedBuffer (byte[] arr)
  18.         {
  19.                 int mySize = textureSize * textureSize;
  20.  
  21.                 if (arr.Length != mySize)
  22.                 {
  23.                         Debug.LogError("Buffer size mismatch. Fog is " + mySize + ", but passed array is " + arr.Length);
  24.                 }
  25.                 else
  26.                 {
  27.                         if (mBuffer0 == null)
  28.                         {
  29.                                 mBuffer0 = new Color32[mySize];
  30.                                 mBuffer1 = new Color32[mySize];
  31.                         }
  32.  
  33.                         for (int i = 0; i < mySize; ++i)
  34.                         {
  35.                                 mBuffer0[i].g = arr[i];
  36.                                 mBuffer1[i].g = arr[i];
  37.                         }
  38.                 }
  39.         }
2. FoW revealers can be updated frequently or just once. For static objects like a stationary guard tower you would set the FoW revealer to do its thing only once, while for a moving object like the FoW revealer on the player you will set it to update frequently. Frequent updates mean frequent raycasts, so your door will automatically block the revealer when it's closed.
Title: Re: Tasharen Fog of War
Post by: GrimmGames on March 07, 2014, 03:43:33 PM
Thanks for the information Aren.  Unfortunately, I have been unable to make a door that blocks and unblocks the player's revealer based on it being opened or closed.  Can you explain how to set this up?  Thanks!
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 08, 2014, 01:51:29 PM
As I mentioned, player's revealer simply performs raycasts from the Revealer's point to each of the points on the FoW map. If the door has a collider that will prevent the ray cast from reaching its target, then the point will be considered to be not visible from FoW's perspective. Just make sure that you've got your door's layer set up correctly to what FOW system will consider to be a blocker.
Title: Re: Tasharen Fog of War
Post by: capitalj on March 19, 2014, 02:53:41 AM
Hi, for a turn based game can I turn off the FOW update frequency so that it only is updated when I call a manual update (when a character actually moves)? in other words I don't need the FOW to be updating anything during the player's turn, only once they end the turn the FOW needs to be updated to see if their characters can see new things.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 20, 2014, 02:41:14 AM
Sure, just disable the FOWSystem component and re-enable it when you need it.
Title: Re: Tasharen Fog of War
Post by: capitalj on March 22, 2014, 06:59:35 PM
Sure, just disable the FOWSystem component and re-enable it when you need it.

Hmm, but that makes the entire Fog disappear if I disable the component. I need it to stay on screen, just not update all the time.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 23, 2014, 03:10:21 AM
Open FOWImageEffect, line 68:
  1.                 if (mFog == null || !mFog.enabled)
  2.                 {
  3.                         enabled = false;
  4.                         return;
  5.                 }
Change it to:
  1.                 if (mFog == null)
  2.                 {
  3.                         enabled = false;
  4.                         return;
  5.                 }
...and disabling FOWSystem will no longer disable the image effect.
Title: Re: Tasharen Fog of War
Post by: capitalj on March 25, 2014, 02:59:16 PM
Cool, that works.

Is there an event I can create somewhere or subscribe to that would let me know when the rebuild of the FOW has completed so I can know when to turn off the FOWSystem component?

ie: when my character stops moving I call: myFOWRevealer.Rebuild(); which only works if the FOWSystem is enabled, so I would like to wait for that to happen but not just use a simple timer as that could get messy if another character moves within the timer time or something.

Also, is there a way to force the FOWSystem to update the textures instead of using the Update Frequency? so when I enable the FOWSystem component, I can just ask it to refresh the textures as well and then disable it. I tried making the mNextUpdate public and setting that to 0 but that seems to have unpredictable results and doesn't always work.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 26, 2014, 03:40:46 AM
FOW is updated on a separate thread so that it doesn't degrade game performance -- and all the logic happens inside FOWSystem.ThreadUpdate. The actual textures are updated in FOWSystem.UpdateTexture, which happens on the main (game) thread. If you want to be notified of them being updated, I would advise adding your callback logic there.
Title: Re: Tasharen Fog of War
Post by: capitalj on March 26, 2014, 08:09:04 PM
I see. Ok well the issue with my implementation is that I don't know when the FOW logic is being updated, but it would be great to just call some function on FOWSystem to have everything updated at that time. So I'm just confused as to how to make this work in a turn based system like my game where I don't need any sort of update frequency happening, it's all manually called. Maybe the game flow is not clear, here's what I am looking to do:

There are 2 teams, which should not be able to see each other's characters if they are in the FOW.

1) Game start: each player's character's FOWRevealer is enabled or disabled depending on whether it is that character's team's turn. The FOW system then needs to update to reveal the current team's characters.

2) Player moves a character. The FOW needs to update to reveal the change based on the character's new position.

3) Player ends their turn. Each character's FOWRevealer is enabled or disabled again based on whether it is that character's team's turn. Then the FOW system needs to update to reveal the new team's characters and hide the old team's characters (if they are in the fog).


So what I don't understand is which components to enable/disable and force update when these 3 stages happen. I have the FOWRevealers all changing properly when the turns change, but the whole FOWsystem stuff is not really working, which must be because it's in the middle of some sort of updates or transitions when I have these stages happening. Sometimes it works perfectly but sometimes there is no update to the FOW.. or the FOWSystem.isvisible reports that a character is visible even though they are in the fog.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 26, 2014, 08:29:10 PM
Sounds like you are overthinking it. Why do you care when the FoW is updated? Its performance hit is minimal. If you truly care about it for some reason, then just enable the FOWSystem when you know it will be updated, and then disable it some time later -- after some delay of a few seconds, for example. FOWSystem is meant to be real-time, not turn-based, so by trying to change it to be updated manually you are only creating headaches for yourself.
Title: Re: Tasharen Fog of War
Post by: zmeinaz on March 28, 2014, 08:27:31 AM
Does this still require Unity Pro if I use the mobile shader implementation? The first page of this thread mentions that Unity Pro is required, but the Asset Store page does not.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 28, 2014, 10:20:58 AM
Unity Pro is needed if you want to use the Image Effect-based approach. If you use the custom shader-based approach you don't need Pro. I just haven't gotten around to updating the info everywhere.
Title: Re: Tasharen Fog of War
Post by: zmeinaz on March 29, 2014, 04:24:41 PM
How difficult would it be to use FOW on the x-y plane instead of the x-z plane in the example. I have a 2.5D game that I am trying to use this with, the character moves on the x-y plane. I have it partially working, however when the character moves on the y plane, the FOW is not updated. it works fine along the x-axis. I am using the shader implementation and have changed my shaders to use worldPos.xy instead of xz. What else would I need to change?
Title: Re: Tasharen Fog of War
Post by: capitalj on March 29, 2014, 05:26:11 PM
Sounds like you are overthinking it. Why do you care when the FoW is updated? Its performance hit is minimal. If you truly care about it for some reason, then just enable the FOWSystem when you know it will be updated, and then disable it some time later -- after some delay of a few seconds, for example. FOWSystem is meant to be real-time, not turn-based, so by trying to change it to be updated manually you are only creating headaches for yourself.

Yeah, maybe. My stuff has to run on mobile so I usually want to try to reduce the cpu use to free up room for other things. I'll keep working away at it. Unfortunately turning the component off and on doesn't really work, the results are not always the same and it doesn't update when it is enabled sometimes.

Unrelated question: what do I have to do to update the FOW system if I have terrain that is loaded at runtime? I've found that if a different terrain is loaded during the game start the FOW still reflects the terrain that was in place before the terrain was changed. Even when my characters move and call Rebuild on their revealers, it still doesn't update to match the new terrain. Is there a way to force it to redraw the FOW from scratch?
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 30, 2014, 08:01:57 AM
@capitalj: Calling FOWSystem.CreateGrid() will re-create your FoW height map.

@zmeinaz: The same FOWSystem.CreateGrid() will need to be modified to perform raycasts using Vector3.forward instead of Vector3.up.
Title: Re: Tasharen Fog of War
Post by: zmeinaz on March 30, 2014, 08:16:35 PM
I'm still not quite getting the desired behavior. I have a 2D tile map on the xy plane. I want the tiles to have the fog of war. I think I need to somehow map the xz plane of the fow texture to the xy plane of my tile map. From what I have seen so far, the x-axis updates correctly, but the y axis is always 0.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 31, 2014, 07:07:24 AM
You will also want to modify the revealer logic (RevealUsingRadius, RevealUsingLOS) -- they use a 3D position.
Title: Re: Tasharen Fog of War
Post by: zmeinaz on March 31, 2014, 08:29:57 PM
Thanks, I ended up having to re-orient the FOWSystem to the xy plane. Works great!
Title: Re: Tasharen Fog of War
Post by: capitalj on April 01, 2014, 01:54:25 AM
Hey, 2 questions about FOW:

1) Is it possible to have only 1 texture? I don't really need an explored and unexplored color. I would like to have everything either be  hidden or visible.

2) the FOW currently projects onto the skybox if viewed from a ground level instead of overhead view: http://cl.ly/image/0e2z31360d22
Title: Re: Tasharen Fog of War
Post by: ArenMook on April 01, 2014, 11:15:23 AM
1. FOW always uses two textures, but not for explored / unexplored color, but for blending. The smooth transitions that occur when you are uncovering the map are done in the shader by blending between two textures. When one texture is at alpha of 100%, the second one can be used for writing the next batch of updated FOW data.

2. When using the Image Effect based approach the skybox is a part of the scene so yes it will be covered by the fog. If you want selective fog of war, you need to use custom shaders on objects as shown in the second example.
Title: Re: Tasharen Fog of War
Post by: capitalj on April 01, 2014, 01:45:00 PM
Ah ok, I'll try out the shader based approach then.

So for the explored/unexplored areas, is there a way to disable that feature? So that areas that are hidden (whether previously explored or not) are always at the same darkness?
Title: Re: Tasharen Fog of War
Post by: ArenMook on April 02, 2014, 06:54:30 PM
Sure, set the explored/unexplored colors accordingly. For example if both are pure black then you will not be able to uncover anything. Only what's immediately visible will be seen. If you set the explored color to pure white, then you will premanently uncover parts of the world as you travel through it.
Title: Re: Tasharen Fog of War
Post by: hummada23 on April 06, 2014, 01:30:28 AM
Hi,

Maybe I'm doing something wrong, but something I've noticed with this asset is that it doesn't black out the top of things that block the revealer's height (like the tops of mountains), as can be seen in the example included with it. Is there something I can do to change this?   
Title: Re: Tasharen Fog of War
Post by: ArenMook on April 06, 2014, 03:12:11 AM
That's how it was written, yes. The top is visible -- you can see the top of the mountain -- so the fog of war node underneath gets marked as "visible". It's either that or it would be "not visible" -- meaning you would never see the top of the mountain as it would be shrouded by a permanent fog layer even if you've uncovered everything around it.
Title: Re: Tasharen Fog of War
Post by: hummada23 on April 06, 2014, 07:49:31 AM
OK great, thanks.
Title: Re: Tasharen Fog of War
Post by: capitalj on April 06, 2014, 10:06:57 PM
You can try setting the Raycast Radius to 0, I'm not sure if that will help in your situation but I remember doing something like that and it hid the backs of mountains.
Title: Re: Tasharen Fog of War
Post by: konsnos on April 10, 2014, 08:19:42 AM
Hello, I have a question.

Is the height of FOW adjustable? I want to restrict the visible area into layers. Bottom won't seen anything on higher layers whatever the height difference, but higher layers will see lower.

Thanks in advance for the response.
Title: Re: Tasharen Fog of War
Post by: ArenMook on April 10, 2014, 08:22:04 AM
FoW is based on a 2D heightmap. The heightmap is created by performing raycasts into the world, determining the height at each pixel (and pixels are pretty big). Once this heightmap is created, it gets reused and won't be re-calculated unless you tell it to. Revealers that navigate the 2D heightmap have their own height, so when a "raycast" is performed, it's done in the 2D space by basically comparing the heights from point A to point B to see if something blocks the "line of sight".
Title: Re: Tasharen Fog of War
Post by: konsnos on April 10, 2014, 01:06:13 PM
FoW is based on a 2D heightmap. The heightmap is created by performing raycasts into the world, determining the height at each pixel (and pixels are pretty big). Once this heightmap is created, it gets reused and won't be re-calculated unless you tell it to. Revealers that navigate the 2D heightmap have their own height, so when a "raycast" is performed, it's done in the 2D space by basically comparing the heights from point A to point B to see if something blocks the "line of sight".
So basically if A can see to B, then B can also see at A? No exceptions?
Title: Re: Tasharen Fog of War
Post by: ArenMook on April 11, 2014, 08:21:05 AM
What are A and B? If they are different players who each have their own fog of war system then not necessarily. It depends on the height of the objects and the height at which the revealer is located.
Title: Re: Tasharen Fog of War
Post by: BuddyBoy on April 13, 2014, 01:41:55 PM
Hi There.

Love this asset. I purchased it last year before Unity 4.3

I am now working on a 2D game and I want to use this but the x/y axis is not supported.

Please can you explain each file to modify and what to change to use the 4.3 2D x-y. I saw somebody else asked this but I was a little lost.

Thanks.
Title: Re: Tasharen Fog of War
Post by: capitalj on April 16, 2014, 03:36:31 PM
2. When using the Image Effect based approach the skybox is a part of the scene so yes it will be covered by the fog. If you want selective fog of war, you need to use custom shaders on objects as shown in the second example.

Do you think it would be possible to have a 2nd camera that only renders the skybox and still use the Image Effect approach and not have the skybox affected by the FOW effect?
Title: Re: Tasharen Fog of War
Post by: ArenMook on April 17, 2014, 02:07:53 PM
You mean render the skybox after the scene? That would be... odd, and I can think of some issues to go along with it. You could likely use the stencil buffer to mark parts of the screen to be eligible for FOW or not, but again -- it would involve a custom shader on at least one of your object (skybox). And modifying the image effect to use the stencil buffer, of course.
Title: Re: Tasharen Fog of War
Post by: capitalj on April 17, 2014, 07:31:18 PM
Or an even easier solution is to just turn off the FOW image effect whenever I switch to the viewpoint that shows the skybox (where the blocked areas aren't visible anyways). :)
Title: Re: Tasharen Fog of War
Post by: konsnos on April 22, 2014, 06:25:45 AM
A small bug report. I have in my project NGUI and just added the FOW. FOW package asked to import file BetterList.cs which I assume is a previous version of the same file NGUI has. It creates a problem with the Pop function not being available if replaced.

Edit: One more thing.

  • Add FOWEffect script to your Main Camera. Make sure it references the "Image Effects/Fog of War" shader.

In the package I bought this must be FOWImage Effect? That's what it worked for me.
Title: Re: Tasharen Fog of War
Post by: ArenMook on April 23, 2014, 08:00:21 AM
Right you are, sounds like I need to update the text.
Title: Re: Tasharen Fog of War
Post by: Chairwalker on April 24, 2014, 07:51:37 AM
Hello, i've been trying to use this FOW on a scene with rooms in a grid-like fashion
and noticed that the walls facing south or west never fully appeared.
To ensure this wasn't caused by any misalignment on my part and to better illustrate the problem i've created a fresh project with only the FOW asset imported,
loaded the example scene that uses the shader approach and changed it into a 30/30 grid with a revealer in the center and four 2/2/3 cubes with colliders placed around it:

(http://i.imgur.com/6w8ol0K.jpg)


Ingame the scene looks like this:

(http://i.imgur.com/6kVJgqB.jpg)

I could 'fix' this by scaling down the colliders to hide the offset within the walls but this won't help with doorways etc.
Anything i'm missing in properly alligning the FOW to the grid?  or is this caused by some parameter in the script that i've overlooked?

Title: Re: Tasharen Fog of War
Post by: ArenMook on April 25, 2014, 10:45:09 AM
You are trying to mix meshes which are based on vertices, and textures which are based on pixels.

Think about it. If you put down a 2x2 texture over a simple quad (which is also 2x2 vertices)... where do the pixels lie?

Answer: the "center" of pixels will be 25% from the edge of the quad -- or half a pixel.

This is also the reason why when you create a Terrain in Unity, you can't specify a 512x512 texture to be the heightmap. It forces it to be 513x513 -- in other words one extra pixel is needed.

Another rule of this conversion: in order to draw a single quad you need 4 vertices (2x2). You can have a 1x1 texture on it, but you can't draw it using 1 vertex, can you? Thus the +1.
Title: Re: Tasharen Fog of War
Post by: Chairwalker on April 25, 2014, 11:47:17 AM
Thank you for that in-depth explanation, it all makes perfect sense now. :)
Title: Re: Tasharen Fog of War
Post by: capitalj on May 07, 2014, 10:40:28 PM
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.
Title: Re: Tasharen Fog of War
Post by: capitalj on May 18, 2014, 05:26:52 PM
Bump.. anyone has any iOS experience with the FOW plugin?
Title: Re: Tasharen Fog of War
Post by: z78 on June 20, 2014, 10:00:53 AM
I recently bought this asset from the asset store and I can't get it to work at all; I was hoping you could help me with that.

I followed the three steps on the start of this thread, and I get nothing; after trying a few things, a closely following the Asset's script, I noticed the FOWImageEffect Script that is attached to my main camera disables itself on Game Start, and the first time I tried to enable it in runtime I got the following error:

unity can't use image filters (npot-RT are not supported or RT are disabled completely)

does this Asset need Unity Pro ti work with?, I there anything else I need to do apart from the 3 steps you detailed?

I would be really grateful if you can help with this issue   
Title: Re: Tasharen Fog of War
Post by: ArenMook on June 22, 2014, 09:12:26 PM
Please stick to one means of contacting me. Forum post, private message and email of the same thing is overkill.

As I explained in the email, image effect-based approach requires Unity Pro. Shader-based approach (second example) will work in Unity Free.
Title: Re: Tasharen Fog of War
Post by: capitalj on July 08, 2014, 01:33:46 AM
Hi, I am trying to use the FOW image effect on Android builds but it doesn't seem to show up. On iOS/Standalone the image effect based approach works fine, but on Android it just doesn't appear. Anything I should be doing different on Android or any special settings? I am using a Nexus 7 (2012) tablet.
Title: Re: Tasharen Fog of War
Post by: ArenMook on July 08, 2014, 01:51:06 PM
I vaguely remember something that had to be done differently... I'll look into it and let you know.
Title: Re: Tasharen Fog of War
Post by: capitalj on July 08, 2014, 02:00:47 PM
Ok that would be great. The game is done, so just have to get the android version ready for release :)
Title: Re: Tasharen Fog of War
Post by: Chairwalker on September 05, 2014, 06:32:33 AM
Could you give me some pointers on how to create a custom CreateGrid function that only checks a certain part of the existing grid?

*edit*

Nevermind, figured it out by myself.
For anyone interested:

  1. protected virtual void UpdateGrid (Transform Updated, int Range)
  2.         {      
  3.                 float texToWorld = (float)worldSize / textureSize;
  4.                 Vector3 pos = mOrigin;
  5.                 pos.y += mSize.y;
  6.  
  7.                 bool useSphereCast = raycastRadius > 0f;
  8.                
  9.                 for (int z = 0; z < textureSize; ++z)
  10.                 {
  11.                         pos.z = mOrigin.z + z * texToWorld;
  12.                        
  13.                         for (int x = 0; x < textureSize; ++x)
  14.                         {
  15.                                 pos.x = mOrigin.x + x * texToWorld;
  16.  
  17.  
  18.                                 if ((pos.x > (Updated.position.x - Range)) && (pos.x < (Updated.position.x + Range)) && (pos.z > (Updated.position.z - Range)) && (pos.z < (Updated.position.z + Range)))
  19.                                 {
  20.                                         RaycastHit hit;
  21.                                         if (useSphereCast)
  22.                                         {
  23.                                                 if (Physics.SphereCast(new Ray(pos, Vector3.down), raycastRadius, out hit, mSize.y, raycastMask))
  24.                                                 {
  25.                                                         mHeights[x, z] = WorldToGridHeight(pos.y - hit.distance - raycastRadius);
  26.                                                         continue;
  27.                                                 }
  28.                                         }
  29.                                         else if (Physics.Raycast(new Ray(pos, Vector3.down), out hit, mSize.y, raycastMask))
  30.                                         {
  31.                                                 mHeights[x, z] = WorldToGridHeight(pos.y - hit.distance);
  32.                                                 continue;
  33.                                         }
  34.                                         mHeights[x, z] = 0;
  35.                                 }
  36.                         }
  37.                 }
  38.         }

Calling UpdateGrid(Object,8) should cast rays only in a 16x16 grid around the transform of Object.
Title: Re: Tasharen Fog of War
Post by: gekido on September 27, 2014, 11:50:16 PM
Just a quick note, the BetterList implementation in the Fog of War package is out of sync with the one that is in the NGUI package.  If you import Fog of War into a project after NGUI, it breaks NGUI if you include the BetterList.cs file (which Unity will automatically overwrite).

(issue is in the BetterList<Transform>.CompareFunc call in UIGrid.cs for what it's worth)

Might want to namepsace and/or rename the file in the Fog of War package maybe?  The newer BetterList in NGUI seems backwards compatibile with the other one, but not the other way around.

Cheers!
Title: Re: Tasharen Fog of War
Post by: ArenMook on September 29, 2014, 07:30:15 AM
Sounds like I need to update it at some point... thanks!
Title: Re: Tasharen Fog of War
Post by: jpcoombs on November 07, 2014, 02:59:31 PM
Love the FOW - works great right out of the box.

However we have a game where players a building walls and dropping objects that should be affected by FoW. Reading through the Forums it looks like after PLAY the FoW collision zone is determined and won't be updated (our tests with generating new objects supports this). Is there a way to clear the cache/resimulate upon placement of new objects in the scene?

Cheers!
Title: Re: Tasharen Fog of War
Post by: ArenMook on November 07, 2014, 04:18:28 PM
You would need to call FOWSystem's CreateGrid() function. Although looking at it, it's protected so you will need to make it public first.
Title: Re: Tasharen Fog of War
Post by: jpcoombs on November 07, 2014, 05:21:34 PM
Awesome! Worked great!
Title: Re: Tasharen Fog of War
Post by: jpcoombs on November 08, 2014, 10:35:34 AM
Aren, one more question -

Do you have any advice for updating the CreateGrid () but essentially removing the complete refresh of the discovered areas - perhaps a third level of FoW that retains discovered areas and overrides the refresh? We got the function to redraw every time that we place a new wall but the fog renewal is a little odd.

Cheers, and thanks for all your support and hard work on the package! Super happy with it.
Title: Re: Tasharen Fog of War
Post by: ounick on November 08, 2014, 05:41:30 PM
Hi.  My screenshots are just from the editor and not while playing but I just wanted to illustrate the positioning. Everything is working correctly for me when my FOWSystem's transform is positioned at the origin(shown in the left screenshot).  However, I am having an issue where whenever I position my FOWSystem's transform to the center of my environment, everything stops working correctly(shown in the right screenshot).  Do you have any idea how to fix my problem? 
(http://i.imgur.com/moznaFK.jpg)
Title: Re: Tasharen Fog of War
Post by: ArenMook on November 09, 2014, 09:55:36 PM
@jpcoombs: What do you mean by complete refresh of discovered areas? CreateGrid shouldn't change the fog of war, just the height map. Looking at the code, that's all it does.

@ounick: When do you move the FoW? It caches its origin position in its Start() function.
Title: Re: Tasharen Fog of War
Post by: jpcoombs on November 10, 2014, 02:48:48 PM
@Aren

Right now we run the CreateGrid function every time we place an object (like building a new wall). As soon as the wall is built, CreateGrid is essentially refreshing the entire map and in that process refreshing all of the "Explored" area. Characters with FOWRevealer have to be clicked and refreshed to reveal the area they are currently in - if that makes sense.

Before and after CreateGrid pictured here.

(http://i.imgur.com/BPUYWVy.png)
 
Title: Re: Tasharen Fog of War
Post by: ArenMook on November 10, 2014, 11:18:33 PM
Looking at the code on my end, absolutely nothing is touching the buffers. CreateGrid() simply changes the heightmap (mHeights array).
  1.         public virtual void CreateGrid ()
  2.         {
  3.                 Vector3 pos = mOrigin;
  4.                 pos.y += mSize.y;
  5.                 float texToWorld = (float)worldSize / textureSize;
  6.                 bool useSphereCast = raycastRadius > 0f;
  7.  
  8.                 for (int z = 0; z < textureSize; ++z)
  9.                 {
  10.                         pos.z = mOrigin.z + z * texToWorld;
  11.  
  12.                         for (int x = 0; x < textureSize; ++x)
  13.                         {
  14.                                 pos.x = mOrigin.x + x * texToWorld;
  15.  
  16.                                 if (raycastMask != 0)
  17.                                 {
  18.                                         RaycastHit hit;
  19.  
  20.                                         if (useSphereCast)
  21.                                         {
  22.                                                 if (Physics.SphereCast(new Ray(pos, Vector3.down), raycastRadius, out hit, mSize.y, raycastMask))
  23.                                                 {
  24.                                                         mHeights[x, z] = WorldToGridHeight(pos.y - hit.distance - raycastRadius);
  25.                                                         continue;
  26.                                                 }
  27.                                         }
  28.                                         else if (Physics.Raycast(new Ray(pos, Vector3.down), out hit, mSize.y, raycastMask))
  29.                                         {
  30.                                                 mHeights[x, z] = WorldToGridHeight(pos.y - hit.distance);
  31.                                                 continue;
  32.                                         }
  33.                                 }
  34.                                 mHeights[x, z] = 0;
  35.                         }
  36.                 }
  37.         }
Title: Re: Tasharen Fog of War
Post by: ounick on November 11, 2014, 02:12:37 PM
I am moving the system's transform only in editor mode.  I am going to start comparing grid to world coordinate values today and maybe that will lead me somewhere.
Title: Re: Tasharen Fog of War - Linear Color Space
Post by: FuriousBroccoli on February 05, 2015, 05:27:48 PM
Hi - I noticed that the fog of war reveal interpolation, with Linear color space, flickers.  This problem does not occur in Gamma space.

I'm no good with Unity shaders - is there an easy fix to make this work smoothly with a linear color space?

Thanks!
Title: Re: Tasharen Fog of War
Post by: ArenMook on February 06, 2015, 10:14:28 PM
Linear color space, despite its name, is not linear -- so interpolation isn't smoothly going from A to B, but rather following a curve. The textures used by the fog of war shouldn't be using linear color space. They need to be forced to use gamma. Try the "bypass sRGB sampling" option.
Title: Re: Tasharen Fog of War
Post by: FuriousBroccoli on February 07, 2015, 12:54:42 AM
Great, that did the trick - the fog textures actually need to have their color space match that of the player.  This was an easy fix in code:

FOWSystem.cs
  1. mTexture0 = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false);
  2. mTexture1 = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false);
  3.  

becomes...

  1. mTexture0 = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false, QualitySettings.activeColorSpace == ColorSpace.Linear);
  2. mTexture1 = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false, QualitySettings.activeColorSpace == ColorSpace.Linear);
  3.  

and it works for both color spaces. Thanks!
Title: Re: Tasharen Fog of War
Post by: ETM on March 12, 2015, 05:16:32 AM
I am having a hard time getting FOW to work in Unity 5.0. It is working correctly with a single camera using image effects, but when I use it with two cameras, the second one rendering to a rendertexture, the fog of war doesn't seem right. Random places appear dark and other visible. Before Unity 5.0 I used the custom shader solution, which worked great, but now I cannot use it anymore due to a shader issue which turns the material pink.

The shader throws the following error:
Too many texture interpolators would be used for ForwardBase pass (12 out of max 10)
 at line 28.

Title: Re: Tasharen Fog of War
Post by: ArenMook on March 12, 2015, 09:26:13 PM
FOW Image Effect needs to be on the camera that doesn't render to texture. Your camera that renders to texture should be drawn first, followed by your main camera (that should also have FOWImageEffect on it).
Title: Re: Tasharen Fog of War
Post by: piotrO on March 15, 2015, 05:37:15 PM
Hi,

I have two pre-purchase questions regarding the FOW plugin.

1) I have a modular dungeon level with a top down camera. My own FOW solution (a semi-transparent plane above the level) works ok when camera looks down from a distance. But from up close it clearly shows that my trivial approach isn't working as expected (Two pics attached for a reference.). My question is: will your plugin handle well scenes like dungeons with corridors, and the fog will smoothly cover both the walls and floors?


2) Is it ease to integrate your plugin (the shader part is what interests me most) with my custom fow logic?

Thanks
(http://)
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 16, 2015, 09:34:11 PM
1. Yes, it will handle it just fine, especially if you stick to the image effect-based approach.

2. Depends on what you'd like to integrate. By default the kit will use an image-effect based approach, meaning it will just work as a post-process effect, without the need to create any shaders on your part.
Title: Re: Tasharen Fog of War
Post by: ETM on March 20, 2015, 09:00:41 AM
FOW Image Effect needs to be on the camera that doesn't render to texture. Your camera that renders to texture should be drawn first, followed by your main camera (that should also have FOWImageEffect on it).

That doesn't help because there is no fog of war on the render texture.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 22, 2015, 08:30:32 AM
Render texture camera erases depth that's used by the fog of war. That's why I said it needs to be taken care of first before main rendering begins.
Title: Re: Tasharen Fog of War
Post by: ETM on March 23, 2015, 09:27:02 AM
I am confused, could you please provide a short step by step tutorial on how to get an rts-like fow working with ugui?
What I did is:
1. Set depth of orthographic camera (minimap) to 1, change to render texture, assign the render texture to a raw image component.
2. Set depth of perspective camera (main camera) to 0, added fowimageeffect to it.
3. Saw the main camera having fog of war, the minimap not so much.

How do you specify the order of drawing if not by setting depth?
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 23, 2015, 10:05:22 AM
The minimap won't have a fog of war. Why would it? Image effect only affects the main camera. What you should be doing instead is using the FOW's textures and blending them in your minimap's shader. Here's a shader I use on my minimap in Windward, as an example:
  1. Shader "Minimap UI/Fog of War"
  2. {
  3.         Properties
  4.         {
  5.                 _MainTex ("TexCoord.y (R)", 2D) = "white" {}
  6.                 _FogTex0 ("Visible (R), Explored (G)", 2D) = "black" {}
  7.                 _FogTex1 ("Visible (R), Explored (G)", 2D) = "black" {}
  8.                 _Gradient ("Gradient", 2D) = "white" {}
  9.         }
  10.  
  11.         SubShader
  12.         {
  13.                 LOD 100
  14.  
  15.                 Tags
  16.                 {
  17.                         "Queue" = "Transparent"
  18.                         "IgnoreProjector" = "True"
  19.                         "RenderType" = "Transparent"
  20.                 }
  21.  
  22.                 Pass
  23.                 {
  24.                         Cull Off
  25.                         Lighting Off
  26.                         ZWrite Off
  27.                         Offset -1, -1
  28.                         Fog { Mode Off }
  29.                         ColorMask RGB
  30.                         Blend SrcAlpha OneMinusSrcAlpha
  31.                
  32.                         CGPROGRAM
  33.                         #pragma vertex vert
  34.                         #pragma fragment frag
  35.                         #include "UnityCG.cginc"
  36.  
  37.                         sampler2D _MainTex;
  38.                         sampler2D _FogTex0;
  39.                         sampler2D _FogTex1;
  40.                         sampler2D _Gradient;
  41.  
  42.                         float4 _MainTex_ST;
  43.                         float4 _FogTex0_ST;
  44.                         half _Blend;
  45.  
  46.                         struct appdata_t
  47.                         {
  48.                                 float4 vertex : POSITION;
  49.                                 half4 color : COLOR;
  50.                                 float2 texcoord : TEXCOORD0;
  51.                                 float2 texcoord1 : TEXCOORD1;
  52.                         };
  53.  
  54.                         struct v2f
  55.                         {
  56.                                 float4 vertex : POSITION;
  57.                                 half4 color : COLOR;
  58.                                 float2 texcoord : TEXCOORD0;
  59.                                 float2 texcoord1 : TEXCOORD1;
  60.                         };
  61.  
  62.                         v2f vert (appdata_t v)
  63.                         {
  64.                                 v2f o;
  65.                                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  66.                                 o.color = v.color;
  67.                                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  68.                                 o.texcoord1 = TRANSFORM_TEX(v.texcoord1, _FogTex0);
  69.                                 return o;
  70.                         }
  71.  
  72.                         half4 frag (v2f IN) : COLOR
  73.                         {
  74.                                 half4 fog = lerp(tex2D(_FogTex0, IN.texcoord1), tex2D(_FogTex1, IN.texcoord1), _Blend);
  75.                                 half offset = tex2D(_MainTex, IN.texcoord).r;
  76.        
  77.                                 half4 visible = tex2D(_Gradient, half2(0.25, offset));
  78.                                 half4 explored = tex2D(_Gradient, half2(0.5, offset));
  79.                                 half4 hidden = tex2D(_Gradient, half2(0.75, offset));
  80.        
  81.                                 half4 final = lerp(lerp(hidden, explored, fog.g), visible, fog.r) * IN.color;
  82.  
  83.                                 float2 temp = abs(IN.texcoord * 2.0 - 1.0);
  84.                                 float val = max(temp.x, temp.y);
  85.                                 val *= val;
  86.                                 val *= val;
  87.                                 val *= val;
  88.                                 val *= val;
  89.                                 final.a *= 1.0 - val;
  90.                                 return final;
  91.                         }
  92.                         ENDCG
  93.                 }
  94.         }
  95. }
  96.  
Note that it won't work for you out-of-the-box as Windward has a script that sets "terrainOffset" and "terrainScale" values inside, and I doubt that it will apply to your game. What I basically do is render the terrain with a top-down camera into a render texture, then draw this render texture as the minimap, applying the shader above. Inside the shader I sample the Fog of War textures (there are 2 of them!), blending from one to the other correctly.
Title: Re: Tasharen Fog of War
Post by: ETM on March 23, 2015, 11:32:34 AM
That really looks like a backward solution to my problem. What's the point of having a fog of war that cannot be used for minimaps easily. The custom shader solution worked great without any problems before 5.0, but is unusable now while the image effect solution has obvious drawbacks. Do you plan to update the terrain shader or should I just ask the Unity for a refund? This whole problem set my release date back for several weeks now.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 24, 2015, 10:56:15 AM
Actually my solution is the proper one. When drawing the minimap, it's not a good idea to draw *everything*. You should only draw only specific layers, then overlay clear indicators for things like NPCs. It's also a good idea to have a stylized looking map, not just a top-down view of the whole region. Look at what the minimap looks like in Windward. Not only is it clearly showing the region's coastlines (drawn with the shader I pasted in the previous post), but all units are clearly visible as icons.

The drawing of the map, since it happens into a render texture, is done only twice per second, rather than every frame like it would need to be done with your approach. This, combined with the need to only draw the terrain using an unlit replacement shader is extremely efficient -- order of magnitude more efficient than what would be happening with your approach.

I will look into why the terrain shader doesn't work in Unity 5, while it worked fine in 4 at some point, but I can't give you an exact timeframe as I have much on my plate.

I'm not sure why you don't just go back to Unity 4 here. Why did you upgrade to a new version right before a release? You yourself said that it worked fine before. Not sure how a refund is going to solve anything either, but that's certainly your choice.
Title: Re: Tasharen Fog of War
Post by: ETM on March 24, 2015, 01:21:00 PM
How do you setup the shader below then? I cannot get it to work. I've added
  1. public Material GetMaterial()
  2.         {
  3.                 return mMat;
  4.         }

to FOWImageEffect.cs and trying to get the textures like this
  1. public FOWImageEffect _imageEffect;
  2.         public Material _fowMaterial;
  3.  
  4.  
  5.         IEnumerator Start ()
  6.         {
  7.                 while (_imageEffect.GetMaterial() == null)
  8.                 {
  9.                         yield return new WaitForSeconds(1f);
  10.                 }
  11.  
  12.                 Material fowMat = _imageEffect.GetMaterial();
  13.                 _fowMaterial.SetTexture("_FogTex0", fowMat.GetTexture("_FogTex0"));
  14.                 _fowMaterial.SetTexture("_FogTex1", fowMat.GetTexture("_FogTex1"));
  15.         }

Not sure what the MainTex and Gradient parameters are and how to find them in the FOWImageEffect material. Notice: _fowMaterial refers to the material posted above. FogTex0 and FogTex1 are retrieved as expected, the texture shown by the RawImage component is white. Not sure where I would put "terrainOffset" and "terrainScale".

Title: Re: Tasharen Fog of War
Post by: ArenMook on March 24, 2015, 01:46:51 PM
Any variable set in the shader must be set in OnRenderImage (which gets called prior to something being drawn). You can't just set it in Start(). Unity doesn't work like that.

Here are the two scripts I'm using:
  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Very basic game map script that takes a camera and renderes it into a texture using the specified replacement shader.
  5. /// </summary>
  6.  
  7. public class MinimapPlain : MonoBehaviour
  8. {
  9.         /// <summary>
  10.         /// Renderer used to draw the map.
  11.         /// </summary>
  12.  
  13.         public Camera mapRenderer;
  14.  
  15.         /// <summary>
  16.         /// Shader used to draw the map with.
  17.         /// </summary>
  18.  
  19.         public Shader mapRendererShader;
  20.  
  21.         /// <summary>
  22.         /// Material to update.
  23.         /// </summary>
  24.  
  25.         public Material material;
  26.  
  27.         /// <summary>
  28.         /// Width of the minimap's texture.
  29.         /// </summary>
  30.  
  31.         public int width = 128;
  32.  
  33.         /// <summary>
  34.         /// Height of the minimap's texture.
  35.         /// </summary>
  36.  
  37.         public int height = 128;
  38.  
  39.         /// <summary>
  40.         /// How often the map will be checked for updates.
  41.         /// </summary>
  42.  
  43.         public float updateFrequency = 0.25f;
  44.  
  45.         [System.NonSerialized] protected Transform mTrans;
  46.         [System.NonSerialized] protected RenderTexture mRT;
  47.         [System.NonSerialized] protected float mNextUpdate = 0f;
  48.         [System.NonSerialized] protected Vector3 mLastPos = Vector3.zero;
  49.         [System.NonSerialized] protected float mLastSize = 0f;
  50.         [System.NonSerialized] protected int mWidth = 0;
  51.         [System.NonSerialized] protected int mHeight = 0;
  52.         [System.NonSerialized] protected bool mRefresh = true;
  53.         [System.NonSerialized] protected bool mSizeChanged = false;
  54.  
  55.         /// <summary>
  56.         /// Return 'false' if you don't want to render the map (such as while the world is loading).
  57.         /// </summary>
  58.  
  59.         protected virtual bool canRender { get { return (Terrain.activeTerrain != null); } }
  60.  
  61.         /// <summary>
  62.         /// Cache the transform and register callbacks.
  63.         /// </summary>
  64.  
  65.         protected virtual void Awake ()
  66.         {
  67.                 mRefresh = true;
  68.                 mTrans = transform;
  69.  
  70.                 if (mapRenderer == null)
  71.                 {
  72.                         Debug.LogError("Expected to find a map renderer to work with", this);
  73.                         enabled = false;
  74.                         return;
  75.                 }
  76.         }
  77.  
  78.         protected void Start () { OnStart(); Update(); }
  79.  
  80.         /// <summary>
  81.         /// Mark the map as changed.
  82.         /// </summary>
  83.  
  84.         protected void Invalidate () { mRefresh = true; mSizeChanged = true; }
  85.         protected void OnApplicationFocus (bool focus) { mRefresh = true; mSizeChanged = true; }
  86.  
  87.         /// <summary>
  88.         /// Update what's necessary.
  89.         /// </summary>
  90.  
  91.         protected virtual void Update ()
  92.         {
  93.                 if (mLastPos != mapRenderer.transform.position || mLastSize != mapRenderer.orthographicSize)
  94.                 {
  95.                         mLastPos = mapRenderer.transform.position;
  96.                         mLastSize = mapRenderer.orthographicSize;
  97.                         mRefresh = true;
  98.                 }
  99.  
  100.                 if (mWidth != width || mHeight != height)
  101.                 {
  102.                         mSizeChanged = true;
  103.                         mWidth = width;
  104.                         mHeight = height;
  105.                         mRefresh = true;
  106.                 }
  107.  
  108.                 if (canRender && (mRefresh || mNextUpdate < Time.time))
  109.                 {
  110.                         mNextUpdate = Time.time + updateFrequency;
  111.  
  112.                         if (mRefresh)
  113.                         {
  114.                                 Terrain ter = Terrain.activeTerrain;
  115.  
  116.                                 if (ter != null)
  117.                                 {
  118.                                         Shader.SetGlobalFloat("terrainOffset", ter.transform.position.y);
  119.                                         Shader.SetGlobalFloat("terrainScale", ter.terrainData.heightmapScale.y);
  120.                                 }
  121.  
  122.                                 if (mSizeChanged && mRT != null)
  123.                                 {
  124.                                         Destroy(mRT);
  125.                                         mRT = null;
  126.                                 }
  127.  
  128.                                 if (mRT == null)
  129.                                 {
  130.                                         mRT = new RenderTexture(mWidth, mHeight, 24, RenderTextureFormat.ARGB32);
  131.                                         mRT.name = name;
  132.                                         mRT.generateMips = false;
  133.                                 }
  134.  
  135.                                 // Render the map into the render texture
  136.                                 mapRenderer.targetTexture = mRT;
  137.                                 mapRenderer.RenderWithShader(mapRendererShader, "");
  138.                                 mapRenderer.targetTexture = null;
  139.                         }
  140.  
  141.                         OnUpdate(mRefresh);
  142.                         mRefresh = false;
  143.                         mSizeChanged = false;
  144.                 }
  145.         }
  146.  
  147.         /// <summary>
  148.         /// Invalidate the render texture's dimensions when returning to the app.
  149.         /// </summary>
  150.  
  151.         protected void OnApplicationPause (bool isPaused)
  152.         {
  153.                 if (!isPaused)
  154.                 {
  155.                         mRefresh = true;
  156.                         mSizeChanged = true;
  157.                         Update();
  158.                 }
  159.         }
  160.  
  161.         /// <summary>
  162.         /// Clear the refresh flag at the end.
  163.         /// </summary>
  164.  
  165.         protected void LateUpdate () { OnLateUpdate(); }
  166.  
  167.         /// <summary>
  168.         /// Anything you need to do in Start.
  169.         /// </summary>
  170.  
  171.         protected virtual void OnStart () { }
  172.  
  173.         /// <summary>
  174.         /// Anything else you might want to update (target indicators and such).
  175.         /// The 'rebuild' parameter will be 'true' if the map texture was rebuilt.
  176.         /// </summary>
  177.  
  178.         protected virtual void OnUpdate (bool rebuild) { }
  179.  
  180.         /// <summary>
  181.         /// Anything you need to do in Late Update.
  182.         /// </summary>
  183.  
  184.         protected virtual void OnLateUpdate () { if (material != null) material.mainTexture = mRT; }
  185.  
  186.         /// <summary>
  187.         /// Anything you need to do in OnDestroy.
  188.         /// </summary>
  189.  
  190.         protected virtual void OnDestroy ()
  191.         {
  192.                 if (mRT != null)
  193.                 {
  194.                         Destroy(mRT);
  195.                         mRT = null;
  196.                 }
  197.         }
  198. }
  199.  
The first script references a top-down orthographic camera (mapRenderer) as well as the replacement shader to use (mapRendererShader). The top-down camera should be positioned and sized in such a way that it's able to cover all of your visible region (or at least whatever you want to be actually visible). It must also be disabled. The script above will take care of enabling it as needed. The shader is simply:
  1. Shader "Minimap/Heightmap"
  2. {
  3.         SubShader
  4.         {
  5.                 LOD 300
  6.                 Tags { "RenderType" = "Opaque" }
  7.                 Fog { Mode Off }
  8.  
  9.                 Pass
  10.                 {
  11.                         CGPROGRAM
  12.                         #pragma vertex vert
  13.                         #pragma fragment frag
  14.                         #include "UnityCG.cginc"
  15.  
  16.                         float terrainOffset = 0.0;
  17.                         float terrainScale = 16.0;
  18.  
  19.                         struct appdata_t
  20.                         {
  21.                                 float4 vertex : POSITION;
  22.                         };
  23.  
  24.                         struct v2f
  25.                         {
  26.                                 float4 pos : POSITION;
  27.                                 float2 uv : TEXCOORD0;
  28.                         };
  29.  
  30.                         v2f vert (appdata_t v)
  31.                         {
  32.                                 v2f o;
  33.                                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  34.                                 float4 worldPos = mul(_Object2World, v.vertex);
  35.                                 float f = (worldPos.y + terrainOffset) / terrainScale;
  36.                                 o.uv = float2(f, f);
  37.                                 return o;
  38.                         }
  39.  
  40.                         fixed4 frag (v2f IN) : COLOR
  41.                         {
  42.                                 return half4(IN.uv.xxx, 1.0);
  43.                         }
  44.                         ENDCG
  45.                 }
  46.         }
  47.         FallBack Off
  48. }
The second script extends the first one, adding Fog of War to it.
  1. using UnityEngine;
  2.  
  3. public class MinimapFoW : MinimapPlain
  4. {
  5.         /// <summary>
  6.         /// Gradient texture used to color the map.
  7.         /// </summary>
  8.  
  9.         public Texture2D gradient;
  10.  
  11.         [System.NonSerialized] protected float mFogScale = 1f;
  12.         [System.NonSerialized] protected float mFogOffset = 0f;
  13.  
  14.         protected override void OnLateUpdate ()
  15.         {
  16.                 base.OnLateUpdate();
  17.  
  18.                 if (FOWSystem.instance != null)
  19.                 {
  20.                         float camRange = mapRenderer.GetComponent<Camera>().orthographicSize * 2f;
  21.                         mFogScale = camRange / FOWSystem.instance.worldSize;
  22.                         mFogOffset = (1f - mFogScale) * 0.5f;
  23.  
  24.                         material.SetFloat("_Blend", FOWSystem.instance.blendFactor);
  25.                         material.SetTexture("_FogTex0", FOWSystem.instance.texture0);
  26.                         material.SetTexture("_FogTex1", FOWSystem.instance.texture1);
  27.                         material.SetTexture("_Gradient", gradient);
  28.                         material.SetTextureScale("_FogTex0", new Vector2(mFogScale, mFogScale));
  29.                         material.SetTextureOffset("_FogTex0", new Vector2(mFogOffset, mFogOffset));
  30.                 }
  31.         }
  32. }
  33.  
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 24, 2015, 01:52:37 PM
As for the gradient... It's a texture that has 3 parts: left, middle and right.

I made it so that the left part is used when the region is visible. Middle = explored, but not currently visible. Right = not yet explored.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 24, 2015, 03:38:32 PM
To make it easier, I created a unitypackage for it. I'll include this example in the next update.
Title: Re: Tasharen Fog of War
Post by: JOKaija on March 24, 2015, 04:16:32 PM
Did I miss something.

I'm going to release my game as multiplatform game.

BUT!!! I can't compile it for Windows Store!

The reason is, that System.Threading doesn't include Thread definition.

HOWEVER, I can compile a native WP8 version without problem. And it works.
But, Micro$oft sucks (as you know), and I have to build Store acceptable
version of my game if I want to sell it on Micro$oft stores...

What to do?
Title: Re: Tasharen Fog of War
Post by: ETM on March 25, 2015, 05:30:45 AM
Much obliged. It works, but I am stuck again. I am trying to get the output shader into ugui. I tried using the minimap fow material for the raw image, putting a camera above the quad to film it and then rendering a texture, but without any success. The raw image component paints a transparent image, when I click on the material, it looks fine in the inspector.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 25, 2015, 11:58:47 AM
@JOKaija: You will need to figure out what's different between their APIs and where Threading now resides I suppose. That sounds like a question for Microsoft's documentation.

@ETM: Sorry, but I can't assist you with uGUI. If it was NGUI, I could. Keep in mind minimap in general is outside the scope of the Fog of War kit to begin with. I included the scripts simply because I already had to do it for Windward.
Title: Re: Tasharen Fog of War
Post by: ETM on March 26, 2015, 04:22:20 AM
Understood. Would you consider lowering the price for this pack, because there are at least two assets with FOW and minimap support for about half the price?

Just curious.
Title: Re: Tasharen Fog of War
Post by: piotrO on March 27, 2015, 03:38:41 AM
1. Yes, it will handle it just fine, especially if you stick to the image effect-based approach.

2. Depends on what you'd like to integrate. By default the kit will use an image-effect based approach, meaning it will just work as a post-process effect, without the need to create any shaders on your part.

Thanks Aren, the plugin works perfectly.
Title: Re: Tasharen Fog of War
Post by: Sudarmin on April 16, 2015, 05:24:14 AM
This plugin works great, but I have 1 issue, when my player gets near a wall, I can see both what's inside my room and what's inside the next room. How do I fix this? I don't want the player to be able to see pass through the wall when he is near it.
Below are the images. I've tried changing the X 'Range' value in FOW Revealer, but it makes the view darker

It works great in here, I can't see the surrounding that is blocked by the wall
(http://i.imgur.com/1qP03YT.jpg)

I can see what's inside the next room and my room when the players is near the wall
(http://i.imgur.com/hJBPKEg.jpg)
Title: Re: Tasharen Fog of War
Post by: ArenMook on April 16, 2015, 08:25:00 AM
This can be caused by a few things off the top of my head:

1. Your sampled grid is not fine enough, so as your player gets close to the wall, it effectively overlaps with the wall's "node", thus being able to see through it due to being inside it.

2. The range's X parameter being set too large. It should be no larger than the radius of your character. Not sure what you meant by "view becoming darker" -- the Y parameter should be clearing that fog, not the X. If it isn't, then it leads to #3:

3. Your revealer may not be in the right place. It should be positioned at the character's eye level, and it needs to be above the floor but below the wall's height.
Title: Re: Tasharen Fog of War
Post by: Sudarmin on April 16, 2015, 11:35:02 PM
Hi Aren, thanks for the quick reply
I found the problem, it is because I accidentally put the Raycast Radius to more than 10, when I put it back to 0.5 and reduce the X range parameter to only the radius of the character, it works as I want it to be  :)
Thanks!!
Title: Re: Tasharen Fog of War
Post by: yuni on May 19, 2015, 03:05:31 AM
Hi
Shader compile error!
Please take a look at what the problem of this code.

Shader "Fog of War/4TexturesDiffuseFastFow"
{
   Properties
   {
      _Color ("Main Color", Color) = (1,1,1,1)
      _Layer1 ("Layer1 (RGB)", 2D) = "white" {}
      _Layer2 ("Layer2 (RGB)", 2D) = "white" {}
      _Layer3 ("Layer3 (RGB)", 2D) = "white" {}
      _Layer4 ("Layer4 (RGB)", 2D) = "white" {}
      _MainTex ("Mask (RGB)", 2D)  = "white" {}   
   }

   SubShader
   {
      Tags
      {
            "SplatCount"      = "4"
            "Queue"         = "Geometry-100"
            "RenderType"      = "Opaque"
      }                   

      CGPROGRAM
      #pragma surface surf Lambert exclude_path:prepass   noforwardadd halfasview novertexlights approxview   
   
      sampler2D _MainTex;
      sampler2D _Layer1, _Layer2, _Layer3,_Layer4;
      fixed4    _Color;

      sampler2D _FOWTex0, _FOWTex1;
      uniform float4    _FOWParams;
      uniform half4     _FOWUnexplored;
      uniform half4     _FOWExplored;      

      struct Input
      {
         float2 uv_MainTex : TEXCOORD0;
         float2 uv_Layer1  : TEXCOORD1;
         float2 uv_Layer2  : TEXCOORD2;
         float2 uv_Layer3  : TEXCOORD3;
         float2 uv_Layer4  : TEXCOORD4;

         float2 fog        : TEXCOORD5;
      };   

      void vert (inout appdata_full v, out Input o)
      {
         UNITY_INITIALIZE_OUTPUT(Input, o);
         
         float4 worldPos = mul (_Object2World, v.vertex);
         o.fog.xy = worldPos.xz * _FOWParams.z + _FOWParams.xy;
      }
                                            
      void surf (Input IN, inout SurfaceOutput o)
      {     
         fixed4 Mask = tex2D(_MainTex, IN.uv_MainTex);
         half4 col;
         col  = Mask.r * tex2D (_Layer1, IN.uv_Layer1);
         col += Mask.g * tex2D (_Layer2, IN.uv_Layer2);
         col += Mask.b * tex2D (_Layer3, IN.uv_Layer3);
         col += Mask.a * tex2D (_Layer4, IN.uv_Layer4);

         half4 fog = lerp(tex2D(_FOWTex0, IN.fog), tex2D(_FOWTex1, IN.fog), _FOWParams.w);

         //col = lerp(lerp(col * _FOWUnexplored, col * _FOWExplored, fog.g), col, fog.r);   <-- this code problem

         o.Albedo = col.rgb;
         o.Alpha  = col.a;
      
      }         
   
      ENDCG
   }
}




Title: Re: Tasharen Fog of War
Post by: ArenMook on June 01, 2015, 09:48:07 PM
That line isn't causing any issues, but the terrain shader does have problems with Unity 5. I don't know what they changed on their end but the shader that works fine in Unity 4 no longer has enough interpolator units and fails to work in Unity 5. It's on the list of things for me to look into at some point, but right now I am extremely busy with Windward.

I suggest sticking with the image-effect based approach, it works fine.
Title: Re: Tasharen Fog of War
Post by: directedchaos on June 20, 2015, 10:51:10 PM
It's a great effect, and using the provided shaders (terrain and diffuse) I got a simple scene running seemingly without a framerate hit on an iPhone 5.  Awesome!  I know it's not your target platform, but you might include some simple notes/suggestions.  For example, here's how I got it working (probably obvious to those more experienced with shaders):
Using those shaders was a bit of a breakthrough for me.  Using the effect on the camera showed cool results, but really hit the framerate hard on mobile.

I have two questions:
Again, it's a great effect, and I'm excited about using it!
Title: Re: Tasharen Fog of War
Post by: ArenMook on June 21, 2015, 09:47:12 PM
You should be able to use the Fog of War/Diffuse shader as an example to create your own version of any shader you need. Check the shader's code: there are 4 things that need to be added for the fog to work, and all 4 are commented for you.

Texture size will naturally increase the memory footprint and will also make the FOW's logic slower as it will need to update more pixels. Larger textures also take longer to upload to the GPU, possibly causing stutters when they need to get updated.
Title: Re: Tasharen Fog of War
Post by: directedchaos on June 22, 2015, 11:10:36 PM
Thanks for getting back to me!  Unfortunately I know very little about shaders, and my simple copy-paste of those 4 sections to the built-in "Transparent/Cutout/Diffuse" (download built-in shaders source code, AlphaTest-Diffuse.shader) results in odd stripes.  I suspect step 3 doesn't apply cleanly to the cutout shader.  I've played with all kinds of conversions (float, half, fixed), nothing seemed to change it, but if I force the fog.g and fog.r properties I can see it is blending properly.  So I think it's just not mapping to the right spot in the fog texture.  Also odd, the striping seems to change with the camera (the revealer isn't moving, just the camera).

Here's what I have:
  1. Shader "Custom/Cutout Diffuse (FoW)" {
  2. Properties {
  3.         _Color ("Main Color", Color) = (1,1,1,1)
  4.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
  5.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
  6. }
  7.  
  8. SubShader {
  9.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
  10.         LOD 200
  11.        
  12. CGPROGRAM
  13. #pragma surface surf Lambert alphatest:_Cutoff
  14.  
  15. sampler2D _MainTex;
  16. fixed4 _Color;
  17.  
  18. // FOW #1: Requires parameters (set by FOWSystem)
  19. sampler2D _FOWTex0, _FOWTex1;
  20. float4 _FOWParams;
  21. half4 _FOWUnexplored, _FOWExplored;
  22.  
  23. struct Input {
  24.         float2 uv_MainTex;
  25.  
  26.         // FOW #2: Fog texture coordinates
  27.         float2 fog : TEXCOORD2;
  28. };
  29.  
  30. void vert (inout appdata_full v, out Input o)
  31. {
  32.         UNITY_INITIALIZE_OUTPUT(Input, o);
  33.  
  34.         // FOW #3: Set the fog texture coordinates
  35.         float4 worldPos = mul (_Object2World, v.vertex);
  36.         o.fog.xy = worldPos.xz * _FOWParams.z + _FOWParams.xy;
  37. }
  38.  
  39. void surf (Input IN, inout SurfaceOutput o) {
  40.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  41.        
  42.         // FOW #4: Tint the final color by the Fog of War
  43.         half4 fog = lerp(tex2D(_FOWTex0, IN.fog), tex2D(_FOWTex1, IN.fog), _FOWParams.w);
  44.         c.rgb = lerp(lerp(c.rgb * _FOWUnexplored, c.rgb * _FOWExplored, fog.g), c.rgb, fog.r);
  45.  
  46.         o.Albedo = c.rgb;
  47.         o.Alpha = c.a;
  48. }
  49. ENDCG
  50. }
  51.  
  52. Fallback "Transparent/Cutout/Diffuse"
  53. }
  54.  

Here's Unity's built-in shader I'm trying to add Fog of War to:
  1. Shader "Transparent/Cutout/Diffuse" {
  2. Properties {
  3.         _Color ("Main Color", Color) = (1,1,1,1)
  4.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
  5.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
  6. }
  7.  
  8. SubShader {
  9.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
  10.         LOD 200
  11.        
  12. CGPROGRAM
  13. #pragma surface surf Lambert alphatest:_Cutoff
  14.  
  15. sampler2D _MainTex;
  16. fixed4 _Color;
  17.  
  18. struct Input {
  19.         float2 uv_MainTex;
  20. };
  21.  
  22. void surf (Input IN, inout SurfaceOutput o) {
  23.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  24.         o.Albedo = c.rgb;
  25.         o.Alpha = c.a;
  26. }
  27. ENDCG
  28. }
  29.  
  30. Fallback "Transparent/Cutout/VertexLit"
  31. }
  32.  
Title: Re: Tasharen Fog of War
Post by: ArenMook on June 25, 2015, 09:45:46 PM
You forgot to specify the vertex shader in your #pragma define:
  1. #pragma surface surf Lambert alphatest:_Cutoff
should be:
  1. #pragma surface surf Lambert vertex:vert alphatest:_Cutoff
Title: Re: Tasharen Fog of War
Post by: zorroMask on June 26, 2015, 02:34:52 AM
     I render my scene with a shadowmap, so I can't use the camera depthTextureMode = DepthTextureMode.Depth , cause there gonna be a depth texture conflict . So I replace the fragment shader _CameraDepthTexture , with a RenderTexture , format depth. That RenderTexture i create as an asset . The fragement shader runs well in Editor, but failed in android/ios gles rendering. I know that gles depth texture use ARB format , but how can i sample the depth texture?I try UnityCG.cginc micro ,like SAMPLE_DEPTH_TEXTURE, but that is included in HLSLSupport.cginc , cause some problem in my IOS rendering . I used unity3d 5.1.1 .


  1. Shader "Image Effects/Fog of War"
  2. {
  3.         Properties
  4.         {
  5.                 _MainTex ("Base (RGB)", 2D) = "white" {}
  6.                 _FogTex0 ("Fog 0", 2D) = "white" {}
  7.                 _FogTex1 ("Fog 1", 2D) = "white" {}
  8.                 _Unexplored ("Unexplored Color", Color) = (0.05, 0.05, 0.05, 0.05)
  9.                 _Explored ("Explored Color", Color) = (0.35, 0.35, 0.35, 0.35)
  10.         }
  11.         SubShader
  12.         {
  13.                 Pass
  14.                 {
  15.                         ZTest Always
  16.                         Cull Off
  17.                         ZWrite Off
  18.                         Fog { Mode off }
  19.                                        
  20. CGPROGRAM
  21. #pragma vertex vert
  22. #pragma fragment frag
  23. #pragma fragmentoption ARB_precision_hint_fastest
  24. #include "UnityCG.cginc"
  25.  
  26. sampler2D _MainTex;
  27. sampler2D _FogTex0;
  28. sampler2D _FogTex1;
  29. sampler2D _DepthTexture;
  30.  
  31. uniform float4x4 _InverseMVP;
  32. uniform float4 _Params;
  33. uniform float4 _CamPos;
  34. uniform half4 _Unexplored;
  35. uniform half4 _Explored;
  36.  
  37. struct Input
  38. {
  39.         float4 position : POSITION;
  40.         float2 uv : TEXCOORD0;
  41. };
  42.  
  43. Input vert (appdata_full v)
  44. {
  45.         Input o;
  46.         o.position = mul(UNITY_MATRIX_MVP, v.vertex);
  47.         o.uv = v.texcoord.xy;
  48.         return o;
  49. }
  50.  
  51. float3 CamToWorld (in float2 uv, in float depth)
  52. {
  53.         float4 pos = float4(uv.x, uv.y, depth, 1.0);
  54.         pos.xyz = pos.xyz * 2.0 - 1.0;
  55.         pos = mul(_InverseMVP, pos);
  56.         return pos.xyz / pos.w;
  57. }
  58.  
  59. fixed4 frag (Input i) : COLOR
  60. {
  61.         half4 original = tex2D(_MainTex, i.uv);  
  62. //      return original;
  63. #if SHADER_API_D3D9||SHADER_API_D3D10||SHADER_API_D3D11
  64.         float2 depthUV = i.uv;  
  65.         //depthUV.y = lerp(depthUV.y, 1.0 - depthUV.y, _CamPos.w);
  66.         //depthUV.y = 1.0 - depthUV.y;
  67.         float depth = tex2D(_DepthTexture, depthUV).r;
  68.         float3 pos = CamToWorld(depthUV, depth);
  69. #else
  70.         float depth = tex2D(_DepthTexture, i.uv).r;
  71.         float3 pos = CamToWorld(i.uv, depth);
  72. #endif
  73.        
  74.         // Limit the fog of war to sea level
  75.         if (pos.y < 0.0)
  76.         {
  77.                 // This is a simplified version of the ray-plane intersection formula: t = -( N.O + d ) / ( N.D )
  78.                 float3 dir = normalize(pos - _CamPos.xyz);
  79.                 pos = _CamPos.xyz - dir * (_CamPos.y / dir.y);
  80.         }
  81.        
  82.         float2 uv = pos.xz * _Params.z + _Params.xy;
  83.         half4 fog = lerp(tex2D(_FogTex0, uv), tex2D(_FogTex1, uv), _Params.w);
  84.        
  85.         return lerp(lerp(original * _Unexplored, original * _Explored, fog.g), original, fog.r);
  86. }
  87.  
Title: Re: Tasharen Fog of War
Post by: ArenMook on June 29, 2015, 09:00:48 AM
Sorry, I don't have enough experience with Unity's shaders to offer you a solution using render textures instead of the provided depth buffer. I'm also not sure what rendering the scene with a shadowmap has to do with the FOW not working. Why don't you do your custom rendering first, prior to rendering the scene? If scene is rendered last, then you shouldn't run into any issues.
Title: Re: Tasharen Fog of War
Post by: directedchaos on July 03, 2015, 11:53:06 PM
You forgot to specify the vertex shader in your #pragma define:
  1. #pragma surface surf Lambert alphatest:_Cutoff
should be:
  1. #pragma surface surf Lambert vertex:vert alphatest:_Cutoff

Thank you so much, that fixed it!  I now have a working Transparent/Cutout/Diffuse shader with Fog of War, very cool!  I'd be nice if you could include it, though I'm not sure about any legal complications since it's based on a free Unity shader.  It's pretty straightforward to add if you know about those steps though.  Another little perf tidbit, I'm finding that about 256x256 is getting to the upper limit on my iPhone 5 in terms of speed -- something like a second or two delay, which can be masked well with the fading.
Title: Re: Tasharen Fog of War
Post by: ArenMook on July 06, 2015, 09:00:55 PM
I've actually added it when I was looking into why your shader wasn't working. It'll be there after the next update ;)
Title: Re: Tasharen Fog of War
Post by: Pilltech on July 10, 2015, 03:57:42 PM
Hello I am having troubles trying to get you fog of war system to hide my grass on my terrains . Would any one be able to help me with this.


Thank you for your further help.
Title: Re: Tasharen Fog of War
Post by: ArenMook on July 12, 2015, 08:13:33 PM
What do you mean? A pic would help.
Title: Re: Tasharen Fog of War
Post by: Pilltech on July 13, 2015, 12:41:39 PM
I figured out my last problem. Thanks. I am currently having another one...   
the fog is reacting to some of my colliders in my scene.
(http://i.imgur.com/hRfT9Ch.png)

Dose any one Know how i would fix this ?
Title: Re: Tasharen Fog of War
Post by: ArenMook on July 15, 2015, 12:55:34 AM
Fog works by sampling screen depth (if you use the image effect-based approach). I don't know how you draw your plant, but whatever you have there effectively drew what looks like a cube into the depth buffer.
Title: Re: Tasharen Fog of War
Post by: devguy on September 13, 2015, 06:05:36 PM
Hello,

I had some quick questions i wanted to ask before purchasing the FOW asset.

-Is this asset still supported in the latest Unity version?
-Does the FOW have an "Explored" area feature? (EXAMPLE: Revealed area = 100% visibility, Explored area = 50% visibility & hidden units, Fog area = 0% visibility & hidden units)
-What is the usual framerate drop when using this?
-Is there also a Minimap example, if not, how difficult would it be to use a ortho camera or render texture to display the FOW for a minimap as well?

Thank you for your time.
Title: Re: Tasharen Fog of War
Post by: ArenMook on September 20, 2015, 04:01:26 AM
- Yes, it works fine with the latest Unity.
- FOWSystem.IsVisible(pos), FOWSystem.IsExplored(pos).
- Negligible, depends on which approach you take. Most of the logic happens on a separate thread. Image effect adds some minor fillrate like all image effects do (bloom etc). Shader based approach is usually effectively free (negligible performance effect).
- Yes there is.
Title: Re: Tasharen Fog of War
Post by: devguy on September 25, 2015, 04:50:04 PM
Thank you for the reply.

Had a new question.

How does the line of sight work in this package? Does it use a raycast in all directions or is it a radius height solution?
Title: Re: Tasharen Fog of War
Post by: ArenMook on September 26, 2015, 05:26:24 PM
There is an internal heightmap that gets generated at start. By default it's done by performing either raycasts or sphere casts downwards to determine the height of each point in the world. These values get cached. When it's time to do a visibility check, the FOW code running on a separate thread simply walks the path between two points, and if there is a node that happens to have a higher height value than what it should be (lerp(start, end, distance)), then the point is considered to be occluded.

Raycasts can't be done from separate threads, and they are too expensive anyway.
Title: Re: Tasharen Fog of War
Post by: John.Bergman on December 03, 2015, 11:09:22 AM
Any plans to upgrade this to Unity 5?  I noticed in this thread that you indicated that there was a shader issue with Unity 5 earlier.

John
Title: Re: Tasharen Fog of War
Post by: ArenMook on December 09, 2015, 01:48:55 PM
Wasn't it all fixed? Everything seems to work fine here.
Title: Re: Tasharen Fog of War
Post by: mboog12 on December 26, 2015, 07:24:14 AM
Hi ArenMook!
I'm interested in buying your asset. I have a 2d tile map on xy plan, like zmeinaz from page 6.
I've seen he had to make modifications to that system, but he doesn't get into much detail.
If the modifications are 2-3 lines of code, that sounds reasonable, but otherwise I wouldn't want to waste 8hours of work to change the whole system.
Any advice on this issue?

Kind regards!
Title: Re: Tasharen Fog of War
Post by: ArenMook on December 26, 2015, 09:40:39 PM
It's not 2-3 lines, more like ~12 across both shader and C# code. As for how long it would take, that would vary on your skill level so can't say for sure.
Title: Re: Tasharen Fog of War
Post by: hannahwills on December 27, 2015, 04:48:14 AM
Hi Aren,

Thanks so much for making such a great plugin! I know this question is very specific to my scene, but I have multiple floors that are stacked on top of each other, and when a revealer is on an inner floor it seems as if there aren't any nodes on that floor. I'm guessing this is because the FOW system is set up to only make one node in the heightmap per xz point, and it's colliding with something above or below it already, but I was wondering if there was a way to make layered floors work within the system. Each floor is exactly 10 units high.

Thanks again!


Title: Re: Tasharen Fog of War
Post by: ArenMook on December 27, 2015, 05:22:53 PM
By default the FOWSystem class uses Physics.SphereCast / Physics.Racast to determine if something is visible or not. This happens in FOWSystem.CreateGrid() function. Inside it all it does is creates the 2D heightmap using the sampled height values. This heightmap is then used in the FOW thread to determine visibility.

You can create a custom FOW class by deriving from FOWSystem and overwriting the CreateGrid() function. When switching from XZ 3D to XY 2D, you will want to do this and alter usage of 'z' to be 'y' inside that function.
Title: Re: Tasharen Fog of War
Post by: hannahwills on January 02, 2016, 07:20:48 PM
Thanks for getting back to me! I'm not sure I asked my question correctly, but I'm not trying to switch to an XY 2D, I want to keep the XZ plane but have multiple stacked height points. So for example, if I'm standing in a room, I'd like to not see the room below me as well as the other rooms on the same floor. With the 2D heightmap the rooms around me get blocked but not the ones above or below, even when I have the raycasting set to collide with the floor layers.

Thanks again!



Title: Re: Tasharen Fog of War
Post by: ArenMook on January 04, 2016, 05:38:35 AM
I would suggest you not show the bottom floor while you're on the 2nd floor, or at least block it somehow. The way the system works is it's flat basically. The height changes are done on a "if above Y, it's visible". There is no "if above Y and below Z" type checks because heightmaps only have one value -- each point's height.
Title: Re: Tasharen Fog of War
Post by: hannahwills on January 04, 2016, 03:58:13 PM
That makes sense, thanks!
Title: Re: Tasharen Fog of War
Post by: zmeinaz on February 28, 2016, 12:52:14 PM
Hello,

I am using FOW on the x-y plane with the shader method and my maps are taller than they are wide (150x350). Is it possible to adapt FOW to rectangular world sizes and texture sizes? I tried updating fowSystem but the revealed area is warped and the indices are off. For example, using the radius revealer, the revealed area is an oval that is wider than it is tall. I am trying to get a world size and texture size such that one pixel in the texture equals one unit on my map.

The idea is to avoid processing an extra 70,000 pixels of the texture that are unused in order to improve performance on low end devices.
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 01, 2016, 06:35:20 AM
If you're passing a rectangular texture to the shader, you will need to adjust the FOW shader to support a rectangular shape by basically stretching it / squishing the UVs in the shader as needed. Unfortunately I can't suggest anything specific...
Title: Re: Tasharen Fog of War
Post by: zmeinaz on March 01, 2016, 07:45:24 AM
I see what you mean... Is it possible to optimize the UpdateTexture function in FOWSystem.cs to only update indices that were changed since the previous update?

The reason I am looking into this sort of optimization is because I was originally using a world and texture size of 512, but I saw large performance spikes on older devices (iPad 3). Reducing to a size of 350 helps a lot, but I think it will still impact even older devices (iPad 1).
Title: Re: Tasharen Fog of War
Post by: ArenMook on March 06, 2016, 01:18:04 PM
Everything is possible, sure... but as I mentioned I can't suggest anything specific. Size 512 is indeed pretty big -- that's a 1 MB texture you're updating. In Windward I believe I use 256x256 if not 128x128. Fog of war is supposed to be pretty blurry anyway, there is little reason for that much precision.
Title: Re: Tasharen Fog of War
Post by: hiramtan on February 14, 2017, 02:40:18 AM
Please check fog of war on unity version 5.5.0f3, it's works on unity5.3.4, but when I upgrade unity to 5.5.0, it's no longer work anymore...really hope can fix this issue, Thanks!
Title: Re: Tasharen Fog of War
Post by: ArenMook on February 18, 2017, 08:08:24 PM
The fog of war package has been deprecated a long time ago now...
Title: Re: Tasharen Fog of War
Post by: hiramtan on February 20, 2017, 09:45:10 PM
I have modified shader to let it work on version 5.5.0,Thank you all the same.
Title: Re: Tasharen Fog of War
Post by: Velo on June 11, 2017, 06:42:23 PM
Hiramtan, when you modified the shader for 5.5.0, does the "Image Effect" fog of war work as well?  I'm really trying to get the Image Effect part of the asset to work again in Unity version 5.5 and beyond.  If so, would you mind sharing what you did to modify the shader?

And ArenMook, if you see this, I know the asset has been deprecated for a long time now, and you're not actively supporting it anymore, but is there any chance you could help me get the Image Effect to work again in 5.5+ (it almost works actually, I feel it just needs a small adjustment somewhere in either the shader or the c# script).  I'm also willing to pay you for it, so contact me if you're interested.

Thanks.
Title: Re: Tasharen Fog of War
Post by: Velo on June 11, 2017, 11:00:15 PM
Ok I figured out how to get the fog of war "Image Effect" to work again with Unity 5.5 (and perhaps higher versions as well).   :D

For anyone else that might want to know, Unity basically switched (inverted) the Zbuffer value around as of Unity 5.5.  So, what I had to do was in the Fog of War shader put in a line of code that is essentially:  "depth = 1 - depth" in the part of the shader that contains the definition for the depth variable.  This modifies the value to align with the change that Unity made regarding the Zbuffer.

So it works now, and I'm very happy about that.