Return
Inside a custom function (Fn node), terminates the function's execution early, equivalent to the `return;` keyword in programming languages.
Core Advantages
It greatly improves code readability and cleanliness through the 'Guard Clause' pattern and optimizes shader performance by skipping unnecessary calculations via early exit. It brings fundamental yet crucial function-level control flow to TSL.
Common Uses
As a performance guard, checking conditions like distance at the start of a function and returning early to implement LOD.
As a logical switch, deciding whether to execute an expensive effect calculation based on conditions like lighting.
In conjunction with `If` to create clear logic branches and avoid deep nesting.
As a debugging breakpoint, temporarily inserted into a function to check intermediate calculation results.
How to adjust
The Return node itself is not configurable. Adjusting its effect means adjusting the `If` condition that triggers it. For example, in a function that draws a circle based on a radius, if the `Return` condition is `distance > radius`, decreasing the `radius` value will cause more pixels to meet the condition and exit early, visually shrinking the drawn circle.
Code Examples
1// Inside a custom function (Fn)
2const len = length( uv().sub( 0.5 ) );
3
4// If the pixel is outside the radius, exit the function early
5If( len.greaterThan( radius ), [
6 Return()
7] );
8
9// ... complex color calculations for inside the circle happen here