跳到主要内容
← Image Manipulation with Shaders

Image Manipulation with Shaders

Gaussian Blur

shader-learning 自学第 14 课:Gaussian Blur

实时渲染 · 纹理 GLSL

第 14 课:Gaussian Blur

我的解

uniform vec2 iResolution;
uniform sampler2D iChannel0;
uniform vec2 iChannelSize0;

const int   RADIUS = 30;
const float SIGMA  = 15.0; // larger values - smoother result
const float PI = 3.14;

void main() {
  vec2 uv = gl_FragCoord.xy / iResolution.xy;
  vec2 texelSize = vec2(1.0) / iChannelSize0;

  float SumOfWeight = 0.0;
  vec3 result = vec3(0.0);
  for (int dx = -RADIUS; dx <= RADIUS; dx++) {
    for (int dy = -RADIUS; dy <= RADIUS; dy++) {
      vec2 offset = vec2(dx, dy);
      vec2 sampleUV = uv + offset * texelSize;
      float Weight = 1.0/(2.0*PI*pow(SIGMA,2.0)) * exp(-(pow(offset.x,2.0)+pow(offset.y,2.0))/(2.0*pow(SIGMA,2.0)));
      vec3 sampleColor = texture(iChannel0, sampleUV).rgb * Weight;
      SumOfWeight += Weight;

      result += sampleColor;
    }
  }


  float N = SumOfWeight;
  gl_FragColor = vec4(result / N, 1.0);
}