Disable Fog for URP Lit ShaderGraph

Scenario: An environment with many objects with different materials that need fog but there is some special ShaderGraph material objects that do not need fog.

Hack for Editor:

  1. Of course you have enabled Fog in LightingSettings
  2. Create a ShaderGraph and add these 3 FOG keywords
  3. Make sure the Reference are exactly “FOG_LINEAR“, “FOG_EXP” and “FOG_EXP2
  4. Create a material and use this ShaderGraph
  5. I’m using Exponential Squared fog in Lighting Settings so I turn off “FOG_EXP” and “FOG_EXP2”, but leaving “FOG_LINEAR” checked does the trick

Hack for Player:

  1. Create a shader variant stripping script
  2. Put it in Editor folder
  3. Make a player build
using System.Collections.Generic;
using UnityEditor.Build;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.Rendering;

class StrippingExample_Shader : IPreprocessShaders
{
    public StrippingExample_Shader()
    {

    }

    public int callbackOrder { get { return 99; } }

    public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> data)
    {
        for (int i = 0; i < data.Count; ++i)
        {
            //Get a string of keywords
            string variantText = "";
            foreach(ShaderKeyword s in data[i].shaderKeywordSet.GetShaderKeywords())
            {
                variantText += " " +s.name;
            }

            bool wantToStrip = false;

            //Only stripping the shader graph that we don't want fog
            if(
                shader.name == "Shader Graphs/ShaderGraphNoFog" &&
                variantText.Contains("FOG_")
            )
            {
                wantToStrip = true;
            }

            if ( wantToStrip )
            {
                //Strip the variant
                data.RemoveAt(i);
                --i;
            }
        }
    }
}

Get the test project here if you need (Open with Unity 2022.2)