min
Returns the minimum of two or more values.
Test min in a live shader
Open the playground, start from a visual preset, and wire min into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo
The min function returns the smallest of the input values. It supports both two-argument and variadic forms, accepting any number of arguments.
Signature
min :: (T a, T b) -> T {...}
min :: (T a, T b, T c, ...) -> T {...}
Where T can be float, float2, float3, float4, int, int2, int3, int4, uint, uint2, uint3, or uint4.
Parameters
| Parameter | Type | Description |
|---|---|---|
a, b, ... | T | Values to compare |
Return Value
Returns the minimum value among all inputs.
Example
pipeline ShadowBlending {
fragment {
// Sample multiple shadow maps and take the darkest
float shadow1 = sample(shadowMap1, input.shadowUV1).r;
float shadow2 = sample(shadowMap2, input.shadowUV2).r;
float shadow3 = sample(shadowMap3, input.shadowUV3).r;
// Minimum shadow = darkest shadow wins
float combinedShadow = min(shadow1, shadow2, shadow3);
float3 lit = baseColor * (ambient + combinedShadow * diffuse);
output.color = float4(lit, 1.0);
}
}Common Use Cases
Clamping Maximum
// Cap a value at maximum threshold
float capped = min(value, maxThreshold);Component-wise Minimum
// Find darkest color channel
float darkest = min(color.r, min(color.g, color.b));
// Or with variadic form:
float darkest = min(color.r, color.g, color.b);Distance Field Operations
// Union of two shapes (SDF)
float shape = min(sphereDist, boxDist);Bounds Limiting
// Limit light attenuation
float attenuation = 1.0 / (distance * distance);
attenuation = min(attenuation, 1.0); // Cap at 1Safe Division
// Prevent division by zero while keeping values bounded
float safeDiv = value / max(divisor, 0.001);
float result = min(safeDiv, maxResult);Variadic Support
BWSL's min function accepts 2 to 255 arguments. When more than 2 arguments are provided, they are combined pairwise during compilation.
Vector Operations
When used with vectors, min operates component-wise. Use with a scalar to clamp all components: min(color, float3(1.0)).
Compiled Output
When compiled to GLSL:
min(a, b)
When compiled to HLSL:
min(a, b)