subsurf_scattering.glsl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* clang-format off */
  2. [vertex]
  3. layout(location = 0) in highp vec4 vertex_attrib;
  4. /* clang-format on */
  5. layout(location = 4) in vec2 uv_in;
  6. out vec2 uv_interp;
  7. void main() {
  8. uv_interp = uv_in;
  9. gl_Position = vertex_attrib;
  10. }
  11. /* clang-format off */
  12. [fragment]
  13. //#define QUALIFIER uniform // some guy on the interweb says it may be faster with this
  14. #define QUALIFIER const
  15. #ifdef USE_25_SAMPLES
  16. const int kernel_size = 25;
  17. /* clang-format on */
  18. QUALIFIER vec2 kernel[25] = vec2[](
  19. vec2(0.530605, 0.0),
  20. vec2(0.000973794, -3.0),
  21. vec2(0.00333804, -2.52083),
  22. vec2(0.00500364, -2.08333),
  23. vec2(0.00700976, -1.6875),
  24. vec2(0.0094389, -1.33333),
  25. vec2(0.0128496, -1.02083),
  26. vec2(0.017924, -0.75),
  27. vec2(0.0263642, -0.520833),
  28. vec2(0.0410172, -0.333333),
  29. vec2(0.0493588, -0.1875),
  30. vec2(0.0402784, -0.0833333),
  31. vec2(0.0211412, -0.0208333),
  32. vec2(0.0211412, 0.0208333),
  33. vec2(0.0402784, 0.0833333),
  34. vec2(0.0493588, 0.1875),
  35. vec2(0.0410172, 0.333333),
  36. vec2(0.0263642, 0.520833),
  37. vec2(0.017924, 0.75),
  38. vec2(0.0128496, 1.02083),
  39. vec2(0.0094389, 1.33333),
  40. vec2(0.00700976, 1.6875),
  41. vec2(0.00500364, 2.08333),
  42. vec2(0.00333804, 2.52083),
  43. vec2(0.000973794, 3.0));
  44. #endif //USE_25_SAMPLES
  45. #ifdef USE_17_SAMPLES
  46. const int kernel_size = 17;
  47. QUALIFIER vec2 kernel[17] = vec2[](
  48. vec2(0.536343, 0.0),
  49. vec2(0.00317394, -2.0),
  50. vec2(0.0100386, -1.53125),
  51. vec2(0.0144609, -1.125),
  52. vec2(0.0216301, -0.78125),
  53. vec2(0.0347317, -0.5),
  54. vec2(0.0571056, -0.28125),
  55. vec2(0.0582416, -0.125),
  56. vec2(0.0324462, -0.03125),
  57. vec2(0.0324462, 0.03125),
  58. vec2(0.0582416, 0.125),
  59. vec2(0.0571056, 0.28125),
  60. vec2(0.0347317, 0.5),
  61. vec2(0.0216301, 0.78125),
  62. vec2(0.0144609, 1.125),
  63. vec2(0.0100386, 1.53125),
  64. vec2(0.00317394, 2.0));
  65. #endif //USE_17_SAMPLES
  66. #ifdef USE_11_SAMPLES
  67. const int kernel_size = 11;
  68. QUALIFIER vec2 kernel[11] = vec2[](
  69. vec2(0.560479, 0.0),
  70. vec2(0.00471691, -2.0),
  71. vec2(0.0192831, -1.28),
  72. vec2(0.03639, -0.72),
  73. vec2(0.0821904, -0.32),
  74. vec2(0.0771802, -0.08),
  75. vec2(0.0771802, 0.08),
  76. vec2(0.0821904, 0.32),
  77. vec2(0.03639, 0.72),
  78. vec2(0.0192831, 1.28),
  79. vec2(0.00471691, 2.0));
  80. #endif //USE_11_SAMPLES
  81. uniform float max_radius;
  82. uniform float camera_z_far;
  83. uniform float camera_z_near;
  84. uniform float unit_size;
  85. uniform vec2 dir;
  86. in vec2 uv_interp;
  87. uniform sampler2D source_diffuse; //texunit:0
  88. uniform sampler2D source_sss; //texunit:1
  89. uniform sampler2D source_depth; //texunit:2
  90. layout(location = 0) out vec4 frag_color;
  91. void main() {
  92. float strength = texture(source_sss, uv_interp).r;
  93. strength *= strength; //stored as sqrt
  94. // Fetch color of current pixel:
  95. vec4 base_color = texture(source_diffuse, uv_interp);
  96. if (strength > 0.0) {
  97. // Fetch linear depth of current pixel:
  98. float depth = texture(source_depth, uv_interp).r * 2.0 - 1.0;
  99. #ifdef USE_ORTHOGONAL_PROJECTION
  100. depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
  101. float scale = unit_size; //remember depth is negative by default in OpenGL
  102. #else
  103. depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near));
  104. float scale = unit_size / depth; //remember depth is negative by default in OpenGL
  105. #endif
  106. // Calculate the final step to fetch the surrounding pixels:
  107. vec2 step = max_radius * scale * dir;
  108. step *= strength; // Modulate it using the alpha channel.
  109. step *= 1.0 / 3.0; // Divide by 3 as the kernels range from -3 to 3.
  110. // Accumulate the center sample:
  111. vec3 color_accum = base_color.rgb;
  112. color_accum *= kernel[0].x;
  113. #ifdef ENABLE_STRENGTH_WEIGHTING
  114. float color_weight = kernel[0].x;
  115. #endif
  116. // Accumulate the other samples:
  117. for (int i = 1; i < kernel_size; i++) {
  118. // Fetch color and depth for current sample:
  119. vec2 offset = uv_interp + kernel[i].y * step;
  120. vec3 color = texture(source_diffuse, offset).rgb;
  121. #ifdef ENABLE_FOLLOW_SURFACE
  122. // If the difference in depth is huge, we lerp color back to "colorM":
  123. float depth_cmp = texture(source_depth, offset).r * 2.0 - 1.0;
  124. #ifdef USE_ORTHOGONAL_PROJECTION
  125. depth_cmp = ((depth_cmp + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
  126. #else
  127. depth_cmp = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth_cmp * (camera_z_far - camera_z_near));
  128. #endif
  129. float s = clamp(300.0f * scale * max_radius * abs(depth - depth_cmp), 0.0, 1.0);
  130. color = mix(color, base_color.rgb, s);
  131. #endif
  132. // Accumulate:
  133. color *= kernel[i].x;
  134. #ifdef ENABLE_STRENGTH_WEIGHTING
  135. float color_s = texture(source_sss, offset).r;
  136. color_weight += color_s * kernel[i].x;
  137. color *= color_s;
  138. #endif
  139. color_accum += color;
  140. }
  141. #ifdef ENABLE_STRENGTH_WEIGHTING
  142. color_accum /= color_weight;
  143. #endif
  144. frag_color = vec4(color_accum, base_color.a); //keep alpha (used for SSAO)
  145. } else {
  146. frag_color = base_color;
  147. }
  148. }