LYGIA Shader Library

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

nan

Dependencies:

Use:

<vec4|vec3|float> ditherBlueNoise(<vec4|vec3|float> value, <vec2> st, <float> time)
<vec4|vec3|float> ditherBlueNoise(<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

#ifndef BLUENOISE_TEXTURE_RESOLUTION
#define BLUENOISE_TEXTURE_RESOLUTION vec2(1024.0)
#endif

#ifdef DITHER_ANIMATED
#define DITHER_BLUENOISE_ANIMATED
#endif

#ifdef DITHER_CHROMATIC
#define DITHER_BLUENOISE_CHROMATIC
#endif

#ifndef DITHER_BLUENOISE
#define DITHER_BLUENOISE

float remap_pdf_tri_unity( float v ) {
    v = v*2.0-1.0;
    v = sign(v) * (1.0 - sqrt(1.0 - abs(v)));
    return 0.5 + 0.5*v;
}

const vec2 blueNoiseTexturePixel = 1.0/BLUENOISE_TEXTURE_RESOLUTION;

float ditherBlueNoise(SAMPLER_TYPE tex, in float b, const HIGHP in float time) {
    vec2 st = gl_FragCoord.xy;
    #ifdef DITHER_BLUENOISE_ANIMATED 
    st += 1337.0*fract(time);
    #endif
    float bn = SAMPLER_FNC(tex, st * blueNoiseTexturePixel).r;
    float bn_tri = remap_pdf_tri_unity(bn);
    return b + (bn_tri*2.0-0.5)/255.0;
}

vec3 ditherBlueNoise(SAMPLER_TYPE tex, in vec3 rgb, const HIGHP in float time) {
    vec2 st = gl_FragCoord.xy;
    #ifdef DITHER_BLUENOISE_ANIMATED
    st += 1337.0*fract(time * 0.1);
    #endif

    #ifdef DITHER_BLUENOISE_CHROMATIC
    vec3 bn = SAMPLER_FNC(tex, st * blueNoiseTexturePixel).rgb;
    vec3 bn_tri = vec3( remap_noise_tri_erp(bn.x), 
                        remap_noise_tri_erp(bn.y), 
                        remap_noise_tri_erp(bn.z) );
    rgb += (bn_tri*2.0-0.5)/255.0;
    #else
    float bn = SAMPLER_FNC(tex, st * blueNoiseTexturePixel).r;
    float bn_tri = remap_pdf_tri_unity(bn);
    rgb += (bn_tri*2.0-0.5)/255.0;
    #endif

    return rgb;
}

vec4 ditherBlueNoise(SAMPLER_TYPE tex, in vec4 rgba, const HIGHP in float time) {
    return vec4(ditherBlueNoise(tex, rgba.rgb, time), rgba.a);
}

#if defined(BLUENOISE_TEXTURE)
float ditherBlueNoise(const in float b, const HIGHP in float time) {
    return ditherBlueNoise(BLUENOISE_TEXTURE, b, time);
}

vec3 ditherBlueNoise(const in vec3 rgb, const HIGHP in float time) {
    return ditherBlueNoise(BLUENOISE_TEXTURE, rgb, time);
}

vec4 ditherBlueNoise(const in vec4 rgba, const HIGHP in float time) {
    return ditherBlueNoise(BLUENOISE_TEXTURE, rgba, time);
}
#endif

#endif

Dependencies:

Use:

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

Check it on Github



#ifndef BLUENOISE_TEXTURE_RESOLUTION
#define BLUENOISE_TEXTURE_RESOLUTION float2(1024.0, 1024.0)
#endif

#ifdef DITHER_ANIMATED
#define DITHER_BLUENOISE_ANIMATED
#endif

#ifdef DITHER_CHROMATIC
#define DITHER_BLUENOISE_CHROMATIC
#endif

#ifndef DITHER_BLUENOISE
#define DITHER_BLUENOISE

float remap_pdf_tri_unity( float v ) {
    v = v*2.0-1.0;
    v = sign(v) * (1.0 - sqrt(1.0 - abs(v)));
    return 0.5 + 0.5*v;
}

const float2 blueNoiseTexturePixel = 1.0/BLUENOISE_TEXTURE_RESOLUTION;

float ditherBlueNoise(SAMPLER_TYPE tex, in float b, float2 fragcoord, const in float time) {
    #ifdef DITHER_BLUENOISE_ANIMATED 
    fragcoord += 1337.0 * frac(time);
    #endif
    float bn = SAMPLER_FNC(tex, fragcoord * blueNoiseTexturePixel).r;
    float bn_tri = remap_pdf_tri_unity(bn);
    return b + (bn_tri*2.0-0.5)/255.0;
}

float3 ditherBlueNoise(SAMPLER_TYPE tex, in float3 rgb, float2 fragcoord, const in float time) {
    #ifdef DITHER_BLUENOISE_ANIMATED
    fragcoord += 1337.0 * frac(time * 0.1);
    #endif

    #ifdef DITHER_BLUENOISE_CHROMATIC
    float3 bn = SAMPLER_FNC(tex, fragcoord * blueNoiseTexturePixel).rgb;
    float3 bn_tri = float3( remap_noise_tri_erp(bn.x), 
                        remap_noise_tri_erp(bn.y), 
                        remap_noise_tri_erp(bn.z) );
    rgb += (bn_tri*2.0-0.5)/255.0;
    #else
    float bn = SAMPLER_FNC(tex, fragcoord * blueNoiseTexturePixel).r;
    float bn_tri = remap_pdf_tri_unity(bn);
    rgb += (bn_tri*2.0-0.5)/255.0;
    #endif

    return rgb;
}

float4 ditherBlueNoise(SAMPLER_TYPE tex, in float4 rgba, float2 fragcoord, const in float time) {
    return float4(ditherBlueNoise(tex, rgba.rgb, fragcoord, time), rgba.a);
}

#if defined(BLUENOISE_TEXTURE)
float ditherBlueNoise(const in float b, float2 fragcoord, const in float time) {
    return ditherBlueNoise(BLUENOISE_TEXTURE, b, fragcoord, time);
}

float3 ditherBlueNoise(const in float3 rgb, float2 fragcoord, const in float time) {
    return ditherBlueNoise(BLUENOISE_TEXTURE, rgb, fragcoord, time);
}

float4 ditherBlueNoise(const in float4 rgba, float2 fragcoord, const in float time) {
    return ditherBlueNoise(BLUENOISE_TEXTURE, rgba, fragcoord, time);
}
#endif

#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