defined
A JavaScript utility function for safely extracting the core value from either a primitive (e.g., 0.5) or a wrapper object (e.g., a Uniform) during initialization, simplifying API design.
Core Advantages
Its core advantage is enhancing the flexibility of the JavaScript-side API, allowing the same property or function parameter to accept both static primitive values and dynamic wrapper objects (like Uniforms), thus simplifying the development transition from static to dynamic values.
Common Uses
Uniformly handling static values and Uniform inputs when setting node properties
Allowing props to accept both numbers and Uniform objects when designing configurable components (e.g., in React)
Preprocessing parameters to support multiple input forms when writing generic helper functions
How to adjust
The function itself has no visual effect, but its usage determines the nature of the TSL property. Passing a primitive value (e.g., `0.8`) creates a static node. Per the provided implementation, passing a Uniform object also only extracts its initial value to create a static node, severing the dynamic link. Consequently, modifying the Uniform's value later in JavaScript will cause no visual change.
Code Examples
1// Assuming a material's setter uses defined() internally
2const roughnessUniform = new THREE.Uniform( 0.1 );
3
4// On assignment, defined() extracts the snapshot value 0.1
5myMaterial.roughness = roughnessUniform;
6
7// Later, modifying the Uniform has no effect, as the dynamic link is lost
8roughnessUniform.value = 1.0; // No visual change