Changing how shaders work using keywords
How Unity compiles branching shaders
Shader keywords workflowTo control how shaders behave in different situations, use shaderA program that runs on the GPU. More info
See in Glossary keywords. Each keyword acts as a toggle to change the shaderâs behavior.
You can use keywords to do the following:
Use the following steps:
if
statement to branch the shader code depending on the state of the keyword.Declare a shader keyword, or a set of shader keywords, using a #pragma
statement. The following example defines a set of 2 keywords called RED
and GREEN
:
#pragma shader_feature RED GREEN
For more information, refer to Declare shader keywords.
Branch the shader codeUse an if
statement to make parts of your shader code execute based on the state of the keyword. For example, use the RED
and GREEN
keywords to control the color the shader outputs:
void frag() : SV_Target {
if (RED)
{
// Output red
}
else if (GREEN)
{
// Output green
}
}
For more information, refer to Make shader behavior conditional on keywords.
Note: You can also use HLSL conditional statements like if
and preprocessor statements like #if
without using shader keywords.
To control the shader from the Editor or at runtime, refer to the following:
Additional resourcesChanging how shaders work using keywords
How Unity compiles branching shaders
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4