LYGIA Shader Library

nearest (lygia/space/nearest)

sampling function to make a texture behave like GL_NEAREST

Use:

nearest(vec2 st, <vec2> res)

Check it on Github


#ifndef NEAREST_FLOOR_FNC
#define NEAREST_FLOOR_FNC(UV) floor(UV)
#endif

#ifndef FNC_NEAREST
#define FNC_NEAREST
vec2 nearest(in vec2 v, in vec2 res) {
    vec2 offset = 0.5 / (res - 1.0);
    return NEAREST_FLOOR_FNC(v * res) / res + offset;
}
#endif

Use:

nearest(float2 st, <float2> resolution)

Check it on Github


#ifndef FNC_NEAREST
#define FNC_NEAREST
float2 nearest(in float2 st, in float2 resolution) {
    float2 offset = .5 / (resolution - 1.);
    return floor(st * resolution) / resolution + offset;
}
#endif

Check it on Github

fn nearest(v: vec2f, res: vec2f) -> vec2f {
    let offset = 0.5 / (res - 1.0);
    return floor(v * res) / res + offset;
}

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