Fixed water light extinction

This commit is contained in:
Joshua Barretto
2021-10-05 00:01:49 +01:00
parent 96e23ae2d4
commit f13a8a643b
2 changed files with 218 additions and 202 deletions

View File

@ -204,7 +204,7 @@ void main() {
const float R_s2s1 = pow((1.0 - 1.3325) / (1.0 + 1.3325), 2);
const float R_s1s2 = pow((1.3325 - 1.0) / (1.3325 + 1.0), 2);
// float faces_fluid = faces_fluid && f_pos.z <= floor(f_alt);
float fluid_alt = max(f_pos.z + 1, floor(f_alt));
float fluid_alt = max(f_pos.z + 1, floor(f_alt + 1));
float R_s = /*(f_pos.z < f_alt)*/faces_fluid /*&& f_pos.z <= fluid_alt*/ ? mix(R_s2s1 * R_s1s0, R_s1s0, medium.x) : mix(R_s2s0, R_s1s2 * R_s2s0, medium.x);
// vec3 surf_color = /*srgb_to_linear*/(f_col);

View File

@ -1,7 +1,7 @@
use crate::{
all::ForestKind,
sim::{local_cells, Cave, Path, RiverKind, SimChunk, WorldSim},
util::Sampler,
util::{RandomField, Sampler},
IndexRef, CONFIG,
};
use common::{
@ -106,8 +106,9 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
let gradient = sim.get_gradient_approx(chunk_pos);
let lake_width = (TerrainChunkSize::RECT_SIZE.x as f64 * (2.0f64.sqrt())) + 5.0;
let neighbor_river_data = neighbor_river_data.map(|(posj, chunkj, river)| {
let lake_width = (TerrainChunkSize::RECT_SIZE.x as f64 * 2.0f64.sqrt()) + 6.0;
let neighbor_river_data = neighbor_river_data
.map(|(posj, chunkj, river)| {
let kind = match river.river_kind {
Some(kind) => kind,
None => {
@ -140,9 +141,13 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
lake_width
};
let downhill_chunk = sim.get(downhill_pos).expect("How can this not work?");
let coeffs =
river_spline_coeffs(neighbor_wpos, chunkj.river.spline_derivative, downhill_wpos);
let (direction, coeffs, downhill_chunk, river_t, river_pos, river_dist) = match kind {
let coeffs = river_spline_coeffs(
neighbor_wpos,
chunkj.river.spline_derivative,
downhill_wpos,
);
let (direction, coeffs, downhill_chunk, river_t, river_pos, river_dist) = match kind
{
RiverKind::River { .. } => {
if let Some((t, pt, dist)) = quadratic_nearest_point(
&coeffs,
@ -203,8 +208,11 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
neighbor_pass_pos.map(|e| e as f64) + neighbor_coef * 0.5;
let neighbor_pass_pos = neighbor_pass_pos
.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| e / sz as i32);
let coeffs =
river_spline_coeffs(neighbor_wpos, spline_derivative, neighbor_pass_wpos);
let coeffs = river_spline_coeffs(
neighbor_wpos,
spline_derivative,
neighbor_pass_wpos,
);
let direction = neighbor_wpos - neighbor_pass_wpos;
// Lakes get a special distance function to avoid cookie-cutter edges
@ -219,7 +227,8 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
.get((wposf.map(|e| e as f64).div(32.0)).into_array());
let water_aabr = Aabr {
min: water_chunk * neighbor_coef + 4.0 - lake_width_noise * 8.0,
max: (water_chunk + 1.0) * neighbor_coef - 4.0 + lake_width_noise * 8.0,
max: (water_chunk + 1.0) * neighbor_coef - 4.0
+ lake_width_noise * 8.0,
};
let pos = water_aabr.projected_point(wposf);
(
@ -282,8 +291,9 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
)
},
};
let river_width_max =
if let Some(RiverKind::River { cross_section }) = downhill_chunk.river.river_kind {
let river_width_max = if let Some(RiverKind::River { cross_section }) =
downhill_chunk.river.river_kind
{
// Harmless hack that prevents a river growing wildly outside its bounds to
// create water walls
(cross_section.x as f64).min(river_width_min * 1.75)
@ -294,7 +304,8 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
// water walls
lake_width * 0.5
};
let river_width_noise = (sim.gen_ctx.small_nz.get((river_pos.div(16.0)).into_array()))
let river_width_noise =
(sim.gen_ctx.small_nz.get((river_pos.div(16.0)).into_array()))
.max(-1.0)
.min(1.0)
.mul(0.5)
@ -324,7 +335,8 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
(river_t, (river_pos, coeffs), downhill_chunk),
)),
)
});
})
.collect::<Vec<_>>();
debug_assert!(sim_chunk.water_alt >= CONFIG.sea_level);
@ -397,7 +409,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
// reflect that. Additionally, the river must experience a rapid
// change in elevation. Pooling into a lake produces a rapid.
// TODO: Find a better way to produce rapids along the course of a river?
(chunk_pos.sum() as u32 % 19 < 2
(RandomField::new(3119).chance(chunk_pos.with_z(0), 0.1)
|| matches!(
downhill_chunk.river.river_kind,
Some(RiverKind::Lake { .. })
@ -418,7 +430,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
}
// Use this to temporarily alter the sea level
let base_sea_level = CONFIG.sea_level + 0.01;
let base_sea_level = CONFIG.sea_level - 1.0 + 0.01;
// What's going on here?
//
@ -440,7 +452,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
// they do not result in artifacts, even in edge cases. The exact configuration
// of this code is the product of hundreds of hours of testing and
// refinement and I ask that you do not take that effort lightly.
let (river_water_level, in_river, lake_water_level, lake_dist, water_dist, unbounded_water_level) = neighbor_river_data.clone().fold(
let (river_water_level, in_river, lake_water_level, lake_dist, water_dist, unbounded_water_level) = neighbor_river_data.iter().copied().fold(
(
WeightedSum::default().with_max(base_sea_level),
false,
@ -566,7 +578,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
// has been carefully designed to handle innumeral edge cases. Please
// test any changes to this code extremely well to avoid regressions: some
// edge cases are very rare indeed!
let alt = neighbor_river_data.clone().fold(
let alt = neighbor_river_data.into_iter().fold(
WeightedSum::default().with(riverless_alt, 1.0),
|alt, (river_chunk_idx, river_chunk, river, dist_info)| match (
river.river_kind,
@ -593,12 +605,16 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
Some((river_water_alt, cross_section.y as f32, None))
},
RiverKind::Lake { .. } | RiverKind::Ocean => {
let lake_water_alt = river_water_alt(
let lake_water_alt = if matches!(kind, RiverKind::Ocean) {
base_sea_level
} else {
river_water_alt(
river_chunk.alt.max(river_chunk.water_alt),
downhill_chunk.alt.max(downhill_chunk.water_alt),
river_t as f32,
is_waterfall(river_chunk_idx, river_chunk, downhill_chunk),
);
)
};
let depth = water_level
- Lerp::lerp(
@ -630,7 +646,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
if let Some((water_alt, water_depth, min_alt)) = water_alt {
if river_edge_dist <= 0.0 {
const MIN_DEPTH: f32 = 1.0;
let near_centre = ((river_dist / (river_width * 0.5)) as f32)
let near_center = ((river_dist / (river_width * 0.5)) as f32)
.min(1.0)
.mul(f32::consts::PI)
.cos()
@ -652,14 +668,14 @@ impl<'a> Sampler<'a> for ColumnGen<'a> {
/ TerrainChunkSize::RECT_SIZE.x as f32
};
let riverbed_depth =
near_centre * water_depth + MIN_DEPTH + waterfall_boost;
near_center * water_depth + MIN_DEPTH + waterfall_boost;
// Handle rivers debouching into the ocean nicely by 'flattening' their
// bottom
let riverbed_alt = (water_alt - riverbed_depth)
.max(riverless_alt.min(CONFIG.sea_level - MIN_DEPTH));
.max(riverless_alt.min(base_sea_level - MIN_DEPTH));
alt.with(
min_alt.unwrap_or(riverbed_alt).min(riverbed_alt),
near_centre * BANK_STRENGTH,
near_center * BANK_STRENGTH,
)
.with_min(min_alt.unwrap_or(riverbed_alt).min(riverbed_alt))
} else {