namespace
Creates an independent compilation context for a node to ensure the correct data flow when it's reused across different shader stages (e.g., vertex and fragment).
Core Advantages
By providing precise control over the compilation context, it ensures that modified vertex data (like a displaced world position) is correctly passed to the fragment shader, preventing rendering errors and unlocking advanced effects.
Common Uses
Getting the correct, post-displacement vertex position in the fragment shader
Creating separate scopes for `.once()` optimization, allowing it to execute once per shader stage
How to adjust
In a material that performs vertex displacement, failing to wrap the `positionWorld` in the fragment shader with `namespace` will cause the color pattern to be calculated based on the object's original, pre-displacement position, resulting in a visual mismatch with the final distorted geometry.
Code Examples
1// In the fragment shader, use namespace to get the new, post-displacement position
2const finalPosition = namespace( positionWorld, 'fragment' );
3
4// Calculate color based on the new position
5material.colorNode = finalPosition.normalize();