You can create shaders that share some common code, but have different functionality when a given keyword is enabled or disabled.
Internally, shader keywords work by creating shader variants. Before you use shader keywords, it is important to understand how shader variants work, and the potential impact on performance and workflow. For information on this topic, see Shader variants.
You declare shader keywords in sets. A set contains mutually exclusive keywords.
For example, the following set contains three keywords:
Internally, shader keywords use
#define preprocessor directives. When you declare a set of shader keywords, Unity compiles shader variants with matching
Note: In Shader Graph, the terminology is different: a set of keywords is called a Keyword, and the keywords in a set are called states. Internally, the functionality is the same: Unity compiles them in the same way, you work with them the same way with C# scripts, and so on.
The number of keywords that you declare and the way that you declare these keywords has a significant impact on the number of shader variants that Unity compiles, which in turn can have a significant effect on the performance of your project and application. For more information, see Shader variants.
When you declare a set of keywords, you choose whether the keywords in the set have local or global scope. This affects Unity’s behavior when you enable and disable shader keywords. For more information on enabling and disabling shader keywords, see Using shader keywords with C# scripts.
Note: If a keyword with the same name exists in a shader source file and its dependencies, the scope of the keyword in the source file overrides the scope in the dependencies. Dependencies comprise all Shaders that are included via the Fallback command, and Passes that are included via the UsePass command.
When you declare a set of keywords, you choose the technique that Unity uses to define them. This affects the number of shader variants that Unity compiles.
Which option is best depends on how you use the keywords. If you use the keywords to configure materials in your project and do not change their value from C# scripts at runtime, then you should use “shader feature” to reduce the number of shader keywords and variants in your project. If you enable and disable keywords at runtime using C# scripts, then you should use “multi compile” to prevent variants being stripped in error. For more information on shader stripping, see Shader variant stripping.
By default, Unity generates keyword variants for each stage of your shader. For example, if your shader contains a vertex stage and a fragment stage, Unity generates variants for every keyword combination for both the vertex and the fragment shader programs. If a set of keywords is only used in one of those stages, this results in identical variants for the other stage. Unity automatically identifies and deduplicates identical variants so that they do not increase build size, but they still result in wasted compilation time, increased shader loading times, and increased runtime memory usage.
To avoid this problem, when you declare a set of keywords in a hand-coded shader, you can instruct Unity to compile them only for a given shader stage.
Note: You are responsible for ensuring that the keywords are only used in the specified shader stages.
Unity does not fully support the use of stage-specific keyword directives for the following graphics APIs:
To set this value in hand-coded shaders, see Declaring and using shader keywords in HLSL. You cannot change this value in Shader Graph; by default, all keywords affect all stages.
You can mark sections of your shader source file so that Unity only includes that functionality in variants that are used when a given keyword is enabled.
The way that you do this is different in hand-coded shaders and in Shader Graph. For instructions for hand-coded shaders, see Defining and using shader keywords. For instructions for Shader Graph, see Shader Graph: Keyword Node.
You can enable or disable shader keywords. When you enable or disable a shader keyword, Unity uses the appropriate shader variant for rendering.
There are two ways to enable and disable shader keywords:
Unity uses predefined sets of shader keywords to enable common functionality. It adds the following sets of shader keywords at compile time:
Unity can use up to 4,294,967,294 global shader keywords. Individual shaders and compute shaders can use up to 65,534 local shader keywords.
If Unity encounters a shader keyword with the same name multiple times, it only counts towards the limit once.
If a shader uses more than 128 keywords in total, it incurs a small runtime performance penalty; therefore, it is best to keep the number of keywords low.