When I write shader with variants via multi_compile_local (not multi_compile) Code (CSharp): #pragma multi_compile_local MASK_A MASK_R MASK_G MASK_B Complete shader code Code (CSharp): Shader "Custom/MyMaskTest" { Properties { _MainTex ("Main Texture", 2D) = "white" {} _MaskTex ("Mask Texture", 2D) = "white" {} } SubShader { Tags { "RenderType" = "Opaque" "Queue" = "Transparent" } Lighting Off ZWrite Off Cull Off Pass { CGPROGRAM #pragma multi_compile_local MASK_A MASK_R MASK_G MASK_B #include "UnityCG.cginc" #pragma vertex ComputeVertex #pragma fragment ComputeFragment sampler2D _MainTex; sampler2D _MaskTex; struct VertexInput { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; }; struct VertexOutput { float4 vertex : SV_POSITION; half2 texcoord : TEXCOORD0; }; VertexOutput ComputeVertex (VertexInput vertexInput) { VertexOutput vertexOutput; vertexOutput.vertex = UnityObjectToClipPos(vertexInput.vertex); vertexOutput.texcoord = vertexInput.texcoord; return vertexOutput; } fixed4 ComputeFragment (VertexOutput vertexOutput) : SV_Target { half4 colorFromMask = tex2D(_MaskTex, vertexOutput.texcoord); half testComponent; #if defined(MASK_A) testComponent = colorFromMask.a; //return half4(0,0,0,1); // todo TEST #endif #if defined(MASK_R) testComponent = colorFromMask.r; //return half4(1,0,0,1); // todo TEST #endif #if defined(MASK_G) testComponent = colorFromMask.g; //return half4(0,1,0,1); // todo TEST #endif #if defined(MASK_B) testComponent = colorFromMask.b; //return half4(0,0,1,1); // todo TEST #endif if (testComponent < 1) { discard; } return tex2D(_MainTex, vertexOutput.texcoord); } ENDCG } } } Then select shader variant via C# code Code (CSharp): _materialMyMaskTest.DisableKeyword("MASK_A"); _materialMyMaskTest.DisableKeyword("MASK_R"); _materialMyMaskTest.DisableKeyword("MASK_G"); _materialMyMaskTest.DisableKeyword("MASK_B"); // if (partType == ..G) { _materialMyMaskTest.EnableKeyword("MASK_G"); } else if (partType == ..B) { _materialMyMaskTest.EnableKeyword("MASK_B"); } else if (partType == ..A) { _materialMyMaskTest.EnableKeyword("MASK_A"); } else { _materialMyMaskTest.EnableKeyword("MASK_R"); } _materialMyMaskTest.SetTexture("_MaskTex", _combinedMask); Graphics.Blit(textureOrigin, textureResult, _materialMyMaskTest); In runtime I get error Code (CSharp): Shader error in 'Custom/MyMaskTest': variable 'testComponent' used without having been completely initialized at line 81 (on d3d11) Compiling Fragment program Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA ______ Note: I don't have "__ " (in first place of multi_compile_local or multi_compile) in allowed variants, i.e. only 4 - R | G | B | A, but not have "disabled" ______ Questions: * Why shader variants is not selected if - multi_compile_local (without block of 4s DisableKeyword it is NOT work too) * What is the semantic of multi_compile_local when I change it on material Code (CSharp): _materialMyMaskTest.EnableKeyword("MASK_G"); does it changes on concrete instance of material? In that case I can have 2 materials with different variant (A and G mask for example) * If I change to Code (CSharp): #pragma multi_compile MASK_A MASK_R MASK_G MASK_B (instead of multi_compile_local) the error is gone and all works as expected (but I thought multi_compile_local is better due to not expose keywords to global scope, as doc says) Why it work in that case? * If I change to "multi_compile" without "local" - does keywords can be assigned from any material or shader? or shader of type "Custom/MyMaskTest" only? Does keywords have any other problems? Possibility of collisions? (unity 5.6.5p4 if it's a bug of unity)
5.6.5p4 was released in 2017. It does not have the multi_compile_local pragma parameter, that was introduced in Unity 2019.1. Is there a specific reason you're still on such an old Unity version?
Thanks for info! We have tested unity 2017 - 2018 - 2019, and all the time we have encountered many unity bugs.. because of this we stay in the stable version, in which we develop our project in last years
What kind of bugs? Bugs should be reported so they can be fixed. If you are seeing red errors, that's not really a "bug", it just means some of your code is using deprecated/removed methods, but there are always better replacements for those methods, usually suggested to you in the error message so you can alter your code to use them. But if you're using some third-party addons that haven't been updated in years for newer Unity versions and you can't modify them yourself then yeah that can be an issue. 2018.4 is very stable though as it's the long-term-support release (though it doesn't have this multi_compile_local feature you want).
Thank you for info. We will migrate to 2018-2019 version in future. In past we have encountered bugs with webgl in runtime or build, which is not simple to reproduce or report. multi_compile_local is not must-have to us, if it will take many time to regressions tests and waiting for fixes of new unity versions