LYGIA Shader Library

ratio (lygia/space/ratio)

Fix the aspect ratio of a space keeping things squared for you, in a similar way that aspect.glsl does, but while scaling the space to keep the entire 0.0,0.0 ~ 1.0,1.0 range visible

Use:

<vec2> ratio(<vec2> st, <vec2> st_size)

Check it on Github


#ifndef FNC_RATIO
#define FNC_RATIO
vec2 ratio(in vec2 v, in vec2 s) {
    return mix( vec2((v.x*s.x/s.y)-(s.x*.5-s.y*.5)/s.y,v.y),
                vec2(v.x,v.y*(s.y/s.x)-(s.y*.5-s.x*.5)/s.x),
                step(s.x,s.y));
}
#endif

Use:

<float2> ratio(<float2> st, <float2> st_size)

Check it on Github


#ifndef FNC_RATIO
#define FNC_RATIO
float2 ratio(in float2 st, in float2 s) {
    return lerp(    float2((st.x*s.x/s.y)-(s.x*.5-s.y*.5)/s.y,st.y),
                    float2(st.x,st.y*(s.y/s.x)-(s.y*.5-s.x*.5)/s.x),
                    step(s.x, s.y));
}
#endif

Check it on Github

fn ratio(st: vec2f, s: vec2f) -> vec2f {
    return mix( vec2f((st.x*s.x/s.y)-(s.x*.5-s.y*.5)/s.y,st.y),
                vec2f(st.x,st.y*(s.y/s.x)-(s.y*.5-s.x*.5)/s.x),
                step(s.x,s.y));
}

Dependencies:

Use:

ratio(float2 st, float2 st_size)

Check it on Github



#ifndef FNC_RATIO
#define FNC_RATIO
inline __host__ __device__ float2 ratio(float2 st, float2 s) {
    return lerp(    make_float2((st.x*s.x/s.y)-(s.x*.5-s.y*.5)/s.y, st.y),
                    make_float2( st.x,st.y*(s.y/s.x)-(s.y*.5-s.x*.5)/s.x),
                    step(s.x, s.y) );
}
#endif

Examples

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