LYGIA Shader Library

map (lygia/math/map)

Map a v between one range to another.

Use:

<float|vec2|vec3|vec4> map(<float|vec2|vec3|vec4> v, <float|vec2|vec3|vec4> inMin, <float|vec2|vec3|vec4> inMax, <float|vec2|vec3|vec4> outMin, <float|vec2|vec3|vec4> outMax)

Check it on Github


#ifndef FNC_MAP
#define FNC_MAP
float map(float v, float iMin, float iMax ) { return (v-iMin)/(iMax-iMin); }
vec2 map(vec2 v, vec2 iMin, vec2 iMax ) { return (v-iMin)/(iMax-iMin); }
vec3 map(vec3 v, vec3 iMin, vec3 iMax ) { return (v-iMin)/(iMax-iMin); }
vec4 map(vec4 v, vec4 iMin, vec4 iMax ) { return (v-iMin)/(iMax-iMin); }

float map(in float v, in float iMin, in float iMax, in float oMin, in float oMax) { return oMin + (oMax - oMin) * (v - iMin) / (iMax - iMin); }
vec2 map(in vec2 v, in vec2 iMin, in vec2 iMax, in vec2 oMin, in vec2 oMax) { return oMin + (oMax - oMin) * (v - iMin) / (iMax - iMin); }
vec3 map(in vec3 v, in vec3 iMin, in vec3 iMax, in vec3 oMin, in vec3 oMax) { return oMin + (oMax - oMin) * (v - iMin) / (iMax - iMin); }
vec4 map(in vec4 v, in vec4 iMin, in vec4 iMax, in vec4 oMin, in vec4 oMax) { return oMin + (oMax - oMin) * (v - iMin) / (iMax - iMin); }
#endif

Use:

<float|float2|float3|float4> map(<float|float2|float3|float4> value, <float|float2|float3|float4> inMin, <float|float2|float3|float4> inMax, <float|float2|float3|float4> outMin, <float|float2|float3|float4> outMax)

Check it on Github


#ifndef FNC_MAP
#define FNC_MAP

float map( float value, float inMin, float inMax ) { return (value-inMin)/(inMax-inMin); }
float2 map( float2 value, float2 inMin, float2 inMax ) { return (value-inMin)/(inMax-inMin); }
float3 map( float3 value, float3 inMin, float3 inMax ) { return (value-inMin)/(inMax-inMin); }
float4 map( float4 value, float4 inMin, float4 inMax ) { return (value-inMin)/(inMax-inMin); }

float map(in float value, in float inMin, in float inMax, in float outMin, in float outMax) {
    return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

float2 map(in float2 value, in float2 inMin, in float2 inMax, in float2 outMin, in float2 outMax) {
    return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

float3 map(in float3 value, in float3 inMin, in float3 inMax, in float3 outMin, in float3 outMax) {
    return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

float4 map(in float4 value, in float4 inMin, in float4 inMax, in float4 outMin, in float4 outMax) {
    return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

#endif

Use:

<float|float2|float3|float4> map(<float|float2|float3|float4> v, <float|float2|float3|float4> inMin, <float|float2|float3|float4> inMax, <float|float2|float3|float4> outMin, <float|float2|float3|float4> outMax)

Check it on Github


#ifndef FNC_MAP
#define FNC_MAP
float map(float v, float iMin, float iMax ) { return (v-iMin)/(iMax-iMin); }
float2 map(float2 v, float2 iMin, float2 iMax ) { return (v-iMin)/(iMax-iMin); }
float3 map(float3 v, float3 iMin, float3 iMax ) { return (v-iMin)/(iMax-iMin); }
float4 map(float4 v, float4 iMin, float4 iMax ) { return (v-iMin)/(iMax-iMin); }

float map(float v, float iMin, float iMax, float oMin, float oMax) { return oMin+ (oMax - oMin) * (v - iMin) / (iMax - iMin); }
float2 map(float2 v, float2 iMin, float2 iMax, float2 oMin, float2 oMax) { return oMin+ (oMax - oMin) * (v - iMin) / (iMax - iMin); }
float3 map(float3 v, float3 iMin, float3 iMax, float3 oMin, float3 oMax) { return oMin+ (oMax - oMin) * (v - iMin) / (iMax - iMin); }
float4 map(float4 v, float4 iMin, float4 iMax, float4 oMin, float4 oMax) { return oMin+ (oMax - oMin) * (v - iMin) / (iMax - iMin); }
#endif

Check it on Github


fn map(v: f32, iMin: f32, iMax: f32, oMin: f32, oMax: f32) -> f32 { return oMin + (oMax - oMin) * (v - iMin) / (iMax - iMin); }
fn map2(v: vec2f, iMin: vec2f, iMax: vec2f, oMin: vec2f, oMax: vec2f) -> vec2f { return oMin + (oMax - oMin) * (v - iMin) / (iMax - iMin); }
fn map3(v: vec3f, iMin: vec3f, iMax: vec3f, oMin: vec3f, oMax: vec3f) -> vec3f { return oMin + (oMax - oMin) * (v - iMin) / (iMax - iMin); }
fn map4(v: vec4f, iMin: vec4f, iMax: vec4f, oMin: vec4f, oMax: vec4f) -> vec4f { return oMin + (oMax - oMin) * (v - iMin) / (iMax - iMin); }


Dependencies:

Use:

<float|float2|float3|float4> map(<float|float2|float3|float4> value, <float|float2|float3|float4> inMin, <float|float2|float3|float4> inMax, <float|float2|float3|float4> outMin, <float|float2|float3|float4> outMax)

Check it on Github



#ifndef FNC_MAP
#define FNC_MAP

inline __host__ __device__ float map( float value, float inMin, float inMax ) { return (value-inMin)/(inMax-inMin); }
inline __host__ __device__ float2 map( float2 value, float2 inMin, float2 inMax ) { return (value-inMin)/(inMax-inMin); }
inline __host__ __device__ float3 map( float3 value, float3 inMin, float3 inMax ) { return (value-inMin)/(inMax-inMin); }
inline __host__ __device__ float4 map( float4 value, float4 inMin, float4 inMax ) { return (value-inMin)/(inMax-inMin); }

inline __host__ __device__ float map(float value, float inMin, float inMax, float outMin, float outMax) {
  return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

inline __host__ __device__ float2 map(float2 value, float2 inMin, float2 inMax, float2 outMin, float2 outMax) {
  return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

inline __host__ __device__ float3 map(float3 value, float3 inMin, float3 inMax, float3 outMin, float3 outMax) {
  return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

inline __host__ __device__ float4 map(float4 value, float4 inMin, float4 inMax, float4 outMin, float4 outMax) {
  return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

#endif

Examples

Licenses

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository