Custom Fog of War

Hi everyone! :slight_smile:

I’m actually searching for an advanced fog of war feature. I searched for days but as I am not an expert in shader programming, it’s very difficult for me to find what I want (and even more to understand how)… :frowning:

So, I’m searching for a fog that hides only specific elements (instead of everything until now), so that the background remains always visible:


Actually, I already have a fog of war, wich is a plane, placed over the entire scene. It has a shader that displays as many visibility zones as necessary, it also darkens the background but the coins behind are visible.

Here is my shader:

Shader "FogOfWarShader" {
    Properties {
        _Color("Color", Color) = (1,1,1,1)
        _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
        FogMaxRadius("FogMaxRadius", Float) = 0.5
    }

    SubShader {
        Tags {"Queue" = "Transparent+500" "IgnoreProjector" = "True" "RenderType" = "Transparent+500"}
        LOD 200
        Blend SrcAlpha OneMinusSrcAlpha
        Cull Off

        CGPROGRAM
        #pragma surface surf Lambert vertex:vert alpha:blend

        sampler2D   _MainTex;
        fixed4      _Color;
        float       FogRadius[30];
        float       FogMaxRadius;
        int         NumberOfZone = 0;
        float4      VisibilityZones[30];

        struct Input {
            float2 uv_MainTex;
            float2 location;
        };

        float powerForPos(float4 pos, float radius, float2 nearVertex);

        void vert(inout appdata_full vertexData, out Input outData) {
            float4 pos = UnityObjectToClipPos(vertexData.vertex);
            float4 posWorld = mul(unity_ObjectToWorld, vertexData.vertex);
            outData.uv_MainTex = vertexData.texcoord;
            outData.location = posWorld.xz;
        }

        void surf(Input IN, inout SurfaceOutput o) {
            fixed4 baseColor = tex2D(_MainTex, IN.uv_MainTex) * _Color;

            float alpha = 1.0 - baseColor.a;
            for (int i = 0; i < NumberOfZone; i++)
            {
                alpha -= powerForPos(VisibilityZones[i], FogRadius[i], IN.location);
            }
            o.Albedo = baseColor.rgb;
            o.Alpha = alpha;
        }

        //return 0 if (pos - nearVertex) > _FogRadius
        float powerForPos(float4 pos, float radius, float2 nearVertex)
        {
            float atten = clamp(radius - length(pos.xz - nearVertex.xy), 0.0, radius);

            return (1.0 / FogMaxRadius) * atten / radius;
        }
        ENDCG
    }
    Fallback "Transparent/VertexLit"
}

I’ve seen some logic to do with different cameras but I don’t understand how to make it:

I’ve also found this that might help:

PS: I don’t use URP, only Basic Pipeline.
PS2: I don’t want a SphereCast where elements appears and disappears on the map according to the visibility zones positions. I want a smooth transition.

Can someone help me? :slight_smile:

Have you searched in the asset store?