Skip to content

Commit

Permalink
edit: night shadows adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sufhal committed Sep 16, 2024
1 parent 3564611 commit e48f9e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/modules/core/directional_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,18 @@ impl DirectionalLight {
}

pub fn update(&mut self, camera_view_proj: Matrix4<f32>, terrain: &Terrain) {
let [x, y, z, _] = terrain.environment.sun.uniform.sun_position;
let source = Vector3::from([x, y, z]);
let mut center = Vector3::from(terrain.center);
center.y -= 200.0; // this helps to have less stretched shadows when the sun is low
self.direction = (center - source).normalize();
let source = if terrain.environment.cycle.day_factor > 0.0 {
let [x, y, z, _] = terrain.environment.sun.uniform.sun_position;
Vector3::from([x, y, z])
} else {
let [x, y, z, _] = terrain.environment.sun.uniform.moon_position;
Vector3::from([x, y, z])
};
self.direction = {
let mut center = Vector3::from(terrain.center);
center.y -= 100.0; // this helps to have less stretched shadows when the sun is low
(center - source).normalize()
};
for i in 0..3 {
let near = self.cascade_splits[i];
let far = self.cascade_splits[i + 1];
Expand Down
10 changes: 10 additions & 0 deletions src/shaders/terrain.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,21 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
var shadow_strength = 0.0;

if cycle.day_factor >= SHADOW_START && cycle.day_factor < SHADOW_ZENITH {
// morning
shadow_strength = ease_out_expo(normalize_value_between(cycle.day_factor, SHADOW_START, SHADOW_ZENITH));
}
else if cycle.day_factor >= SHADOW_ZENITH && cycle.day_factor <= SHADOW_END {
// afternoon
shadow_strength = ease_out_expo(1.0 - normalize_value_between(cycle.day_factor, SHADOW_ZENITH, SHADOW_END));
}
if cycle.night_factor > 0.0 && cycle.night_factor < SHADOW_ZENITH {
// early night
shadow_strength = ease_out_expo(normalize_value_between(cycle.night_factor, 0.0, SHADOW_ZENITH)) * 0.75;
}
else if cycle.night_factor >= SHADOW_ZENITH && cycle.night_factor <= 1.0 {
// late night
shadow_strength = ease_out_expo(1.0 - normalize_value_between(cycle.night_factor, SHADOW_ZENITH, 1.0)) * 0.75;
}
shadow = mix(1.0, shadow, shadow_strength);
final_color *= shadow;

Expand Down

0 comments on commit e48f9e1

Please sign in to comment.