loop
A core flow-control node for creating structured for-loops within TSL, fundamental for implementing advanced iterative algorithms.
Core Advantages
Safely brings imperative for-loop logic into TSL's declarative node system. It abstracts low-level loop syntax into a clean configuration and callback, greatly enhancing readability and safety, and making complex algorithms like Ray Marching, fractals, and FBM noise possible in pure TSL.
Common Uses
Generating FBM (Fractional Brownian Motion) clouds or terrain by accumulating multiple layers of noise.
Serving as the core of Ray Marching algorithms to render complex shapes defined by mathematics.
Implementing high-quality blur or bloom effects by iteratively sampling neighboring pixels.
Generating fractal art (like the Mandelbrot set) by repeatedly applying mathematical iterations to each pixel.
How to adjust
Adjusting its `count` parameter or modifying the `loopBody` callback is key to controlling its visual effect. For example, in FBM noise, increasing `count` from 2 to 8 will transform blurry, large patches of clouds into a detailed, complex texture. Similarly, introducing a `time` node inside the `loopBody` to alter the calculation (e.g., moving a rendered object's position) can turn a static algorithm (rendering a sphere) into a dynamic animation (a wobbling sphere).
Code Examples
1// Example of a loop that accumulates a value 8 times
2const finalValue = loop( { count: 8, initialValue: 0 }, ( { value, i } ) => {
3
4 // 'value' is the result from the previous iteration, 'i' is the current index (0-7)
5 const newValue = value.add( i.mul( 0.1 ) );
6
7 return newValue; // Return the updated value for this iteration
8
9} );