~npisanti/scrapbook

56070afa3be4e83352fbef3788968f5e8367c7d2 — Nicola Pisanti 2 months ago 7b40fef
new cam dither frag
1 files changed, 26 insertions(+), 14 deletions(-)

M filters/dither.frag
M filters/dither.frag => filters/dither.frag +26 -14
@@ 10,27 10,39 @@ uniform vec2 u_resolution;
uniform float u_time;
uniform sampler2D u_tex0;

void main(){
  
    vec2 st = gl_FragCoord.xy/u_resolution;
// code ported from alex-charlton.com/posts/Dithering_on_the_GPU/
// adapted for running on GLSL ES 2 ----
const mat4 dither_matrix = mat4( 
		vec4( 0,  8, 2, 10), 
		vec4( 12, 4, 14, 6), 
		vec4( 3, 11,  1, 9), 
		vec4( 15, 7, 13, 5)
);
float dither_4x4( float x, vec2 coord ){
	int ix = int(mod(coord.x, 4.0));
    int iy = int(mod(coord.y, 4.0));
    float d = dither_matrix[iy][ix] / 16.0;
    
    float closest = step( x, 0.5 );
    float distance = abs(closest - x);
	float branch = step( distance, d );

    //co.x = 1.0-co.x; // flip horizontally 
    //co.y = 1.0-co.y;  // flip vertically 
    return 2.0*branch*closest + 1.0 - closest - branch;
}
// -------------------------------------

void main(){
    vec2 st = gl_FragCoord.xy/u_resolution;
    vec4 source = texture2D( u_tex0, st ); // for texture access
    
    vec3 color = source.rgb;

	float pat1 = step( fract( gl_FragCoord.x/4.0 ), 0.25); 
	//pat1 *= step( fract( gl_FragCoord.y/2.0 ), 0.5); 
    
    float luminance = color.r*0.299 + color.g*0.587 + color.b*0.114;	

	float shade = step( 0.25, luminance ) * pat1;
    float light = step( 0.40, luminance );

    float alpha = shade * ( 1.0-light) + light; 
    float low = 0.2;
    float high = 0.7;    
    float alpha = smoothstep( low, high, luminance );
    
    alpha = dither_4x4( alpha, gl_FragCoord.xy );
    
    gl_FragColor = vec4( vec3(alpha), 1.0 );
    // gl_FragColor = vec4( 1.0, 1.0, 1.0, alpha );
}