- Home /
How to show render texture in a 2D game (Fog of War).
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
}
}
}
Answer by ArtOfProgramming · Oct 25, 2021 at 10:34 AM
I think you can look into spritemasks for this to solve it. not sure but I think it will help you find the solution for it.
Can you be a little bit more specific? I will look into this though, thank you.
well with spritemasks you can like reveal an area. for example that your units have explored. this will require to change the spritemask at runtime but I think it can be a solid solution if done right
Oh I get it, however, currently the problem is, I have 2 render textures set up, where both shows a black area (currently unexplored) and pink area (explored or is currently exploring) , these textures will update on runtime, now the problem is, how do I get these two shaders onto the game scene, meaning, how do I make it so, the black area are completely black, and the pink areas are either half black or no vision block.
Your answer
Follow this Question
Related Questions
Shaders making raw image invisible 0 Answers
Fog of War Explored Area Gray out / un-updated areas 0 Answers
RTS fog of war texture remapping shader,Fog of war plane texture remapping shader HDRP 0 Answers
Sprite texture incorrect 0 Answers
point light illuminates makes transparent object visible 0 Answers