Sslf.frag.glsl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // Screen space lens flare. Used the technique from here
  6. // http://john-chapman-graphics.blogspot.no/2013/02/pseudo-lens-flare.html
  7. #include "shaders/Common.glsl"
  8. #define MAX_GHOSTS 4
  9. #define GHOST_DISPERSAL (0.7)
  10. #define HALO_WIDTH 0.4
  11. #define CHROMATIC_DISTORTION 3.0
  12. #define ENABLE_CHROMATIC_DISTORTION 1
  13. #define ENABLE_HALO 1
  14. #define HALO_OPACITY 0.5
  15. layout(location = 0) in vec2 in_texCoord;
  16. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_rt;
  17. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_lensDirtTex;
  18. layout(location = 0) out vec3 out_color;
  19. vec3 textureDistorted(in sampler2D tex,
  20. in vec2 texcoord,
  21. in vec2 direction, // direction of distortion
  22. in vec3 distortion) // per-channel distortion factor
  23. {
  24. #if ENABLE_CHROMATIC_DISTORTION
  25. return vec3(texture(tex, texcoord + direction * distortion.r).r,
  26. texture(tex, texcoord + direction * distortion.g).g,
  27. texture(tex, texcoord + direction * distortion.b).b);
  28. #else
  29. return texture(tex, texcoord).rgb;
  30. #endif
  31. }
  32. void main()
  33. {
  34. vec2 texcoord = vec2(1.0) - in_texCoord;
  35. vec2 ghostVec = (vec2(0.5) - texcoord) * GHOST_DISPERSAL;
  36. const vec2 texelSize = 1.0 / vec2(TEX_DIMENSIONS);
  37. const vec3 distortion = vec3(-texelSize.x * CHROMATIC_DISTORTION, 0.0, texelSize.x * CHROMATIC_DISTORTION);
  38. const float lenOfHalf = length(vec2(0.5));
  39. vec2 direction = normalize(ghostVec);
  40. vec3 result = vec3(0.0);
  41. // sample ghosts:
  42. for(int i = 0; i < MAX_GHOSTS; ++i)
  43. {
  44. vec2 offset = fract(texcoord + ghostVec * float(i));
  45. float weight = length(vec2(0.5) - offset) / lenOfHalf;
  46. weight = pow(1.0 - weight, 10.0);
  47. result += textureDistorted(u_rt, offset, direction, distortion) * weight;
  48. }
  49. // sample halo
  50. #if ENABLE_HALO
  51. vec2 haloVec = normalize(ghostVec) * HALO_WIDTH;
  52. float weight = length(vec2(0.5) - fract(texcoord + haloVec)) / lenOfHalf;
  53. weight = pow(1.0 - weight, 20.0);
  54. result += textureDistorted(u_rt, texcoord + haloVec, direction, distortion) * (weight * HALO_OPACITY);
  55. #endif
  56. // lens dirt
  57. result *= texture(u_lensDirtTex, in_texCoord).rgb;
  58. // Write
  59. out_color = result;
  60. }