-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathshader.js
75 lines (71 loc) · 2.38 KB
/
shader.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
export default `#version 300 es
precision highp float;
uniform sampler2D u_texture;
uniform vec2 u_resolution;
uniform bool u_rotate;
uniform bool u_greyscale;
uniform bool u_active;
out vec4 colour;
vec4 tpix(float x, float y) {
vec3 color = texture(u_texture, vec2(x, y)).rgb;
if (!u_greyscale) {
return vec4(color, 1.0);
}
float grey = dot(color, vec3(0.299, 0.587, 0.114));
return vec4(vec3(grey), 1.0);
}
void main() {
if (!u_active) {
colour = vec4(0, 0, 0, 1.0);
return;
}
ivec2 size = textureSize(u_texture, 0);
float ar_texture = float(size.x) / float(size.y);
if (u_rotate) {
float ar_resolution = u_resolution.y / u_resolution.x;
if (ar_resolution >= ar_texture) {
float height = float(u_resolution.x) * ar_texture;
float border_height = (u_resolution.y - height) / 2.0;
if ((gl_FragCoord.y < border_height) ||
(gl_FragCoord.y >= (border_height + height))) {
colour = vec4(0);
} else {
colour = tpix(1.0 - (gl_FragCoord.y - border_height) / height,
gl_FragCoord.x / u_resolution.x);
}
} else {
float width = float(u_resolution.y) / ar_texture;
float border_width = (u_resolution.x - width) / 2.0;
if ((gl_FragCoord.x < border_width) ||
(gl_FragCoord.x >= (border_width + width))) {
colour = vec4(0);
} else {
colour = tpix(1.0 - gl_FragCoord.y / u_resolution.y,
(gl_FragCoord.x - border_width) / width);
}
}
} else {
float ar_resolution = u_resolution.x / u_resolution.y;
if (ar_resolution >= ar_texture) {
float width = float(u_resolution.y) * ar_texture;
float border_width = (u_resolution.x - width) / 2.0;
if ((gl_FragCoord.x < border_width) ||
(gl_FragCoord.x >= (border_width + width))) {
colour = vec4(0);
} else {
colour = tpix((gl_FragCoord.x - border_width) / width,
gl_FragCoord.y / u_resolution.y);
}
} else {
float height = float(u_resolution.x) / ar_texture;
float border_height = (u_resolution.y - height) / 2.0;
if ((gl_FragCoord.y < border_height) ||
(gl_FragCoord.y >= (border_height + height))) {
colour = vec4(0);
} else {
colour = tpix(gl_FragCoord.x / u_resolution.x,
(gl_FragCoord.y - border_height) / height);
}
}
}
}`;