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
|
/* ******************************************************************* */
/* Resolve RIM pass and add blur if needed */
/* ******************************************************************* */
uniform sampler2D strokeColor;
uniform sampler2D strokeDepth;
uniform sampler2D strokeRim;
uniform vec3 mask_color;
uniform int mode;
out vec4 FragColor;
#define MODE_NORMAL 0
#define MODE_OVERLAY 1
#define MODE_ADD 2
#define MODE_SUB 3
#define MODE_MULTIPLY 4
#define MODE_DIVIDE 5
float overlay_color(float a, float b)
{
float rtn;
if (a < 0.5) {
rtn = 2.0 * a * b;
}
else {
rtn = 1.0 - 2.0 * (1.0 - a) * (1.0 - b);
}
return rtn;
}
vec4 get_blend_color(int mode, vec4 src_color, vec4 mix_color)
{
vec4 outcolor;
if (mode == MODE_NORMAL) {
outcolor = mix_color;
}
else if (mode == MODE_OVERLAY) {
outcolor.r = overlay_color(src_color.r, mix_color.r);
outcolor.g = overlay_color(src_color.g, mix_color.g);
outcolor.b = overlay_color(src_color.b, mix_color.b);
}
else if (mode == MODE_ADD) {
outcolor = src_color + mix_color;
}
else if (mode == MODE_SUB) {
outcolor = src_color - mix_color;
}
else if (mode == MODE_MULTIPLY) {
outcolor = src_color * mix_color;
}
else if (mode == MODE_DIVIDE) {
outcolor = src_color / mix_color;
}
else {
outcolor = mix_color;
}
/* use always the alpha of source color */
outcolor.a = src_color.a;
/* use alpha to calculate the weight of the mixed color */
outcolor = mix(src_color, outcolor, mix_color.a);
return outcolor;
}
void main()
{
ivec2 uv = ivec2(gl_FragCoord.xy);
float stroke_depth = texelFetch(strokeDepth, uv.xy, 0).r;
vec4 src_pixel = texelFetch(strokeColor, uv.xy, 0);
vec4 rim_pixel = texelFetch(strokeRim, uv.xy, 0);
vec4 outcolor = src_pixel;
/* is transparent */
if (src_pixel.a == 0.0f) {
discard;
}
/* pixel is equal to mask color, keep */
else if (src_pixel.rgb == mask_color.rgb) {
outcolor = src_pixel;
}
else {
if (rim_pixel.a == 0.0f) {
outcolor = src_pixel;
}
else {
outcolor = get_blend_color(mode, src_pixel, rim_pixel);
}
}
gl_FragDepth = stroke_depth;
FragColor = outcolor;
}
|