LYGIA Shader Library

triangleNoise (lygia/v1.1.6/color/dither/triangleNoise)

Gjøl 2016, "Banding in Games: A Noisy Rant"

Use:

<vec4|vec3|float> ditherTriangleNoise(<vec4|vec3|float> value, <vec2> st, <float> time)
<vec4|vec3|float> ditherTriangleNoise(<vec4|vec3|float> value, <float> time)

Check it on Github



#ifndef HIGHP
#if defined(TARGET_MOBILE) && defined(GL_ES)
#define HIGHP highp
#else
#define HIGHP
#endif
#endif

#ifdef DITHER_ANIMATED
#define DITHER_TRIANGLENOISE_ANIMATED
#endif

#ifdef DITHER_CHROMATIC
#define DITHER_TRIANGLENOISE_CHROMATIC
#endif


#ifndef DITHER_TRIANGLENOISE
#define DITHER_TRIANGLENOISE

float triangleNoise(HIGHP in vec2 n, const HIGHP in float time) {
    // triangle noise, in [-1.0..1.0[ range
    #ifdef DITHER_TRIANGLENOISE_ANIMATED
    n += vec2(0.07 * fract(time));
    #endif
    n  = fract(n * vec2(5.3987, 5.4421));
    n += dot(n.yx, n.xy + vec2(21.5351, 14.3137));

    HIGHP float xy = n.x * n.y;
    // compute in [0..2[ and remap to [-1.0..1.0[
    return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;
}

float ditherTriangleNoise(const in float b, const HIGHP in vec2 st, const HIGHP in float time) {
    return b + triangleNoise(st, time) / 255.0;
}

vec3 ditherTriangleNoise(const in vec3 rgb, const HIGHP in vec2 st, const HIGHP in float time) {

    #ifdef DITHER_TRIANGLENOISE_CHROMATIC 
    vec3 dither = vec3(
            triangleNoise(st, time),
            triangleNoise(st + 0.1337, time),
            triangleNoise(st + 0.3141, time));
    #else
    vec3 dither = vec3(triangleNoise(st, time));
    #endif

    return rgb + dither / 255.0;
}

vec4 ditherTriangleNoise(const in vec4 rgba, const HIGHP in vec2 st, const HIGHP in float time) {
    #ifdef DITHER_TRIANGLENOISE_CHROMATIC 
    vec3 dither = vec3(
            triangleNoise(st, time),
            triangleNoise(st + 0.1337, time),
            triangleNoise(st + 0.3141, time));
    #else
    vec3 dither = vec3(triangleNoise(st, time));
    #endif
    return (rgba + vec4(dither, dither.x)) / 255.0;
}

#if defined(RESOLUTION)
float ditherTriangleNoise(const in float b, const HIGHP in float time) {
    return ditherTriangleNoise(b, gl_FragCoord.xy / RESOLUTION, time);
}

vec3 ditherTriangleNoise(const in vec3 rgb, const HIGHP in float time) {
    return ditherTriangleNoise(rgb, gl_FragCoord.xy / RESOLUTION, time);
}

vec4 ditherTriangleNoise(const in vec4 b, const HIGHP in float time) {
    return ditherTriangleNoise(b, gl_FragCoord.xy / RESOLUTION, time);
}
#endif

#endif

Use:

<float4|float3|float> ditherTriangleNoise(<float4|float3|float> value, <float2> fragcoord, <float> time)
<float4|float3|float> ditherTriangleNoise(<float4|float3|float> value, <float> time)

Check it on Github


#ifdef DITHER_ANIMATED
#define DITHER_TRIANGLENOISE_ANIMATED
#endif

#ifdef DITHER_CHROMATIC
#define DITHER_TRIANGLENOISE_CHROMATIC
#endif

#ifndef RESOLUTION
#define RESOLUTION _ScreenParams
#endif

#ifndef DITHER_TRIANGLENOISE
#define DITHER_TRIANGLENOISE

float triangleNoise(in float2 n, const in float time) {
    // triangle noise, in [-1.0..1.0[ range
    #ifdef DITHER_TRIANGLENOISE_ANIMATED
    n += float2(0.07 * frac(time));
    #endif
    n  = frac(n * float2(5.3987, 5.4421));
    n += dot(n.yx, n.xy + float2(21.5351, 14.3137));

    float xy = n.x * n.y;
    // compute in [0..2[ and remap to [-1.0..1.0[
    return frac(xy * 95.4307) + frac(xy * 75.04961) - 1.0;
}

float ditherTriangleNoise(const in float b, in float2 fragcoord, const in float time) {
    fragcoord /= RESOLUTION;
    return b + triangleNoise(fragcoord, time) / 255.0;
}

float3 ditherTriangleNoise(const in float3 rgb, in float2 fragcoord, const in float time) {
    fragcoord /= RESOLUTION;

    #ifdef DITHER_TRIANGLENOISE_CHROMATIC 
    float3 dither = float3(
            triangleNoise(fragcoord, time),
            triangleNoise(fragcoord + 0.1337, time),
            triangleNoise(fragcoord + 0.3141, time));
    #else
    float n = triangleNoise(fragcoord, time);
    float3 dither = float3(n, n, n);
    #endif

    return rgb + dither / 255.0;
}

float4 ditherTriangleNoise(const in float4 rgba, in float2 fragcoord, const in float time) {
    fragcoord /= RESOLUTION;

    #ifdef DITHER_TRIANGLENOISE_CHROMATIC 
    float3 dither = float3(
            triangleNoise(fragcoord, time),
            triangleNoise(fragcoord + 0.1337, time),
            triangleNoise(fragcoord + 0.3141, time));
    #else
    float n = triangleNoise(fragcoord, time);
    float3 dither = float3(n, n, n);
    #endif
    return (rgba + float4(dither, dither.x)) / 255.0;
}

#endif

Examples

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