viewof noiseGrainShader = shaderShow(
`#version 300 es
precision highp float;
uniform float noiseSeed;
uniform float grainAmount;
uniform float grainScale;
uniform float jitterAmount;
uniform float jitterScale;
uniform sampler2D tex;
out vec4 fragColor;
vec3 hash( vec3 p ) // replace this by something better
{
p = vec3( dot(p,vec3(127.1,311.7, 74.7)),
dot(p,vec3(269.5,183.3,246.1)),
dot(p,vec3(113.5,271.9,124.6)));
return -1.0 + 2.0*fract(sin(p)*43758.5453123);
}
float noise( in vec3 p )
{
vec3 i = floor( p );
vec3 f = fract( p );
vec3 u = f*f*(3.0-2.0*f);
return mix( mix( mix( dot( hash( i + vec3(0.0,0.0,0.0) ), f - vec3(0.0,0.0,0.0) ),
dot( hash( i + vec3(1.0,0.0,0.0) ), f - vec3(1.0,0.0,0.0) ), u.x),
mix( dot( hash( i + vec3(0.0,1.0,0.0) ), f - vec3(0.0,1.0,0.0) ),
dot( hash( i + vec3(1.0,1.0,0.0) ), f - vec3(1.0,1.0,0.0) ), u.x), u.y),
mix( mix( dot( hash( i + vec3(0.0,0.0,1.0) ), f - vec3(0.0,0.0,1.0) ),
dot( hash( i + vec3(1.0,0.0,1.0) ), f - vec3(1.0,0.0,1.0) ), u.x),
mix( dot( hash( i + vec3(0.0,1.0,1.0) ), f - vec3(0.0,1.0,1.0) ),
dot( hash( i + vec3(1.0,1.0,1.0) ), f - vec3(1.0,1.0,1.0) ), u.x), u.y), u.z );
}
void main() {
vec2 iResolution = vec2(textureSize(tex, 0));
vec3 n = grainAmount * (0.3 - vec3 (noise (grainScale * vec3(gl_FragCoord.xy, noiseSeed*1000.+1331.)),
noise (grainScale * vec3(gl_FragCoord.xy, noiseSeed*1139.+2135.)),
noise (grainScale * vec3(gl_FragCoord.xy, noiseSeed*1313.+7713.))));
float ang = 6.2832 * noise(jitterScale * vec3(gl_FragCoord.xy, noiseSeed*1831.+3762.));
float r = noise(jitterScale * vec3(gl_FragCoord.xy, noiseSeed*1385.+5137.));
vec2 uv = (gl_FragCoord.xy + jitterAmount * vec2(r*cos(ang),r*sin(ang))) / iResolution;
vec3 texColor = texture(tex,uv).xyz;
fragColor = vec4(texColor+n, 1.);
}
`,
{ label: "noiseGrainShader" }
)