2021-09-04 18:59:18 +00:00
|
|
|
#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
|
|
|
|
|
2022-01-21 00:42:20 +00:00
|
|
|
// 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)
|
2020-11-29 21:38:03 +00:00
|
|
|
uniform texture2D t_src_color;
|
2022-03-15 18:04:21 +00:00
|
|
|
layout(set = 2, binding = 1)
|
2020-11-29 21:38:03 +00:00
|
|
|
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)
|
2020-11-29 21:38:03 +00:00
|
|
|
uniform texture2D t_src_depth;
|
2022-03-15 18:04:21 +00:00
|
|
|
layout(set = 2, binding = 3)
|
2020-11-29 21:38:03 +00:00
|
|
|
uniform sampler s_src_depth;
|
2020-11-15 22:18:35 +00:00
|
|
|
|
2020-12-06 21:42:01 +00:00
|
|
|
layout(location = 0) in vec2 uv;
|
2020-11-29 21:38:03 +00:00
|
|
|
|
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 proj_mat_inv;
|
|
|
|
mat4 view_mat_inv;
|
|
|
|
};
|
|
|
|
|
2020-11-29 21:38:03 +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;
|
2020-11-15 22:18:35 +00:00
|
|
|
mat4 inv = view_mat_inv * proj_mat_inv;//inverse(all_mat);
|
2020-12-06 16:22:35 +00:00
|
|
|
vec4 clip_space = vec4((uv * 2.0 - 1.0) * vec2(1, -1), buf_depth, 1.0);
|
2020-11-15 22:18:35 +00:00
|
|
|
vec4 view_space = inv * clip_space;
|
|
|
|
view_space /= view_space.w;
|
2020-12-17 05:13:49 +00:00
|
|
|
if (buf_depth == 0.0) {
|
2020-11-15 22:18:35 +00:00
|
|
|
vec3 direction = normalize(view_space.xyz);
|
2020-12-17 05:13:49 +00:00
|
|
|
return direction.xyz * 524288.0625 + cam_pos.xyz;
|
2020-11-15 22:18:35 +00:00
|
|
|
} else {
|
|
|
|
return view_space.xyz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 18:56:45 +00:00
|
|
|
mat4 spin_in_axis(vec3 axis, float angle)
|
|
|
|
{
|
|
|
|
axis = normalize(axis);
|
|
|
|
float s = sin(angle);
|
|
|
|
float c = cos(angle);
|
|
|
|
float oc = 1.0 - c;
|
|
|
|
|
|
|
|
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0,
|
|
|
|
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0,
|
|
|
|
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
}
|
|
|
|
|
2020-11-15 22:18:35 +00:00
|
|
|
void main() {
|
2020-11-29 21:38:03 +00:00
|
|
|
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;
|
|
|
|
|
2020-11-29 21:38:03 +00:00
|
|
|
// 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 13:35:40 +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);
|
2020-11-15 22:18:35 +00:00
|
|
|
#endif
|
|
|
|
|
2022-04-19 19:43:40 +00:00
|
|
|
vec3 old_color = color.rgb;
|
|
|
|
|
2022-05-30 21:02:22 +00:00
|
|
|
dir = (vec4(dir, 0) * rel_rain_dir_mat).xyz;
|
2022-04-19 19:43:40 +00:00
|
|
|
|
|
|
|
float z = (-1 / (abs(dir.z) - 1) - 1) * sign(dir.z);
|
|
|
|
vec2 dir_2d = normalize(dir.xy);
|
|
|
|
vec2 view_pos = vec2(atan2(dir_2d.x, dir_2d.y), z);
|
|
|
|
|
|
|
|
vec3 cam_wpos = cam_pos.xyz + focus_off.xyz;
|
2022-06-08 13:58:56 +00:00
|
|
|
// Rain density is now only based on the cameras current position.
|
|
|
|
// This could be affected by a setting where rain_density_at is instead
|
|
|
|
// called each iteration of the loop. With the current implementation
|
|
|
|
// of rain_dir this has issues with being in a place where it doesn't rain
|
|
|
|
// and seeing rain.
|
2022-06-11 20:57:07 +00:00
|
|
|
if (medium.x == MEDIUM_AIR && rain_density > 0.0) {
|
2022-06-04 01:25:17 +00:00
|
|
|
float rain_dist = 50.0;
|
|
|
|
for (int i = 0; i < 4; i ++) {
|
|
|
|
float old_rain_dist = rain_dist;
|
|
|
|
rain_dist *= 0.3;
|
|
|
|
|
|
|
|
vec2 drop_density = vec2(30, 1);
|
|
|
|
|
|
|
|
vec2 rain_pos = (view_pos * rain_dist);
|
|
|
|
rain_pos.y += integrated_rain_vel;
|
|
|
|
|
|
|
|
vec2 cell = floor(rain_pos * drop_density) / drop_density;
|
|
|
|
|
|
|
|
float drop_depth = mix(
|
|
|
|
old_rain_dist,
|
|
|
|
rain_dist,
|
|
|
|
fract(hash(fract(vec4(cell, rain_dist, 0) * 0.1)))
|
|
|
|
);
|
|
|
|
vec3 rpos = vec3(vec2(dir_2d), view_pos.y) * drop_depth;
|
|
|
|
float dist_to_rain = length(rpos);
|
|
|
|
if (dist < dist_to_rain || cam_wpos.z + rpos.z > CLOUD_AVG_ALT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dot(rpos * vec3(1, 1, 0.5), rpos) < 1.0) {
|
|
|
|
break;
|
|
|
|
}
|
2022-06-11 20:57:07 +00:00
|
|
|
float rain_density = rain_density * rain_occlusion_at(cam_pos.xyz + rpos.xyz) * 10.0;
|
2022-06-04 01:25:17 +00:00
|
|
|
|
|
|
|
if (rain_density < 0.001 || fract(hash(fract(vec4(cell, rain_dist, 0) * 0.01))) > rain_density) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
vec2 near_drop = cell + (vec2(0.5) + (vec2(hash(vec4(cell, 0, 0)), 0.5) - 0.5) * vec2(2, 0)) / drop_density;
|
|
|
|
|
|
|
|
vec2 drop_size = vec2(0.0008, 0.03);
|
|
|
|
float avg_alpha = (drop_size.x * drop_size.y) * 10 / 1;
|
|
|
|
float alpha = sign(max(1 - length((rain_pos - near_drop) / drop_size * 0.1), 0));
|
|
|
|
float light = sqrt(dot(old_color, vec3(1))) + (get_sun_brightness() + get_moon_brightness()) * 0.01;
|
|
|
|
color.rgb = mix(color.rgb, vec3(0.3, 0.4, 0.5) * light, mix(avg_alpha, alpha, min(1000 / dist_to_rain, 1)) * 0.25);
|
2022-02-06 18:56:45 +00:00
|
|
|
}
|
2022-04-19 19:43:40 +00:00
|
|
|
}
|
2022-02-06 18:56:45 +00:00
|
|
|
|
2020-11-15 22:18:35 +00:00
|
|
|
tgt_color = vec4(color.rgb, 1);
|
|
|
|
}
|