- Home /
Set shader's AlphaCutoff programmatically in HDRP
I've changed _AlphaCutoff for HDRP/Lit via C# like so:
Material output;
output.SetFloat("_AlphaCutoffEnable", alpha_threshold);
output.SetFloat("_AlphaCutoff", alpha_threshold);
output.SetFloat("_AlphaCutoffShadow", alpha_threshold);
Hit play and the cutoff doesn't work, however instantly upon clicking the material in the inspector (while still running in play mode), the cutoff sorts itself out and falls into effect. Apparently this is a known issue: https://answers.unity.com/questions/1030518/adjusting-alpha-cutoff-during-run-time-doesnt-work.html So I added the lines from the link above.
output.SetFloat("_Cutoff", alpha_threshold);
output.SetOverrideTag("RenderType", "TransparentCutout");
output.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
output.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
output.SetInt("_ZWrite", 1);
output.EnableKeyword("_ALPHATEST_ON");
output.DisableKeyword("_ALPHABLEND_ON");
output.DisableKeyword("_ALPHAPREMULTIPLY_ON");
output.renderQueue = 2450;
Still doesn't work with the new HDRP. Anyone know the alternative?
Answer by Weaver13 · May 06, 2021 at 05:31 PM
Figured it out. Unity deprecated keywords like _ALPHATEST_ON in favor of enums. Anyway shaders are icky so I don't know how much of this is important but here's everything I added.
output.SetFloat("_BlendMode", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
output.SetFloat("_ZTestGBuffer", 3);
output.SetFloat("_ZTestModeDistortion", 4);
output.SetFloat("_ZTestDepthEqualForOpaque", 3);
And now setting alpha cutoff for a material using HDRP/Lit during runtime works without having to touch the inspector.
Your answer
Follow this Question
Related Questions
Unity Mesh Based Shadows Overlap Issue 0 Answers
How to make my shader have a variable transparency 1 Answer
Silhouette overlay shader 0 Answers
Why does the 3D Text shader show specular at certain angles? 2 Answers
Shader Transparency issues 1 Answer