shaders : what is the use of UNITY_FOG_COORDS, UNITY_TRANSFER_FOG, UNITY_APPLY_FOG

hey shader fans,
i’m new to unity shaders, i was wondering what is the use of UNITY_FOG_COORDS, UNITY_TRANSFER_FOG, UNITY_APPLY_FOG commands.
i thought fog was mainly an image effect…
is it for a use in some specific cases like water ?
thanks for explanations

These are some macros to help you respect the global fog settings when writing custom shaders. If you’re writing a surface shader I believe this is handled for you so you can ignore the macros.
In Unity 5, if you create a new “Unlit Shader”, the new file is populated by a template that shows proper use of these macros.

I believe this line is required for the macros to work:

// make fog work
#pragma multi_compile_fog    

Take a look at the vertex shader output struct in the template:

struct v2f
{
    float2 uv : TEXCOORD0;
    UNITY_FOG_COORDS(1)
    float4 vertex : SV_POSITION;
};

The UNITY_FOG_COORDS macro creates a struct member to hold the coordinates for the fog. Since TEXCOORD0 is already in use, an index of 1 is supplied to the macro to tell it to use TEXCOORD1.

In the vertex shader, this line:

UNITY_TRANSFER_FOG(o,o.vertex);

Calculates the fog coordinates and stores them into the v2f struct. And in the fragment shader:

// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;

Applies fog to the fragment color right before returning it.