subBuild
Encapsulate a piece of shader logic into a reusable, named component so it can be efficiently shared across places (e.g., vertex and fragment shaders) while automatically managing cross-stage data flow.
Core Advantages
Implements a “define once, use in many places” pattern. TSL can automatically optimize it to avoid code duplication and, when needed, manage data transfer from the vertex to the fragment shader, greatly simplifying complex shader development.
Common Uses
Generating procedural noise used in both vertex and fragment stages
Computing geometric attributes in the vertex shader (such as curvature) and using them in the fragment shader
Creating modular, composable shader effect libraries
How to adjust
Adjust by modifying its internal node logic; the change propagates to every usage. If the same subBuild node is used in both the vertex and fragment shaders, TSL will automatically create a varying to pass the data.
Code Examples
1// Wrap the core logic into a reusable component
2const wavePattern = subBuild( sin( uv().x.mul( 20 ) ), 'wavePattern' );
3
4// Use it in the vertex shader to create displacement...
5material.positionNode = position.add( vec3( 0, wavePattern.mul( 0.1 ), 0 ) );
6
7// ...and reuse it in the fragment shader for coloring
8material.colorNode = color( wavePattern );