sky.glsl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #[vertex]
  2. #version 450
  3. VERSION_DEFINES
  4. layout(location = 0) out vec2 uv_interp;
  5. layout(push_constant, binding = 1, std430) uniform Params {
  6. mat3 orientation;
  7. vec4 proj;
  8. vec4 position_multiplier;
  9. float time;
  10. }
  11. params;
  12. void main() {
  13. vec2 base_arr[4] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 1.0), vec2(1.0, 1.0), vec2(1.0, -1.0));
  14. uv_interp = base_arr[gl_VertexIndex];
  15. gl_Position = vec4(uv_interp, 1.0, 1.0);
  16. }
  17. #[fragment]
  18. #version 450
  19. VERSION_DEFINES
  20. #define M_PI 3.14159265359
  21. layout(location = 0) in vec2 uv_interp;
  22. layout(push_constant, binding = 1, std430) uniform Params {
  23. mat3 orientation;
  24. vec4 proj;
  25. vec4 position_multiplier;
  26. float time; //TODO consider adding vec2 screen res, and float radiance size
  27. }
  28. params;
  29. #define SAMPLER_NEAREST_CLAMP 0
  30. #define SAMPLER_LINEAR_CLAMP 1
  31. #define SAMPLER_NEAREST_WITH_MIPMAPS_CLAMP 2
  32. #define SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP 3
  33. #define SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_CLAMP 4
  34. #define SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_CLAMP 5
  35. #define SAMPLER_NEAREST_REPEAT 6
  36. #define SAMPLER_LINEAR_REPEAT 7
  37. #define SAMPLER_NEAREST_WITH_MIPMAPS_REPEAT 8
  38. #define SAMPLER_LINEAR_WITH_MIPMAPS_REPEAT 9
  39. #define SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_REPEAT 10
  40. #define SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_REPEAT 11
  41. layout(set = 0, binding = 0) uniform sampler material_samplers[12];
  42. layout(set = 0, binding = 1, std430) restrict readonly buffer GlobalVariableData {
  43. vec4 data[];
  44. }
  45. global_variables;
  46. layout(set = 0, binding = 2, std140) uniform SceneData {
  47. bool volumetric_fog_enabled;
  48. float volumetric_fog_inv_length;
  49. float volumetric_fog_detail_spread;
  50. float fog_aerial_perspective;
  51. vec3 fog_light_color;
  52. float fog_sun_scatter;
  53. bool fog_enabled;
  54. float fog_density;
  55. float z_far;
  56. uint directional_light_count;
  57. }
  58. scene_data;
  59. struct DirectionalLightData {
  60. vec4 direction_energy;
  61. vec4 color_size;
  62. bool enabled;
  63. };
  64. layout(set = 0, binding = 3, std140) uniform DirectionalLights {
  65. DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  66. }
  67. directional_lights;
  68. #ifdef USE_MATERIAL_UNIFORMS
  69. layout(set = 1, binding = 0, std140) uniform MaterialUniforms{
  70. /* clang-format off */
  71. MATERIAL_UNIFORMS
  72. /* clang-format on */
  73. } material;
  74. #endif
  75. layout(set = 2, binding = 0) uniform textureCube radiance;
  76. #ifdef USE_CUBEMAP_PASS
  77. layout(set = 2, binding = 1) uniform textureCube half_res;
  78. layout(set = 2, binding = 2) uniform textureCube quarter_res;
  79. #else
  80. layout(set = 2, binding = 1) uniform texture2D half_res;
  81. layout(set = 2, binding = 2) uniform texture2D quarter_res;
  82. #endif
  83. layout(set = 3, binding = 0) uniform texture3D volumetric_fog_texture;
  84. #ifdef USE_CUBEMAP_PASS
  85. #define AT_CUBEMAP_PASS true
  86. #else
  87. #define AT_CUBEMAP_PASS false
  88. #endif
  89. #ifdef USE_HALF_RES_PASS
  90. #define AT_HALF_RES_PASS true
  91. #else
  92. #define AT_HALF_RES_PASS false
  93. #endif
  94. #ifdef USE_QUARTER_RES_PASS
  95. #define AT_QUARTER_RES_PASS true
  96. #else
  97. #define AT_QUARTER_RES_PASS false
  98. #endif
  99. /* clang-format off */
  100. FRAGMENT_SHADER_GLOBALS
  101. /* clang-format on */
  102. layout(location = 0) out vec4 frag_color;
  103. vec4 volumetric_fog_process(vec2 screen_uv) {
  104. vec3 fog_pos = vec3(screen_uv, 1.0);
  105. return texture(sampler3D(volumetric_fog_texture, material_samplers[SAMPLER_LINEAR_CLAMP]), fog_pos);
  106. }
  107. vec4 fog_process(vec3 view, vec3 sky_color) {
  108. vec3 fog_color = mix(scene_data.fog_light_color, sky_color, scene_data.fog_aerial_perspective);
  109. if (scene_data.fog_sun_scatter > 0.001) {
  110. vec4 sun_scatter = vec4(0.0);
  111. float sun_total = 0.0;
  112. for (uint i = 0; i < scene_data.directional_light_count; i++) {
  113. vec3 light_color = directional_lights.data[i].color_size.xyz * directional_lights.data[i].direction_energy.w;
  114. float light_amount = pow(max(dot(view, directional_lights.data[i].direction_energy.xyz), 0.0), 8.0);
  115. fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
  116. }
  117. }
  118. float fog_amount = clamp(1.0 - exp(-scene_data.z_far * scene_data.fog_density), 0.0, 1.0);
  119. return vec4(fog_color, fog_amount);
  120. }
  121. void main() {
  122. vec3 cube_normal;
  123. cube_normal.z = -1.0;
  124. cube_normal.x = (cube_normal.z * (-uv_interp.x - params.proj.x)) / params.proj.y;
  125. cube_normal.y = -(cube_normal.z * (-uv_interp.y - params.proj.z)) / params.proj.w;
  126. cube_normal = mat3(params.orientation) * cube_normal;
  127. cube_normal.z = -cube_normal.z;
  128. cube_normal = normalize(cube_normal);
  129. vec2 uv = uv_interp * 0.5 + 0.5;
  130. vec2 panorama_coords = vec2(atan(cube_normal.x, cube_normal.z), acos(cube_normal.y));
  131. if (panorama_coords.x < 0.0) {
  132. panorama_coords.x += M_PI * 2.0;
  133. }
  134. panorama_coords /= vec2(M_PI * 2.0, M_PI);
  135. vec3 color = vec3(0.0, 0.0, 0.0);
  136. float alpha = 1.0; // Only available to subpasses
  137. vec4 half_res_color = vec4(1.0);
  138. vec4 quarter_res_color = vec4(1.0);
  139. vec4 custom_fog = vec4(0.0);
  140. #ifdef USE_CUBEMAP_PASS
  141. vec3 inverted_cube_normal = cube_normal;
  142. inverted_cube_normal.z *= -1.0;
  143. #ifdef USES_HALF_RES_COLOR
  144. half_res_color = texture(samplerCube(half_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal);
  145. #endif
  146. #ifdef USES_QUARTER_RES_COLOR
  147. quarter_res_color = texture(samplerCube(quarter_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal);
  148. #endif
  149. #else
  150. #ifdef USES_HALF_RES_COLOR
  151. half_res_color = textureLod(sampler2D(half_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0);
  152. #endif
  153. #ifdef USES_QUARTER_RES_COLOR
  154. quarter_res_color = textureLod(sampler2D(quarter_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0);
  155. #endif
  156. #endif
  157. // unused, just here to make our compiler happy, make sure we don't execute any light code the user adds in..
  158. #ifndef REALLYINCLUDETHIS
  159. {
  160. /* clang-format off */
  161. LIGHT_SHADER_CODE
  162. /* clang-format on */
  163. }
  164. #endif
  165. {
  166. /* clang-format off */
  167. FRAGMENT_SHADER_CODE
  168. /* clang-format on */
  169. }
  170. frag_color.rgb = color * params.position_multiplier.w;
  171. frag_color.a = alpha;
  172. #if !defined(DISABLE_FOG) && !defined(USE_CUBEMAP_PASS)
  173. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  174. if (scene_data.fog_enabled) {
  175. vec4 fog = fog_process(cube_normal, frag_color.rgb);
  176. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  177. }
  178. if (scene_data.volumetric_fog_enabled) {
  179. vec4 fog = volumetric_fog_process(uv);
  180. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  181. }
  182. if (custom_fog.a > 0.0) {
  183. frag_color.rgb = mix(frag_color.rgb, custom_fog.rgb, custom_fog.a);
  184. }
  185. #endif // DISABLE_FOG
  186. // Blending is disabled for Sky, so alpha doesn't blend
  187. // alpha is used for subsurface scattering so make sure it doesn't get applied to Sky
  188. if (!AT_CUBEMAP_PASS && !AT_HALF_RES_PASS && !AT_QUARTER_RES_PASS) {
  189. frag_color.a = 0.0;
  190. }
  191. }