Pixel-Composer/shaders/sh_glow/sh_glow.fsh
2024-03-16 12:23:47 +07:00

67 lines
1.6 KiB
GLSL

#define TAU 6.283185307179586
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform vec2 dimension;
uniform int mode;
uniform float border;
uniform float size;
uniform float strength;
uniform vec4 color;
uniform int render;
float bright(in vec4 col) { return dot(col.rgb, vec3(0.2126, 0.7152, 0.0722)) * col.a; }
vec4 sample(vec2 pos) { return texture2D( gm_BaseTexture, pos ); }
float round(float val) { return fract(val) > 0.5? ceil(val) : floor(val); }
vec2 round(vec2 val) { return vec2(round(val.x), round(val.y)); }
void main() {
vec2 tx = 1. / dimension;
vec2 px = round(v_vTexcoord * dimension);
vec4 base = sample(v_vTexcoord);
if(render == 1) {
gl_FragColor = base;
} else {
if(mode == 0) gl_FragColor = vec4(0., 0., 0., 1.);
if(mode == 1) gl_FragColor = vec4(0., 0., 0., 0.);
}
if(mode == 0 && base.rgb == vec3(1.)) return;
if(mode == 1 && base.a == 1.) return;
float dist = 0.;
float astp = max(64., size * 4.);
for(float i = 1.; i < size; i++)
for(float j = 0.; j <= astp; j++) {
float angle = j / astp * TAU;
vec2 smPos = v_vTexcoord + vec2(cos(angle), sin(angle)) * i * tx;
//vec2 smPx = round(smPos * dimension);
//if(i < 4. && distance(px, smPx) > i) continue;
vec4 samp = sample(smPos);
if((mode == 0 && bright(samp) > bright(base)) || (mode == 1 && samp.a > base.a)) {
dist = i;
i = size;
break;
}
}
if(dist > 0.) {
vec4 cc = color;
float str = (1. - dist / size) * strength;
if(mode == 0) cc.rgb *= str;
if(mode == 1) cc.a *= str;
if(render == 1) gl_FragColor = base + cc;
else gl_FragColor = cc;
}
}