glsl-map
glsl-map copied to clipboard
Include variations with vector operand and other parameters scalar, similar to built-in functions
GLSL built-in functions usually have a variation where the operand is vector and the other parameters are all scalars, making it easier to specify common axes without converting data-type first.
For example, the built-in clamp
function has variations for each data-type, as this module does:
float clamp(float x, float minVal, float maxVal)
vec2 clamp(vec2 x, vec2 minVal, vec2 maxVal)
vec3 clamp(vec3 x, vec3 minVal, vec3 maxVal)
vec4 clamp(vec4 x, vec4 minVal, vec4 maxVal)
But also variations of each data-type operand where the last parameters are all float
:
vec2 clamp(vec2 x, float minVal, float maxVal)
vec3 clamp(vec3 x, float minVal, float maxVal)
vec4 clamp(vec4 x, float minVal, float maxVal)
This makes it easier to call clamp
where the axes of the range are the same, without converting to the operand's data-type first; e.g: the following 2 calls are equivalent:
clamp(vec2(1.0, 2.0), 0.0, 1.0);
clamp(vec2(1.0, 2.0), vec2(0.0), vec2(1.0));
This could be implemented by adding the following:
vec2 map(vec2 value, float inMin, float inMax, float outMin, float outMax) {
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}
vec3 map(vec3 value, float inMin, float inMax, float outMin, float outMax) {
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}
vec4 map(vec4 value, float inMin, float inMax, float outMin, float outMax) {
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}
(Could also be done similarly for int
/bool
as well as float
vector/scalar types; e.g: ivec2
/ivec3
/ivec4
operands with int
; bvec2
/bvec3
/bvec4
operands with bool
)