~npisanti/scrapbook

scrapbook/studies/dither.frag -rw-r--r-- 6.7 KiB View raw
5fbc35cfNicola Pisanti raymarching boilerplate 10 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifdef GL_ES
precision mediump float;
#endif

#define PI 3.1415926535897932384626433832795
#define TWO_PI 6.2831853071795864769252867665590

uniform vec2 u_resolution;
uniform float u_time;
uniform sampler2D u_tex0;

float rand(vec2 st, float t){
    return fract(sin(dot(st.xy + fract(t*0.0013) ,vec2(12.9898,78.233))) * 43758.5453);
}

// ------------------- FUNCTIONS -------------------------------
float dither_a( float x, vec4 thresholds ){
	float p0 = step(0.875, fract(gl_FragCoord.x*0.125));
	p0 *= step(0.875, fract(gl_FragCoord.y*0.125));
	
	float p1 = step(0.75, fract(gl_FragCoord.x*0.25));
	p1 *= step(0.75, fract(gl_FragCoord.y*0.25));

	float p2 = step(0.5,fract(gl_FragCoord.x*0.5));
	p2 *= step(0.5,fract(gl_FragCoord.y*0.5));

	float s3 = step( thresholds[3], x );
	float s2 = step( thresholds[2], x ) - s3;
	float redux = s2 + s3;
	float s1 = step( thresholds[1], x ) - redux;
	redux += s1;
	float s0 = step( thresholds[0], x ) - redux;
	
	return s0 * p0 + s1*p1 + s2*p2 + s3*1.0;
}

float dither_b( float x, vec4 thresholds ){

	float px = gl_FragCoord.x*0.25 + step(0.5, fract(gl_FragCoord.y/8.0))*0.5;
	float p2 = step(0.75,fract(px));
	float p1 = p2 * step(0.25, fract(gl_FragCoord.y/4.0))*0.5;
	float p0 = p2 * step(0.75, fract(gl_FragCoord.y/4.0))*0.5;

	float s3 = step( thresholds[3], x );
	float s2 = step( thresholds[2], x ) - s3;
	float redux = s2 + s3;
	float s1 = step( thresholds[1], x ) - redux;
	redux += s1;
	float s0 = step( thresholds[0], x ) - redux;
	
	return s0 * p0 + s1*p1 + s2*p2 + s3*1.0;
}

float dither_b7( float x, float t0, float t1, float t2, float t3, float t4, float t5 ){
	float px = gl_FragCoord.x*0.25 + step(0.5, fract(gl_FragCoord.y/16.0))*0.5;
	float p4 = step(0.75,fract(px));

	float p3 = p4 * step(0.25, fract(gl_FragCoord.y/8.0))*0.5;
	float p2 = p4 * step(0.5, fract(gl_FragCoord.y/8.0))*0.5;
	float p1 = p4 * step(0.75, fract(gl_FragCoord.y/8.0))*0.5;
	float p0 = p4 * step(0.875, fract(gl_FragCoord.y/8.0))*0.5;

	float s5 = step( t5, x );
	float s4 = step( t4, x ) - s5;
	float redux = s5 + s4;
	float s3 = step( t3, x ) - redux;
	redux += s3;
	float s2 = step( t2, x ) - redux;
	redux += s2;
	float s1 = step( t1, x ) - redux;
	redux += s1;
	float s0 = step( t0, x ) - redux;
	
	return s0*p0 + s1*p1 + s2*p2 + s3*p3 + s4*p4 + s5*1.0;
}

float dither_c( float x, vec4 thresholds ){

	float p2 = step(0.75,fract(gl_FragCoord.x*0.25));
	float p1 = p2 * step(0.25, fract(gl_FragCoord.y/4.0))*0.5;
	float p0 = p2 * step(0.75, fract(gl_FragCoord.y/4.0))*0.5;

	float s3 = step( thresholds[3], x );
	float s2 = step( thresholds[2], x ) - s3;
	float redux = s2 + s3;
	float s1 = step( thresholds[1], x ) - redux;
	redux += s1;
	float s0 = step( thresholds[0], x ) - redux;
	
	return s0 * p0 + s1*p1 + s2*p2 + s3*1.0;
}

float dither_cr( float x, vec4 thresholds, float angle ){

	float sin_theta = sin(angle);
	float cos_theta = cos(angle);
	mat2 rot_mat = mat2( cos_theta,-sin_theta, sin_theta, cos_theta); 
	vec2 rot = gl_FragCoord.xy * rot_mat;

	float p2 = step(0.75,fract(rot.x*0.25));
	float p1 = p2 * step(0.25, fract(rot.y/4.0))*0.5;
	float p0 = p2 * step(0.75, fract(rot.y/4.0))*0.5;

	float s3 = step( thresholds[3], x );
	float s2 = step( thresholds[2], x ) - s3;
	float redux = s2 + s3;
	float s1 = step( thresholds[1], x ) - redux;
	redux += s1;
	float s0 = step( thresholds[0], x ) - redux;
	
	return s0 * p0 + s1*p1 + s2*p2 + s3*1.0;
}

float dither_d( float x, vec4 thresholds ){
	float p0 = step(0.75, fract(gl_FragCoord.x*0.25));
	p0 *= step(0.75, fract(gl_FragCoord.y*0.25));
	
	float p1 = step(0.5, fract(gl_FragCoord.x*0.25));
	p1 *= step(0.5, fract(gl_FragCoord.y*0.25));

	float p2 = step(0.25,fract(gl_FragCoord.x*0.25));
	p2 *= step(0.25,fract(gl_FragCoord.y*0.25));

	float s3 = step( thresholds[3], x );
	float s2 = step( thresholds[2], x ) - s3;
	float redux = s2 + s3;
	float s1 = step( thresholds[1], x ) - redux;
	redux += s1;
	float s0 = step( thresholds[0], x ) - redux;
	
	return s0 * p0 + s1*p1 + s2*p2 + s3*1.0;
}

float dither_e( float x, vec4 thresholds ){

	float theta = PI * 0.25;
	float sin_theta = sin(theta);
	float cos_theta = cos(theta);
	mat2 rot_mat = mat2( cos_theta,-sin_theta, sin_theta, cos_theta); 
	vec2 rot = gl_FragCoord.xy * rot_mat;

	float mx = mod(rot.x, 8.0);
	float my = mod(rot.y, 8.0);

	float p2 = step( 4.0, mx ) * step( 4.0, my );
	
	float p1 = p2 * step(1.0, mod(gl_FragCoord.y, 2.0));
	float p0 = p2 * step(3.0, mod(gl_FragCoord.y, 4.0));

	float s3 = step( thresholds[3], x );
	float s2 = step( thresholds[2], x ) - s3;
	float redux = s2 + s3;
	float s1 = step( thresholds[1], x ) - redux;
	redux += s1;
	float s0 = step( thresholds[0], x ) - redux;
	
	return s0 * p0 + s1*p1 + s2*p2 + s3*1.0;
}

float dither_f( float x, vec4 thresholds ){

	float theta = PI * 0.25;
	float sin_theta = sin(theta);
	float cos_theta = cos(theta);
	mat2 rot_mat = mat2( cos_theta,-sin_theta, sin_theta, cos_theta); 
	vec2 rot = gl_FragCoord.xy * rot_mat;
	float mx = mod(rot.x, 8.0);
	float my = mod(rot.y, 8.0);

	float p2 = step( 2.0, mx ) * step( 1.0, my );
	float p1 = step( 6.0, mx ) * step( 2.0, my );
	float p0 = step( 7.0, mx ) * step( 7.0, my );

	float s3 = step( thresholds[3], x );
	float s2 = step( thresholds[2], x ) - s3;
	float redux = s2 + s3;
	float s1 = step( thresholds[1], x ) - redux;
	redux += s1;
	float s0 = step( thresholds[0], x ) - redux;
	
	return s0 * p0 + s1*p1 + s2*p2 + s3*1.0;
}

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

float dither_r( float x, vec2 coord, float steps, float seed ){
	x = (ceil(x*steps) -2.0)/ (steps-2.0);
	float r = rand( vec2(coord.x, seed), coord.y);
	r = 1.0-r;
	r = r*r*r*r; // remove or add * to change brightness curve
	r = 1.0-r;
    return step( r, x );
}

// ------------------- SHADER ----------------------------------
void main(){
    vec2 st = gl_FragCoord.xy/u_resolution;
	//float a = dither_c( st.x, vec4( 0.2, 0.4, 0.6, 0.8 ) );
	//float a = dither_cr( st.x, vec4( 0.2, 0.4, 0.6, 0.8 ), 0.1*TWO_PI );
	//float a = dither_b7( st.x, 0.15, 0.3, 0.45, 0.6, 0.75, 0.9);
	float a = dither_4x4( st.x, gl_FragCoord.xy );
	//float a = dither_r( st.x, gl_FragCoord.xy, 8.0, u_time*0.00001 );
	//float a = dither_r( st.x*st.x, gl_FragCoord.xy, 32.0, u_time*0.00001 );
    gl_FragColor = vec4( vec3(a), 1.0 );
}