Godot - Shader - 模糊

径向模糊

对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);
}

https://www.shadertoy.com/view/XdfGDH

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,879评论 1 45
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • 小弟: 昨天夜里,我第一次听到蝉鸣,今年夏天的蝉鸣。 我想起年少时候,每年暑假,我们天天在外面找知了猴儿,中午就出...
    捕蛇者阅读 417评论 0 1
  • 这篇文章主要会涵盖市场背景、产品经理分类、产品经理成长路径、产品经理职业发展四大块。 市场背景 中国市面上直接以“...
    Arthurwu24阅读 391评论 0 0
  • 上午接到电话,荣村那点活飞抓紧干,因为上边要检查,今天必须完工,原计划端午期间上午去,凉快而且不用请假,可今天,工...
    吴利苹阅读 243评论 0 0