2019-08-15 01:35:56 +00:00
|
|
|
#include <random.glsl>
|
2020-04-04 00:32:39 +00:00
|
|
|
#include <srgb.glsl>
|
2020-01-19 03:58:25 +00:00
|
|
|
#include <cloud.glsl>
|
2019-08-15 01:35:56 +00:00
|
|
|
|
2019-06-05 15:22:06 +00:00
|
|
|
const float PI = 3.141592;
|
2019-05-31 20:37:11 +00:00
|
|
|
|
2019-08-12 01:53:48 +00:00
|
|
|
const vec3 SKY_DAY_TOP = vec3(0.1, 0.2, 0.9);
|
|
|
|
const vec3 SKY_DAY_MID = vec3(0.02, 0.08, 0.8);
|
2019-11-18 17:18:51 +00:00
|
|
|
const vec3 SKY_DAY_BOT = vec3(0.1, 0.2, 0.3);
|
2019-09-24 06:42:09 +00:00
|
|
|
const vec3 DAY_LIGHT = vec3(1.2, 1.0, 1.0);
|
2019-09-25 13:16:02 +00:00
|
|
|
const vec3 SUN_HALO_DAY = vec3(0.35, 0.35, 0.0);
|
2019-06-15 17:23:01 +00:00
|
|
|
|
2019-08-20 15:26:12 +00:00
|
|
|
const vec3 SKY_DUSK_TOP = vec3(0.06, 0.1, 0.20);
|
|
|
|
const vec3 SKY_DUSK_MID = vec3(0.35, 0.1, 0.15);
|
2019-11-18 17:50:19 +00:00
|
|
|
const vec3 SKY_DUSK_BOT = vec3(0.0, 0.1, 0.23);
|
2019-10-14 09:48:40 +00:00
|
|
|
const vec3 DUSK_LIGHT = vec3(3.0, 1.5, 0.3);
|
2019-11-18 12:28:19 +00:00
|
|
|
const vec3 SUN_HALO_DUSK = vec3(1.2, 0.15, 0.0);
|
2019-06-15 17:23:01 +00:00
|
|
|
|
2019-07-08 21:10:48 +00:00
|
|
|
const vec3 SKY_NIGHT_TOP = vec3(0.001, 0.001, 0.0025);
|
|
|
|
const vec3 SKY_NIGHT_MID = vec3(0.001, 0.005, 0.02);
|
2019-11-18 17:18:51 +00:00
|
|
|
const vec3 SKY_NIGHT_BOT = vec3(0.002, 0.004, 0.004);
|
2019-06-29 15:46:54 +00:00
|
|
|
const vec3 NIGHT_LIGHT = vec3(0.002, 0.01, 0.03);
|
2019-06-15 17:23:01 +00:00
|
|
|
|
2020-03-27 15:13:36 +00:00
|
|
|
const float UNDERWATER_MIST_DIST = 100.0;
|
|
|
|
|
2019-06-18 23:59:40 +00:00
|
|
|
vec3 get_sun_dir(float time_of_day) {
|
|
|
|
const float TIME_FACTOR = (PI * 2.0) / (3600.0 * 24.0);
|
|
|
|
|
|
|
|
float sun_angle_rad = time_of_day * TIME_FACTOR;
|
2019-11-14 16:34:34 +00:00
|
|
|
return vec3(sin(sun_angle_rad), 0.0, cos(sun_angle_rad));
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 get_moon_dir(float time_of_day) {
|
|
|
|
const float TIME_FACTOR = (PI * 2.0) / (3600.0 * 24.0);
|
2019-06-18 23:59:40 +00:00
|
|
|
|
2019-11-14 16:34:34 +00:00
|
|
|
float moon_angle_rad = time_of_day * TIME_FACTOR;
|
2019-11-14 16:42:02 +00:00
|
|
|
return normalize(-vec3(sin(moon_angle_rad), 0.0, cos(moon_angle_rad) - 0.5));
|
2019-06-18 23:59:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 22:29:59 +00:00
|
|
|
const float PERSISTENT_AMBIANCE = 0.025; // 0.1; // 0.025; // 0.1;
|
2019-09-25 09:41:51 +00:00
|
|
|
|
2019-06-18 23:59:40 +00:00
|
|
|
float get_sun_brightness(vec3 sun_dir) {
|
2019-10-16 09:57:43 +00:00
|
|
|
return max(-sun_dir.z + 0.6, 0.0) * 0.9;
|
2019-06-18 23:59:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 16:34:34 +00:00
|
|
|
float get_moon_brightness(vec3 moon_dir) {
|
2019-11-14 16:36:36 +00:00
|
|
|
return max(-moon_dir.z + 0.6, 0.0) * 0.07;
|
2019-11-14 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 22:41:00 +00:00
|
|
|
vec3 get_sun_color(vec3 sun_dir) {
|
|
|
|
return mix(
|
|
|
|
mix(
|
|
|
|
DUSK_LIGHT,
|
|
|
|
NIGHT_LIGHT,
|
|
|
|
max(sun_dir.z, 0)
|
|
|
|
),
|
|
|
|
DAY_LIGHT,
|
|
|
|
max(-sun_dir.z, 0)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 get_moon_color(vec3 moon_dir) {
|
2019-11-18 17:18:51 +00:00
|
|
|
return vec3(0.05, 0.05, 0.6);
|
2019-11-17 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 00:32:39 +00:00
|
|
|
// Calculates extra emission and reflectance (due to sunlight / moonlight).
|
|
|
|
//
|
|
|
|
// reflectence = k_a * i_a + i_a,persistent
|
|
|
|
// emittence = Σ { m ∈ lights } i_m * shadow_m * get_light_reflected(light_m)
|
|
|
|
//
|
|
|
|
// Note that any shadowing to be done that would block the sun and moon, aside from heightmap shadowing (that will be
|
|
|
|
// implemented sooon), should be implicitly provided via k_a, k_d, and k_s. For instance, shadowing via ambient occlusion.
|
|
|
|
//
|
|
|
|
// Also note that the emitted light calculation is kind of lame... we probabbly need something a bit nicer if we ever want to do
|
|
|
|
// anything interesting here.
|
|
|
|
// void get_sun_diffuse(vec3 norm, float time_of_day, out vec3 light, out vec3 diffuse_light, out vec3 ambient_light, float diffusion
|
|
|
|
void get_sun_diffuse(vec3 norm, float time_of_day, vec3 dir, vec3 k_a, vec3 k_d, vec3 k_s, float alpha, out vec3 emitted_light, out vec3 reflected_light) {
|
2019-10-16 09:57:43 +00:00
|
|
|
const float SUN_AMBIANCE = 0.1;
|
2019-06-18 23:59:40 +00:00
|
|
|
|
|
|
|
vec3 sun_dir = get_sun_dir(time_of_day);
|
2019-11-14 16:34:34 +00:00
|
|
|
vec3 moon_dir = get_moon_dir(time_of_day);
|
2019-06-18 23:59:40 +00:00
|
|
|
|
|
|
|
float sun_light = get_sun_brightness(sun_dir);
|
2019-11-14 16:34:34 +00:00
|
|
|
float moon_light = get_moon_brightness(moon_dir);
|
2019-06-18 23:59:40 +00:00
|
|
|
|
2019-11-17 22:41:00 +00:00
|
|
|
vec3 sun_color = get_sun_color(sun_dir);
|
|
|
|
vec3 moon_color = get_moon_color(moon_dir);
|
2019-09-24 20:05:53 +00:00
|
|
|
|
2019-11-14 16:34:34 +00:00
|
|
|
vec3 sun_chroma = sun_color * sun_light;
|
|
|
|
vec3 moon_chroma = moon_color * moon_light;
|
|
|
|
|
2020-04-04 00:32:39 +00:00
|
|
|
/* float NLsun = max(dot(-norm, sun_dir), 0);
|
|
|
|
float NLmoon = max(dot(-norm, moon_dir), 0);
|
|
|
|
vec3 E = -dir; */
|
|
|
|
|
|
|
|
// Globbal illumination "estimate" used to light the faces of voxels which are parallel to the sun or moon (which is a very common occurrence).
|
|
|
|
// Will be attenuated by k_d, which is assumed to carry any additional ambient occlusion information (e.g. about shadowing).
|
2020-04-09 00:38:40 +00:00
|
|
|
float ambient_sides = clamp(mix(0.5, 0.0, abs(dot(-norm, sun_dir)) * mix(0.0, 1.0, abs(sun_dir.z) * 10000.0) * 10000.0), 0.0, 0.5);
|
2020-04-04 00:32:39 +00:00
|
|
|
|
|
|
|
emitted_light = k_a * (ambient_sides + vec3(SUN_AMBIANCE * sun_light + moon_light)) + PERSISTENT_AMBIANCE;
|
|
|
|
// TODO: Add shadows.
|
|
|
|
reflected_light =
|
|
|
|
sun_chroma * light_reflection_factor(norm, dir, sun_dir, k_d, k_s, alpha) +
|
2020-04-09 00:38:40 +00:00
|
|
|
moon_chroma * 1.0 * /*4.0 * */light_reflection_factor(norm, dir, moon_dir, k_d, k_s, alpha);
|
2020-04-04 00:32:39 +00:00
|
|
|
|
|
|
|
/* light = sun_chroma + moon_chroma + PERSISTENT_AMBIANCE;
|
2019-11-14 16:34:34 +00:00
|
|
|
diffuse_light =
|
2020-01-21 13:14:25 +00:00
|
|
|
sun_chroma * mix(1.0, max(dot(-norm, sun_dir) * 0.5 + 0.5, 0.0), diffusion) +
|
2020-01-21 12:57:59 +00:00
|
|
|
moon_chroma * mix(1.0, pow(dot(-norm, moon_dir) * 2.0, 2.0), diffusion) +
|
2019-11-14 16:34:34 +00:00
|
|
|
PERSISTENT_AMBIANCE;
|
2020-04-04 00:32:39 +00:00
|
|
|
ambient_light = vec3(SUN_AMBIANCE * sun_light + moon_light); */
|
2019-06-18 23:59:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 22:29:59 +00:00
|
|
|
void get_sun_diffuse2(vec3 norm, vec3 sun_dir, vec3 moon_dir, vec3 dir, vec3 k_a, vec3 k_d, vec3 k_s, float alpha, out vec3 emitted_light, out vec3 reflected_light) {
|
|
|
|
const float SUN_AMBIANCE = 0.1;
|
|
|
|
|
|
|
|
float sun_light = get_sun_brightness(sun_dir);
|
|
|
|
float moon_light = get_moon_brightness(moon_dir);
|
|
|
|
|
|
|
|
vec3 sun_color = get_sun_color(sun_dir);
|
|
|
|
vec3 moon_color = get_moon_color(moon_dir);
|
|
|
|
|
|
|
|
vec3 sun_chroma = sun_color * sun_light;
|
|
|
|
vec3 moon_chroma = moon_color * moon_light;
|
|
|
|
|
|
|
|
/* float NLsun = max(dot(-norm, sun_dir), 0);
|
|
|
|
float NLmoon = max(dot(-norm, moon_dir), 0);
|
|
|
|
vec3 E = -dir; */
|
|
|
|
|
|
|
|
// Globbal illumination "estimate" used to light the faces of voxels which are parallel to the sun or moon (which is a very common occurrence).
|
|
|
|
// Will be attenuated by k_d, which is assumed to carry any additional ambient occlusion information (e.g. about shadowing).
|
|
|
|
float ambient_sides = clamp(mix(0.5, 0.0, abs(dot(-norm, sun_dir)) * mix(0.0, 1.0, abs(sun_dir.z) * 10000.0) * 10000.0), 0.0, 0.5);
|
|
|
|
|
|
|
|
emitted_light = k_a * (ambient_sides + vec3(SUN_AMBIANCE * sun_light + moon_light)) + PERSISTENT_AMBIANCE;
|
|
|
|
// TODO: Add shadows.
|
|
|
|
reflected_light =
|
|
|
|
sun_chroma * light_reflection_factor(norm, dir, sun_dir, k_d, k_s, alpha) +
|
|
|
|
moon_chroma * 1.0 * /*4.0 * */light_reflection_factor(norm, dir, moon_dir, k_d, k_s, alpha);
|
|
|
|
|
|
|
|
/* light = sun_chroma + moon_chroma + PERSISTENT_AMBIANCE;
|
|
|
|
diffuse_light =
|
|
|
|
sun_chroma * mix(1.0, max(dot(-norm, sun_dir) * 0.5 + 0.5, 0.0), diffusion) +
|
|
|
|
moon_chroma * mix(1.0, pow(dot(-norm, moon_dir) * 2.0, 2.0), diffusion) +
|
|
|
|
PERSISTENT_AMBIANCE;
|
|
|
|
ambient_light = vec3(SUN_AMBIANCE * sun_light + moon_light); */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-04 10:32:50 +00:00
|
|
|
// This has been extracted into a function to allow quick exit when detecting a star.
|
2019-08-04 11:24:56 +00:00
|
|
|
float is_star_at(vec3 dir) {
|
2020-01-21 12:57:59 +00:00
|
|
|
float star_scale = 80.0;
|
|
|
|
|
|
|
|
// Star positions
|
|
|
|
vec3 pos = (floor(dir * star_scale) - 0.5) / star_scale;
|
|
|
|
|
|
|
|
// Noisy offsets
|
|
|
|
pos += (3.0 / star_scale) * rand_perm_3(pos);
|
|
|
|
|
|
|
|
// Find distance to fragment
|
|
|
|
float dist = length(normalize(pos) - dir);
|
|
|
|
|
|
|
|
// Star threshold
|
|
|
|
if (dist < 0.0015) {
|
|
|
|
return 1.0;
|
2019-06-23 17:46:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 09:55:45 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
2019-06-23 17:46:04 +00:00
|
|
|
|
2019-11-19 08:37:54 +00:00
|
|
|
vec3 get_sky_color(vec3 dir, float time_of_day, vec3 origin, vec3 f_pos, float quality, bool with_stars, out vec4 clouds) {
|
2019-08-03 09:55:45 +00:00
|
|
|
// Sky color
|
2019-06-23 17:46:04 +00:00
|
|
|
vec3 sun_dir = get_sun_dir(time_of_day);
|
2019-11-14 16:34:34 +00:00
|
|
|
vec3 moon_dir = get_moon_dir(time_of_day);
|
2019-06-23 17:46:04 +00:00
|
|
|
|
2019-08-03 09:55:45 +00:00
|
|
|
// Add white dots for stars. Note these flicker and jump due to FXAA
|
2019-08-20 15:26:12 +00:00
|
|
|
float star = 0.0;
|
2020-01-27 01:03:55 +00:00
|
|
|
if (with_stars) {
|
2020-01-27 01:22:07 +00:00
|
|
|
vec3 star_dir = normalize(sun_dir * dir.z + cross(sun_dir, vec3(0, 1, 0)) * dir.x + vec3(0, 1, 0) * dir.y);
|
|
|
|
star = is_star_at(star_dir);
|
2019-08-20 15:26:12 +00:00
|
|
|
}
|
2019-08-03 09:55:45 +00:00
|
|
|
|
2019-11-18 10:47:52 +00:00
|
|
|
// Sun
|
|
|
|
const vec3 SUN_SURF_COLOR = vec3(1.5, 0.9, 0.35) * 200.0;
|
|
|
|
|
|
|
|
vec3 sun_halo_color = mix(
|
|
|
|
SUN_HALO_DUSK,
|
|
|
|
SUN_HALO_DAY,
|
|
|
|
max(-sun_dir.z, 0)
|
|
|
|
);
|
|
|
|
|
|
|
|
vec3 sun_halo = pow(max(dot(dir, -sun_dir) + 0.1, 0.0), 8.0) * sun_halo_color;
|
|
|
|
vec3 sun_surf = pow(max(dot(dir, -sun_dir) - 0.001, 0.0), 3000.0) * SUN_SURF_COLOR;
|
|
|
|
vec3 sun_light = (sun_halo + sun_surf) * clamp(dir.z * 10.0, 0, 1);
|
|
|
|
|
|
|
|
// Moon
|
|
|
|
const vec3 MOON_SURF_COLOR = vec3(0.7, 1.0, 1.5) * 500.0;
|
2019-11-18 12:28:19 +00:00
|
|
|
const vec3 MOON_HALO_COLOR = vec3(0.015, 0.015, 0.05);
|
2019-11-18 10:47:52 +00:00
|
|
|
|
2019-11-18 12:28:19 +00:00
|
|
|
vec3 moon_halo = pow(max(dot(dir, -moon_dir) + 0.1, 0.0), 8.0) * MOON_HALO_COLOR;
|
2019-11-18 10:47:52 +00:00
|
|
|
vec3 moon_surf = pow(max(dot(dir, -moon_dir) - 0.001, 0.0), 3000.0) * MOON_SURF_COLOR;
|
2020-02-04 22:58:56 +00:00
|
|
|
vec3 moon_light = clamp(moon_halo + moon_surf, vec3(0), vec3(max(dir.z * 3.0, 0)));
|
2019-11-18 10:47:52 +00:00
|
|
|
|
2019-08-03 09:55:45 +00:00
|
|
|
// Replaced all clamp(sun_dir, 0, 1) with max(sun_dir, 0) because sun_dir is calculated from sin and cos, which are never > 1
|
|
|
|
|
2019-06-23 17:46:04 +00:00
|
|
|
vec3 sky_top = mix(
|
|
|
|
mix(
|
2019-11-18 10:47:52 +00:00
|
|
|
SKY_DUSK_TOP + star / (1.0 + moon_surf * 100.0),
|
|
|
|
SKY_NIGHT_TOP + star / (1.0 + moon_surf * 100.0),
|
2019-09-25 09:41:51 +00:00
|
|
|
max(pow(sun_dir.z, 0.2), 0)
|
2019-08-03 09:55:45 +00:00
|
|
|
),
|
2019-06-23 17:46:04 +00:00
|
|
|
SKY_DAY_TOP,
|
2019-08-03 09:55:45 +00:00
|
|
|
max(-sun_dir.z, 0)
|
2019-08-04 14:30:45 +00:00
|
|
|
);
|
2019-06-23 17:46:04 +00:00
|
|
|
|
|
|
|
vec3 sky_mid = mix(
|
|
|
|
mix(
|
|
|
|
SKY_DUSK_MID,
|
|
|
|
SKY_NIGHT_MID,
|
2019-09-25 09:41:51 +00:00
|
|
|
max(pow(sun_dir.z, 0.2), 0)
|
2019-06-23 17:46:04 +00:00
|
|
|
),
|
|
|
|
SKY_DAY_MID,
|
2019-08-03 09:55:45 +00:00
|
|
|
max(-sun_dir.z, 0)
|
2019-06-23 17:46:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
vec3 sky_bot = mix(
|
|
|
|
mix(
|
|
|
|
SKY_DUSK_BOT,
|
|
|
|
SKY_NIGHT_BOT,
|
2019-09-25 09:41:51 +00:00
|
|
|
max(pow(sun_dir.z, 0.2), 0)
|
2019-06-23 17:46:04 +00:00
|
|
|
),
|
2019-06-29 14:18:13 +00:00
|
|
|
SKY_DAY_BOT,
|
2019-08-03 09:55:45 +00:00
|
|
|
max(-sun_dir.z, 0)
|
2019-06-23 17:46:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
vec3 sky_color = mix(
|
|
|
|
mix(
|
|
|
|
sky_mid,
|
|
|
|
sky_bot,
|
2019-08-03 09:55:45 +00:00
|
|
|
pow(max(-dir.z, 0), 0.4)
|
2019-06-23 17:46:04 +00:00
|
|
|
),
|
|
|
|
sky_top,
|
2019-08-03 09:55:45 +00:00
|
|
|
max(dir.z, 0)
|
2019-06-23 17:46:04 +00:00
|
|
|
);
|
|
|
|
|
2019-11-18 11:00:59 +00:00
|
|
|
// Approximate distance to fragment
|
2019-11-19 08:37:54 +00:00
|
|
|
float f_dist = distance(origin, f_pos);
|
2019-11-18 11:00:59 +00:00
|
|
|
|
2019-11-17 22:41:00 +00:00
|
|
|
// Clouds
|
2019-11-19 08:37:54 +00:00
|
|
|
clouds = get_cloud_color(dir, origin, time_of_day, f_dist, quality);
|
2020-02-04 22:58:56 +00:00
|
|
|
clouds.rgb *= get_sun_brightness(sun_dir) * (sun_halo * 1.5 + get_sun_color(sun_dir)) + get_moon_brightness(moon_dir) * (moon_halo * 80.0 + get_moon_color(moon_dir));
|
2019-11-17 22:41:00 +00:00
|
|
|
|
2019-11-18 11:08:43 +00:00
|
|
|
if (f_dist > 5000.0) {
|
2019-11-18 11:00:59 +00:00
|
|
|
sky_color += sun_light + moon_light;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mix(sky_color, clouds.rgb, clouds.a);
|
2019-06-05 15:22:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 14:58:14 +00:00
|
|
|
float fog(vec3 f_pos, vec3 focus_pos, uint medium) {
|
2019-11-22 12:40:48 +00:00
|
|
|
return max(1.0 - 5000.0 / (1.0 + distance(f_pos.xy, focus_pos.xy)), 0.0);
|
2019-11-22 10:09:00 +00:00
|
|
|
|
2019-08-16 14:58:14 +00:00
|
|
|
float fog_radius = view_distance.x;
|
|
|
|
float mist_radius = 10000000.0;
|
2019-06-05 15:22:06 +00:00
|
|
|
|
2019-08-16 14:58:14 +00:00
|
|
|
float min_fog = 0.5;
|
|
|
|
float max_fog = 1.0;
|
|
|
|
|
|
|
|
if (medium == 1u) {
|
2020-03-27 15:13:36 +00:00
|
|
|
mist_radius = UNDERWATER_MIST_DIST;
|
2019-08-16 14:58:14 +00:00
|
|
|
min_fog = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float fog = distance(f_pos.xy, focus_pos.xy) / fog_radius;
|
|
|
|
float mist = distance(f_pos, focus_pos) / mist_radius;
|
|
|
|
|
|
|
|
return pow(clamp((max(fog, mist) - min_fog) / (max_fog - min_fog), 0.0, 1.0), 1.7);
|
2019-08-04 11:24:56 +00:00
|
|
|
}
|
2019-11-21 20:57:46 +00:00
|
|
|
|
2020-04-04 00:32:39 +00:00
|
|
|
/* vec3 illuminate(vec3 color, vec3 light, vec3 diffuse, vec3 ambience) {
|
2019-11-21 20:57:46 +00:00
|
|
|
float avg_col = (color.r + color.g + color.b) / 3.0;
|
|
|
|
return ((color - avg_col) * light + (diffuse + ambience) * avg_col) * (diffuse + ambience);
|
2020-04-04 00:32:39 +00:00
|
|
|
} */
|
|
|
|
vec3 illuminate(vec3 emitted, vec3 reflected) {
|
|
|
|
const float gamma = /*0.5*/1.0;//1.0;
|
|
|
|
vec3 color = emitted + reflected;
|
|
|
|
return srgb_to_linear(/*0.5*//*0.125 * */vec3(pow(color.x, gamma), pow(color.y, gamma), pow(color.z, gamma)));
|
2019-11-21 20:57:46 +00:00
|
|
|
}
|