rh.frag 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // From http://graphics.cs.aueb.gr/graphics/research_illumination.html
  2. // "Real-Time Diffuse Global Illumination Using Radiance Hints"
  3. // paper and shader code
  4. uniform float R_wcs = 10.; // Rmax: maximum sampling distance (in WCS units)
  5. uniform vec3 extents;
  6. uniform mat4 RHMatrix;
  7. uniform mat4 RSMMatrix;
  8. uniform sampler2D dtex;
  9. uniform sampler2D ctex;
  10. uniform sampler2D ntex;
  11. uniform vec3 suncol;
  12. flat in int slice;
  13. layout (location = 0) out vec4 SHRed;
  14. layout (location = 1) out vec4 SHGreen;
  15. layout (location = 2) out vec4 SHBlue;
  16. vec3 resolution = vec3(32, 16, 32);
  17. #define SAMPLES 16
  18. vec4 SHBasis (const in vec3 dir)
  19. {
  20. float L00 = 0.282095;
  21. float L1_1 = 0.488603 * dir.y;
  22. float L10 = 0.488603 * dir.z;
  23. float L11 = 0.488603 * dir.x;
  24. return vec4 (L11, L1_1, L10, L00);
  25. }
  26. vec4 DirToSh(vec3 dir, float flux)
  27. {
  28. return SHBasis (dir) * flux;
  29. }
  30. // We need to manually unroll the loop, otherwise Nvidia driver crashes.
  31. void loop(in int i,
  32. in vec3 RHcenter,in vec3 RHCellSize, in vec2 RHuv, in float RHdepth,
  33. inout vec4 SHr, inout vec4 SHg, inout vec4 SHb)
  34. {
  35. // produce a new sample location on the RSM texture
  36. float alpha = (i + .5) / SAMPLES;
  37. float theta = 2. * 3.14 * 7. * alpha;
  38. float h = alpha;
  39. vec2 offset = h * vec2(cos(theta), sin(theta));
  40. vec2 uv = RHuv + offset * 0.01;
  41. // Get world position and normal from the RSM sample
  42. float depth = texture(dtex, uv).x;
  43. vec4 RSMPos = inverse(RSMMatrix) * (2. * vec4(uv, depth, 1.) - 1.);
  44. RSMPos /= RSMPos.w;
  45. vec3 RSMAlbedo = texture(ctex, uv).xyz;
  46. vec3 normal = normalize(2. * texture(ntex, uv).xyz - 1.);
  47. // Sampled location inside the RH cell
  48. vec3 offset3d = vec3(uv, 0);
  49. vec3 SamplePos = RHcenter + .5 * offset3d.xzy * RHCellSize;
  50. // Normalize distance to RSM sample
  51. float dist = distance(SamplePos, RSMPos.xyz) / R_wcs;
  52. // Determine the incident direction.
  53. // Avoid very close samples (and numerical instability problems)
  54. vec3 RSM_to_RH_dir = (dist <= 0.1) ? vec3(0.) : normalize(SamplePos - RSMPos.xyz);
  55. float dotprod = max(dot(RSM_to_RH_dir, normal.xyz), 0.);
  56. float factor = dotprod / (0.1 + dist * dist);
  57. vec3 color = RSMAlbedo.rgb * factor * suncol.rgb;
  58. SHr += DirToSh(RSM_to_RH_dir, color.r);
  59. SHg += DirToSh(RSM_to_RH_dir, color.g);
  60. SHb += DirToSh(RSM_to_RH_dir, color.b);
  61. }
  62. void main(void)
  63. {
  64. vec3 normalizedRHCenter = 2. * vec3(gl_FragCoord.xy, slice) / resolution - 1.;
  65. vec3 RHcenter = (RHMatrix * vec4(normalizedRHCenter * extents, 1.)).xyz;
  66. vec4 ShadowProjectedRH = RSMMatrix * vec4(RHcenter, 1.);
  67. vec3 RHCellSize = extents / resolution;
  68. vec2 RHuv = .5 * ShadowProjectedRH.xy / ShadowProjectedRH.w + .5;
  69. float RHdepth = .5 * ShadowProjectedRH.z / ShadowProjectedRH.w + .5;
  70. vec4 SHr = vec4(0.);
  71. vec4 SHg = vec4(0.);
  72. vec4 SHb = vec4(0.);
  73. int x = int(gl_FragCoord.x), y = int(gl_FragCoord.y);
  74. float phi = 30. * (x ^ y) + 10. * x * y;
  75. loop(0, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  76. loop(1, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  77. loop(2, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  78. loop(3, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  79. loop(4, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  80. loop(5, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  81. loop(6, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  82. loop(7, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  83. loop(8, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  84. loop(9, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  85. loop(10, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  86. loop(11, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  87. loop(12, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  88. loop(13, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  89. loop(14, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  90. loop(15, RHcenter, RHCellSize, RHuv, RHdepth, SHr, SHg, SHb);
  91. SHr /= 3.14159 * SAMPLES;
  92. SHg /= 3.14159 * SAMPLES;
  93. SHb /= 3.14159 * SAMPLES;
  94. SHRed = SHr;
  95. SHGreen = SHg;
  96. SHBlue = SHb;
  97. }