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)

URP and GPU instancing & MPB

Tested with: 2020.1.0b16.4138 + URP 9.0.0-preview.38, Windows DX11

Objects with same material & same property values

SRP batcher ON
material GPU instancing OFF
SRP batched
SRP batcher ON
material GPU instancing ON
SRP batched
(because if SRP batcher is ON, gpu instancing will be ignored)
SRP batcher OFF
material GPU instancing ON
GPU instanced

Objects with different material & different property values

SRP batcher ON
material GPU instancing OFF
SRP batched
SRP batcher ON
material GPU instancing ON
SRP batched
(because if SRP batcher is ON, gpu instancing will be ignored)
SRP batcher OFF
material GPU instancing ON
Not batched nor instanced
(expected because they are different materials)

Objects with same material & different property values set by MaterialPropertyBlock

SRP batcher ON
material GPU instancing OFF
Not batched nor instanced
(because MPB values are not same)
SRP batcher ON
material GPU instancing ON
Not batched nor instanced
(same as above)
SRP batcher OFF
material GPU instancing ON
Not batched nor instanced
(because properties are non-instanced)

According to Unity Documentation about GPU instancing, user can use MaterialProperyBlock to have different material properties for each instances.
Apparently the test showed that this is not true anymore in Universal Render Pipeline.


Reason:

URP shader properties are all wrapped by SRP batcher macros “CBUFFER_START(UnityPerMaterial)”.
GPU instancing need to use this macro “UNITY_INSTANCING_BUFFER_START(MyProps)”


Solution:

Continue reading “URP and GPU instancing & MPB”

3D scene need Linear but UI need Gamma

update: including UniversalRP (URP) workaround at bottom!

Having this problem?
https://forum.unity.com/threads/gamma-colors-for-ui-and-linear-for-scene.529923/

20190530_problem.jpg

Looking for a way to make only the UI matches to what is designed in Photoshop? Don’t want artists to change any workflow(possible solution for artists please refer to this comment) because you are in the middle of development? Try this.

Continue reading “3D scene need Linear but UI need Gamma”