2014-04-02 18:14:53 -04:00
|
|
|
uniform sampler2D blendMap;
|
|
|
|
uniform sampler2D colorMap;
|
|
|
|
|
|
|
|
out vec4 FragColor;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
|
|
|
void main() {
|
2015-02-24 16:08:49 -05:00
|
|
|
vec2 uv = gl_FragCoord.xy / screen;
|
|
|
|
vec2 uv_left = uv + vec2(-1., 0.) / screen;
|
|
|
|
vec2 uv_top = uv + vec2(0., 1.) / screen;
|
|
|
|
vec2 uv_right = uv + vec2(1., 0.) / screen;
|
|
|
|
vec2 uv_bottom = uv + vec2(0., -1.) / screen;
|
|
|
|
|
2013-11-30 16:33:06 -05:00
|
|
|
// Fetch the blending weights for current pixel:
|
2014-01-19 12:53:35 -05:00
|
|
|
vec4 topLeft = texture(blendMap, uv);
|
2015-02-24 16:08:49 -05:00
|
|
|
float bottom = texture(blendMap, uv_bottom).g;
|
|
|
|
float right = texture(blendMap, uv_right).a;
|
2013-11-30 16:33:06 -05:00
|
|
|
vec4 a = vec4(topLeft.r, bottom, topLeft.b, right);
|
|
|
|
|
|
|
|
// Up to 4 lines can be crossing a pixel (one in each edge). So, we perform
|
|
|
|
// a weighted average, where the weight of each line is 'a' cubed, which
|
|
|
|
// favors blending and works well in practice.
|
|
|
|
vec4 w = a * a * a;
|
|
|
|
|
|
|
|
// There is some blending weight with a value greater than 0.0?
|
|
|
|
float sum = dot(w, vec4(1.0));
|
|
|
|
if (sum < 1e-5)
|
|
|
|
discard;
|
|
|
|
|
|
|
|
vec4 color = vec4(0.0);
|
|
|
|
|
|
|
|
// Add the contributions of the possible 4 lines that can cross this pixel:
|
2014-04-13 21:30:42 -04:00
|
|
|
vec4 C = texture(colorMap, uv);
|
2015-02-24 16:08:49 -05:00
|
|
|
vec4 Cleft = texture(colorMap, uv_left);
|
|
|
|
vec4 Ctop = texture(colorMap, uv_top);
|
|
|
|
vec4 Cright = texture(colorMap, uv_right);
|
|
|
|
vec4 Cbottom = texture(colorMap, uv_bottom);
|
2013-11-30 16:33:06 -05:00
|
|
|
color = mix(C, Ctop, a.r) * w.r + color;
|
|
|
|
color = mix(C, Cbottom, a.g) * w.g + color;
|
|
|
|
color = mix(C, Cleft, a.b) * w.b + color;
|
|
|
|
color = mix(C, Cright, a.a) * w.a + color;
|
|
|
|
|
|
|
|
// Normalize the resulting color and we are finished!
|
2014-04-13 21:30:42 -04:00
|
|
|
FragColor = vec4(color / sum);
|
2013-11-30 16:33:06 -05:00
|
|
|
}
|