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

Introduction to the shader programming language

3.0.8. MPD Toggle

This post is also available in…

Within ShaderLab we cannot use boolean type properties, instead, we have the Toggle that fulfils the same function. This drawer is going to allow switching from one state to another using a condition within our shader. To run it, we must first add the word Toggle between brackets and then declare our property, taking into account that it must be a Float type.

Its default value must be an integer, either zero or one, why? Because zero symbolizes “Off” and one symbolizes “On”.

Its syntax is as follows:

[Toggle] _PropertieName (“Display Name”, Float) = 0

As we see: we add the Toggle in brackets, then we declare the property, then the display name, followed by the Float data type and finally we initialize the property to “Off” since we add a zero in its default value.

Something that we must consider when working with this drawer is that, if we want to implement it in our code, we will have to use the #pragma shader_feature. This belongs to the shader variants and its function is to generate different conditions depending on the state it is in (enabled or disabled). To understand its implementation, we will do the following operation:

Shader “InspectorPath / shaderName”
        _Color (“Color”, Color) = (1, 1, 1, 1)    
        // declare Toggle drawer 
        [Toggle] _Enable (“Enable ?”, Float) = 0        
            // declare pragma 
            #pragma shader_feature _ENABLE_ON
            float4 _Color;    
            half4 frag (v2f i) : SV_Target
                half4 col = tex2D(_MainTex, i.uv);    
                // generate condition  
                #if _ENABLE_ON            
                    return col;        
                    return col * _Color;        

In this example, we declared a Toggle-type property called “_Enable”. Then we added it to the shader_feature found in the CGPROGRAM, however, unlike the property in our program, the Toggle has been declared as “_ENABLE_ON”, why is this? The variants added in shader_feature are “constants” therefore they are written in capitals, this means that if, for example, our property had been called _Change, then in the shader variant it should be added as “_CHANGE”. The word _ON corresponds to the default state of the Toggle, so, if the _Enable property is active, we return the default texture color in the fragment shader stage, otherwise we multiply the _Color property by itself.

It is worth mentioning that shader_feature cannot compile multiple variants for an application, what does this mean? Unity will not include variants that we are not using in the final build, which means that we will not be able to move from one state to another at execution time. For this, we will have to use the KeywordEnum drawer that has the variant shader “multi_compile”.

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 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.