Back to shaders

Shader test bench

Noise Adapt.fs

vidvox-isf-files generative glsl runnable fragment MIT
Source
runnable fragment

Complete GLSL fragment shader. Stronghold runs it directly when the browser can compile it.

Code

uniform sampler2D inputImage;
uniform float adaptRate;
uniform float threshold;
uniform int useRGBA;
precision mediump float;
/*{
    "CATEGORIES": [
        "Noise"
    ],
    "CREDIT": "by VIDVOX",
    "DESCRIPTION": "Pixels that change become noise until they match the input again",
    "INPUTS": [
        {
            "NAME": "inputImage",
            "TYPE": "image"
        },
        {
            "DEFAULT": 0.5,
            "MAX": 1,
            "MIN": 0,
            "NAME": "adaptRate",
            "TYPE": "float"
        },
        {
            "DEFAULT": 0.05,
            "MAX": 1,
            "MIN": 0,
            "NAME": "threshold",
            "TYPE": "float"
        },
        {
            "DEFAULT": 1,
            "NAME": "useRGBA",
            "TYPE": "bool"
        }
    ],
    "ISFVSN": "2",
    "PASSES": [
        {
            "PERSISTENT": true,
            "TARGET": "adaptiveBuffer1"
        },
        {
        }
    ]
}
*/


float	minThresh = 3.0/255.0;


float rand(vec2 co){
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
float steppedRand(vec2 co){
    return floor(256.0*rand(co))/255.0;
}
vec4 randColor(vec2 co){
	vec4	c = vec4(0.0);
	c.r = steppedRand(co+0.234);
	c.g = steppedRand(co+0.193);
	c.b = steppedRand(co+0.625);
	c.a = 1.0;
    return c;
}

float luma(vec4 c)	{
	return c.a*(c.r+c.g+c.b)/3.0;	
}


void main()
{
	
	vec4		freshPixel = texture2D(inputImage, (gl_FragCoord.xy) / u_resolution.xy);
	vec4		stalePixel = texture2D(adaptiveBuffer1, (gl_FragCoord.xy) / u_resolution.xy);
	if (useRGBA == false)	{
		float		freshLuma = luma(freshPixel);
		float		staleLuma = luma(stalePixel);
		float		thresh = (threshold < minThresh) ? minThresh : threshold;
		if (abs(freshLuma-staleLuma)>=thresh)	{
			stalePixel = randColor(gl_FragCoord.xy+u_time);
		}
	}
	else	{
		float	thresh = (threshold < minThresh) ? minThresh : threshold;
		if (abs(freshPixel.r-stalePixel.r)>=thresh)	{
			stalePixel.r = steppedRand(gl_FragCoord.xy+u_time+1.234);
		}
		if (abs(freshPixel.g-stalePixel.g)>=thresh)	{
			stalePixel.g = steppedRand(gl_FragCoord.xy+u_time+2.193);
		}
		if (abs(freshPixel.b-stalePixel.b)>=thresh)	{
			stalePixel.b = steppedRand(gl_FragCoord.xy+u_time+3.625);
		}
		if (abs(freshPixel.a-stalePixel.a)>=thresh)	{
			stalePixel.a = steppedRand(gl_FragCoord.xy+u_time+4.479);
		}
	}
	gl_FragColor = mix(stalePixel,freshPixel,adaptRate);

}