Skip to content
0.2.0-dev Download ZIP
86 lines ·1.6 KB Raw
1/* clang-format off */
2[vertex]
3
4#ifdef USE_GLES_OVER_GL
5#define lowp
6#define mediump
7#define highp
8#else
9precision highp float;
10precision highp int;
11#endif
12
13layout(location = 0) in highp vec2 vertex;
14/* clang-format on */
15
16uniform vec2 offset;
17uniform vec2 scale;
18
19out vec2 uv_interp;
20
21void main() {
22 uv_interp = vertex.xy * 2.0 - 1.0;
23
24 vec2 v = vertex.xy * scale + offset;
25 gl_Position = vec4(v, 0.0, 1.0);
26}
27
28/* clang-format off */
29[fragment]
30
31#ifdef USE_GLES_OVER_GL
32#define lowp
33#define mediump
34#define highp
35#else
36#if defined(USE_HIGHP_PRECISION)
37precision highp float;
38precision highp int;
39#else
40precision mediump float;
41precision mediump int;
42#endif
43#endif
44
45uniform sampler2D source; //texunit:0
46/* clang-format on */
47
48uniform vec2 eye_center;
49uniform float k1;
50uniform float k2;
51uniform float upscale;
52uniform float aspect_ratio;
53
54in vec2 uv_interp;
55
56layout(location = 0) out vec4 frag_color;
57
58void main() {
59 vec2 coords = uv_interp;
60 vec2 offset = coords - eye_center;
61
62 // take aspect ratio into account
63 offset.y /= aspect_ratio;
64
65 // distort
66 vec2 offset_sq = offset * offset;
67 float radius_sq = offset_sq.x + offset_sq.y;
68 float radius_s4 = radius_sq * radius_sq;
69 float distortion_scale = 1.0 + (k1 * radius_sq) + (k2 * radius_s4);
70 offset *= distortion_scale;
71
72 // reapply aspect ratio
73 offset.y *= aspect_ratio;
74
75 // add our eye center back in
76 coords = offset + eye_center;
77 coords /= upscale;
78
79 // and check our color
80 if (coords.x < -1.0 || coords.y < -1.0 || coords.x > 1.0 || coords.y > 1.0) {
81 frag_color = vec4(0.0, 0.0, 0.0, 1.0);
82 } else {
83 coords = (coords + vec2(1.0)) / vec2(2.0);
84 frag_color = texture(source, coords);
85 }
86}