跳到主要内容
← Image Manipulation with Shaders

Image Manipulation with Shaders

Box Filter

shader-learning 自学第 13 课:Box Filter

实时渲染 · 纹理 GLSL

第 13 课:Box Filter

我的解

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

int RADIUS = 10;

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

  vec3 colorSum = vec3(0.0);
  for(int i = -RADIUS; i < RADIUS+1; i++){
    for(int j = -RADIUS; j < RADIUS+1; j++){
      vec2 sampleUV = uv + vec2(float(i), float(j)) * texelSize;
      colorSum += texture(iChannel0, sampleUV).rgb;
    }
  }
  float N = pow(float(2 * RADIUS + 1),2.0);
  colorSum = colorSum/N;

  gl_FragColor = vec4(colorSum, 1.0);
}