Pixel-Composer/shaders/sh_blur_simple/sh_blur_simple.fsh

75 lines
1.5 KiB
Text
Raw Normal View History

2022-12-27 04:00:50 +01:00
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float size;
uniform vec2 dimension;
uniform int useMask;
uniform sampler2D mask;
uniform int sampleMode;
2023-01-17 08:11:55 +01:00
uniform int overrideColor;
uniform vec4 overColor;
2023-01-01 02:06:02 +01:00
float sampleMask() {
if(useMask == 0) return 1.;
vec4 m = texture2D( mask, v_vTexcoord );
return (m.r + m.g + m.b) / 3. * m.a;
}
2022-12-27 04:00:50 +01:00
vec4 sampleTexture(vec2 pos) {
2023-01-17 08:11:55 +01:00
if(pos.x >= 0. && pos.y >= 0. && pos.x <= 1. && pos.y <= 1.)
2022-12-27 04:00:50 +01:00
return texture2D(gm_BaseTexture, pos);
if(sampleMode == 0)
return vec4(0.);
if(sampleMode == 1)
return texture2D(gm_BaseTexture, clamp(pos, 0., 1.));
if(sampleMode == 2)
return texture2D(gm_BaseTexture, fract(pos));
return vec4(0.);
}
void main() {
vec4 clr = vec4(0.);
float totalWeight = 0.;
vec2 texel = 1. / dimension;
float realSize = size;
2023-01-01 02:06:02 +01:00
realSize *= sampleMask();
2022-12-27 04:00:50 +01:00
if(realSize < 1.) {
gl_FragColor = sampleTexture( v_vTexcoord );
return;
} else if(realSize < 2.)
realSize = 1.;
float cel = ceil(realSize);
float frac = fract(realSize);
for( float i = -cel; i <= cel; i++ )
for( float j = -cel; j <= cel; j++ ) {
if(i + j >= cel * 2.) continue;
vec4 sam = sampleTexture( v_vTexcoord + vec2(i, j) * texel );
float wei = 1. - (abs(i) + abs(j)) / (realSize * 2.);
wei *= clamp(abs(i + j - floor(realSize) * 2.), 0., 1.);
totalWeight += wei;
clr += sam * wei;
}
clr /= totalWeight;
gl_FragColor = clr;
2024-01-08 08:10:50 +01:00
if(overrideColor == 1) {
2023-01-17 08:11:55 +01:00
gl_FragColor.rgb = overColor.rgb;
2024-01-08 08:10:50 +01:00
gl_FragColor.a *= overColor.a;
}
2022-12-27 04:00:50 +01:00
}