径向模糊
对x方向周围像素进行权重混合
shader_type canvas_item;
uniform int SAMPLES = 6;
uniform float WIDTH = 4.0;
uniform vec4 OUTLINE_COLOR = vec4(0.,0.,0.,1.);
uniform vec2 TEXTURE_SCALE = vec2(0.94);
float scurve(float x){
x = x*2. - 1.;
return -x*abs(x)*.5+x+.5;
}
vec4 gaussian_blur_h(sampler2D src, vec2 size, vec2 uv, float radius){
if(radius>=1.0){
vec4 c = vec4(0.);
// float width = 1./size.x;
float divsor = 0.;
float weight = 0.;
float radius_multiplier = 1./radius;
for(float x=-10.; x<=10.;x++){
weight = smoothstep(-1.,1.,1.-abs(x)*radius_multiplier);
c+=texture(src, uv+vec2(x*size.x,0.))*weight;
divsor+=weight;
}
return vec4(c.r/divsor, c.g/divsor, c.b/divsor, 1.0);
}
return texture(src,uv);
}
void fragment(){
COLOR = gaussian_blur_h(TEXTURE ,TEXTURE_PIXEL_SIZE ,UV , 5.);
}
源码:
https://www.shadertoy.com/view/Mtl3Rj
高斯模糊
shader_type canvas_item;
float normpdf(in float x, in float sigma){
return 0.39894*exp(-.5*x*x/(sigma*sigma))/sigma;
}
vec4 gaussian_blur(sampler2D src, vec2 size, vec2 uv, int m_size){
vec4 cc = texture(src, uv);
vec3 c = cc.rgb;
int k_size = (m_size-1)/2;
float sigma = 7.;
vec3 final_color = vec3(0.);
float z = 0.;
for(int i=-k_size;i<=k_size;i++){
float n = normpdf(float(i), sigma);
z+=n;
for(int j=-k_size;j<=k_size;j++){
final_color+=n*n*texture(src,uv+size*vec2(float(i),float(j))).rgb;
}
}
return vec4(final_color/z/z,1.);
}
void fragment(){
COLOR = gaussian_blur(TEXTURE ,TEXTURE_PIXEL_SIZE ,UV , 10);
}