refract.frag 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #version 120
  2. uniform sampler2D texture;
  3. uniform sampler2D desktop;
  4. uniform vec2 screenSize;
  5. uniform float time;
  6. varying vec2 uvCoord;
  7. vec2 getDesktopUV( vec2 uv ) {
  8. // Desktop image is upside-down, blame X11 lmao
  9. uv.y = -uv.y;
  10. return uv;
  11. }
  12. const float pi = 3.14159265f;
  13. void main() {
  14. float sigma = 5;
  15. float numBlurPixelsPerSide = 2.0f;
  16. float blurMultiplier = 2.0f;
  17. vec2 tc = uvCoord.xy;
  18. vec2 p = -1.0 + 2.0 * tc;
  19. float len = length(p);
  20. vec2 offset = (p/len)*cos(len*12.0-time*4.0)*0.005;
  21. // Incremental Gaussian Coefficent Calculation (See GPU Gems 3 pp. 877 - 889)
  22. float blurSize = 1.f/screenSize.x*blurMultiplier;
  23. vec2 blurMultiplyVec = vec2(0.0f, 1.0f);
  24. vec3 incrementalGaussian;
  25. incrementalGaussian.x = 1.0f / (sqrt(2.0f * pi) * sigma);
  26. incrementalGaussian.y = exp(-0.5f / (sigma * sigma));
  27. incrementalGaussian.z = incrementalGaussian.y * incrementalGaussian.y;
  28. vec4 avgValue = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  29. float coefficientSum = 0.0f;
  30. // Take the central sample first...
  31. vec4 color = texture2D( desktop, getDesktopUV( uvCoord.xy ) + offset );
  32. avgValue += color * incrementalGaussian.x;
  33. coefficientSum += incrementalGaussian.x;
  34. incrementalGaussian.xy *= incrementalGaussian.yz;
  35. // Go through the remaining 8 vertical samples (4 on each side of the center)
  36. for (float i = 1.0f; i <= numBlurPixelsPerSide; i++) {
  37. vec2 uv = (uvCoord.xy - i * blurSize * blurMultiplyVec);
  38. color = texture2D( desktop, getDesktopUV( uv ) + offset );
  39. avgValue += color * incrementalGaussian.x;
  40. uv = (uvCoord.xy + i * blurSize * blurMultiplyVec);
  41. color = texture2D( desktop, getDesktopUV( uv ) + offset );
  42. avgValue += color * incrementalGaussian.x;
  43. coefficientSum += 2 * incrementalGaussian.x;
  44. incrementalGaussian.xy *= incrementalGaussian.yz;
  45. }
  46. //Reset
  47. blurSize = 1.f/screenSize.x*blurMultiplier;
  48. blurMultiplyVec = vec2(1.0f, 0.0f);
  49. incrementalGaussian.x = 1.0f / (sqrt(2.0f * pi) * sigma);
  50. incrementalGaussian.y = exp(-0.5f / (sigma * sigma));
  51. incrementalGaussian.z = incrementalGaussian.y * incrementalGaussian.y;
  52. // Go through the remaining 8 horizontal samples (4 on each side of the center)
  53. for (float i = 1.0f; i <= numBlurPixelsPerSide; i++) {
  54. vec2 uv = (uvCoord.xy - i * blurSize * blurMultiplyVec);
  55. vec4 color = texture2D( desktop, getDesktopUV( uv ) + offset );
  56. avgValue += color * incrementalGaussian.x;
  57. uv = (uvCoord.xy + i * blurSize * blurMultiplyVec);
  58. color = texture2D( desktop, getDesktopUV( uv ) + offset );
  59. avgValue += color * incrementalGaussian.x;
  60. coefficientSum += 2 * incrementalGaussian.x;
  61. incrementalGaussian.xy *= incrementalGaussian.yz;
  62. }
  63. vec4 rect = texture2D(texture,uvCoord);
  64. gl_FragColor = (avgValue / coefficientSum)*rect;
  65. }