LYGIA Shader Library

grain (lygia/distort/grain)

Natural looking film grain using 3D noise functions (original source https://github.com/mattdesl/glsl-film-grain). Inspired by Martins Upitis.

Dependencies:

Use:

grain(<vec2> texCoord, <vec2> resolution [, <float> t, <float> multiplier])
grain(<SAMPLER_TYPE> texture, <vec2> texCoord, <float|vec2> resolution [, <float> t, <float> multiplier])

Check it on Github



#ifndef GRAIN_TYPE
#define GRAIN_TYPE vec3
#endif

#ifndef GRAIN_SAMPLER_FNC
#define GRAIN_SAMPLER_FNC(TEX, UV) SAMPLER_FNC(TEX, UV).rgb
#endif

#ifndef FNC_GRAIN
#define FNC_GRAIN
float grain(vec2 texCoord, vec2 resolution, float t, float multiplier) {
    vec2 mult = texCoord * resolution;
    float offset = snoise(vec3(mult / multiplier, t));
    float n1 = pnoise(vec3(mult, offset), vec3(1. / texCoord * resolution, 1.));
    return n1 / 2. + .5;
}

float grain(vec2 texCoord, vec2 resolution, float t) {
    return grain(texCoord, resolution, t, 2.5);
}

float grain(vec2 texCoord, vec2 resolution) {
    return grain(texCoord, resolution, 0.);
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, vec2 st, vec2 resolution, float t, float multiplier ) {
    GRAIN_TYPE org = GRAIN_SAMPLER_FNC(tex, st);

    float g = grain(st, resolution, t, multiplier);

    //get the luminance of the background
    float luminance = luma(org);

    //reduce the noise based on some 
    //threshold of the background luminance
    float response = smoothstep(0.05, 0.5, luminance);
    return mix( blendSoftLight(org, GRAIN_TYPE(g)), 
                org, 
                response * response);
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, vec2 st, vec2 resolution, float t ) {
    return grain(tex, st, resolution, t, 2.5 );
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, vec2 st, vec2 resolution) {
    return grain(tex, st, resolution, 0.);
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, vec2 st, float resolution, float t, float multiplier  ) {
    return grain(tex, st, vec2(resolution), t, multiplier );
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, vec2 st, float resolution, float t ) {
    return grain(tex, st, resolution, t, 2.5 );
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, vec2 st, float resolution) {
    return grain(tex, st, resolution, 0.);
}

#endif

Dependencies:

Use:

grain(<float2> texCoord, <float2> resolution [, <float> t, <float> multiplier])
grain(<SAMPLER_TYPE> texture, <float2> texCoord, <float|float2> resolution [, <float> t, <float> multiplier])

Check it on Github



#ifndef GRAIN_TYPE
#define GRAIN_TYPE float3
#endif

#ifndef GRAIN_SAMPLER_FNC
#define GRAIN_SAMPLER_FNC(TEX, UV) SAMPLER_FNC(TEX, UV).rgb
#endif

#ifndef FNC_GRAIN
#define FNC_GRAIN
float grain(float2 texCoord, float2 resolution, float t, float multiplier) {
    float2 mult = texCoord * resolution;
    float offset = snoise(float3(mult / multiplier, t));
    float n1 = pnoise(float3(mult, offset), float3(1. / texCoord * resolution, 1.));
    return n1 / 2. + .5;
}

float grain(float2 texCoord, float2 resolution, float t) {
    return grain(texCoord, resolution, t, 2.5);
}

float grain(float2 texCoord, float2 resolution) {
    return grain(texCoord, resolution, 0.);
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, float2 st, float2 resolution, float t, float multiplier ) {
    GRAIN_TYPE org = GRAIN_SAMPLER_FNC(tex, st);

    float g = grain(st, resolution, t, multiplier);

    //get the luminance of the background
    float luminance = luma(org);

    //reduce the noise based on some 
    //threshold of the background luminance
    float response = smoothstep(0.05, 0.5, luminance);
    return lerp(   blendSoftLight(org, float4(g, g, g, 1.)), 
                    org, 
                    response * response);
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, float2 st, float2 resolution, float t ) {
    return grain(tex, st, resolution, t, 2.5 );
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, float2 st, float2 resolution) {
    return grain(tex, st, resolution, 0.);
}

#endif

Dependencies:

Use:

grain(<float2> texCoord, <float2> resolution [, <float> t, <float> multiplier])
grain(<SAMPLER_TYPE> texture, <float2> texCoord, <float|float2> resolution [, <float> t, <float> multiplier])

Check it on Github



#ifndef GRAIN_TYPE
#define GRAIN_TYPE float3
#endif

#ifndef GRAIN_SAMPLER_FNC
#define GRAIN_SAMPLER_FNC(TEX, UV) SAMPLER_FNC(TEX, UV).rgb
#endif

#ifndef FNC_GRAIN
#define FNC_GRAIN
float grain(float2 texCoord, float2 resolution, float t, float multiplier) {
    float2 mult = texCoord * resolution;
    float offset = snoise(float3(mult / multiplier, t));
    float n1 = pnoise(float3(mult, offset), float3(1. / texCoord * resolution, 1.));
    return n1 / 2. + .5;
}

float grain(float2 texCoord, float2 resolution, float t) {
    return grain(texCoord, resolution, t, 2.5);
}

float grain(float2 texCoord, float2 resolution) {
    return grain(texCoord, resolution, 0.);
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, float2 st, float2 resolution, float t, float multiplier ) {
    GRAIN_TYPE org = GRAIN_SAMPLER_FNC(tex, st);

    float g = grain(st, resolution, t, multiplier);

    //get the luminance of the background
    float luminance = luma(org);

    //reduce the noise based on some 
    //threshold of the background luminance
    float response = smoothstep(0.05, 0.5, luminance);
    return mix( blendSoftLight(org, GRAIN_TYPE(g)), 
                org, 
                response * response);
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, float2 st, float2 resolution, float t ) {
    return grain(tex, st, resolution, t, 2.5 );
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, float2 st, float2 resolution) {
    return grain(tex, st, resolution, 0.);
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, float2 st, float resolution, float t, float multiplier  ) {
    return grain(tex, st, float2(resolution), t, multiplier );
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, float2 st, float resolution, float t ) {
    return grain(tex, st, resolution, t, 2.5 );
}

GRAIN_TYPE grain(SAMPLER_TYPE tex, float2 st, float resolution) {
    return grain(tex, st, resolution, 0.);
}

#endif

License

MIT License (MIT) Copyright (c) 2015 Matt DesLauriers

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