Skip to content

Instantly share code, notes, and snippets.

@yagero
Last active February 13, 2022 15:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yagero/71aec53a1f7d2738590b547e9e8b63bc to your computer and use it in GitHub Desktop.
Save yagero/71aec53a1f7d2738590b547e9e8b63bc to your computer and use it in GitHub Desktop.
Helpful extension methods to customize Unity's shaders render state from script
public static class ShaderRenderState
{
public enum ZWrite //couldn't find any similar enum in UnityEngine.Rendering
{
Off = 0,
On = 1
}
public static void SetStencilRef(this Material mat, int value) { mat.SetInt("_StencilRef", value); }
public static void SetStencilComp(this Material mat, UnityEngine.Rendering.CompareFunction value) { mat.SetInt("_StencilComp", (int)value); }
public static void SetCull(this Material mat, UnityEngine.Rendering.CullMode value) { mat.SetInt("_Cull", (int)value); }
public static void SetZWrite(this Material mat, ZWrite value) { mat.SetInt("_ZWrite", (int)value); }
public static void SetZTest(this Material mat, UnityEngine.Rendering.CompareFunction value) { mat.SetInt("_ZTest", (int)value); }
}
/*
USAGE FROM SCRIPT:
material.SetCull(UnityEngine.Rendering.CullMode.Back);
material.SetZWrite(ShaderRenderState.ZWrite.Off);
material.SetZTest(UnityEngine.Rendering.CompareFunction.Greater);
USAGE FROM SHADER:
SubShader
{
Cull[_Cull]
ZWrite[_ZWrite]
ZTest[_ZTest]
// ...
Pass
{
CGPROGRAM
// ...
ENDCG
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment