ScreenSpaceLensFlare.ankiprog 2.5 KB

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