~npisanti/scrapbook

5fbc35cf1836067134a536a9cb1ffd48abee2ad9 — Nicola Pisanti 10 days ago 56070af master
raymarching boilerplate
1 files changed, 28 insertions(+), 9 deletions(-)

M studies/raymarching.frag
M studies/raymarching.frag => studies/raymarching.frag +28 -9
@@ 12,6 12,29 @@ uniform vec2 u_resolution;
uniform float u_time;
uniform sampler2D u_tex0;


// 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 );

    return 2.0*branch*closest + 1.0 - closest - branch;
}
// -------------------------------------


// -------------- RAYMARCHING BOILERPLATE ---------------------------------------

const int MAX_MARCHING_STEPS = 255;


@@ 181,14 204,10 @@ void main(){
    float c = K_a;
    c += phongContribForLight(K_d, p, camera, light_pos );

	float shade = step( 0.2,  c );
	float min = 1.0 - shade;
    float light = step( 0.4,  c );
	shade -= light;

    float a = min * dark_pattern + shade * mid_pattern + light * light_pattern; 
	gl_FragColor = vec4(vec3(a), 1.0);
	gl_FragColor = vec4(vec3(c), 1.0);

	// pre-dither result
	//gl_FragColor = vec4(vec3(c), 1.0);
	// dithering 
    //c *= 1.4;
	//float a = dither_4x4( c, gl_FragCoord.xy );
	//gl_FragColor = vec4(vec3(a), 1.0);
}
\ No newline at end of file