Break
Immediately terminates a `Loop` from within based on a specific condition, equivalent to the `break;` keyword in programming languages.
Core Advantages
It provides essential dynamic control for TSL loops, allowing algorithms to exit early once a condition is met, thus avoiding unnecessary computations. This is crucial for performance-sensitive algorithms with non-fixed iteration counts, such as ray marching.
Common Uses
In ray marching, `Break` the loop immediately when the ray hits an object (distance is less than a threshold).
In search algorithms, `Break` the loop as soon as the target is found to stop further searching.
In effect accumulation loops, `Break` to optimize performance when the contribution of further iterations becomes negligible.
How to adjust
The Break node itself is not configurable as it accepts no parameters. Its behavior is entirely determined by the `If` condition that triggers it. Adjusting this condition (e.g., changing the hit distance threshold in ray marching) alters when the loop terminates early, thereby affecting the algorithm's precision and performance.
Code Examples
1Loop( { end: MAX_STEPS }, () => {
2
3 const dist = sceneSDF( currentPos );
4
5 // If an object is hit, break the loop
6 If( dist.lessThan( HIT_EPSILON ), Break() );
7
8 // ...otherwise, continue marching
9 currentPos.addAssign( rayDir.mul( dist ) );
10
11} );