time proof time of day in shaders

This commit is contained in:
Isse 2023-09-28 12:08:51 +02:00
parent eaeeb0165f
commit 6d4be9bd01
13 changed files with 44 additions and 45 deletions

View File

@ -236,19 +236,19 @@ void main() {
uvec4 new_mat = texelFetch(usampler2D(t_src_mat, s_src_depth), clamp(ivec2(new_uv * mat_sz), ivec2(0), ivec2(mat_sz) - 1), 0);
// If it's the sky, just go determine the sky color analytically to avoid sampling the incomplete skybox
// Otherwise, pull the color from the screen-space color buffer
vec3 sky_col = min(get_sky_color(refl_dir, time_of_day.x, wpos, vec3(-100000), 0.125, false, 0.0, true, 0.0), vec3(1)) * not_underground;
vec3 sky_col = min(get_sky_color(refl_dir, wpos, vec3(-100000), 0.125, false, 0.0, true, 0.0), vec3(1)) * not_underground;
if (new_mat.a == MAT_SKY) {
refl_col = sky_col;
} else {
refl_col = mix(sky_col, texelFetch(sampler2D(t_src_color, s_src_color), clamp(ivec2(new_uv * col_sz), ivec2(0), ivec2(col_sz) - 1), 0).rgb, merge);
}
// Apply clouds to reflected colour
refl_col = mix(refl_col, get_cloud_color(refl_col, refl_dir, wpos, time_of_day.x, distance(new_wpos, wpos.xyz), 1.0), not_underground);
refl_col = mix(refl_col, get_cloud_color(refl_col, refl_dir, wpos, distance(new_wpos, wpos.xyz), 1.0), not_underground);
} else {
// No: assume that anything off-screen is the colour of the sky
refl_col = min(get_sky_color(refl_dir, time_of_day.x, wpos, vec3(-100000), 0.125, true, 1.0, true, 1.0) * not_underground, vec3(1));
refl_col = min(get_sky_color(refl_dir, wpos, vec3(-100000), 0.125, true, 1.0, true, 1.0) * not_underground, vec3(1));
// Apply clouds to reflection
refl_col = mix(refl_col, get_cloud_color(refl_col, refl_dir, wpos, time_of_day.x, 100000.0, 1.0), not_underground);
refl_col = mix(refl_col, get_cloud_color(refl_col, refl_dir, wpos, 100000.0, 1.0), not_underground);
}
color.rgb = mix(color.rgb, refl_col, color.a);
cloud_blend = 1;
@ -260,7 +260,7 @@ void main() {
}
}
}
color.rgb = mix(color.rgb, get_cloud_color(color.rgb, dir, cam_pos.xyz, time_of_day.x, dist, 1.0), cloud_blend);
color.rgb = mix(color.rgb, get_cloud_color(color.rgb, dir, cam_pos.xyz, dist, 1.0), cloud_blend);
#if (CLOUD_MODE == CLOUD_MODE_NONE)
color.rgb = apply_point_glow(cam_pos.xyz + focus_off.xyz, dir, dist, color.rgb);

View File

@ -288,8 +288,8 @@ void main() {
/*
if ((material & (1u << 1u)) > 0u && false) {
vec3 reflect_ray_dir = reflect(cam_to_frag, f_norm);
reflect_color = get_sky_color(reflect_ray_dir, time_of_day.x, f_pos, vec3(-100000), 0.125, true);
reflect_color = get_cloud_color(reflect_color, reflect_ray_dir, cam_pos.xyz, time_of_day.x, 100000.0, 0.25);
reflect_color = get_sky_color(reflect_ray_dir, f_pos, vec3(-100000), 0.125, true);
reflect_color = get_cloud_color(reflect_color, reflect_ray_dir, cam_pos.xyz, 100000.0, 0.25);
reflectance = 1.0;
}
*/

View File

@ -163,7 +163,7 @@ void main() {
vec3 reflect_color = vec3(0.0);
#if (REFLECTION_MODE >= REFLECTION_MODE_MEDIUM)
reflect_color = get_sky_color(reflect_ray_dir, time_of_day.x, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
reflect_color = get_sky_color(reflect_ray_dir, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
#endif
vec3 emitted_light, reflected_light;
@ -183,7 +183,7 @@ void main() {
// vec3 surf_color = /*srgb_to_linear*/(vec3(0.4, 0.7, 2.0));
float max_light = 0.0;
max_light += get_sun_diffuse2(sun_info, moon_info, f_norm, /*time_of_day.x*//*-cam_to_frag*/sun_view_dir/*view_dir*/, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, /*vec3(0.0)*/k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
max_light += get_sun_diffuse2(sun_info, moon_info, f_norm, sun_view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, /*vec3(0.0)*/k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
emitted_light *= not_underground;
reflected_light *= not_underground;

View File

@ -275,7 +275,7 @@ void main() {
/* reflect_color = get_cloud_color(reflect_color, ray_dir, f_pos.xyz, time_of_day.x, 100000.0, 0.1); */
reflect_color = vec3(0);
#else
reflect_color = get_sky_color(ray_dir, time_of_day.x, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
reflect_color = get_sky_color(ray_dir, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
#endif
// Sort of non-physical, but we try to balance the reflection intensity with the direct light from the sun,
// resulting in decent reflection of the ambient environment even after the sun has gone down.
@ -339,7 +339,7 @@ void main() {
float passthrough = max(dot(cam_norm, -cam_to_frag), 0) * 0.75;
float max_light = 0.0;
max_light += get_sun_diffuse2(sun_info, moon_info, cam_norm, /*time_of_day.x*/sun_view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, vec3(k_d), /*vec3(f_light * point_shadow)*//*reflect_color*/k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
max_light += get_sun_diffuse2(sun_info, moon_info, cam_norm, sun_view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, vec3(k_d), /*vec3(f_light * point_shadow)*//*reflect_color*/k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
emitted_light *= not_underground;
reflected_light *= not_underground;

View File

@ -1,12 +1,12 @@
#include <lod.glsl>
#include <sky.glsl>
vec3 get_cloud_color(vec3 surf_color, vec3 dir, vec3 origin, float time_of_day, float max_dist, float quality) {
vec3 get_cloud_color(vec3 surf_color, vec3 dir, vec3 origin, float max_dist, float quality) {
// Underwater light attenuation
surf_color = water_diffuse(surf_color, dir, max_dist);
if (max_dist < DIST_CAP) {
vec3 sky_light = get_sky_light(dir, time_of_day, false);
vec3 sky_light = get_sky_light(dir, false);
surf_color = mix(sky_light, surf_color, 1.0 / exp(max_dist / 5000.0));
}

View File

@ -142,7 +142,7 @@ vec4 cloud_at(vec3 pos, float dist, out vec3 emission, out float not_underground
if (emission_strength <= 0.0) {
emission = vec3(0);
} else {
float nz = textureLod(sampler2D(t_noise, s_noise), wind_pos.xy * 0.00005 - time_of_day.x * 0.0001, 0).x;//noise_3d(vec3(wind_pos.xy * 0.00005 + cloud_tendency * 0.2, time_of_day.x * 0.0002));
float nz = textureLod(sampler2D(t_noise, s_noise), wind_pos.xy * 0.00005 - time_of_day.y, 0).x;//noise_3d(vec3(wind_pos.xy * 0.00005 + cloud_tendency * 0.2, time_of_day.x * 0.0002));
float emission_alt = alt * 0.5 + 1000 + 1000 * nz;
float emission_height = 1000.0;
@ -195,7 +195,7 @@ float dist_to_step(float dist, float quality) {
// consistently support forward declarations (not surprising, it's designed for single-pass compilers).
#include <point_glow.glsl>
vec3 get_cloud_color(vec3 surf_color, vec3 dir, vec3 origin, const float time_of_day, float max_dist, const float quality) {
vec3 get_cloud_color(vec3 surf_color, vec3 dir, vec3 origin, float max_dist, const float quality) {
// Limit the marching distance to reduce maximum jumps
max_dist = min(max_dist, DIST_CAP);
@ -205,7 +205,7 @@ vec3 get_cloud_color(vec3 surf_color, vec3 dir, vec3 origin, const float time_of
// improves visual quality for low cloud settings
float splay = 1.0;
#if (CLOUD_MODE == CLOUD_MODE_MINIMAL)
splay += (textureLod(sampler2D(t_noise, s_noise), vec2(atan2(dir.x, dir.y) * 2 / PI, dir.z) * 5.0 - time_of_day * 0.00005, 0).x - 0.5) * 0.025 / (1.0 + pow(dir.z, 2) * 10);
splay += (textureLod(sampler2D(t_noise, s_noise), vec2(atan2(dir.x, dir.y) * 2 / PI, dir.z) * 5.0 - time_of_day.y * 0.5, 0).x - 0.5) * 0.025 / (1.0 + pow(dir.z, 2) * 10);
#endif
const vec3 RAYLEIGH = vec3(0.025, 0.1, 0.5);
@ -215,7 +215,7 @@ vec3 get_cloud_color(vec3 surf_color, vec3 dir, vec3 origin, const float time_of
float moon_scatter = dot(-dir, moon_dir.xyz) * 0.5 + 0.7;
float net_light = get_sun_brightness() + get_moon_brightness();
vec3 sky_color = RAYLEIGH * net_light;
vec3 sky_light = get_sky_light(dir, time_of_day, false);
vec3 sky_light = get_sky_light(dir, false);
vec3 sun_color = get_sun_color();
vec3 moon_color = get_moon_color();

View File

@ -152,8 +152,7 @@ float alt_at_real(vec2 pos) {
}
float horizon_at2(vec4 f_horizons, float alt, vec3 pos, /*float time_of_day*/vec4 light_dir) {
// vec3 sun_dir = get_sun_dir(time_of_day);
float horizon_at2(vec4 f_horizons, float alt, vec3 pos, vec4 light_dir) {
const float PI_2 = 3.1415926535897932384626433832795 / 2.0;
const float MIN_LIGHT = 0.0;//0.115/*0.0*/;

View File

@ -94,7 +94,7 @@ vec3 glow_light(vec3 pos) {
float CLOUD_AVG_ALT = view_distance.z + (view_distance.w - view_distance.z) * 1.25;
const float wind_speed = 0.25;
vec2 wind_offset = vec2(time_of_day.x * wind_speed);
vec2 wind_offset = vec2(time_of_day.y * wind_speed * (3600.0 * 24.0));
float cloud_scale = view_distance.z / 150.0;
@ -140,7 +140,7 @@ float cloud_shadow(vec3 pos, vec3 light_dir) {
#endif
}
float magnetosphere = sin(time_of_day.x / (3600 * 24));
float magnetosphere = sin(time_of_day.y);
#if (CLOUD_MODE <= CLOUD_MODE_LOW)
const vec3 magnetosphere_tint = vec3(1);
#else
@ -156,7 +156,7 @@ float magnetosphere = sin(time_of_day.x / (3600 * 24));
#if (CLOUD_MODE > CLOUD_MODE_NONE)
float emission_strength = clamp((magnetosphere - 0.3) * 1.3, 0, 1) * max(-moon_dir.z, 0);
#if (CLOUD_MODE >= CLOUD_MODE_MEDIUM)
float emission_br = abs(pow(fract(time_of_day.x * 0.000005) * 2 - 1, 2));
float emission_br = abs(pow(fract(time_of_day.y * 0.5) * 2 - 1, 2));
#else
float emission_br = 0.5;
#endif
@ -489,7 +489,6 @@ float get_sun_diffuse2(DirectionalLight sun_info, DirectionalLight moon_info, ve
// This has been extracted into a function to allow quick exit when detecting a star.
float is_star_at(vec3 dir) {
float star_scale = 80.0;
// Star positions
@ -516,7 +515,7 @@ float is_star_at(vec3 dir) {
return power * max(sun_dir.z, 0.1) / (1.0 + pow(dist * 750, 8));
}
vec3 get_sky_light(vec3 dir, float time_of_day, bool with_stars) {
vec3 get_sky_light(vec3 dir, bool with_stars) {
// Add white dots for stars. Note these flicker and jump due to FXAA
float star = 0.0;
if (with_stars) {
@ -580,15 +579,11 @@ vec3 get_sky_light(vec3 dir, float time_of_day, bool with_stars) {
return sky_color * magnetosphere_tint;
}
vec3 get_sky_color(vec3 dir, float time_of_day, vec3 origin, vec3 f_pos, float quality, bool with_features, float refractionIndex, bool fake_clouds, float sun_shade_frac) {
vec3 get_sky_color(vec3 dir, vec3 origin, vec3 f_pos, float quality, bool with_features, float refractionIndex, bool fake_clouds, float sun_shade_frac) {
// Sky color
/* vec3 sun_dir = get_sun_dir(time_of_day);
vec3 moon_dir = get_moon_dir(time_of_day); */
vec3 sun_dir = sun_dir.xyz;
vec3 moon_dir = moon_dir.xyz;
// sun_dir = sun_dir.z <= 0 ? refract(sun_dir/*-view_dir*/, vec3(0.0, 0.0, 1.0), refractionIndex) : sun_dir;
// moon_dir = moon_dir.z <= 0 ? refract(moon_dir/*-view_dir*/, vec3(0.0, 0.0, 1.0), refractionIndex) : moon_dir;
// Sun
const vec3 SUN_SURF_COLOR = vec3(1.5, 0.9, 0.35) * 10.0;
@ -660,10 +655,10 @@ vec3 get_sky_color(vec3 dir, float time_of_day, vec3 origin, vec3 f_pos, float q
#else
if (fake_clouds || medium.x == MEDIUM_WATER) {
#endif
sky_color = get_sky_light(dir, time_of_day, !fake_clouds);
sky_color = get_sky_light(dir, !fake_clouds);
} else {
if (medium.x == MEDIUM_WATER) {
sky_color = get_sky_light(dir, time_of_day, true);
sky_color = get_sky_light(dir, true);
} else {
vec3 star_dir = normalize(sun_dir.xyz * dir.z + cross(sun_dir.xyz, vec3(0, 1, 0)) * dir.x + vec3(0, 1, 0) * dir.y);
float star = is_star_at(star_dir);
@ -674,12 +669,12 @@ vec3 get_sky_color(vec3 dir, float time_of_day, vec3 origin, vec3 f_pos, float q
return sky_color + sun_light + moon_light;
}
vec3 get_sky_color(vec3 dir, float time_of_day, vec3 origin, vec3 f_pos, float quality, bool with_features, float refractionIndex) {
return get_sky_color(dir, time_of_day, origin, f_pos, quality, with_features, refractionIndex, false, 1.0);
vec3 get_sky_color(vec3 dir, vec3 origin, vec3 f_pos, float quality, bool with_features, float refractionIndex) {
return get_sky_color(dir, origin, f_pos, quality, with_features, refractionIndex, false, 1.0);
}
vec3 get_sky_color(vec3 dir, float time_of_day, vec3 origin, vec3 f_pos, float quality, bool with_stars) {
return get_sky_color(dir, time_of_day, origin, f_pos, quality, with_stars, 1.0, false, 1.0);
vec3 get_sky_color(vec3 dir, vec3 origin, vec3 f_pos, float quality, bool with_stars) {
return get_sky_color(dir, origin, f_pos, quality, with_stars, 1.0, false, 1.0);
}
float fog(vec3 f_pos, vec3 focus_pos, uint medium) {

View File

@ -662,10 +662,10 @@ void main() {
vec3 reflect_color;
#if (FLUID_MODE == FLUID_MODE_HIGH)
reflect_color = get_sky_color(reflect_ray, time_of_day.x, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
reflect_color = get_cloud_color(reflect_color, reflect_ray, cam_pos.xyz, time_of_day.x, 100000.0, 0.1);
reflect_color = get_sky_color(reflect_ray, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
reflect_color = get_cloud_color(reflect_color, reflect_ray, cam_pos.xyz, 100000.0, 0.1);
#else
reflect_color = get_sky_color(reflect_ray, time_of_day.x, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
reflect_color = get_sky_color(reflect_ray, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
#endif
reflect_color *= sun_shade_frac * 0.75 + 0.25;
@ -678,7 +678,7 @@ void main() {
surf_alpha = 1.0 - passthrough;
#else
surf_alpha = 0.9;
surf_color = get_sky_color(reflect_ray, time_of_day.x, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
surf_color = get_sky_color(reflect_ray, f_pos, vec3(-100000), 0.125, true, 1.0, true, sun_shade_frac);
#endif
} else {
mat = MAT_LOD;

View File

@ -310,7 +310,7 @@ void main() {
float dist = distance(wpos, cam_pos.xyz);
vec3 dir = (wpos - cam_pos.xyz) / dist;
aa_color.rgb = get_cloud_color(aa_color.rgb, dir, cam_pos.xyz, time_of_day.x, dist, 1.0);
aa_color.rgb = get_cloud_color(aa_color.rgb, dir, cam_pos.xyz, dist, 1.0);
#endif
*/

View File

@ -56,6 +56,6 @@ void main() {
} */
vec3 wpos = cam_pos.xyz + /*normalize(f_pos)*/cam_dir * dist;
tgt_color = vec4(cam_attenuation * get_sky_color(normalize(f_pos), time_of_day.x, cam_pos.xyz, wpos, 1.0, true, refractionIndex, false, 1.0), 1.0);
tgt_color = vec4(cam_attenuation * get_sky_color(normalize(f_pos), cam_pos.xyz, wpos, 1.0, true, refractionIndex, false, 1.0), 1.0);
tgt_mat = uvec4(uvec3(0), MAT_SKY);
}

View File

@ -387,7 +387,7 @@ void main() {
vec3 emitted_light = vec3(1.0);
vec3 reflected_light = vec3(1.0);
float sun_diffuse = get_sun_diffuse2(/*time_of_day.x, */sun_info, moon_info, f_norm, view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
float sun_diffuse = get_sun_diffuse2(sun_info, moon_info, f_norm, view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
max_light += sun_diffuse;
// emitted_light *= f_light * point_shadow * max(shade_frac, MIN_SHADOW);

View File

@ -127,7 +127,12 @@ impl Globals {
focus_off: Vec4::from(focus_pos).map(|e: f32| e.trunc()).into_array(),
focus_pos: Vec4::from(focus_pos).map(|e: f32| e.fract()).into_array(),
view_distance: [view_distance, tgt_detail, map_bounds.x, map_bounds.y],
time_of_day: [time_of_day as f32; 4],
time_of_day: [
(time_of_day % (3600.0 * 24.0)) as f32,
(time_of_day / (3600.0 * 24.0) % (300_000.0)) as f32,
0.0,
0.0,
],
sun_dir: Vec4::from_direction(Self::get_sun_dir(time_of_day)).into_array(),
moon_dir: Vec4::from_direction(Self::get_moon_dir(time_of_day)).into_array(),
tick: [tick as f32; 4],
@ -176,8 +181,8 @@ impl Globals {
}
fn get_angle_rad(time_of_day: f64) -> f32 {
const TIME_FACTOR: f32 = (std::f32::consts::PI * 2.0) / (3600.0 * 24.0);
time_of_day as f32 * TIME_FACTOR
const TIME_FACTOR: f64 = (std::f64::consts::PI * 2.0) / (3600.0 * 24.0);
((time_of_day * TIME_FACTOR) % (std::f64::consts::PI * 2.0)) as f32
}
/// Computes the direction of light from the sun based on the time of day.