How to compute fog in HLSL on URP?

Hi,
Anyone has a simple example on how to implement basic fog in URP hlsl?
The built-in way documented here does not work properly on URP.

Thanks!

I am wondering the same thing. I found this ( From Built-in to URP ) but the functions mentioned don’t seem to work properly. I’m trying to figure out why but I just can’t get it to work …

Did anyone figure this out?

I ended up figuring it out.
In ShaderVariablesFunctions.hlsl you have the fog methods.

I made this function to apply it.

void Applyfog(inout float4 color, float3 positionWS)
{
    float4 inColor = color;
  
    #if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
    float viewZ = -TransformWorldToView(positionWS).z;
    float nearZ0ToFarZ = max(viewZ - _ProjectionParams.y, 0);
    float density = 1.0f - ComputeFogIntensity(ComputeFogFactorZ0ToFar(nearZ0ToFarZ));

    color = lerp(color, unity_FogColor,  density);

    #else
    color = color;
    #endif
}
1 Like