Adding fog to my sidescroller scene

Hi, my knowledge of shaders is really poor, but I’m trying to learn doing some stuff.

I have to add global fog with different colors to my game, which is a 2D game (Everything is sprites) but it has different layers (main terrain, trees, mountains in the background…) depending on how far is the object, more fog, of course.

What is the best way of doing this? Custom shaders? I guess it’s as simple as lerping the color in a shader, depending on the distance, but I don’t want to have to add a specific material to all my objects, it should be a global thing, don’t know if maybe added to the camera…

I am trying adding a shader to the camera, which is a custom shader from the GlobalFog one. but it doesn’t work at all… dpth is 1 always and rawDepth around 7000.

This is the code :

Shader "AlexTest/TestFog" {
    Properties{
        _MainTex("Base (RGB)", 2D) = "black" {}
    }

        CGINCLUDE

#include "UnityCG.cginc"

    uniform sampler2D _MainTex;
    uniform sampler2D_float _CameraDepthTexture;

    // for fast world space reconstruction
    uniform float4x4 _FrustumCornersWS;
    uniform float4 _CameraWS;

    struct v2f {
        float4 pos : SV_POSITION;
        float2 uv : TEXCOORD0;
        float2 uv_depth : TEXCOORD1;
        float4 interpolatedRay : TEXCOORD2;
    };

    v2f vert(appdata_img v)
    {
        v2f o;
        half index = v.vertex.z;
        v.vertex.z = 0.1;
        o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
        o.uv = v.texcoord.xy;
        o.uv_depth = v.texcoord.xy;   

        o.interpolatedRay = _FrustumCornersWS[(int)index];
        o.interpolatedRay.w = index;

        return o;
    }

    // Applies one of standard fog formulas, given fog coordinate (i.e. distance)
    half ComputeFogFactor(float coord)
    {
        float fogFac = coord;
       
        return saturate(fogFac);
    }

    // Distance-based fog
    float ComputeDistance(float3 camDir, float zdepth)
    {
        float dist;
        dist = zdepth * _ProjectionParams.z;
        // Built-in fog starts at near plane, so match that by
        // subtracting the near value. Not a perfect approximation
        // if near plane is very large, but good enough.
        dist -= _ProjectionParams.y;
        return dist;
    }

    half4 ComputeFog(v2f i) : SV_Target
    {
        half4 sceneColor = tex2D(_MainTex, i.uv);

        // Reconstruct world space position & direction
        // towards this screen pixel.
        float rawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,i.uv_depth);
        float dpth = Linear01Depth(rawDepth);
        float4 wsDir = dpth * i.interpolatedRay;
        float4 wsPos = _CameraWS + wsDir;

        // Compute fog distance
        float g = ComputeDistance(wsDir, dpth);

        // Compute fog amount
        half fogFac = ComputeFogFactor(max(0.0,g));

        //return fogFac; // for debugging
        return dpth; //For debugging

        // Lerp between fog color & original scene color
        // by fog amount
        return lerp(unity_FogColor, sceneColor, fogFac);
    }

        ENDCG

        SubShader
    {
        ZTest Always Cull Off ZWrite Off Fog{ Mode Off }

            Pass
        {
            CGPROGRAM
#pragma vertex vert
#pragma fragment frag
            half4 frag(v2f i) : SV_Target{ return ComputeFog(i); }
            ENDCG
        }
    }

    Fallback off

}

I would like to know what happens… depthTextureMode is set to Depth.

Thanks a lot :smile:

Bump!