Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.