Author Topic: Tasharen Fog of War  (Read 145052 times)

zorroMask

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Tasharen Fog of War
« Reply #150 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.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tasharen Fog of War
« Reply #151 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.

directedchaos

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Tasharen Fog of War
« Reply #152 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tasharen Fog of War
« Reply #153 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 ;)

Pilltech

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Tasharen Fog of War
« Reply #154 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tasharen Fog of War
« Reply #155 on: July 12, 2015, 08:13:33 PM »
What do you mean? A pic would help.

Pilltech

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Tasharen Fog of War
« Reply #156 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.


Dose any one Know how i would fix this ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tasharen Fog of War
« Reply #157 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.

devguy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Tasharen Fog of War
« Reply #158 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tasharen Fog of War
« Reply #159 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.

devguy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Tasharen Fog of War
« Reply #160 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tasharen Fog of War
« Reply #161 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.

John.Bergman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Tasharen Fog of War
« Reply #162 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

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tasharen Fog of War
« Reply #163 on: December 09, 2015, 01:48:55 PM »
Wasn't it all fixed? Everything seems to work fine here.

mboog12

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Tasharen Fog of War
« Reply #164 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!