- Home /
Fog of War Shader help - prevent light changes
Hello,
I followed this old tutorial on implementing a fog of war shader... (https://www.youtube.com/watch?v=PNAvNeOTnSE) In essence, it's implemented by putting a plane over the entire game world, applying a raw texture to that plane which has circles at each units position, and using a custom shader to make those circles transparent. The circles are bright green and the background is bright blue so it knows where to apply transparency.
I have got it mostly working, the only problem is it seems to effect the lighting. I don't know enough about shaders to know why this is.
I've attached two screenshots, one is with the FOW plane disabled, the other is with it enabled. The game becomes a lot brighter with the FOW plane enabled. This screenshot is taken at midday (when the sun is at it's highest intensity), so the opposite effect happens when at midnight (night time), it becomes REALLY dark.
I don't want the light to be affected at all, the ONLY thing I want is for the circles to be fully transparent, and for the remaining plane to be semi-transparent with a black colouring like it currently is. Please assist me if possible and thanks for reading.
Code:
Shader "Custom/FogOfWar" {
Properties {
_Color("Main Color", Color) = (1,1,1,1)
_MainTex("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags {"RenderType"="Transparent" "LightMode"="ForwardBase"}
Blend SrcAlpha OneMinusSrcAlpha
Lighting Off
LOD 200
CGPROGRAM
#pragma surface surf NoLighting noambient alpha
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, float aten)
{
fixed4 color;
color.rgb = s.Albedo;
color.a = s.Alpha;
return color;
}
sampler2D _MainTex;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
};
void surf(Input IN, inout SurfaceOutput o) {
half4 baseColor = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = _Color.rgb * baseColor.b;
o.Alpha = _Color.a - baseColor.g; // Green - colour of aperture mask
}
ENDCG
}
}
Answer by Namey5 · Aug 20, 2020 at 04:40 AM
A few things;
1. The 'LightMode; tag should only go inside a pass and isn't necessary in surface shaders. For transparent shaders however, you should set the 'Queue' tag to transparent;
Tags {"RenderType"="Transparent" "Queue"="Transparent"}
2. This is a surface shader, so you don't need to manually handle blending;
//Remove this
Blend SrcAlpha OneMinusSrcAlpha
...
//Change 'alpha' to 'alpha:fade'
CGPROGRAM
#pragma surface surf NoLighting noambient alpha:fade
3. Make sure your alpha is only in a range of [0,1], otherwise you can end up with unexpected behaviour if you aren't careful;
o.Alpha = saturate (_Color.a - baseColor.g);
Your answer
Follow this Question
Related Questions
RTS fog of war texture remapping shader,Fog of war plane texture remapping shader HDRP 0 Answers
Change shader properties during rungtime 1 Answer
Fog of War Effect Help 0 Answers
How to hide a texture partially without rendering to it? 0 Answers
How to show render texture in a 2D game (Fog of War). 1 Answer