LYGIA Shader Library

aafloor (lygia/math/aafloor)

Is similar to floor() but has a 2-pixel wide gradient between clamped steps to allow the edges in the result to be anti-aliased. Based on examples https://www.shadertoy.com/view/4l2BRD and https://www.shadertoy.com/view/3tSGWy

Dependencies:

Use:

<float> aafloor(<float> x)

Check it on Github



#ifndef FNC_AAFLOOR
#define FNC_AAFLOOR

#if defined(GL_OES_standard_derivatives)
#extension GL_OES_standard_derivatives : enable
#endif

float aafloor(float x) {
#if !defined(GL_ES) || __VERSION__ >= 300 || defined(GL_OES_standard_derivatives)
    float afwidth = 2.0 * length(vec2(dFdx(x), dFdy(x)));
    float fx = fract(x);
    float idx = 1. - afwidth;
    return (fx < idx) ? x - fx : map(fx, idx, 1., x-fx, x);
#elif defined(AA_EDGE)
    float afwidth = AA_EDGE;
    float fx = fract(x);
    float idx = 1. - afwidth;
    return (fx < idx) ? x - fx : map(fx, idx, 1., x-fx, x);
#else 
    return floor(x);
#endif
}

vec2 aafloor(vec2 x) { return vec2(aafloor(x.x), aafloor(x.y)); }

#endif

Dependencies:

Use:

<float> aafloor(<float> x)

Check it on Github



#ifndef FNC_AAFLOOR
#define FNC_AAFLOOR
float aafloor(float x) {
#if defined(AA_EDGE)
    float afwidth = AA_EDGE;
#else 
    float afwidth = 2.0 * fwidth(x);
#endif
    float fx = frac(x);
    float idx = 1. - afwidth;
    return (fx < idx) ? x - fx : map(fx, idx, 1., x-fx, x);
}

float2 aafloor(float2 x) { return float2(aafloor(x.x), aafloor(x.y)); }
#endif

Dependencies:

Check it on Github



fn aafloor(x: f32) -> f32 {
    let afwidth = 2.0 * fwidth(x);
    let fx = fract(x);
    let idx = 1. - afwidth;
    return select(map(fx, idx, 1., x-fx, x), x - fx, fx < idx);
}

fn aafloor2(x: vec2f) -> vec2f { return vec2f(aafloor(x.x), aafloor(x.y)); }

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