Use poisson disk distribution for point generation

This commit is contained in:
Niklas Korz 2025-03-31 19:59:39 +02:00
parent aae2ecef85
commit 542708b439
3 changed files with 217 additions and 28 deletions

View file

@ -4,13 +4,11 @@ use bevy_vello::{
prelude::*,
vello::kurbo::{PathEl, Point},
};
use fast_poisson::Poisson2D;
use noise::{NoiseFn, Simplex};
use rand::Rng;
use std::ops::DerefMut;
use std::{ops::DerefMut, time::SystemTime};
use voronator::VoronoiDiagram;
const JITTER: f64 = 0.5;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
@ -28,34 +26,27 @@ fn setup_vector_graphics(mut commands: Commands) {
fn draw_map(mut query_scene: Single<(&mut Transform, &mut VelloScene)>) {
let (transform, scene) = query_scene.deref_mut();
scene.reset();
transform.scale = Vec3::ONE * 30.0;
let grid_size_x = 40;
let grid_max_x = grid_size_x / 2;
let grid_min_x = -grid_max_x;
let grid_size_x = 20.0;
let grid_size_y = 10.0;
let grid_size_y = 20;
let grid_max_y = grid_size_y / 2;
let grid_min_y = -grid_max_y;
let mut rng = rand::rng();
let mut points = Vec::new();
for x in grid_min_x..=grid_max_x {
for y in grid_min_y..=grid_max_y {
points.push((
(x as f64) + rng.random_range(-JITTER..=JITTER),
(y as f64) + rng.random_range(-JITTER..=JITTER),
));
}
}
let points: Vec<_> = Poisson2D::new()
.with_dimensions([grid_size_x, grid_size_y], 0.2)
.iter()
.map(|[x, y]| (x, y))
.collect();
let diagram = VoronoiDiagram::<voronator::delaunator::Point>::from_tuple(
&(grid_min_x as f64, grid_min_y as f64),
&(grid_max_x as f64, grid_max_y as f64),
&(0.0, 0.0),
&(grid_size_x, grid_size_y),
&points,
)
.unwrap();
let noise = Simplex::new(rng.random());
let seed = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as u32;
let noise = Simplex::new(seed);
for cell in diagram.cells() {
let mut first = true;
let mut path = Vec::new();
@ -73,8 +64,8 @@ fn draw_map(mut query_scene: Single<(&mut Transform, &mut VelloScene)>) {
}
}
let nx = (center.x + grid_max_x as f64) / (grid_size_x as f64) - 0.5;
let ny = (center.y + grid_max_y as f64) / (grid_size_y as f64) - 0.5;
let nx = center.x / (grid_size_x as f64) - 0.5;
let ny = center.y / (grid_size_y as f64) - 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());
@ -93,4 +84,9 @@ fn draw_map(mut query_scene: Single<(&mut Transform, &mut VelloScene)>) {
&kurbo::BezPath::from_vec(path),
);
}
let scale = 60.0;
transform.scale = Vec3::ONE * scale;
transform.translation =
Vec3::new(-(grid_size_x as f32 / 2.0), grid_size_y as f32 / 2.0, 0.0) * scale;
}