AO improvements, day/night cycle

This commit is contained in:
Joshua Barretto 2019-06-19 00:59:40 +01:00
parent d10ef37528
commit 5e96e29ff1
6 changed files with 85 additions and 70 deletions

View File

@ -32,13 +32,8 @@ void main() {
vec4(f_norm, 0.0)
).xyz;
float ambient = 0.5;
vec3 sun_dir = normalize(vec3(1.3, 1.7, 1.1));
float sun_diffuse = dot(sun_dir, world_norm) * 0.5;
vec3 surf_color = model_col.rgb * f_col * (ambient + sun_diffuse);
float light = get_sun_diffuse(world_norm, time_of_day.x);
vec3 surf_color = model_col.rgb * f_col * 2.0 * light;
float fog_level = fog(f_pos.xy, focus_pos.xy);
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x);

View File

@ -26,41 +26,73 @@ float noise(vec3 p){
return o4.y * d.y + o4.x * (1.0 - d.y);
}
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;
}
vec3 get_sky_color(vec3 dir, float time_of_day) {
bool objects = true;
const float TIME_FACTOR = (PI * 2.0) / (3600.0 * 24.0);
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);
const vec3 SUN_HALO_COLOR = vec3(1.0, 0.7, 0.5) * 0.5;
const vec3 SUN_HALO_COLOR = vec3(1.0, 0.4, 0.3) * 0.5;
const vec3 SUN_SURF_COLOR = vec3(1.0, 0.9, 0.35) * 200.0;
float sun_angle_rad = time_of_day * TIME_FACTOR;
vec3 sun_dir = vec3(sin(sun_angle_rad), 0.0, cos(sun_angle_rad));
vec3 sun_dir = get_sun_dir(time_of_day);
float sky_brightness = get_sun_brightness(sun_dir);
vec3 sun_halo = pow(max(dot(dir, sun_dir), 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_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;
vec3 sky_top;
float brightess = (sky_brightness + PERSISTENT_AMBIANCE);
vec3 sky_top = SKY_TOP * brightess;
if (objects) {
vec3 p = vec3(pos2d + time_of_day * 0.0002, time_of_day * 0.0001);
sky_top = mix(SKY_TOP, vec3(1), pow(noise(p) * 0.8 + noise(p * 3.0) * 0.2, 2.5) * 3.0);
} else {
sky_top = SKY_TOP;
// 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);
}
vec3 sky_color = mix(mix(SKY_MIDDLE, sky_top, clamp(dir.z, 0, 1)), SKY_BOTTOM, clamp(-dir.z * 5.0, 0, 1));
if (objects) {
sky_color += sun_light;
sky_top += sun_light;
}
vec3 sky_color = mix(
mix(
SKY_MIDDLE * brightess,
sky_top,
clamp(dir.z * 3.0, 0, 1)
),
SKY_BOTTOM * brightess,
clamp(-dir.z * 3.0, 0, 1)
);
return sky_color;
}

View File

@ -28,19 +28,7 @@ void main() {
f_norm = vec3(0.0, 0.0, 1.0) * norm_dir;
}
float glob_ambience = 0.0005;
float sun_ambience = 0.3;
vec3 sun_dir = normalize(vec3(0.7, 1.3, 2.1));
float sun_diffuse = max(dot(sun_dir, f_norm), 0.0);
float sun_light = sun_ambience + sun_diffuse;
float static_light = glob_ambience + min(sun_light, f_light);
vec3 light = vec3(static_light);
float light = get_sun_diffuse(f_norm, time_of_day.x) * f_light;
vec3 surf_color = f_col * light;
float fog_level = fog(f_pos.xy, focus_pos.xy);

View File

@ -56,6 +56,35 @@ impl<V: BaseVol<Vox = Block> + ReadVol + Debug, S: VolSize + Clone> Meshable for
for z in (range.min.z..range.max.z).rev() {
let pos = Vec3::new(x, y, z);
// Create mesh polygons
if let Some(col) = self.get(pos).ok().and_then(|vox| vox.get_color()) {
let avg_light = neighbour_light
.iter()
.map(|row| row.iter())
.flatten()
.map(|col| col.iter())
.flatten()
.fold(0.0, |a, x| a + x)
/ 27.0;
let light = avg_light;
let col = col.map(|e| e as f32 / 255.0);
let offs = (pos - range.min * Vec3::new(1, 1, 0)).map(|e| e as f32)
- Vec3::new(1.0, 1.0, 0.0);
vol::push_vox_verts(
&mut mesh,
self,
pos,
offs,
col,
|pos, norm, col, ao, light| TerrainVertex::new(pos, norm, Lerp::lerp(Rgb::zero(), col, ao), light),
false,
&neighbour_light,
);
}
// Shift lighting
neighbour_light[2] = neighbour_light[1];
neighbour_light[1] = neighbour_light[0];
@ -82,35 +111,6 @@ impl<V: BaseVol<Vox = Block> + ReadVol + Debug, S: VolSize + Clone> Meshable for
.flatten()
.copied()
.fold(0.0, |a, x| a + x) / 9.0; 3]; 3];
// Create mesh polygons
if let Some(col) = self.get(pos).ok().and_then(|vox| vox.get_color()) {
let avg_light = neighbour_light
.iter()
.map(|row| row.iter())
.flatten()
.map(|col| col.iter())
.flatten()
.fold(0.0, |a, x| a + x)
/ 27.0;
let light = avg_light;
let col = col.map(|e| e as f32 / 255.0);
let offs = (pos - range.min * Vec3::new(1, 1, 0)).map(|e| e as f32)
- Vec3::new(1.0, 1.0, 0.0);
vol::push_vox_verts(
&mut mesh,
self,
pos,
offs,
col,
|pos, norm, col, ao, light| TerrainVertex::new(pos, norm, col * ao, light),
false,
&neighbour_light,
);
}
}
}
}

View File

@ -66,7 +66,7 @@ fn create_quad<P: Pipeline, F: Fn(Vec3<f32>, Vec3<f32>, Rgb<f32>, f32, f32) -> P
let darkness = darkness_ao.map(|e| e.0);
let ao = darkness_ao.map(|e| e.1);
let ao_map = ao;//ao.map(|e| 0.2 + e.powf(1.0) * 0.8);
let ao_map = ao.map(|e| 0.05 + e.powf(1.6) * 0.95);
if ao[0].min(ao[2]) < ao[1].min(ao[3]) {
Quad::new(

View File

@ -55,12 +55,12 @@ fn generate_name<R: Rng>(rng: &mut R) -> String {
];
let mut name = String::new();
for i in 0..rand::random::<u32>() % 2 {
name += rand::thread_rng().choose(&consts).unwrap();
name += rand::thread_rng().choose(&vowels).unwrap();
for i in 0..rng.gen::<u32>() % 2 {
name += rng.choose(&consts).unwrap();
name += rng.choose(&vowels).unwrap();
}
name += rand::thread_rng().choose(&consts).unwrap();
name += rand::thread_rng().choose(&tails).unwrap();
name += rng.choose(&consts).unwrap();
name += rng.choose(&tails).unwrap();
name
}