Skip to content

Instantly share code, notes, and snippets.

@RChehowski
Created October 6, 2016 12:51
Show Gist options
  • Save RChehowski/d09ab410040f6f04e1088af6e86b711f to your computer and use it in GitHub Desktop.
Save RChehowski/d09ab410040f6f04e1088af6e86b711f to your computer and use it in GitHub Desktop.
entry
uniform sampler2D alpha;
uniform sampler2D textureRgba;
uniform int mode;
uniform vec4 color;
uniform vec4 multiplierColor;
varying vec2 texCoord;
uniform int width;
uniform int height;
uniform float outlineStrength;
uniform float radius;
vec4 sample2D(vec2 texCoord)
{
vec4 rgb = texture2D(textureRgba, texCoord);
vec4 a = texture2D(alpha, texCoord);
vec4 clr = multiplierColor * vec4(rgb.xyz, a.r * float(1 - mode) + rgb.a * float(mode));
return clr;
}
vec4 outline(vec2 texCoord)
{
float t = 0.0;
float w = outlineStrength/float(width);
float h = outlineStrength/float(height);
float sq = 1.0 / sqrt(2.0);
t = sample2D(texCoord).a;
t = max(t, sample2D(texCoord + vec2(0.0, - h)).a);
t = max(t, sample2D(texCoord + vec2(0.0, h)).a);
t = max(t, sample2D(texCoord + vec2(-w, 0.0)).a);
t = max(t, sample2D(texCoord + vec2(w, 0.0)).a);
t = max(t, sample2D(texCoord + vec2(-w * sq, - h * sq)).a);
t = max(t, sample2D(texCoord + vec2(w * sq, - h * sq)).a);
t = max(t, sample2D(texCoord + vec2(w * sq, h * sq)).a);
t = max(t, sample2D(texCoord + vec2(w * sq, h * sq)).a);
return vec4(color.xyz, t * color.a);
}
vec4 blur(vec2 texCoord)
{
float dx = radius / float(width);
float dy = radius / float(height);
float sq = 1.0 / sqrt(2.0);
vec4 c = outline(texCoord);
c += outline(texCoord + vec2( dx, 0.0));
c += outline(texCoord + vec2(-dx, 0.0));
c += outline(texCoord + vec2( 0.0, -dy));
c += outline(texCoord + vec2( 0.0, dy));
c += outline(texCoord + vec2(-dx * sq, dy * sq));
c += outline(texCoord + vec2( dx * sq, -dy * sq));
c += outline(texCoord + vec2( dx * sq, dy * sq));
c += outline(texCoord + vec2(-dx * sq, -dy * sq));
return vec4(c.r * 0.125, c.g * 0.125, c.b * 0.125, c.a * 0.125);
}
void main()
{
vec4 clr = sample2D(texCoord);
vec4 blured = blur(texCoord);
vec4 r = vec4(0.0, 0.0, 0.0, 0.0);
if (clr.a < 0.5)
r = blured;
else
r = clr;
gl_FragColor = r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment