Version: Unity 6 (6000.0)
Language : English
Introduction to static and dynamic branching
Branch in a shader via built-in macros

Static and dynamic branching in a shader

For more information, refer to Introduction to static and dynamic branching in shaders.

Static branching

You can use static branching in your shadersA program that runs on the GPU. More info
See in Glossary
in the following ways:

  • In hand-coded shaders:
    • Use #if, #elif, #else, and #endif preprocessor directives, or #ifdef and #ifndef preprocessor directives to create static branches.
    • Use an if statement that evaluates a compile-time constant value. Although if statements can also be used for dynamic branches, the compiler detects the compile-time constant value and creates a static branch instead.
    • Unity provides built-in macros for some compile-time constants that you can use with static branches.

Note: Static branching is available only in hand-coded shaders. You cannot create static branches in Shader Graph.

Dynamic branching

You can use dynamic branching in your shaders in the following ways:

  • In hand-coded shaders:
    • Optional: Set up shader keywords for use with with dynamic branching. You can use dynamic branching without shader keywords, but this provides simple set up and per-material configuration.
    • Use an if statement that evaluates the shader keywords (if used) or any other runtime state. You can use attributes to force the GPU to execute both branches, or to execute only one branch.
  • In Shader Graph, use a Branch Node. This always executes both branches.
Introduction to static and dynamic branching
Branch in a shader via built-in macros