OnBeforeMaterialUpdate
Registers a pre-material-update lifecycle event inside a TSL function (Fn) scope. The provided callback runs right before the material updates, letting you push JS-side values into nodes/uniforms ahead of GPU evaluation.
Core Advantages
A native, composable lifecycle hook for materials. It removes the need for manual render-loop plumbing or onBeforeCompile and binds to the Fn scope to avoid global side effects or duplicate registrations.
Common Uses
Write per-frame uniforms (time, mouse, GUI values) before evaluation to drive animation
Reset/sync state before switching textures or node branches to avoid race conditions
Upload or revise cached data (buffers, palette indices) prior to shading
Gate parts of the node graph via a boolean uniform toggle at update time
How to adjust
The node has no parameters. Adjust the effect by changing what the callback writes each frame. Prefer updating .value or material fields rather than constructing new nodes in the callback. Register once via Fn(...).call(). To disable, guard the callback body with a boolean uniform.
Code Examples
1// 1) A uniform updated by the event
2const uTime = uniform( 0 );
3
4// 2) Register the pre-update event within an Fn; call once to mount it
5Fn( () => {
6 OnBeforeMaterialUpdate( () => {
7 // Runs before each material update: push latest JS value
8 uTime.value = performance.now() * 0.001;
9 } );
10} ).call();
11
12// 3) Use the event-driven uniform to build a pulsing emissive
13const pulse = sin( uTime.mul( 2.0 ) ).mul( 0.5 ).add( 0.5 );
14material.emissiveNode = vec3( pulse );