Author Topic: Fog of War in Unity Basic (non pro)  (Read 12789 times)

VimDev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Fog of War in Unity Basic (non pro)
« on: January 19, 2014, 03:06:57 PM »
I have trying to get Tasharen Fog of War to work using Unity Basic (non pro version)
I have the example scene loaded and the red capsules disappear like they should when the green capsules are not able to see them. However: the terrain is not being shaded as unexplored.(Image attached)

I took a look at the officially recommended instructions from shiva.js here: http://forum.unity3d.com/threads/146697-Tasharen-Fog-of-War?p=1022831&viewfull=1#post1022831

Which are incredibly vague, it recommends that I "add the following to your environment shader/s"
without actually explaining where to find those shaders or where to add the following code to.

Is there any documentation or official instructions on how to use or troubleshoot this product? Not that I have found.

So far Tasharen Entertainment is disappointing me in terms of purchase satisfaction.

Please help me in locating a clear explanation on how to get Tasharen Fog of War to work with Unity basic. I'm prototyping at the moment so I don't even care if it just blacks out the unexplored areas.

Thanks in advance for any help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,152
  • Toronto, Canada
    • View Profile
Re: Fog of War in Unity Basic (non pro)
« Reply #1 on: January 20, 2014, 02:39:27 AM »
Instead of attaching FOWEffect, use this script:
  1. using UnityEngine;
  2.  
  3. public class FOWUpdater : MonoBehaviour
  4. {
  5.         public Color unexploredColor = new Color(0.05f, 0.05f, 0.05f, 1f);
  6.  
  7.         public Color exploredColor = new Color(0.2f, 0.2f, 0.2f, 1f);
  8.  
  9.         FOWSystem mFog;
  10.  
  11.         void LateUpdate ()
  12.         {
  13.                 if (mFog == null)
  14.                 {
  15.                         mFog = FOWSystem.instance;
  16.                         if (mFog == null) mFog = FindObjectOfType(typeof(FOWSystem)) as FOWSystem;
  17.                 }
  18.  
  19.                 if (mFog == null || !mFog.enabled)
  20.                 {
  21.                         enabled = false;
  22.                         return;
  23.                 }
  24.  
  25.                 float invScale = 1f / mFog.worldSize;
  26.                 Transform t = mFog.transform;
  27.                 float x = t.position.x - mFog.worldSize * 0.5f;
  28.                 float z = t.position.z - mFog.worldSize * 0.5f;
  29.                 Vector4 p = new Vector4(-x * invScale, -z * invScale, invScale, mFog.blendFactor);
  30.  
  31.                 Shader.SetGlobalColor("_Unexplored", unexploredColor);
  32.                 Shader.SetGlobalColor("_Explored", exploredColor);
  33.                 Shader.SetGlobalVector("_Params", p);
  34.                 Shader.SetGlobalTexture("_FogTex0", mFog.texture0);
  35.                 Shader.SetGlobalTexture("_FogTex1", mFog.texture1);
  36.         }
  37. }
You will also need to modify the shaders to sample the Fog of War textures. Here is an example of the Diffuse shader modified to sample the Fog of War:
  1. Shader "Fog of War/Diffuse"
  2. {
  3.         Properties
  4.         {
  5.                 _Color ("Main Color", Color) = (1,1,1,1)
  6.                 _MainTex ("Base (RGB)", 2D) = "white" {}
  7.         }
  8.         SubShader
  9.         {
  10.                 Tags { "RenderType"="Opaque" }
  11.                 LOD 200
  12.  
  13.                 CGPROGRAM
  14.                 #pragma surface surf Lambert vertex:vert
  15.  
  16.                 sampler2D _MainTex;
  17.                 fixed4 _Color;
  18.  
  19.                 sampler2D _FogTex0;
  20.                 sampler2D _FogTex1;
  21.  
  22.                 uniform float4 _Params;
  23.                 uniform half4 _Unexplored;
  24.                 uniform half4 _Explored;
  25.  
  26.                 struct Input
  27.                 {
  28.                         float2 uv_MainTex;
  29.                         float2 fog : TEXCOORD2;
  30.                 };
  31.  
  32.                 void vert (inout appdata_full v, out Input o)
  33.                 {
  34.                         UNITY_INITIALIZE_OUTPUT(Input, o);
  35.  
  36.                         float4 worldPos = mul (_Object2World, v.vertex);
  37.                         o.fog.xy = worldPos.xz * _Params.z + _Params.xy;
  38.                 }
  39.  
  40.                 void surf (Input IN, inout SurfaceOutput o)
  41.                 {
  42.                         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  43.  
  44.                         half4 fog = lerp(tex2D(_FogTex0, IN.fog), tex2D(_FogTex1, IN.fog), _Params.w);
  45.                         c.rgb = lerp(lerp(c.rgb * _Unexplored, c.rgb * _Explored, fog.g), c.rgb, fog.r);
  46.  
  47.                         o.Albedo = c.rgb;
  48.                         o.Alpha = c.a;
  49.                 }
  50.                 ENDCG
  51.         }
  52.         Fallback "Diffuse"
  53. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,152
  • Toronto, Canada
    • View Profile
Re: Fog of War in Unity Basic (non pro)
« Reply #2 on: January 20, 2014, 02:41:44 AM »
And just for clarity, here is the same Diffuse shader with FOW stuff commented out:
  1. Shader "Fog of War/Diffuse"
  2. {
  3.         Properties
  4.         {
  5.                 _Color ("Main Color", Color) = (1,1,1,1)
  6.                 _MainTex ("Base (RGB)", 2D) = "white" {}
  7.         }
  8.         SubShader
  9.         {
  10.                 Tags { "RenderType"="Opaque" }
  11.                 LOD 200
  12.  
  13.                 CGPROGRAM
  14.                 #pragma surface surf Lambert vertex:vert
  15.  
  16.                 sampler2D _MainTex;
  17.                 fixed4 _Color;
  18.  
  19.                 //sampler2D _FogTex0;
  20.                 //sampler2D _FogTex1;
  21.  
  22.                 //uniform float4 _Params;
  23.                 //uniform half4 _Unexplored;
  24.                 //uniform half4 _Explored;
  25.  
  26.                 struct Input
  27.                 {
  28.                         float2 uv_MainTex;
  29.                         //float2 fog : TEXCOORD2;
  30.                 };
  31.  
  32.                 void vert (inout appdata_full v, out Input o)
  33.                 {
  34.                         UNITY_INITIALIZE_OUTPUT(Input, o);
  35.  
  36.                         //float4 worldPos = mul (_Object2World, v.vertex);
  37.                         //o.fog.xy = worldPos.xz * _Params.z + _Params.xy;
  38.                 }
  39.  
  40.                 void surf (Input IN, inout SurfaceOutput o)
  41.                 {
  42.                         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  43.  
  44.                         //half4 fog = lerp(tex2D(_FogTex0, IN.fog), tex2D(_FogTex1, IN.fog), _Params.w);
  45.                         //c.rgb = lerp(lerp(c.rgb * _Unexplored, c.rgb * _Explored, fog.g), c.rgb, fog.r);
  46.  
  47.                         o.Albedo = c.rgb;
  48.                         o.Alpha = c.a;
  49.                 }
  50.                 ENDCG
  51.         }
  52.         Fallback "Diffuse"
  53. }

VimDev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Fog of War in Unity Basic (non pro)
« Reply #3 on: January 26, 2014, 02:15:31 PM »
Okay, it took some figuring out but I have it working. (I want to post what I did when I have some more time)

My game is using the 2D physics engine so I had to change FOWSystem.cs to recognize 2D colliders, and that works. However I think the shaders you gave me sample the fog assuming the height map is along the y axis? And 2D colliders just won't work that way.

I'm looking through the shaders trying to confirm that is my (new) issue or not. It seems reasonable but I don't understand shaders enough to read them yet.

I will also try to send Tasharen my adaptation for 2D FoW when I have it working perfectly because I believe anyone buying this deserves to have that as well.

Thank you, updates to come.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,152
  • Toronto, Canada
    • View Profile
Re: Fog of War in Unity Basic (non pro)
« Reply #4 on: January 26, 2014, 09:27:43 PM »
Yup, it samples the vertical XZ position:
  1. o.fog.xy = worldPos.xz * _Params.z + _Params.xy;
Note the "worldPos.xz" part. You can change it to 'xy' if you need it.

jchowdown

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Fog of War in Unity Basic (non pro)
« Reply #5 on: April 15, 2015, 04:58:29 PM »
Just curious about this section of shader code:

  1. lerp(c.rgb * _Unexplored, c.rgb * _Explored, fog.g)
  2.  

Should/Can the 'c.rgb' be pulled out of the Lerp() to save one multiply?

  1. c.rgb * lerp(_Unexplored, _Explored, fog.g)
  2.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,152
  • Toronto, Canada
    • View Profile
Re: Fog of War in Unity Basic (non pro)
« Reply #6 on: April 16, 2015, 08:19:36 AM »
Yes, that makes sense. While at it, change it to do .rgb on _Unexplored and _Explored.
  1. c.rgb = lerp(c.rgb * lerp(_FOWUnexplored.rgb, _FOWExplored.rgb, fog.g), c.rgb, fog.r);