blur_shader.fsh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifdef GL_ES
  2. precision mediump float;
  3. #endif
  4. varying vec4 v_fragmentColor;
  5. varying vec2 v_texCoord;
  6. uniform vec2 resolution;
  7. uniform float blurRadius;
  8. uniform float sampleNum;
  9. vec4 blur(vec2);
  10. void main(void)
  11. {
  12. vec4 col = blur(v_texCoord); //* v_fragmentColor.rgb;
  13. gl_FragColor = vec4(col) * v_fragmentColor;
  14. }
  15. vec4 blur(vec2 p)
  16. {
  17. resolution.x = 720;
  18. resolution.y = 1280;
  19. blurRadius = 8; //为模糊半径,此参数越大,模糊效果越明显(尽量不超过8)
  20. sampleNum = 5; //为模糊采样,此参数越大,模糊效果越细腻(尽量不超过8)
  21. if (blurRadius > 0.0 && sampleNum > 1.0)
  22. {
  23. vec4 col = vec4(0);
  24. vec2 unit = 1.0 / resolution.xy;
  25. float r = blurRadius;
  26. float sampleStep = r / sampleNum;
  27. float count = 0.0;
  28. for(float x = -r; x < r; x += sampleStep)
  29. {
  30. for(float y = -r; y < r; y += sampleStep)
  31. {
  32. float weight = (r - abs(x)) * (r - abs(y));
  33. col += texture2D(CC_Texture0, p + vec2(x * unit.x, y * unit.y)) * weight;
  34. count += weight;
  35. }
  36. }
  37. return col / count;
  38. }
  39. return texture2D(CC_Texture0, p);
  40. }