veloren/voxygen/shaders/include/sky.glsl

147 lines
3.3 KiB
Plaintext
Raw Normal View History

2019-06-05 15:22:06 +00:00
const float PI = 3.141592;
2019-05-31 20:37:11 +00:00
const vec3 SKY_DAY_TOP = vec3(0.2, 0.3, 0.9);
const vec3 SKY_DAY_MID = vec3(0.15, 0.2, 0.8);
const vec3 SKY_DAY_BOT = vec3(0.02, 0.1, 0.3);
2019-07-03 19:58:09 +00:00
const vec3 DAY_LIGHT = vec3(0.5, 0.5, 1.0);
2019-06-15 17:23:01 +00:00
const vec3 SKY_DUSK_TOP = vec3(0.1, 0.15, 0.3);
const vec3 SKY_DUSK_MID = vec3(0.9, 0.3, 0.2);
const vec3 SKY_DUSK_BOT = vec3(0.01, 0.05, 0.15);
const vec3 DUSK_LIGHT = vec3(0.9, 0.2, 0.1);
2019-06-15 17:23:01 +00:00
const vec3 SKY_NIGHT_TOP = vec3(0.002, 0.002, 0.005);
const vec3 SKY_NIGHT_MID = vec3(0.002, 0.01, 0.03);
const vec3 SKY_NIGHT_BOT = vec3(0.002, 0.002, 0.005);
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-07-03 19:58:09 +00:00
return max(-sun_dir.z + 0.6, 0.0);
2019-06-18 23:59:40 +00:00
}
const float PERSISTENT_AMBIANCE = 0.008;
2019-06-18 23:59:40 +00:00
vec3 get_sun_diffuse(vec3 norm, float time_of_day) {
const float SUN_AMBIANCE = 0.075;
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);
vec3 sun_color = normalize(mix(
mix(
DUSK_LIGHT,
NIGHT_LIGHT,
clamp(sun_dir.z, 0, 1)
),
DAY_LIGHT,
clamp(-sun_dir.z, 0, 1)
2019-06-29 21:09:36 +00:00
));
2019-06-29 21:09:36 +00:00
vec3 diffuse_light = (SUN_AMBIANCE + max(dot(-norm, sun_dir), 0.0) * sun_color) * sun_light + PERSISTENT_AMBIANCE;
return diffuse_light;
2019-06-18 23:59:40 +00:00
}
2019-06-23 17:46:04 +00:00
vec3 rand_offs(vec3 pos) {
2019-06-23 19:18:29 +00:00
return sin(pos * vec3(1473.7 * pos.z + 472.3, 8891.1 * pos.x + 723.1, 3813.3 * pos.y + 982.5));
2019-06-23 17:46:04 +00:00
}
2019-06-05 15:22:06 +00:00
vec3 get_sky_color(vec3 dir, float time_of_day) {
2019-06-23 17:46:04 +00:00
// Stars
2019-06-23 19:18:29 +00:00
float star_scale = 30.0;
float star = 0.0;
2019-06-23 17:46:04 +00:00
for (int i = 0; i < 2; i ++) {
for (int j = 0; j < 2; j ++) {
for (int k = 0; k < 2; k ++) {
2019-06-23 19:18:29 +00:00
// Star positions
2019-06-23 17:46:04 +00:00
vec3 pos = (floor(dir * star_scale) + vec3(i, j, k) - vec3(0.5)) / star_scale;
2019-06-23 19:18:29 +00:00
// Noisy offsets
pos += 3.0 * rand_offs(pos) / star_scale;
// Find distance to fragment
float dist = length(normalize(pos) - dir);
// Star threshold
if (dist < 0.0015) {
star = 1.0;
}
2019-06-23 17:46:04 +00:00
}
}
}
// Sky color
vec3 sun_dir = get_sun_dir(time_of_day);
vec3 sky_top = mix(
mix(
SKY_DUSK_TOP,
SKY_NIGHT_TOP,
clamp(sun_dir.z, 0, 1)
) + star,
SKY_DAY_TOP,
clamp(-sun_dir.z, 0, 1)
);
vec3 sky_mid = mix(
mix(
SKY_DUSK_MID,
SKY_NIGHT_MID,
clamp(sun_dir.z, 0, 1)
),
SKY_DAY_MID,
clamp(-sun_dir.z, 0, 1)
);
vec3 sky_bot = mix(
mix(
SKY_DUSK_BOT,
SKY_NIGHT_BOT,
clamp(sun_dir.z, 0, 1)
),
2019-06-29 14:18:13 +00:00
SKY_DAY_BOT,
2019-06-23 17:46:04 +00:00
clamp(-sun_dir.z, 0, 1)
);
vec3 sky_color = mix(
mix(
sky_mid,
sky_bot,
pow(clamp(-dir.z, 0, 1), 0.4)
),
sky_top,
pow(clamp(dir.z, 0, 1), 1.0)
);
// Sun
const vec3 SUN_HALO_COLOR = vec3(1.0, 0.35, 0.1) * 0.3;
const vec3 SUN_SURF_COLOR = vec3(1.0, 0.9, 0.35) * 200.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.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-06-05 18:08:03 +00:00
float fog(vec2 f_pos, vec2 focus_pos) {
float dist = distance(f_pos, focus_pos) / view_distance.x;
float min_fog = 0.75;
2019-06-05 16:32:33 +00:00
float max_fog = 1.0;
2019-06-05 15:22:06 +00:00
return clamp((dist - min_fog) / (max_fog - min_fog), 0.0, 1.0);
}