Is there any way to turn on/off shader's alpha test function in a C# script? This is the shader sample. I want to set off and on the alpha test in script. (* The alpha test is added using pragma keywords. *) Code (CSharp): Shader "Alpharized Shadow" { Properties { _Color ("Main Color", Color) = (1, 1, 1, 1) _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {} _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5 _Multiplier("Color Multiplier",Range(1,10)) = 1.0 } SubShader { Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" } LOD 200 Cull Off Lighting Off CGPROGRAM #pragma surface surf Lambert alphatest:_Cutoff sampler2D _MainTex; fixed4 _Color; float _Multiplier; struct Input { float2 uv_MainTex; }; void surf (Input IN, inout SurfaceOutput o) { fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color * _Multiplier; o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } Fallback "Transparent/Cutout/VertexLit" }
I'm no shader expert, but maybe this will help you a bit: http://forum.unity3d.com/threads/changing-the-alpha-component-of-a-material.893/
@asd234w4r5 that thread is from late 2005. It's so outdated it's not even funny. @jitwtttl I found a way, though you'll have to move the alphatest to the surf function and use discard. What you do is to use #pragma shader_feature to create a variant with alphatest. I'll include the altered CGPROGRAM, as everything else is the same. Bolded lines are changed. Code (csharp): CGPROGRAM #pragma shader_feature ALPHATEST #pragma surface surf Lambert sampler2D _MainTex; fixed4 _Color; float _Multiplier; float _Cutoff; struct Input { float2 uv_MainTex; }; void surf (Input IN, inout SurfaceOutput o) { fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color * _Multiplier; #if defined (ALPHATEST) if(c.a < _Cutoff) discard; #endif o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG To activate the ALPHATEST stuff, use material.EnableKeyword to turn it on, and material.DisableKeyword to turn it off. The reason for why I'm using discard instead of alphatest:_Cutoff is that it seems like you can't wrap #pragma statements in an #if defined-block, if I try to do this: Code (csharp): #if defined (ALPHATEST) #pragma surface surf Lambert alphatest:_Cutoff #else #pragma surface surf Lambert #endif I get the error message "Duplicate #pragma surface found" on the statement without the alphatest. The discard should do the same thing, though it might be a bit slower, as it contains an if-statement, though there's a good chance that it's optimized away in the compiled shader.
Great Idea! Baste. Thank you. : ) And I think shader's multi_compile can be another alternatives. Official Document Link
Yup. I based my advice on shader_feature instead of multi_compile because the docs says that "shader_feature makes most sense for keywords that will be set on the materials, while multi_compile for keywords that will be set from code globally." In addition, I think this: Code (csharp): #pragma shader_feature ALPHATEST is prettier than this: Code (csharp): #pragma multi_compile __ ALPHATEST Where "__" means "if ALPHATEST is not defined"