three.js
three.js copied to clipboard
TSL: Wrong compilation of `min(...)` and `max(...)` with 3 or more parameters
Description
TSL allows many parameters to functions min(...) and max(...). Using more than 2 parameters, however, fails:
- Both GLSL and WGSL trigger a syntax error, as they support only two parameters, but the code has 3 parameters
- The compiled code contains only the first three parameters; the rest are silently ignored
The GLSL error is:
THREE.WebGLBackend: Must have a compiled fragment shader attached:
SHADER_INFO_LOG:
ERROR: 0:56: 'min' : no matching overloaded function found
ERROR: 0:56: 'max' : no matching overloaded function found
The WGSL error is (significantly shortened):
Error while parsing WGSL: no matching call to
'min(abstract-float, abstract-float, abstract-float)'
overload expects 2 arguments, call passed 3 arguments
Suggested solutions
- compile
min(a,b,c,d,...)intomin(a,min(b,min(c,...))); and the same formax - throw an error message if more than 2 parameters are used
- describe in the documentation what functions accept multiple parameters and how they are dealt with when the functions are chained
Untested situation
Chained min(...) and max(...) are not tested
Reproduction steps
- Use
minormaxwith more than 3 parameters:
mn = min( 11, 12, 13, 14, 15 );
mx = max( 21, 22, 23, 24, 25 );
- Observe the error message in the console
Code
(see the live example)
Live example
Minimal code. Does not render anything meaningful, just generates the error messages.
https://codepen.io/boytchev/pen/RwmyQLY
Screenshots
Demonstration that only the first three parameters are compiled:
Using the TSL editor in GLSL mode:
Using the TSL editor in WGSL mode:
Version
r165
Device
Desktop
Browser
Chrome, Firefox
OS
Windows