Switch
Selects one output from a list of options based on an integer index, serving as the core node for multi-way branching (switch-case) in TSL.
Core Advantages
It abstracts the underlying GLSL switch logic into a clean node, allowing multiple visual effects to be encapsulated within a single material. By updating one integer uniform, effects can be switched dynamically at runtime, avoiding the performance overhead of managing or swapping materials and greatly improving code readability and interactivity.
Common Uses
Implementing terrain splat mapping by selecting grass, sand, or rock materials based on an index from a texture.
Creating a PBR material debugger to switch between views like final color, normals, UVs, or metalness via a uniform.
Toggling different visual effects based on character state (e.g., normal, poisoned, enraged).
Implementing material-level LOD by switching between a simple lighting model and a complex PBR model based on distance.
How to adjust
There are two primary ways to adjust a Switch node. First, by changing its `index` input (typically an integer uniform), which determines which `case` is selected, thus switching between predefined visual options like from a 'final color' view to a 'normal' view. Second, by modifying a node within the `cases` array itself, which alters the content of that specific option, for instance, replacing a purple 'poisoned' effect with a blue 'frozen' effect.
Code Examples
1// debugMode is a uniform integer (e.g., 0, 1, 2)
2const finalOutput = Switch( debugMode, [
3 pbrColor, // Case 0: Final PBR color
4 normalWorld, // Case 1: World-space normal
5 vec3( uv, 0 ) // Case 2: UV coordinates
6] );