Material.DisableKeyword Utilization

How is the disable keyword supposed to be used? I have a shader with a few custom keywords. Example of what I do below

#pragma multi_compile COMPOSITE_ON COMPOSITE_OFF
#pragma multi_compile OTHER_OPTION_ON OTHER_OPTION_OFF

//some stuff here

#if defined(COMPOSITE_ON)
float4 result;
result = tex2D(_MainTex,i.uv) * mask;
result.a = 1;
return result;
#else
return half4(mask,mask,mask,1);
#endif

Then to turn compositing on I call

material.EnableKeyword("COMOSITE_ON");

and to turn it off I call

material.EnableKeyword("COMPOSITE_OFF");

This works but doesn’t utilize the DisableKeyword() that I’d like to learn how to use.
More importantly, for some reason this doesn’t update in the editor until I click play and again wont update until I unclick play. its very annoying and I’m hoping that using DisableKeyword() can fix this.

Lastly here are other things I’ve tried that DO NOT work. These are examples I found to do on other posts, etc.

if defined in combination with EnableKeyword() and DisableKeyword(). Shader acts as if never defined

//no multi_compile pragma
#if defined(COMPOSITE_ON)
//some stuff
#else
//somestuff
#endif

using #pragma multi_compile with EnableKeyword() and DisableKeyward()
shader acts as if compositing is always on and can not be disabled.

#pragma multi_compile COMPOSITE_ON
#pragma multi_compile OTHER_OPTION_ON

//some stuff
#ifdef COMPOSITE_ON
//composite
#else
//dont composite
#endif

For anyone else wondering how to do this. I sort of figured it out

define your keywords like this

#pragma multi_compile COMPOSITE_ON COMPOSITE_OFF

inside your custom inspector code you could either recreate the keyword list or do what i did.

            if(isCompositingOn)
            {
                myTarget.material.DisableKeyword(COMPOSITING_OFF);
                myTarget.material.EnableKeyword(COMPOSITING_ON);
            }
            else
            {
                myTarget.material.DisableKeyword(COMPOSITING_ON);
                myTarget.material.EnableKeyword(COMPOSITING_OFF);
            }

material.EnableKeyword() will add the keyword to the shader list but won’t remove the other keyword. In order to do that you must use material.DisableKeyword()

Hello there!
I’m having a similar problem, using EnableKeyword and DisableKeyword doesnt really update my game windows.
It only seems to work in Scene view. To make it work in Game view I need to actually stop and re-run the game (similar to what you said before).
Did you manage to make it work properly?
Thanks!

Hi. sorry for the late reply.
I did manage to make it work properly but I don’t know completely why. I made a custom inspector for my post process effect. That seemed to make the game view update immediately. I’m attaching the package so that you can view it if you’d like.

1672449–104769–SobelEdgeDetection.unitypackage (15.3 KB)

I was just having a quick look into this myself, the documentation is quite solid in this area: Unity - Manual: Declaring and using shader keywords in HLSL

It says:

This means that for your above example, you should be able to do something like:

// Can define two shader variants like this
#pragma multi_compile __ COMPOSITE_ON

// instead of this (the composite_off keyword doesn't need to be defined)
//    #pragma multi_compile COMPOSITE_ON COMPOSITE_OFF

Here’s a quick example with ā€˜RED_TINT_ON’

Calling mat.EnableKeyword(ā€œRED_TINT_ONā€) should turn on the red tint, and mat.DisableKeyword(ā€œRED_TINT_ONā€) should turn it off.

Shader "Example/MultiCompile" {
  Properties {
      _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf Lambert
      #pragma multi_compile __ RED_TINT_ON
      struct Input {
          float2 uv_MainTex;
      };
      sampler2D _MainTex;
      void surf (Input IN, inout SurfaceOutput o) {
          #ifdef RED_TINT_ON
          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * half3(1.0,0.75,0.75);
          #else
          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
          #endif
      }
      ENDCG
    }
    Fallback "Diffuse"
  }
1 Like