0.00
60.0 fps

Mixed noises

Mix noises while preserving the histogram.

Log in to post a comment.

#version 300 es
precision highp float;

uniform float iTime;
uniform vec2  iResolution;

uniform float HistogramPreserve; // value=0, min=0, max=1, step=0.001

out vec4 fragColor;

float hash12(vec2 p) {
	vec3 p3  = fract(vec3(p.xyx) * .1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

void main() {
    float n0 = hash12(gl_FragCoord.xy);
    float n1 = hash12(gl_FragCoord.yx + 0.5);
    float f = .5 + .5 * sin(gl_FragCoord.x / iResolution.x * 20. + 2. * iTime);
    float h = mix(n0, n1, f);
    
    // histogram preserved noise fast
    float h0 = clamp((h - .5) / length(vec2(f, 1.-f)) + .5, 0., 1.);
    
    // histogram preserved noise correct
    float f1 = min(f, 1.-f), x = min(h, 1.-h);
    float e = f1 / (2. - 2.*f1);

    float h1 = x < f1 
        ? e * pow((x / f1), 2.)
        : e + (1. - 2.*e) * (h - f1) / (1. - 2.*f1);
    
    if (h > 1. - f1) h1 = 1. - h1;
    
    vec3 col = vec3(mix(h, h1, HistogramPreserve));

    fragColor = vec4(col, 1.);
}