Var
Declares a local variable within a shader to store the result of a node's (or node tree's) calculation, allowing it to be reused in multiple places to avoid redundant computation and logical clutter.
Core Advantages
Ensures a complex calculation is performed only once per shader execution, significantly improving performance, and greatly enhances the readability and maintainability of the node graph by allowing intermediate results to be named.
Common Uses
Caching complex procedural noise values
Creating a shared animation driver value for multiple uses
Storing a mask value for blending or conditional logic
How to adjust
The Var node itself cannot be adjusted; its output is entirely determined by its input node. To change the result of a Var, you must modify the node tree passed into it, e.g., `Var(uv().mul(10))` will store a UV value scaled by 10.
Code Examples
1// 1. Define a calculation (e.g., a sine wave)
2const sineWave = timerLocal().sin();
3
4// 2. Use Var to store the result in a variable, ensuring it's calculated only once
5const myVar = Var( sineWave );
6
7// 3. Reuse the variable in multiple places, e.g., driving color and position
8const finalColor = color( myVar, myVar, 0 );