overloadingFn
Allows bundling multiple function implementations for different parameter types (e.g., float, vec3) into a single overloaded function, where TSL automatically selects the correct version based on input types at call time.
Core Advantages
Its core advantage is unifying the API, allowing developers to use one function name for multiple data types (e.g., a `noise` function supporting both 2D and 3D coordinates). The TSL compiler automatically resolves and matches the correct implementation, enabling the creation of clean, type-safe shader function libraries.
Common Uses
Creating polymorphic math utility functions (e.g., an `abs` function for float and vec3)
Implementing flexible custom noise functions (a `noise` function accepting vec2 or vec3 coordinates)
Designing a generic `mix` function that can interpolate different types (float, vec3, etc.)
How to adjust
Adjusted by modifying the array of function implementations passed at creation. For example, adding a new implementation that handles the `vec4` type to the array 'teaches' the overloaded function how to process `vec4` inputs. Conversely, removing an implementation will cause it to lose that capability and result in an error if called with that type.
Code Examples
1// 1. Define implementations for float and vec3 versions
2const addFloat = fn( [ float(), float() ], ( a, b ) => a.add( b ) );
3const addVec3 = fn( [ vec3(), vec3() ], ( a, b ) => a.add( b ) );
4
5// 2. Bundle them into a single overloaded function
6const addValue = overloadingFn( [ addFloat, addVec3 ] );