Get an 20% OFF using the RELEASE code on your book purchase. For a limited time.

Introduction to the shader programming language

3.0.9. MPD KeywordEnum

This post is also available in…

This drawer generates a pop-up style menu in the material inspector. Unlike a Toggle, this drawer allows you to configure up to nine different states for the shader. To execute it we must add the word “KeywordEnum” in brackets and then list the set of states that we are going to use.

[KeywordEnum(State_Off, State_01, etc...)] 
    _PropertieName (“Display name”, Float) = 0

In the previous example, we add the KeywordEnum drawer in brackets and then we list its states, where the first corresponds to the default state (StateOff). We continue with the property declaration, display name in the material inspector, its Float data type and finally, we initialize with its default value. 

To declare this drawer within our code we can use both the shader variant shader_feature and multi_compile. The choice will depend on the number of variants that we want to include in the final build.

As we already know, shader_feature will only export the selected variant from the material inspector, whereas multi_compile exports all variants that are found in the shader, regardless of whether they are used or not. Given this feature, multi_compile is great for exporting or compiling multiple states that will change at execution time (e.g. star status in Super Mario).

To understand its implementation, we will perform the following operation: 

Shader “InspectorPath / shaderName”
{
    Properties 
    {
    // declare Toggle drawer
    [KeywordEnum(Off, Red, Blue)] 
        _Options (“Color Options”, Float) = 0
    } 
    SubShader
    {
        Pass
        {
            CGPROGRAM
            ...
            // declare pragma and conditions 
            #pragma multi_compile _OPTIONS_OFF _OPTIONS_RED  _OPTIONS_BLUE
            ...
            half4 frag (v2f i) : SV_Target
            {
                half4 col = tex2D(_MainTex, i.uv);    
    
                // generate conditions 
                #if _OPTIONS_OFF 
                     return col;        
                #elif _OPTIONS_RED 
                     return col * float4(1, 0, 0, 1);        
                #elif _OPTIONS_BLUE
                     return col * float4(0, 0, 1, 1);    
                #endif
            }
            ENDCG
        }
    }
}

In this example, we declare a KeywordEnum type property called “_Options” and configure three states for it (Off, Red and Blue). Later we add them to the multi_compile found in CGPROGRAM and declare them as constants. 

#pragma multi_compile _OPTIONS_OFF _OPTIONS_RED  _OPTIONS_BLUE

Finally, using the conditionals we define the three states for our shader that correspond to color changes for the main texture.

Follow us to stay informed about all the latest news, updates, and more.

Join the group to share your experiences with other developers.

Subscribe to our channel and keep learning game dev!

jettelly-logo

Jettelly Team

We are a team of indie developers with more than 9 years of experience in video games. As an independent studio, we have developed Nom Noms in which we published with Hyperbeard in 2019. We are currently developing The Unity Shader Bible.

Follow us on our social networks.