2019-08-15 01:35:56 +00:00
|
|
|
#include <random.glsl>
|
|
|
|
|
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);
|
|
|
|
const vec3 SKY_DAY_BOT = vec3(0.02, 0.01, 0.3);
|
2019-09-24 06:42:09 +00:00
|
|
|
const vec3 DAY_LIGHT = vec3(1.2, 1.0, 1.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);
|
|
|
|
const vec3 SKY_DUSK_BOT = vec3(0.0, 0.1, 0.13);
|
2019-09-23 19:37:50 +00:00
|
|
|
const vec3 DUSK_LIGHT = vec3(3.0, 1.0, 0.3);
|
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-06-29 15:34:32 +00:00
|
|
|
const vec3 SKY_NIGHT_BOT = vec3(0.002, 0.002, 0.005);
|
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
|
|
|
|
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;
|
|
|
|
vec3 sun_dir = vec3(sin(sun_angle_rad), 0.0, cos(sun_angle_rad));
|
|
|
|
|
|
|
|
return sun_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
float get_sun_brightness(vec3 sun_dir) {
|
2019-09-24 09:01:50 +00:00
|
|
|
return max(-sun_dir.z + 0.6, 0.0);
|
2019-06-18 23:59:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:05:53 +00:00
|
|
|
const float PERSISTENT_AMBIANCE = 0.1;
|
2019-06-18 23:59:40 +00:00
|
|
|
|
2019-09-24 09:01:50 +00:00
|
|
|
void get_sun_diffuse(vec3 norm, float time_of_day, out vec3 light, out vec3 diffuse_light, out vec3 ambient_light) {
|
|
|
|
const float SUN_AMBIANCE = 0.1;
|
2019-06-18 23:59:40 +00:00
|
|
|
|
|
|
|
vec3 sun_dir = get_sun_dir(time_of_day);
|
|
|
|
|
|
|
|
float sun_light = get_sun_brightness(sun_dir);
|
|
|
|
|
2019-08-03 09:55:45 +00:00
|
|
|
// clamp() changed to max() as sun_dir.z is produced from a cos() function and therefore never greater than 1
|
|
|
|
|
2019-08-20 15:26:12 +00:00
|
|
|
vec3 sun_color = mix(
|
2019-06-29 15:34:32 +00:00
|
|
|
mix(
|
2019-06-29 15:46:54 +00:00
|
|
|
DUSK_LIGHT,
|
|
|
|
NIGHT_LIGHT,
|
2019-08-03 09:55:45 +00:00
|
|
|
max(sun_dir.z, 0)
|
2019-06-29 15:34:32 +00:00
|
|
|
),
|
2019-06-29 15:46:54 +00:00
|
|
|
DAY_LIGHT,
|
2019-08-03 09:55:45 +00:00
|
|
|
max(-sun_dir.z, 0)
|
2019-08-20 15:26:12 +00:00
|
|
|
);
|
2019-06-29 15:34:32 +00:00
|
|
|
|
2019-09-24 20:05:53 +00:00
|
|
|
vec3 sun_chroma = sun_color * sun_light;
|
|
|
|
|
|
|
|
light = vec3(sun_chroma + PERSISTENT_AMBIANCE);
|
|
|
|
diffuse_light = sun_chroma * (dot(-norm, sun_dir) * 0.5 + 0.5);
|
|
|
|
ambient_light = vec3(SUN_AMBIANCE * sun_light + PERSISTENT_AMBIANCE);
|
2019-06-18 23:59:40 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2019-08-04 10:32:50 +00:00
|
|
|
float star_scale = 30.0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i ++) {
|
|
|
|
for (int j = 0; j < 2; j ++) {
|
|
|
|
for (int k = 0; k < 2; k ++) {
|
|
|
|
// Star positions
|
|
|
|
vec3 pos = (floor(dir * star_scale) + vec3(i, j, k) - vec3(0.5)) / star_scale;
|
|
|
|
|
|
|
|
// Noisy offsets
|
2019-08-15 01:35:56 +00:00
|
|
|
pos += (3.0 / star_scale) * rand_perm_3(pos);
|
2019-08-04 10:32:50 +00:00
|
|
|
|
|
|
|
// 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-08-20 15:26:12 +00:00
|
|
|
vec3 get_sky_color(vec3 dir, float time_of_day, bool with_stars) {
|
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-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;
|
|
|
|
if (with_stars) {
|
|
|
|
star = is_star_at(dir);
|
|
|
|
}
|
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-08-04 14:51:17 +00:00
|
|
|
SKY_DUSK_TOP + star,
|
2019-08-04 14:30:45 +00:00
|
|
|
SKY_NIGHT_TOP + star,
|
2019-08-03 09:55:45 +00:00
|
|
|
max(sun_dir.z, 0)
|
|
|
|
),
|
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-08-03 09:55:45 +00:00
|
|
|
max(sun_dir.z, 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-08-03 09:55:45 +00:00
|
|
|
max(sun_dir.z, 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
|
|
|
);
|
|
|
|
|
|
|
|
// Sun
|
|
|
|
|
2019-09-23 19:37:50 +00:00
|
|
|
const vec3 SUN_HALO_COLOR = vec3(1.5, 0.65, 0.0) * 0.3;
|
2019-08-20 15:26:12 +00:00
|
|
|
const vec3 SUN_SURF_COLOR = vec3(1.5, 0.9, 0.35) * 200.0;
|
2019-06-23 17:46:04 +00:00
|
|
|
|
|
|
|
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.0045, 0.0), 1000.0) * SUN_SURF_COLOR;
|
|
|
|
vec3 sun_light = (sun_halo + sun_surf) * clamp(dir.z * 10.0, 0, 1);
|
|
|
|
|
|
|
|
return sky_color + sun_light;
|
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) {
|
|
|
|
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) {
|
2019-09-09 19:11:40 +00:00
|
|
|
mist_radius = 96.0;
|
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
|
|
|
}
|