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

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),
);