spncryn

Gaussian blur shader

Jun 16th, 2020
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// Vertex (vsh)
  2. // ATTRIBUTION: This shader uses elements from DukeSoft's "Simple bloom shader"
  3. // which can be found here (https://marketplace.yoyogames.com/assets/4729/simple-bloom-shader).
  4.  
  5. //
  6. // Simple passthrough vertex shader
  7. //
  8. attribute vec3 in_Position;                  // (x,y,z)
  9. //attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
  10. attribute vec4 in_Colour;                    // (r,g,b,a)
  11. attribute vec2 in_TextureCoord;              // (u,v)
  12.  
  13. varying vec2 v_vTexcoord;
  14. varying vec4 v_vColour;
  15.  
  16. void main()
  17. {
  18.     vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
  19.     gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
  20.    
  21.     v_vColour = in_Colour;
  22.     v_vTexcoord = in_TextureCoord;
  23. }
  24.  
  25. /// Fragment (fsh)
  26. varying vec2 v_vTexcoord;
  27. varying vec4 v_vColour;
  28. uniform vec3 size;//width,height,radius
  29. uniform float blurSize;
  30. uniform float intensity;
  31.  
  32. const int Quality = 8;
  33. const int Directions = 16;
  34. const float Pi = 6.28318530718;//pi * 2
  35.  
  36. void main()
  37. {
  38.      vec4 sum = vec4(0);
  39.    int j;
  40.    int i;
  41.  
  42.    // take nine samples, with the distance blurSize between them
  43.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x - 4.0*blurSize, v_vTexcoord.y)) * 0.05;
  44.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x - 3.0*blurSize, v_vTexcoord.y)) * 0.09;
  45.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x - 2.0*blurSize, v_vTexcoord.y)) * 0.12;
  46.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x - blurSize, v_vTexcoord.y)) * 0.15;
  47.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y)) * 0.16;
  48.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x + blurSize, v_vTexcoord.y)) * 0.15;
  49.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x + 2.0*blurSize, v_vTexcoord.y)) * 0.12;
  50.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x + 3.0*blurSize, v_vTexcoord.y)) * 0.09;
  51.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x + 4.0*blurSize, v_vTexcoord.y)) * 0.05;
  52.  
  53.    // take nine samples, with the distance blurSize between them
  54.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y - 4.0*blurSize)) * 0.05;
  55.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y - 3.0*blurSize)) * 0.09;
  56.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y - 2.0*blurSize)) * 0.12;
  57.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y - blurSize)) * 0.15;
  58.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y)) * 0.16;
  59.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y + blurSize)) * 0.15;
  60.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y + 2.0*blurSize)) * 0.12;
  61.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y + 3.0*blurSize)) * 0.09;
  62.    sum += texture2D(gm_BaseTexture, vec2(v_vTexcoord.x, v_vTexcoord.y + 4.0*blurSize)) * 0.05;
  63.    
  64.     vec2 radius = size.z/size.xy;
  65.     vec4 Color = texture2D( gm_BaseTexture, v_vTexcoord);
  66.     for( float d=0.0;d<Pi;d+=Pi/float(Directions) )
  67.     {
  68.         for( float i=1.0/float(Quality);i<=1.0;i+=1.0/float(Quality) )
  69.         {
  70.                 Color += texture2D( gm_BaseTexture, v_vTexcoord+vec2(cos(d),sin(d))*radius*i);
  71.         }
  72.     }
  73.     Color /= float(Quality)*float(Directions)+1.0;
  74.     gl_FragColor =  (sum * intensity + Color) *  v_vColour;
  75. }
Add Comment
Please, Sign In to add comment