veloren/voxygen/shaders/include/sky.glsl

108 lines
2.8 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
2019-06-15 17:23:01 +00:00
float mod289(float x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 mod289(vec4 x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 perm(vec4 x){return mod289(((x * 34.0) + 1.0) * x);}
float noise(vec3 p){
vec3 a = floor(p);
vec3 d = p - a;
d = d * d * (3.0 - 2.0 * d);
vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
vec4 k1 = perm(b.xyxy);
vec4 k2 = perm(k1.xyxy + b.zzww);
vec4 c = k2 + a.zzzz;
vec4 k3 = perm(c);
vec4 k4 = perm(c + 1.0);
vec4 o1 = fract(k3 * (1.0 / 41.0));
vec4 o2 = fract(k4 * (1.0 / 41.0));
vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
return o4.y * d.y + o4.x * (1.0 - d.y);
}
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) {
return max(-sun_dir.z, 0.0);
}
const float PERSISTENT_AMBIANCE = 0.015;
float get_sun_diffuse(vec3 norm, float time_of_day) {
const float SUN_AMBIANCE = 0.2;
vec3 sun_dir = get_sun_dir(time_of_day);
float sun_light = get_sun_brightness(sun_dir);
return (SUN_AMBIANCE + max(dot(-norm, sun_dir), 0.0)) * sun_light + PERSISTENT_AMBIANCE;
}
2019-06-05 15:22:06 +00:00
vec3 get_sky_color(vec3 dir, float time_of_day) {
2019-06-15 17:23:01 +00:00
bool objects = true;
vec2 pos2d = dir.xy / dir.z;
const vec3 SKY_TOP = vec3(0.2, 0.3, 0.9);
const vec3 SKY_MIDDLE = vec3(0.1, 0.15, 0.7);
const vec3 SKY_BOTTOM = vec3(0.025, 0.15, 0.35);
2019-06-05 15:22:06 +00:00
2019-06-18 23:59:40 +00:00
const vec3 SUN_HALO_COLOR = vec3(1.0, 0.4, 0.3) * 0.5;
2019-06-05 15:22:06 +00:00
const vec3 SUN_SURF_COLOR = vec3(1.0, 0.9, 0.35) * 200.0;
2019-06-18 23:59:40 +00:00
vec3 sun_dir = get_sun_dir(time_of_day);
float sky_brightness = get_sun_brightness(sun_dir);
2019-06-05 15:22:06 +00:00
2019-06-18 23:59:40 +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;
2019-06-05 15:22:06 +00:00
vec3 sun_light = sun_halo + sun_surf;
2019-06-18 23:59:40 +00:00
float brightess = (sky_brightness + PERSISTENT_AMBIANCE);
vec3 sky_top = SKY_TOP * brightess;
2019-06-19 00:15:43 +00:00
vec3 sky_middle = SKY_MIDDLE * brightess;
2019-06-15 17:23:01 +00:00
if (objects) {
2019-06-18 23:59:40 +00:00
// Clouds
// vec3 p = vec3(pos2d + time_of_day * 0.0002, time_of_day * 0.00003);
// sky_top = mix(sky_top, vec3(1) * brightess, pow(noise(p) * 0.8 + noise(p * 3.0) * 0.2, 2.5) * 3.0);
2019-06-15 17:23:01 +00:00
}
if (objects) {
2019-06-18 23:59:40 +00:00
sky_top += sun_light;
2019-06-19 00:15:43 +00:00
sky_middle += sun_light;
2019-06-15 17:23:01 +00:00
}
2019-06-18 23:59:40 +00:00
vec3 sky_color = mix(
mix(
2019-06-19 00:15:43 +00:00
sky_middle,
2019-06-18 23:59:40 +00:00
sky_top,
2019-06-19 00:15:43 +00:00
clamp(dir.z * 1.0, 0, 1)
2019-06-18 23:59:40 +00:00
),
SKY_BOTTOM * brightess,
clamp(-dir.z * 3.0, 0, 1)
);
2019-06-15 17:23:01 +00:00
return sky_color;
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);
}