assign
Performs an assignment operation (=) in TSL, used to change the value of a mutable variable (VarNode) or modify built-in geometry/material attributes.
Core Advantages
It is the core of state management in TSL. By storing computation results in variables or modifying built-in attributes (like `position`), it enables key functionalities such as logic control, code reuse, and vertex animation, while avoiding graph redundancy and performance loss from repeated calculations.
Common Uses
In conditional branches (If), assigning different values to a variable based on logic, such as changing a color.
As an accumulator in loops (Loop), continuously updating a variable's value, like summing light contributions.
In the vertex shader, modifying the built-in `position` attribute to achieve vertex animation or deformation.
Caching complex calculation results (like the Fresnel effect) into a variable for subsequent reuse, improving efficiency.
How to adjust
Adjusting the effect of an `assign` node means adjusting its `sourceNode` (the value being assigned). For example, in vertex animation, changing the value assigned to `position` from `position.mul(2)` (scaling the model) to `position.add(offset)` (breathing along normals) directly alters the model's geometry. In the fragment shader, changing the value assigned to `metalness` from `1.0` (fully metallic) to a sample from a noise texture will make the object's surface appear with a patchy, non-uniform metallic finish.
Code Examples
1// Calculate an offset based on time and normals
2const offset = normal.mul( sin( time() ).mul( 0.1 ) );
3
4// Assign the new position to the built-in 'position' attribute for vertex animation
5assign( position, position.add( offset ) );