-
Notifications
You must be signed in to change notification settings - Fork 1
/
convolution.glsl.js
369 lines (276 loc) · 10.4 KB
/
convolution.glsl.js
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
////////////////VERTEX SHADERS////////////////
var passthruVertexShaderSrc = `
precision mediump float;
attribute vec2 position;
attribute vec2 texcoord;
uniform vec2 resolution;
varying vec2 v_texcoord;
void main() {
gl_Position = vec4(((position / resolution) * 2.0 - 1.0) * vec2(1.0, -1.0), 0.0, 1.0);
v_texcoord = texcoord;} `;
var box8VertexShaderSrc = `
precision mediump float;
attribute vec2 position;
attribute vec2 texcoord;
uniform vec2 resolution;
uniform float width;
varying vec2 texcoord00;
varying vec2 texcoord01;
varying vec2 texcoord02;
varying vec2 texcoord10;
varying vec2 texcoord12;
varying vec2 texcoord20;
varying vec2 texcoord21;
varying vec2 texcoord22;
void main() {
gl_Position = vec4(((position / resolution) * 2.0 - 1.0) * vec2(1.0, -1.0), 0.0, 1.0);
texcoord00 = texcoord + vec2(-width, -width);
texcoord01 = texcoord + vec2( 0, -width);
texcoord02 = texcoord + vec2( width, -width);
texcoord10 = texcoord + vec2(-width, 0);
texcoord12 = texcoord + vec2( width, 0);
texcoord20 = texcoord + vec2(-width, width);
texcoord21 = texcoord + vec2( 0, width);
texcoord22 = texcoord + vec2( width, width);} `;
var diag5VertexShaderSrc = `
precision mediump float;
attribute vec2 position;
attribute vec2 texcoord;
uniform vec2 resolution;
uniform float width;
varying vec2 texcoord11;
varying vec2 texcoord00;
varying vec2 texcoord02;
varying vec2 texcoord20;
varying vec2 texcoord22;
void main() {
gl_Position = vec4(((position / resolution) * 2.0 - 1.0) * vec2(1.0, -1.0), 0.0, 1.0);
texcoord11 = texcoord;
texcoord00 = texcoord + vec2(-width, -width);
texcoord02 = texcoord + vec2( width, -width);
texcoord20 = texcoord + vec2( width, width);
texcoord22 = texcoord + vec2(-width, width);} `;
var cross5VertexShaderSrc = `
precision mediump float;
attribute vec2 position;
attribute vec2 texcoord;
uniform vec2 resolution;
uniform float width;
varying vec2 texcoord01;
varying vec2 texcoord10;
varying vec2 texcoord11;
varying vec2 texcoord12;
varying vec2 texcoord21;
void main() {
gl_Position = vec4(((position / resolution) * 2.0 - 1.0) * vec2(1.0, -1.0), 0.0, 1.0);
texcoord01 = texcoord + vec2(0, -width);
texcoord10 = texcoord + vec2(-width, 0);
texcoord11 = texcoord;
texcoord12 = texcoord + vec2(width, 0);
texcoord21 = texcoord + vec2(0, width);} `;
var embossVertexShaderSrc = `
precision mediump float;
attribute vec2 position;
attribute vec2 texcoord;
uniform vec2 resolution;
uniform float width;
varying vec2 texcoord00;
varying vec2 texcoord01;
varying vec2 texcoord02;
varying vec2 texcoord10;
varying vec2 texcoord12;
varying vec2 texcoord20;
varying vec2 texcoord21;
varying vec2 texcoord22;
void main() {
gl_Position = vec4(((position / resolution) * 2.0 - 1.0) * vec2(1.0, -1.0), 0.0, 1.0);
texcoord00 = texcoord + vec2(-width - 0.01, -width - 0.01);
texcoord01 = texcoord + vec2( 0, -width - 0.01);
texcoord02 = texcoord + vec2( width + 0.01, -width - 0.01);
texcoord10 = texcoord + vec2(-width - 0.01, 0);
texcoord12 = texcoord + vec2( width + 0.01, 0);
texcoord20 = texcoord + vec2(-width - 0.01, width + 0.01);
texcoord21 = texcoord + vec2( 0, width + 0.01);
texcoord22 = texcoord + vec2( width + 0.01, width + 0.01);} `;
var laplaceVertexShaderSrc = `
precision mediump float;
attribute vec2 position;
attribute vec2 texcoord;
uniform vec2 resolution;
uniform float width;
varying vec2 texcoord01;
varying vec2 texcoord10;
varying vec2 texcoord11;
varying vec2 texcoord12;
varying vec2 texcoord21;
void main() {
gl_Position = vec4(((position / resolution) * 2.0 - 1.0) * vec2(1.0, -1.0), 0.0, 1.0);
texcoord01 = texcoord + vec2(0, -width - 0.01);
texcoord10 = texcoord + vec2(-width - 0.01, 0);
texcoord11 = texcoord;
texcoord12 = texcoord + vec2(width + 0.01, 0);
texcoord21 = texcoord + vec2(0, width + 0.01);} `;
var medianVertexShaderSrc = `
precision mediump float;
attribute vec2 position;
attribute vec2 texcoord;
uniform vec2 resolution;
uniform float width;
varying vec2 texcoord0;
varying vec2 texcoord1;
varying vec2 texcoord2;
void main() {
gl_Position = vec4(((position / resolution) * 2.0 - 1.0) * vec2(1.0, -1.0), 0.0, 1.0);
texcoord0 = texcoord;
texcoord1 = texcoord - width;
texcoord2 = texcoord + width;} `;
////////////////FRAGMENT SHADERS////////////////
var blurFragmentShaderSrc = `
precision mediump float;
uniform sampler2D image;
varying vec2 texcoord11;
varying vec2 texcoord00;
varying vec2 texcoord02;
varying vec2 texcoord20;
varying vec2 texcoord22;
void main() {
vec4 blur;
blur = texture2D(image, texcoord11);
blur += texture2D(image, texcoord00);
blur += texture2D(image, texcoord02);
blur += texture2D(image, texcoord20);
blur += texture2D(image, texcoord22);
gl_FragColor = 0.2 * blur;} `;
var dilateFragmentShaderSrc = `
precision mediump float;
uniform sampler2D image;
varying vec2 texcoord00;
varying vec2 texcoord01;
varying vec2 texcoord02;
varying vec2 texcoord10;
varying vec2 texcoord12;
varying vec2 texcoord20;
varying vec2 texcoord21;
varying vec2 texcoord22;
void main() {
vec4 dilate = texture2D(image, 0.5 * (texcoord10 + texcoord12));
dilate = max(dilate, texture2D(image, texcoord00));
dilate = max(dilate, texture2D(image, texcoord01));
dilate = max(dilate, texture2D(image, texcoord02));
dilate = max(dilate, texture2D(image, texcoord10));
dilate = max(dilate, texture2D(image, texcoord12));
dilate = max(dilate, texture2D(image, texcoord20));
dilate = max(dilate, texture2D(image, texcoord21));
dilate = max(dilate, texture2D(image, texcoord22));
gl_FragColor = dilate;} `;
var embossFragmentShaderSrc = `
precision mediump float;
uniform sampler2D image;
uniform vec4 offset;
varying vec2 texcoord00;
varying vec2 texcoord01;
varying vec2 texcoord02;
varying vec2 texcoord10;
varying vec2 texcoord12;
varying vec2 texcoord20;
varying vec2 texcoord21;
varying vec2 texcoord22;
void main() {
vec4 emboss = texture2D(image, texcoord00);
emboss += texture2D(image, texcoord01);
emboss += texture2D(image, texcoord10);
emboss -= texture2D(image, texcoord12);
emboss -= texture2D(image, texcoord21);
emboss -= texture2D(image, texcoord22);
gl_FragColor = emboss + offset;} `;
var erodeFragmentShaderSrc = `
precision mediump float;
uniform sampler2D image;
varying vec2 texcoord00;
varying vec2 texcoord01;
varying vec2 texcoord02;
varying vec2 texcoord10;
varying vec2 texcoord12;
varying vec2 texcoord20;
varying vec2 texcoord21;
varying vec2 texcoord22;
void main() {
vec4 erode = texture2D(image, 0.5 * (texcoord10 + texcoord12));
erode = min(erode, texture2D(image, texcoord00));
erode = min(erode, texture2D(image, texcoord01));
erode = min(erode, texture2D(image, texcoord02));
erode = min(erode, texture2D(image, texcoord10));
erode = min(erode, texture2D(image, texcoord12));
erode = min(erode, texture2D(image, texcoord20));
erode = min(erode, texture2D(image, texcoord21));
erode = min(erode, texture2D(image, texcoord22));
gl_FragColor = erode;} `;
var laplaceFragmentShaderSrc = `
precision mediump float;
uniform sampler2D image;
uniform vec4 offset;
varying vec2 texcoord01;
varying vec2 texcoord10;
varying vec2 texcoord11;
varying vec2 texcoord12;
varying vec2 texcoord21;
void main() {
vec4 f01 = texture2D(image, texcoord01);
vec4 f10 = texture2D(image, texcoord10);
vec4 f11 = texture2D(image, texcoord11);
vec4 f12 = texture2D(image, texcoord12);
vec4 f21 = texture2D(image, texcoord21);
vec4 laplace = 4.0 * f11 - f01 - f10 - f12 - f21;
gl_FragColor = laplace + offset;} `;
var medianFragmentShaderSrc = `
precision mediump float;
uniform sampler2D image;
varying vec2 texcoord0;
varying vec2 texcoord1;
varying vec2 texcoord2;
void main() {
vec4 sample0 = texture2D(image, texcoord0);
vec4 sample1 = texture2D(image, texcoord1);
vec4 sample2 = texture2D(image, texcoord2);
vec4 max0 = max(sample0, sample1);
vec4 max1 = max(sample1, sample2);
vec4 max2 = max(sample2, sample0);
vec4 median = min(min(max0, max1), max2);
gl_FragColor = median;} `;
var radialblurFragmentShaderSrc = `
precision mediump float;
uniform sampler2D tex0;
uniform vec2 resolution;
uniform float width;
varying vec2 v_texcoord;
void main() {
vec2 orn = 0.0*resolution;
vec2 dst = v_texcoord-orn;
vec2 off = dst*width*-0.05;
vec4 pr1 = texture2D(tex0,dst+orn);
vec4 pr2 = texture2D(tex0,dst+off+orn);
vec4 pr3 = texture2D(tex0,(dst+off*2.)+orn);
vec4 pr4 = texture2D(tex0,(dst+off*3.)+orn);
vec4 pr5 = texture2D(tex0,(dst+off*4.)+orn);
vec4 pr6 = texture2D(tex0,(dst+off*5.)+orn);
vec4 pr7 = texture2D(tex0,(dst+off*6.)+orn);
vec4 pr8 = texture2D(tex0,(dst+off*7.)+orn);
vec4 pr9 = texture2D(tex0,(dst+off*8.)+orn);
vec4 avg9 = (pr1+pr2+pr3+pr4+pr5+pr6+pr7+pr8+pr9)/9.0;
gl_FragColor = avg9;} `;
var sharpenFragmentShaderSrc = `
precision mediump float;
uniform sampler2D image;
varying vec2 texcoord11;
varying vec2 texcoord00;
varying vec2 texcoord02;
varying vec2 texcoord20;
varying vec2 texcoord22;
void main() {
vec4 s11 = texture2D(image, texcoord11);
vec4 s00 = texture2D(image, texcoord00);
vec4 s02 = texture2D(image, texcoord02);
vec4 s20 = texture2D(image, texcoord20);
vec4 s22 = texture2D(image, texcoord22);
vec4 sharp = 5.0 * s11 - (s00 + s02 + s20 + s22);
gl_FragColor = sharp;} `;