Shader keywords workflow
Learn about defining shader keywords to create shaders that share some common code, but have different functionality when you enable or disable a keyword.
Read time 2 minutesLast updated 21 days ago
To control how shaders behave in different situations, use shader keywords. Each keyword acts as a toggle to change the shader's behavior.
You can use keywords to do the following:
- Configure different settings for a material.
- Make a shader work differently on different hardware.
- Dynamically change the behavior of a shader at runtime.
Use the following steps:
- Declare a shader keyword, or a set of shader keywords.
- Use an statement to branch the shader code depending on the state of the keyword.
if - Toggle the shader keyword to control which branch is active.
Declare a shader keyword
Declare a shader keyword, or a set of shader keywords, using a statement. The following example defines a set of 2 keywords called and :
#pragmaREDGREEN#pragma shader_feature RED GREEN
For more information, refer to Declare shader keywords.
Branch the shader code
Use an statement to make parts of your shader code execute based on the state of the keyword. For example, use the and keywords to control the color the shader outputs:
ifREDGREENvoid frag() : SV_Target { if (RED) { // Output red } else if (GREEN) { // Output green }}
For more information, refer to Make shader behavior conditional on keywords.
Toggle the shader keyword
To control the shader from the Editor or at runtime, refer to the following: