LYGIA Shader Library

within (lygia/math/within)

Similar to step but for an interval instead of a threshold. Returns 1 is x is between left and right, 0 otherwise

Use:

<float> within(<float> _min, <float|vec2|vec3|vec4> maxVal, <float|vec2|vec3|vec4> x)

Check it on Github


#ifndef FNC_WITHIN
#define FNC_WITHIN
float within(in float x, in float _min, in float _max) {
    return step(_min, x) * (1. - step(_max, x));
}

float within(in vec2 x, in vec2 _min, in vec2 _max) {
    vec2 rta = step(_min, x) * (1. - step(_max, x));
    return rta.x * rta.y;
}

float within(in vec3 x, in vec3 _min, in vec3 _max) {
    vec3 rta = step(_min, x) * (1. - step(_max, x));
    return rta.x * rta.y * rta.z;
}

float within(in vec4 x, in vec4 _min, in vec4 _max) {
    vec4 rta = step(_min, x) * (1. - step(_max, x));
    return rta.x * rta.y * rta.z * rta.w;
}
#endif

Use:

<float> within(<float> minVal, <float|float2|float3|float4> maxVal, <float|float2|float3|float4> x)

Check it on Github



#ifndef FNC_WITHIN
#define FNC_WITHIN
float within(in float minVal, in float maxVal, in float x) {
    return step(minVal, x) * (1. - step(maxVal, x));
}

float within(in float2 minVal, in float2 maxVal, in float2 x) {
    float2 rta = step(minVal, x) * (1. - step(maxVal, x));
    return rta.x * rta.y;
}

float within(in float3 minVal, in float3 maxVal, in float3 x) {
    float3 rta = step(minVal, x) * (1. - step(maxVal, x));
    return rta.x * rta.y * rta.z;
}

float within(in float4 minVal, in float4 maxVal, in float4 x) {
    float4 rta = step(minVal, x) * (1. - step(maxVal, x));
    return rta.x * rta.y * rta.z * rta.w;
}
#endif

Check it on Github


fn within(x: f32, _min: f32, _max: f32) -> f32 {
    return step(_min, x) * (1. - step(_max, x));
}

fn within2(x: vec2f, _min: vec2f, _max: vec2f) -> f32 {
    let rta = step(_min, x) * (1. - step(_max, x));
    return rta.x * rta.y;
}

fn within3(x: vec3f, _min: vec3f, _max: vec3f) -> f32 {
    let rta = step(_min, x) * (1. - step(_max, x));
    return rta.x * rta.y * rta.z;
}

fn within4(x: vec4f, _min: vec4f, _max: vec4f) -> f32 {
    let rta = step(_min, x) * (1. - step(_max, x));
    return rta.x * rta.y * rta.z * rta.w;
}

Dependencies:

Use:

<float> within(<float> minVal, <float|float2|float3|float4> maxVal, <float|float2|float3|float4> x)

Check it on Github



#ifndef FNC_WITHIN
#define FNC_WITHIN
inline __host__ __device__ float within(float minVal, float maxVal, float x) {
    return step(minVal, x) * (1.0f - step(maxVal, x));
}

inline __host__ __device__ float within(const float2& minVal, const float2& maxVal, const float2& x) {
    float2 rta = step(minVal, x) * (1.0f - step(maxVal, x));
    return rta.x * rta.y;
}

inline __host__ __device__ float within(const float3& minVal, const float3& maxVal, const float3& x) {
    float3 rta = step(minVal, x) * (1.0f - step(maxVal, x));
    return rta.x * rta.y * rta.z;
}

inline __host__ __device__ float within(const float4& minVal, const float4& maxVal, const float4& x) {
    float4 rta = step(minVal, x) * (1.0f - step(maxVal, x));
    return rta.x * rta.y * rta.z * rta.w;
}
#endif

License

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