veloren/assets/voxygen/shaders/clouds-frag.glsl

130 lines
4.4 KiB
Plaintext
Raw Normal View History

#version 420 core
2020-11-15 22:18:35 +00:00
#include <constants.glsl>
#define LIGHTING_TYPE (LIGHTING_TYPE_TRANSMISSION | LIGHTING_TYPE_REFLECTION)
#define LIGHTING_REFLECTION_KIND LIGHTING_REFLECTION_KIND_SPECULAR
#if (FLUID_MODE == FLUID_MODE_CHEAP)
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_IMPORTANCE
#elif (FLUID_MODE == FLUID_MODE_SHINY)
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_RADIANCE
#endif
#define LIGHTING_DISTRIBUTION_SCHEME LIGHTING_DISTRIBUTION_SCHEME_MICROFACET
#define LIGHTING_DISTRIBUTION LIGHTING_DISTRIBUTION_BECKMANN
// Must come before includes
#define IS_POSTPROCESS
2020-11-15 22:18:35 +00:00
#include <globals.glsl>
// Note: The sampler uniform is declared here because it differs for MSAA
#include <anti-aliasing.glsl>
#include <srgb.glsl>
#include <cloud.glsl>
2022-01-18 21:09:30 +00:00
#include <light.glsl>
2022-01-19 11:29:19 +00:00
// This *MUST* come after `cloud.glsl`: it contains a function that depends on `cloud.glsl` when clouds are enabled
#include <point_glow.glsl>
2020-11-15 22:18:35 +00:00
2022-03-15 18:04:21 +00:00
layout(set = 2, binding = 0)
uniform texture2D t_src_color;
2022-03-15 18:04:21 +00:00
layout(set = 2, binding = 1)
uniform sampler s_src_color;
2020-11-15 22:18:35 +00:00
2022-03-15 18:04:21 +00:00
layout(set = 2, binding = 2)
uniform texture2D t_src_depth;
2022-03-15 18:04:21 +00:00
layout(set = 2, binding = 3)
uniform sampler s_src_depth;
2020-11-15 22:18:35 +00:00
layout(location = 0) in vec2 uv;
2022-03-15 18:04:21 +00:00
layout (std140, set = 2, binding = 4)
2020-11-15 22:18:35 +00:00
uniform u_locals {
mat4 all_mat_inv;
2020-11-15 22:18:35 +00:00
};
layout(location = 0) out vec4 tgt_color;
2020-11-15 22:18:35 +00:00
vec3 wpos_at(vec2 uv) {
2020-12-06 17:43:31 +00:00
float buf_depth = texture(sampler2D(t_src_depth, s_src_depth), uv).x;
vec4 clip_space = vec4((uv * 2.0 - 1.0) * vec2(1, -1), buf_depth, 1.0);
vec4 view_space = all_mat_inv * clip_space;
2020-11-15 22:18:35 +00:00
view_space /= view_space.w;
if (buf_depth == 0.0) {
2020-11-15 22:18:35 +00:00
vec3 direction = normalize(view_space.xyz);
return direction.xyz * 524288.0625 + cam_pos.xyz;
2020-11-15 22:18:35 +00:00
} else {
return view_space.xyz;
}
}
void main() {
vec4 color = texture(sampler2D(t_src_color, s_src_color), uv);
2020-11-15 22:18:35 +00:00
2022-02-09 12:56:21 +00:00
#ifdef EXPERIMENTAL_BAREMINIMUM
tgt_color = vec4(color.rgb, 1);
return;
#endif
2022-01-18 21:09:30 +00:00
vec3 wpos = wpos_at(uv);
float dist = distance(wpos, cam_pos.xyz);
vec3 dir = (wpos - cam_pos.xyz) / dist;
// Apply clouds
2022-01-21 17:29:59 +00:00
float cloud_blend = 1.0;
if (color.a < 1.0) {
cloud_blend = 1.0 - color.a;
2022-01-23 16:25:15 +00:00
dist = DIST_CAP;
2022-01-21 17:29:59 +00:00
}
color.rgb = mix(color.rgb, get_cloud_color(color.rgb, dir, cam_pos.xyz, time_of_day.x, dist, 1.0), cloud_blend);
2022-01-21 17:29:59 +00:00
#if (CLOUD_MODE == CLOUD_MODE_NONE)
2022-01-21 00:50:14 +00:00
color.rgb = apply_point_glow(cam_pos.xyz + focus_off.xyz, dir, dist, color.rgb);
2022-07-15 07:06:11 +00:00
#else
2022-07-08 19:49:51 +00:00
if (medium.x == MEDIUM_AIR && rain_density > 0.001) {
2022-07-08 13:47:43 +00:00
vec3 cam_wpos = cam_pos.xyz + focus_off.xyz;
vec3 adjusted_dir = (vec4(dir, 0) * rain_dir_mat).xyz;
vec2 dir2d = adjusted_dir.xy;
2022-07-08 17:09:13 +00:00
vec3 rorigin = cam_pos.xyz + focus_off.xyz + 0.5;
2022-07-08 14:09:38 +00:00
vec3 rpos = vec3(0.0);
2022-07-08 13:47:43 +00:00
float t = 0.0;
2022-07-10 12:53:31 +00:00
const float PLANCK = 0.01;
2022-07-15 07:06:11 +00:00
for (int i = 0; i < 14 /* log2(64) * 2 + 2 */; i ++) {
2022-07-08 19:40:08 +00:00
float scale = min(pow(2, ceil(t / 2.0)), 32);
2022-07-08 19:18:07 +00:00
vec2 deltas = (step(vec2(0), dir2d) - fract(rpos.xy / scale + 100.0)) / dir2d;
2022-07-08 14:09:38 +00:00
float jump = max(min(deltas.x, deltas.y) * scale, PLANCK);
t += jump;
2022-07-10 15:40:49 +00:00
if (t >= 64.0) { break; }
2022-07-08 15:07:15 +00:00
rpos = rorigin + adjusted_dir * t;
2022-07-08 13:47:43 +00:00
vec2 diff = abs(round(rpos.xy) - rpos.xy);
2022-07-08 15:09:36 +00:00
vec3 wall_pos = vec3((diff.x > diff.y) ? rpos.xy : rpos.yx, rpos.z + integrated_rain_vel);
2022-07-08 13:47:43 +00:00
wall_pos.xz *= vec2(4, 0.3);
2022-07-10 12:53:31 +00:00
wall_pos.z += hash_two(uvec2(wall_pos.xy + vec2(0, 0.5)));
2022-07-08 13:47:43 +00:00
2022-07-10 15:40:49 +00:00
float depth_adjust = fract(hash_two(uvec2(wall_pos.xz) + 500u));
2022-07-08 15:41:31 +00:00
float wpos_dist = t - jump * depth_adjust;
vec3 wpos = cam_pos.xyz + dir * wpos_dist;
2022-07-08 14:09:38 +00:00
2022-07-08 15:41:31 +00:00
if (wpos_dist > dist) { break; }
2022-07-08 19:18:07 +00:00
if (length((fract(wall_pos.xz) - 0.5)) < 0.1 + pow(max(0.0, wpos_dist - (dist - 0.25)) / 0.25, 4.0) * 0.2) {
2022-07-10 12:53:31 +00:00
float density = rain_density * rain_occlusion_at(wpos);
2022-07-10 15:40:49 +00:00
if (fract(hash_two(uvec2(wall_pos.xz) + 1000u)) >= density) { continue; }
2022-07-10 12:53:31 +00:00
2022-07-08 19:18:07 +00:00
float alpha = 0.5 * clamp((wpos_dist - 1.0) * 0.5, 0.0, 1.0);
2022-07-10 15:40:49 +00:00
float light = dot(color.rgb, vec3(1)) + 0.05 + (get_sun_brightness() + get_moon_brightness()) * 0.2;
color.rgb = mix(color.rgb, vec3(0.3, 0.35, 0.5) * light, alpha);
2022-07-08 13:47:43 +00:00
}
}
}
#endif
2022-02-06 18:56:45 +00:00
2020-11-15 22:18:35 +00:00
tgt_color = vec4(color.rgb, 1);
}