glowBlurPostProcess.fragment.fx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Samplers
  2. varying vec2 vUV;
  3. uniform sampler2D textureSampler;
  4. // Parameters
  5. uniform vec2 screenSize;
  6. uniform vec2 direction;
  7. uniform float blurWidth;
  8. // Transform color to luminance.
  9. float getLuminance(vec3 color)
  10. {
  11. return dot(color, vec3(0.2126, 0.7152, 0.0722));
  12. }
  13. void main(void)
  14. {
  15. float weights[7];
  16. weights[0] = 0.05;
  17. weights[1] = 0.1;
  18. weights[2] = 0.2;
  19. weights[3] = 0.3;
  20. weights[4] = 0.2;
  21. weights[5] = 0.1;
  22. weights[6] = 0.05;
  23. vec2 texelSize = vec2(1.0 / screenSize.x, 1.0 / screenSize.y);
  24. vec2 texelStep = texelSize * direction * blurWidth;
  25. vec2 start = vUV - 3.0 * texelStep;
  26. vec4 baseColor = vec4(0., 0., 0., 0.);
  27. vec2 texelOffset = vec2(0., 0.);
  28. for (int i = 0; i < 7; i++)
  29. {
  30. // alpha blur.
  31. vec4 texel = texture2D(textureSampler, start + texelOffset);
  32. baseColor.a += texel.a * weights[i];
  33. // Highest Luma for outline.
  34. float luminance = getLuminance(baseColor.rgb);
  35. float luminanceTexel = getLuminance(texel.rgb);
  36. float choice = step(luminanceTexel, luminance);
  37. baseColor.rgb = choice * baseColor.rgb + (1.0 - choice) * texel.rgb;
  38. texelOffset += texelStep;
  39. }
  40. gl_FragColor = baseColor;
  41. }