-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoutline.glsl
91 lines (67 loc) · 2.36 KB
/
outline.glsl
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
#START_VERTEX
#version 140
/**
-------------- outline vertex shader -------------
author: Richman Stewart
simple vertex shader that sets the position
to the specified matrix and position while
passing the vertex colour and tex coords
to the fragment shader
**/
in vec2 a_position;
in vec2 a_tex_coord;
in vec4 a_colour;
uniform mat4 matrix;
out vec4 v_colour;
out vec2 tex_coords;
void main() {
v_colour = a_colour;
tex_coords = a_tex_coord;
gl_Position = matrix * vec4(a_position, 0, 1);
}
#END_VERTEX
#START_FRAGMENT
#version 140
/**
------------ one pass outline shader ------------
author: Richman Stewart
applies a gaussian blur horizontally and vertically
behind the original texture and makes it black
------------------ use ------------------------
outline_thickness - outline spread amount
outline_colour - colour of the outline
**/
in vec4 v_colour;
in vec2 tex_coords;
out vec4 pixel;
uniform sampler2D t0;
uniform float outline_thickness = .2;
uniform vec3 outline_colour = vec3(0, 0, 1);
uniform float outline_threshold = .5;
void main() {
pixel = texture(t0, tex_coords);
if (pixel.a <= outline_threshold) {
ivec2 size = textureSize(t0, 0);
float uv_x = tex_coords.x * size.x;
float uv_y = tex_coords.y * size.y;
float sum = 0.0;
for (int n = 0; n < 9; ++n) {
uv_y = (tex_coords.y * size.y) + (outline_thickness * float(n - 4.5));
float h_sum = 0.0;
h_sum += texelFetch(t0, ivec2(uv_x - (4.0 * outline_thickness), uv_y), 0).a;
h_sum += texelFetch(t0, ivec2(uv_x - (3.0 * outline_thickness), uv_y), 0).a;
h_sum += texelFetch(t0, ivec2(uv_x - (2.0 * outline_thickness), uv_y), 0).a;
h_sum += texelFetch(t0, ivec2(uv_x - outline_thickness, uv_y), 0).a;
h_sum += texelFetch(t0, ivec2(uv_x, uv_y), 0).a;
h_sum += texelFetch(t0, ivec2(uv_x + outline_thickness, uv_y), 0).a;
h_sum += texelFetch(t0, ivec2(uv_x + (2.0 * outline_thickness), uv_y), 0).a;
h_sum += texelFetch(t0, ivec2(uv_x + (3.0 * outline_thickness), uv_y), 0).a;
h_sum += texelFetch(t0, ivec2(uv_x + (4.0 * outline_thickness), uv_y), 0).a;
sum += h_sum / 9.0;
}
if (sum / 9.0 >= 0.0001) {
pixel = vec4(outline_colour, 1);
}
}
}
#END_FRAGMENT