sky.glsl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* clang-format off */
  2. #[modes]
  3. mode_background =
  4. mode_half_res = #define USE_HALF_RES_PASS
  5. mode_quarter_res = #define USE_QUARTER_RES_PASS
  6. mode_cubemap = #define USE_CUBEMAP_PASS
  7. mode_cubemap_half_res = #define USE_CUBEMAP_PASS \n#define USE_HALF_RES_PASS
  8. mode_cubemap_quarter_res = #define USE_CUBEMAP_PASS \n#define USE_QUARTER_RES_PASS
  9. #[specializations]
  10. USE_MULTIVIEW = false
  11. USE_INVERTED_Y = true
  12. #[vertex]
  13. layout(location = 0) in vec2 vertex_attrib;
  14. out vec2 uv_interp;
  15. /* clang-format on */
  16. void main() {
  17. #ifdef USE_INVERTED_Y
  18. uv_interp = vertex_attrib;
  19. #else
  20. // We're doing clockwise culling so flip the order
  21. uv_interp = vec2(vertex_attrib.x, vertex_attrib.y * -1.0);
  22. #endif
  23. gl_Position = vec4(uv_interp, 1.0, 1.0);
  24. }
  25. /* clang-format off */
  26. #[fragment]
  27. #define M_PI 3.14159265359
  28. #include "tonemap_inc.glsl"
  29. in vec2 uv_interp;
  30. /* clang-format on */
  31. uniform samplerCube radiance; //texunit:-1
  32. #ifdef USE_CUBEMAP_PASS
  33. uniform samplerCube half_res; //texunit:-2
  34. uniform samplerCube quarter_res; //texunit:-3
  35. #elif defined(USE_MULTIVIEW)
  36. uniform sampler2DArray half_res; //texunit:-2
  37. uniform sampler2DArray quarter_res; //texunit:-3
  38. #else
  39. uniform sampler2D half_res; //texunit:-2
  40. uniform sampler2D quarter_res; //texunit:-3
  41. #endif
  42. layout(std140) uniform GlobalShaderUniformData { //ubo:1
  43. vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
  44. };
  45. struct DirectionalLightData {
  46. vec4 direction_energy;
  47. vec4 color_size;
  48. bool enabled;
  49. };
  50. layout(std140) uniform DirectionalLights { //ubo:4
  51. DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  52. }
  53. directional_lights;
  54. /* clang-format off */
  55. #ifdef MATERIAL_UNIFORMS_USED
  56. layout(std140) uniform MaterialUniforms{ //ubo:3
  57. #MATERIAL_UNIFORMS
  58. };
  59. #endif
  60. /* clang-format on */
  61. #GLOBALS
  62. #ifdef USE_CUBEMAP_PASS
  63. #define AT_CUBEMAP_PASS true
  64. #else
  65. #define AT_CUBEMAP_PASS false
  66. #endif
  67. #ifdef USE_HALF_RES_PASS
  68. #define AT_HALF_RES_PASS true
  69. #else
  70. #define AT_HALF_RES_PASS false
  71. #endif
  72. #ifdef USE_QUARTER_RES_PASS
  73. #define AT_QUARTER_RES_PASS true
  74. #else
  75. #define AT_QUARTER_RES_PASS false
  76. #endif
  77. // mat4 is a waste of space, but we don't have an easy way to set a mat3 uniform for now
  78. uniform mat4 orientation;
  79. uniform vec4 projection;
  80. uniform vec3 position;
  81. uniform float time;
  82. uniform float luminance_multiplier;
  83. uniform float fog_aerial_perspective;
  84. uniform vec3 fog_light_color;
  85. uniform float fog_sun_scatter;
  86. uniform bool fog_enabled;
  87. uniform float fog_density;
  88. uniform float z_far;
  89. uniform uint directional_light_count;
  90. #ifdef USE_MULTIVIEW
  91. layout(std140) uniform MultiviewData { // ubo:5
  92. highp mat4 projection_matrix_view[MAX_VIEWS];
  93. highp mat4 inv_projection_matrix_view[MAX_VIEWS];
  94. highp vec4 eye_offset[MAX_VIEWS];
  95. }
  96. multiview_data;
  97. #endif
  98. layout(location = 0) out vec4 frag_color;
  99. #ifdef USE_DEBANDING
  100. // https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
  101. vec3 interleaved_gradient_noise(vec2 pos) {
  102. const vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f);
  103. float res = fract(magic.z * fract(dot(pos, magic.xy))) * 2.0 - 1.0;
  104. return vec3(res, -res, res) / 255.0;
  105. }
  106. #endif
  107. void main() {
  108. vec3 cube_normal;
  109. #ifdef USE_MULTIVIEW
  110. // In multiview our projection matrices will contain positional and rotational offsets that we need to properly unproject.
  111. vec4 unproject = vec4(uv_interp.x, uv_interp.y, 1.0, 1.0);
  112. vec4 unprojected = multiview_data.inv_projection_matrix_view[ViewIndex] * unproject;
  113. cube_normal = unprojected.xyz / unprojected.w;
  114. cube_normal += multiview_data.eye_offset[ViewIndex].xyz;
  115. #else
  116. cube_normal.z = -1.0;
  117. cube_normal.x = (uv_interp.x + projection.x) / projection.y;
  118. cube_normal.y = (-uv_interp.y - projection.z) / projection.w;
  119. #endif
  120. cube_normal = mat3(orientation) * cube_normal;
  121. cube_normal = normalize(cube_normal);
  122. vec2 uv = gl_FragCoord.xy; // uv_interp * 0.5 + 0.5;
  123. vec2 panorama_coords = vec2(atan(cube_normal.x, -cube_normal.z), acos(cube_normal.y));
  124. if (panorama_coords.x < 0.0) {
  125. panorama_coords.x += M_PI * 2.0;
  126. }
  127. panorama_coords /= vec2(M_PI * 2.0, M_PI);
  128. vec3 color = vec3(0.0, 0.0, 0.0);
  129. float alpha = 1.0; // Only available to subpasses
  130. vec4 half_res_color = vec4(1.0);
  131. vec4 quarter_res_color = vec4(1.0);
  132. vec4 custom_fog = vec4(0.0);
  133. #ifdef USE_CUBEMAP_PASS
  134. #ifdef USES_HALF_RES_COLOR
  135. half_res_color = texture(samplerCube(half_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_normal);
  136. #endif
  137. #ifdef USES_QUARTER_RES_COLOR
  138. quarter_res_color = texture(samplerCube(quarter_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_normal);
  139. #endif
  140. #else
  141. #ifdef USES_HALF_RES_COLOR
  142. #ifdef USE_MULTIVIEW
  143. half_res_color = textureLod(sampler2DArray(half_res, material_samplers[SAMPLER_LINEAR_CLAMP]), vec3(uv, ViewIndex), 0.0);
  144. #else
  145. half_res_color = textureLod(sampler2D(half_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0);
  146. #endif
  147. #endif
  148. #ifdef USES_QUARTER_RES_COLOR
  149. #ifdef USE_MULTIVIEW
  150. quarter_res_color = textureLod(sampler2DArray(quarter_res, material_samplers[SAMPLER_LINEAR_CLAMP]), vec3(uv, ViewIndex), 0.0);
  151. #else
  152. quarter_res_color = textureLod(sampler2D(quarter_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0);
  153. #endif
  154. #endif
  155. #endif
  156. {
  157. #CODE : SKY
  158. }
  159. color *= luminance_multiplier;
  160. // Convert to Linear for tonemapping so color matches scene shader better
  161. color = srgb_to_linear(color);
  162. color *= exposure;
  163. color = apply_tonemapping(color, white);
  164. color = linear_to_srgb(color);
  165. #ifdef USE_BCS
  166. color = apply_bcs(color, bcs);
  167. #endif
  168. #ifdef USE_COLOR_CORRECTION
  169. color = apply_color_correction(color, color_correction);
  170. #endif
  171. frag_color.rgb = color;
  172. frag_color.a = alpha;
  173. #ifdef USE_DEBANDING
  174. frag_color.rgb += interleaved_gradient_noise(gl_FragCoord.xy);
  175. #endif
  176. }