cache
A core performance optimization node that prevents expensive, repetitive calculations within a single shader execution. It 'caches' a node's result into a temporary variable for subsequent reuse.
Core Advantages
It automates performance enhancement without breaking the declarative TSL workflow. By simply wrapping a costly node with cache(), TSL intelligently generates optimized GLSL, avoiding manual temporary variables and significantly boosting the frame rate (FPS) for complex materials.
Common Uses
Caching procedural noise: When a complex noise function is used to drive both vertex displacement and fragment color.
Caching texture processing results: When a texture sample undergoes complex adjustments (e.g., color correction) and the result is needed in multiple places (e.g., for base color and opacity).
Caching complex math formulas: When a computationally heavy lighting factor (like the Fresnel effect) influences diffuse, specular, and emission simultaneously.
How to adjust
This node has zero visual impact on the final render. The rendered image is pixel-for-pixel identical with or without cache. Its effect is on performance: a complex shader without cache might cause stuttering (low FPS), while the same shader with cache will run smoothly (high FPS).
Code Examples
1// Cache the expensive noise calculation
2const noise = cache( fbm( uv.mul(5) ) );
3
4// Use the cached result for both displacement and color
5const displacedPosition = position.add( normal.mul( noise ) );
6const finalColor = baseColor.mul( noise );