Simplex noise for elevation

This commit is contained in:
Niklas Korz 2025-03-31 18:27:54 +02:00
parent ffbdb33659
commit a51fed5026
3 changed files with 49 additions and 6 deletions

21
Cargo.lock generated
View file

@ -2876,6 +2876,17 @@ dependencies = [
"libc",
]
[[package]]
name = "noise"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6da45c8333f2e152fc665d78a380be060eb84fad8ca4c9f7ac8ca29216cff0cc"
dependencies = [
"num-traits",
"rand 0.8.5",
"rand_xorshift",
]
[[package]]
name = "nom"
version = "7.1.3"
@ -2898,6 +2909,7 @@ version = "0.1.0"
dependencies = [
"bevy",
"bevy_vello",
"noise",
"rand 0.9.0",
"voronator",
]
@ -3549,6 +3561,15 @@ dependencies = [
"rand 0.8.5",
]
[[package]]
name = "rand_xorshift"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f"
dependencies = [
"rand_core 0.6.4",
]
[[package]]
name = "range-alloc"
version = "0.1.4"

View file

@ -14,5 +14,6 @@ opt-level = 3
[dependencies]
bevy = "0.15.3"
bevy_vello = "0.7.1"
noise = "0.9.0"
rand = "0.9.0"
voronator = "0.2.1"

View file

@ -4,9 +4,10 @@ use bevy_vello::{
prelude::*,
vello::kurbo::{PathEl, Point},
};
use noise::{NoiseFn, Simplex};
use rand::Rng;
use voronator::VoronoiDiagram;
use std::ops::DerefMut;
use voronator::VoronoiDiagram;
const JITTER: f64 = 0.5;
@ -39,13 +40,23 @@ fn draw_map(mut query_scene: Single<(&mut Transform, &mut VelloScene)>) {
));
}
}
let diagram = VoronoiDiagram::<voronator::delaunator::Point>::from_tuple(&(-20.0, -10.0), &(20.0, 10.0), &points).unwrap();
let diagram = VoronoiDiagram::<voronator::delaunator::Point>::from_tuple(
&(-20.0, -10.0),
&(20.0, 10.0),
&points,
)
.unwrap();
let noise = Simplex::new(rng.random());
for cell in diagram.cells() {
let mut first = true;
let mut path = Vec::new();
let mut center = Point::new(0.0, 0.0);
let points = cell.points().len() as f64;
for point in cell.points() {
let point = Point::new(point.x, point.y);
center.x += point.x / points;
center.y += point.y / points;
if first {
first = false;
path.push(PathEl::MoveTo(point));
@ -53,13 +64,23 @@ fn draw_map(mut query_scene: Single<(&mut Transform, &mut VelloScene)>) {
path.push(PathEl::LineTo(point));
}
}
let r = rng.random_range(0..=255);
let g = rng.random_range(0..=255);
let b = rng.random_range(0..=255);
let nx = (center.x + 20.0) / 40.0 - 0.5;
let ny = (center.y + 10.0) / 20.0 - 0.5;
let elevation = (1.0 + noise.get([nx / 0.5, ny / 0.5])) / 2.0;
let d = 2.0 * nx.abs().max(ny.abs());
let elevation = (1.0 + elevation - d) / 2.0;
let color = if elevation < 0.5 {
peniko::Color::from_rgb8(89, 89, 166)
} else {
peniko::Color::from_rgb8(128, 153, 102)
};
scene.fill(
peniko::Fill::NonZero,
kurbo::Affine::default(),
peniko::Color::from_rgb8(r, g, b),
color,
None,
&kurbo::BezPath::from_vec(path),
);