I am making a RTS game, and I am currently implementing a Fog of War system. I set it up so I currently have 2 render textures, and these render textures will update when any of the Units or Structures are in the scene and moving (I gave each unit/structure a FOV GameObject and gave it a sphere mesh renderer + filter). I also have 2 cameras that represents the “UnexploredArea” and “ExploredArea”, and the size is set to fit the entire map.
So basically, my idea is to have unexplored area to be completely black, and explored but currently not in vision area to be semi black and the area within units vision will be completely white.
The render textures updates (I can see it update in the editor at runtime) , however, I saw some articles and they said to use 2 projectors to project the render textures onto the screen, the problem is his tutorial is for a 3D game, whereas mine is a 2D game. I believe that using the projector for my game wouldn’t work because my game is 2D. I have also tried moving the projectors on the Z axis and went into 3D mode to make sure the projector is ‘projecting’ onto my map/tilemap. But no luck there.
I was wondering if there is any way I can get my render textures onto the screen or maybe I used my projectors wrong.
For both my projectors, I have a material with a custom shader script (see below) on it:
Shader "Custom/FogProjection"
{
Properties
{
_PrevTexture("Previous Texture", 2D) = "white" {}
_CurrTexture("Current Texture", 2D) = "white" {}
_Color("Color", Color) = (0, 0, 0, 0)
_Blend("Blend", Float) = 0
}
SubShader
{
Tags { "Queue" = "Transparent+100" } // to cover other transparent non-z-write things
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
ZTest Equal
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float4x4 unity_Projector;
sampler2D _CurrTexture;
sampler2D _PrevTexture;
fixed4 _Color;
float _Blend;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = mul(unity_Projector, v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float aPrev = tex2Dproj(_PrevTexture, i.uv).a;
float aCurr = tex2Dproj(_CurrTexture, i.uv).a;
fixed a = lerp(aPrev, aCurr, _Blend);
// weird things happen to minimap if alpha value gets negative
_Color.a = max(0, _Color.a - a);
return _Color;
}
ENDCG
}
}
}