Added rivers, improved mountains

Former-commit-id: c1b76c54d2aa8f0616c26b0724fe34c0d80ac4fd
This commit is contained in:
Joshua Barretto 2019-05-20 16:01:27 +01:00
parent a0dfa3fddc
commit 5118da0e0e
2 changed files with 68 additions and 27 deletions

View File

@ -1,7 +1,7 @@
mod sim;
use std::{
ops::{Add, Neg},
ops::{Add, Mul, Neg},
time::Duration,
};
use noise::{NoiseFn, Perlin, Seedable};
@ -54,12 +54,22 @@ impl World {
let wpos2d = Vec2::new(x, y) + Vec3::from(chunk_pos) * TerrainChunkSize::SIZE.map(|e| e as i32);
let wposf2d = wpos2d.map(|e| e as f64);
let max_z = self.sim
.get_interpolated(wpos2d, |chunk| chunk.get_max_z())
.unwrap_or(0.0) as i32;
let chaos = self.sim
.get_interpolated(wpos2d, |chunk| chunk.chaos)
.unwrap_or(0.0);
let ridge_freq = 1.0 / 128.0;
let ridge_ampl = 96.0;
let damp = self.sim
.get_interpolated(wpos2d, |chunk| chunk.damp)
.unwrap_or(0.0)
.abs().neg().add(1.0)
.mul(1.0 - chaos);
let ridge_freq = 1.0 / 125.0;
let ridge_ampl = 125.0;
let ridge = ridge_nz
.get((wposf2d * ridge_freq).into_array()).abs().neg().add(1.0) as f32 * ridge_ampl * chaos.powf(8.0);
@ -67,19 +77,20 @@ impl World {
let height_z = self.sim
.get_interpolated(wpos2d, |chunk| chunk.alt)
.unwrap_or(0.0)
+ ridge;
+ ridge
- damp.powf(20.0) * 32.0;
for z in base_z..base_z + 256 {
for z in base_z..max_z {
let lpos = Vec3::new(x, y, z);
let wpos = lpos
+ Vec3::from(chunk_pos) * TerrainChunkSize::SIZE.map(|e| e as i32);
let wposf = wpos.map(|e| e as f64);
let warp_freq = 1.0 / 48.0;
let warp_ampl = 24.0;
let warp_freq = 1.0 / 40.0;
let warp_ampl = 45.0;
let height = height_z
+ warp_nz.get((wposf * warp_freq).into_array()) as f32 * warp_ampl * (chaos + 0.05);
+ warp_nz.get((wposf * warp_freq).into_array()) as f32 * warp_ampl * (chaos + 0.05).min(0.85);
let temp =
(temp_nz.get(Vec2::from(wposf * (1.0 / 64.0)).into_array()) as f32 + 1.0) * 0.5;

View File

@ -1,8 +1,8 @@
use std::{
ops::{Add, Mul, Div},
ops::{Add, Mul, Div, Neg},
f32,
};
use noise::{NoiseFn, OpenSimplex, Seedable};
use noise::{NoiseFn, SuperSimplex, OpenSimplex, Seedable};
use vek::*;
use common::{
terrain::TerrainChunkSize,
@ -19,10 +19,14 @@ pub struct WorldSim {
impl WorldSim {
pub fn generate(seed: u32) -> Self {
let mut gen_ctx = GenCtx {
alt_nz: OpenSimplex::new()
base_nz: OpenSimplex::new()
.set_seed(seed + 0),
chaos_nz: OpenSimplex::new()
damp_nz: SuperSimplex::new()
.set_seed(seed + 1),
chaos_nz: SuperSimplex::new()
.set_seed(seed + 2),
alt_nz: OpenSimplex::new()
.set_seed(seed + 3),
};
let mut chunks = Vec::new();
@ -60,22 +64,26 @@ impl WorldSim {
.fold(None, |a: Option<f32>, x| a.map(|a| a.min(x)).or(Some(x))))
}
pub fn get_interpolated<F: FnMut(&SimChunk) -> f32>(&self, pos: Vec2<i32>, mut f: F) -> Option<f32> {
pub fn get_interpolated<T, F>(&self, pos: Vec2<i32>, mut f: F) -> Option<T>
where
T: Copy + Default + Add<Output=T> + Mul<f32, Output=T>,
F: FnMut(&SimChunk) -> T,
{
let pos = pos.map2(TerrainChunkSize::SIZE.into(), |e, sz: u32| e as f64 / sz as f64);
fn cubic(a: f32, b: f32, c: f32, d: f32, x: f32) -> f32 {
let cubic = |a: T, b: T, c: T, d: T, x: f32| -> T {
let x2 = x * x;
// Catmull-Rom splines
let co0 = -0.5 * a + 1.5 * b - 1.5 * c + 0.5 * d;
let co1 = a - 2.5 * b + 2.0 * c - 0.5 * d;
let co2 = -0.5 * a + 0.5 * c;
let co0 = a * -0.5 + b * 1.5 + c * -1.5 + d * 0.5;
let co1 = a + b * -2.5 + c * 2.0 + d * -0.5;
let co2 = a * -0.5 + c * 0.5;
let co3 = b;
co0 * x2 * x + co1 * x2 + co2 * x + co3
}
};
let mut y = [0.0; 4];
let mut y = [T::default(); 4];
for (y_idx, j) in (-1..3).enumerate() {
let x0 = f(self.get(pos.map2(Vec2::new(-1, j), |e, q| (e.max(0.0) as i32 + q) as u32))?);
@ -98,11 +106,16 @@ impl WorldSim {
}
struct GenCtx {
chaos_nz: OpenSimplex,
base_nz: OpenSimplex,
damp_nz: SuperSimplex,
chaos_nz: SuperSimplex,
alt_nz: OpenSimplex,
}
const Z_TOLERANCE: f32 = 32.0;
pub struct SimChunk {
pub damp: f32,
pub chaos: f32,
pub alt: f32,
}
@ -111,16 +124,31 @@ impl SimChunk {
fn generate(pos: Vec2<u32>, gen_ctx: &mut GenCtx) -> Self {
let wposf = (pos * Vec2::from(TerrainChunkSize::SIZE)).map(|e| e as f64);
let base = (gen_ctx.base_nz.get((wposf.div(6000.0)).into_array()) as f32)
.add(1.0).mul(0.5)
.mul(100.0)
.add(32.0);
let damp = (0.0
+ gen_ctx.damp_nz.get((wposf.div(2000.0)).into_array())
+ gen_ctx.damp_nz.get((wposf.div(250.0)).into_array()) * 0.25
) as f32;
let chaos = (gen_ctx.chaos_nz
.get((wposf.div(400.0)).into_array()) as f32)
.max(0.0)
.get((wposf.div(1000.0)).into_array()) as f32)
.add(1.0).mul(0.5)
.mul(damp.max(0.0))
.add(0.15)
.powf(2.0);
.powf(2.0)
.min(1.0);
Self {
damp,
chaos,
alt: (gen_ctx.alt_nz
.get((wposf.div(125.0)).into_array()) as f32)
alt: base + ((0.0
+ gen_ctx.alt_nz.get((wposf.div(650.0)).into_array()) * 0.7
+ gen_ctx.alt_nz.get((wposf.div(100.0)).into_array()) * 0.3
) as f32)
.add(1.0).mul(0.5)
.mul(750.0)
.mul(chaos.max(0.05)),
@ -128,8 +156,10 @@ impl SimChunk {
}
pub fn get_base_z(&self) -> f32 {
const BASE_Z_TOLERANCE: f32 = 32.0;
self.alt - Z_TOLERANCE
}
self.alt - BASE_Z_TOLERANCE
pub fn get_max_z(&self) -> f32 {
self.alt + Z_TOLERANCE
}
}