LYGIA Shader Library

aafract (lygia/math/aafract)

Anti-aliasing fract function. It clamp except for a 2-pixel wide gradient along the edge Based on this example https://www.shadertoy.com/view/4l2BRD

Dependencies:

Use:

<float> aafract(<float> x)

Check it on Github



#ifndef FNC_AAFRACT
#define FNC_AAFRACT

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

float aafract(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;
    float v = (fx < idx) ? fx/idx : (1.0-fx)/afwidth;
    return nyquist(v, afwidth);
#elif defined(AA_EDGE)
    float afwidth = AA_EDGE;
    float fx = fract(x);
    float idx = 1. - afwidth;
    float v = (fx < idx) ? fx/idx : (1.0-fx)/afwidth;
    return nyquist(v, afwidth);
#else 
    return fract(x);
#endif
}

vec2 aafract(vec2 v) { return vec2(aafract(v.x), aafract(v.y)); }

#endif

Dependencies:

Use:

<float> aafract(<float> x)

Check it on Github



#ifndef FNC_AAFRACT
#define FNC_AAFRACT

float aafract(float x) {
#if defined(AA_EDGE)
    float afwidth = AA_EDGE;
#else 
    float afwidth = 2.0 * length(float2(ddx(x), ddy(x)));
#endif
    float fx = frac(x);
    float idx = 1. - afwidth;
    float v = (fx < idx) ? fx/idx : (1.0-fx)/afwidth;
    return nyquist(v, afwidth);
}

float2 aafract(float2 v) { return float2(aafract(v.x), aafract(v.y)); }

#endif

Dependencies:

Use:

<float> aafract(<float> x)

Check it on Github



#ifndef FNC_AAFRACT
#define FNC_AAFRACT

float aafract(float x) {

#if defined(AA_EDGE)
    float afwidth = AA_EDGE;
    float fx = fract(x);
    float idx = 1. - afwidth;
    return (fx < idx) ? fx : map(fx, idx, 1., fx, 0.);
#else 
    float afwidth = 2.0 * length(float2(dfdx(x), dfdy(x)));
    float fx = fract(x);
    float idx = 1. - afwidth;
    return (fx < idx) ? fx : map(fx, idx, 1., fx, 0.);
#endif
}

float2 aafract(float2 v) { return float2(aafract(v.x), aafract(v.y)); }

#endif

Dependencies:

Check it on Github



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

fn aafract2(v: vec2f) -> vec2f { return vec2(aafract(v.x), aafract(v.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