New modular biomes system and updated world generator

This commit is contained in:
2024-07-06 20:41:50 -04:00
parent e6e969f053
commit 97f2497940
16 changed files with 341 additions and 190 deletions

View File

@@ -1,7 +1,7 @@
use asset_loader::create_asset_loader;
use serde::{Deserialize, Serialize};
use crate::{prelude::GeneratorLayer, tile_mapper::TileMapperAsset};
use crate::{prelude::NoiseConfig, tile_mapper::TileMapperAsset};
#[derive(Serialize, Deserialize, Asset, TypePath, Debug, Clone)]
pub struct BiomeAsset {
@@ -12,17 +12,13 @@ pub struct BiomeAsset {
#[serde(skip)]
pub tile_mapper: Handle<TileMapperAsset>,
pub tile_mapper_path: String,
pub generator_layers: Vec<GeneratorLayer>,
pub noise: NoiseConfig,
}
impl BiomeAsset {
pub fn distance(&self, moisture: f32, temperature: f32, continentality: f32) -> f32 {
let a = Vec3::new(
self.moisture - moisture,
self.temperature - temperature,
self.continentality - continentality,
);
return a.length();
pub fn distance(&self, data: Vec3) -> f32 {
let a = Vec3::new(self.moisture, self.temperature, self.continentality);
return (a - data).length();
}
}

View File

@@ -6,7 +6,7 @@ use bevy::{
};
use serde::{Deserialize, Serialize};
use crate::biome_asset::BiomeAsset;
use crate::{biome_asset::BiomeAsset, map::biome_map::BiomeData};
#[derive(Serialize, Deserialize, Debug, TypePath, Asset, Clone)]
pub struct BiomePainterAsset {
@@ -16,20 +16,14 @@ pub struct BiomePainterAsset {
}
impl BiomePainterAsset {
pub fn sample_biome(
&self,
assets: &Assets<BiomeAsset>,
moisture: f32,
temperature: f32,
continentality: f32,
) -> Handle<BiomeAsset> {
pub fn sample_biome(&self, assets: &Assets<BiomeAsset>, data: &BiomeData) -> Handle<BiomeAsset> {
assert!(self.biomes.length() != 0, "There are no biomes");
let mut biome = self.biomes.first().unwrap().clone();
let mut dist = f32::INFINITY;
for b in &self.biomes {
let asset = assets.get(b).unwrap();
let d = asset.distance(moisture, temperature, continentality);
let d = asset.distance(data.into());
if d < dist {
biome = b.clone();
dist = d;
@@ -38,6 +32,38 @@ impl BiomePainterAsset {
return biome;
}
pub fn build(&self, assets: &Assets<BiomeAsset>) -> BiomePainter {
let mut biomes = Vec::with_capacity(self.biomes.len());
for b in &self.biomes {
let asset = assets.get(b.clone()).unwrap();
biomes.push(asset.clone());
}
return BiomePainter { biomes };
}
}
#[derive(Resource)]
pub struct BiomePainter {
pub biomes: Vec<BiomeAsset>,
}
impl BiomePainter {
pub fn sample_biome(&self, data: &BiomeData) -> &BiomeAsset {
assert!(self.biomes.length() != 0, "There are no biomes");
let mut biome = &self.biomes[0];
let mut dist = f32::INFINITY;
for b in &self.biomes {
let d = b.distance(data.into());
if d < dist {
biome = b;
dist = d;
}
}
return biome;
}
}
create_asset_loader!(

View File

@@ -27,14 +27,11 @@ pub fn generate_packed_chunk_mesh(
for z in 0..Chunk::SIZE {
for x in 0..Chunk::SIZE {
let height = chunk.heights[x + z * Chunk::SIZE];
let moisture = chunk.moisture[x + z * Chunk::SIZE];
let temperature = chunk.temperature[x + z * Chunk::SIZE];
let data = chunk.biome_data[x + z * Chunk::SIZE];
let coord =
HexCoord::from_offset(IVec2::new(x as i32, z as i32) + (chunk.chunk_offset * Chunk::SIZE as i32));
let n = map.get_neighbors(&coord);
let biome = biomes
.get(painter.sample_biome(biomes, moisture, temperature, 1.))
.unwrap();
let biome = biomes.get(painter.sample_biome(biomes, &data)).unwrap();
let mapper = mappers.get(biome.tile_mapper.clone());
let tile_handle = mapper.unwrap().sample_tile(height);

View File

@@ -1,19 +1,24 @@
use bevy::math::IVec2;
use bevy::prelude::{FloatExt, Vec2};
use bevy::utils::default;
use bevy::utils::petgraph::data;
use noise::{NoiseFn, SuperSimplex};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use crate::biome_painter::{self, BiomePainter};
use crate::map::biome_map::{self, BiomeChunk, BiomeData, BiomeMap};
use crate::prelude::*;
pub fn generate_heightmap(cfg: &GenerationConfig, seed: u32) -> Map {
pub fn generate_heightmap(cfg: &GenerationConfig, seed: u32, painter: &BiomePainter) -> Map {
let biomes = &generate_biomes(cfg, seed);
// let mut chunks: Vec<Chunk> = Vec::with_capacity(cfg.size.length_squared() as usize);
let chunks = (0..cfg.size.y)
.into_par_iter()
.flat_map(|z| {
(0..cfg.size.x)
.into_par_iter()
.map(move |x| generate_chunk(x as f64, z as f64, cfg, seed))
(0..cfg.size.x).into_par_iter().map(move |x| {
let biome_chunk = &biomes.chunks[x as usize + z as usize * cfg.size.x as usize];
return generate_chunk(x, z, cfg, seed, &biome_chunk, painter);
})
})
.collect();
return Map {
@@ -24,49 +29,104 @@ pub fn generate_heightmap(cfg: &GenerationConfig, seed: u32) -> Map {
};
}
pub fn generate_chunk(chunk_x: f64, chunk_z: f64, cfg: &GenerationConfig, seed: u32) -> Chunk {
pub fn generate_biomes(cfg: &GenerationConfig, seed: u32) -> BiomeMap {
let mut map = BiomeMap::new(cfg.size, 8);
map.chunks = (0..cfg.size.y)
.into_par_iter()
.flat_map(|y| {
(0..cfg.size.x).into_par_iter().map(move |x| {
return generate_biome_chunk(x as usize, y as usize, cfg, seed);
})
})
.collect();
map.blend(cfg.biome_blend);
return map;
}
pub fn generate_biome_chunk(chunk_x: usize, chunk_y: usize, cfg: &GenerationConfig, seed: u32) -> BiomeChunk {
let mut chunk = BiomeChunk {
data: [BiomeData::default(); Chunk::AREA],
tiles: Vec::with_capacity(Chunk::AREA),
};
let noise_m = SuperSimplex::new(seed + 1);
let noise_t = SuperSimplex::new(seed + 2);
let noise_c = SuperSimplex::new(seed + 3);
for z in 0..Chunk::SIZE {
for x in 0..Chunk::SIZE {
let moisture = sample_point(
x as f64 + chunk_x as f64 * Chunk::SIZE as f64,
z as f64 + chunk_y as f64 * Chunk::SIZE as f64,
&cfg.moisture_layer,
&noise_m,
cfg.size.as_vec2(),
cfg.border_size,
);
let temperature = sample_point(
x as f64 + chunk_x as f64 * Chunk::SIZE as f64,
z as f64 + chunk_y as f64 * Chunk::SIZE as f64,
&cfg.temperature_layer,
&noise_t,
cfg.size.as_vec2(),
cfg.border_size,
);
let continentality = sample_point(
x as f64 + chunk_x as f64 * Chunk::SIZE as f64,
z as f64 + chunk_y as f64 * Chunk::SIZE as f64,
&cfg.continent_layer,
&noise_c,
cfg.size.as_vec2(),
cfg.border_size,
);
chunk.data[x + z * Chunk::SIZE] = BiomeData {
moisture,
temperature,
continentality,
};
}
}
return chunk;
}
pub fn generate_chunk(
chunk_x: u32,
chunk_z: u32,
cfg: &GenerationConfig,
seed: u32,
biome_chunk: &BiomeChunk,
biome_painter: &BiomePainter,
) -> Chunk {
let mut result: [f32; Chunk::SIZE * Chunk::SIZE] = [0.; Chunk::SIZE * Chunk::SIZE];
let mut moisture = [0.; Chunk::SIZE * Chunk::SIZE];
let mut temp = [0.; Chunk::SIZE * Chunk::SIZE];
let mut data = [BiomeData::default(); Chunk::SIZE * Chunk::SIZE];
let noise = SuperSimplex::new(seed);
for z in 0..Chunk::SIZE {
for x in 0..Chunk::SIZE {
let biome_data = biome_chunk.get_biome_data(x, z);
let biome = biome_painter.sample_biome(biome_data);
let sample = sample_point(
x as f64 + chunk_x * Chunk::SIZE as f64,
z as f64 + chunk_z * Chunk::SIZE as f64,
&cfg,
x as f64 + chunk_x as f64 * Chunk::SIZE as f64,
z as f64 + chunk_z as f64 * Chunk::SIZE as f64,
&biome.noise,
&noise,
cfg.size.as_vec2(),
cfg.border_size,
);
result[x + z * Chunk::SIZE] = sample;
moisture[x + z * Chunk::SIZE] = noise.get([
(x as f64 + chunk_x * Chunk::SIZE as f64) / &cfg.noise_scale,
(z as f64 + chunk_z * Chunk::SIZE as f64) / &cfg.noise_scale,
]) as f32;
temp[x + z * Chunk::SIZE] =
sample_tempurature(z as f32 + chunk_z as f32 * Chunk::SIZE as f32, sample, &cfg, 100.);
data[x + z * Chunk::SIZE] = biome_data.clone();
}
}
return Chunk {
heights: result,
moisture: moisture,
temperature: temp,
biome_data: data,
chunk_offset: IVec2::new(chunk_x as i32, chunk_z as i32),
..default()
};
}
fn sample_tempurature(z: f32, height: f32, cfg: &GenerationConfig, equator: f32) -> f32 {
let d = (equator - z).abs();
let max_d = equator.max(cfg.get_total_height() as f32 - equator);
let t_mod = d.remap(0., max_d, 0., 1.).clamp(0., 1.);
// let max_d = d.max()
return (height.remap(0., 50., 0., 1.).clamp(0., 1.) + t_mod) / 2.;
}
fn sample_point(x: f64, z: f64, cfg: &GenerationConfig, noise: &impl NoiseFn<f64, 2>) -> f32 {
let x_s = x / cfg.noise_scale;
let z_s = z / cfg.noise_scale;
fn sample_point(x: f64, z: f64, cfg: &NoiseConfig, noise: &impl NoiseFn<f64, 2>, size: Vec2, border_size: f32) -> f32 {
let x_s = x / cfg.scale;
let z_s = z / cfg.scale;
let mut elevation: f64 = 0.;
let mut first_layer: f64 = 0.;
@@ -82,26 +142,25 @@ fn sample_point(x: f64, z: f64, cfg: &GenerationConfig, noise: &impl NoiseFn<f64
first_layer = value;
}
if layer.first_layer_mask {
elevation += mask(first_layer, value, cfg.sea_level);
elevation += mask(first_layer, value);
} else {
elevation += value;
}
}
let outer = cfg.size.as_vec2() * Chunk::SIZE as f32;
let outer = size * Chunk::SIZE as f32;
let p = Vec2::new(x as f32, z as f32);
let d1 = p.x.min(p.y);
let od = outer - p;
let d2 = od.x.min(od.y);
let d = d1.min(d2).min(cfg.border_size).remap(0., cfg.border_size, 0., 1.);
let d = d1.min(d2).min(border_size).remap(0., border_size, 0., 1.);
return (elevation as f32) * d;
}
fn mask(mask: f64, value: f64, sea_level: f64) -> f64 {
let m = (mask - sea_level).max(0.);
return value * m;
fn mask(mask: f64, value: f64) -> f64 {
return value * mask;
}
fn sample_simple(x: f64, z: f64, cfg: &GeneratorLayer, noise: &impl NoiseFn<f64, 2>) -> f64 {

View File

@@ -1,5 +1,5 @@
use bevy::math::UVec2;
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
use bevy::math::{UVec2, Vec3};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use super::chunk::Chunk;
@@ -7,16 +7,36 @@ pub struct BiomeMap {
pub height: usize,
pub width: usize,
pub biome_count: usize,
pub tiles: Vec<Vec<f32>>,
pub chunks: Vec<BiomeChunk>,
}
#[derive(Default, Clone, Copy)]
pub struct BiomeData {
pub moisture: f32,
pub temperature: f32,
pub continentality: f32,
}
impl Into<Vec3> for &BiomeData {
fn into(self) -> Vec3 {
return Vec3::new(self.moisture, self.temperature, self.continentality);
}
}
impl Into<Vec3> for BiomeData {
fn into(self) -> Vec3 {
return Vec3::new(self.moisture, self.temperature, self.continentality);
}
}
impl BiomeMap {
pub fn new(size: UVec2, biome_count: usize) -> Self {
let len = size.x as usize * size.y as usize * Chunk::AREA;
return BiomeMap {
height: size.y as usize * Chunk::SIZE,
width: size.x as usize * Chunk::SIZE,
biome_count,
tiles: Vec::with_capacity(size.x as usize * size.y as usize * Chunk::AREA),
chunks: Vec::with_capacity(len),
};
}
@@ -28,35 +48,44 @@ impl BiomeMap {
}
fn blend_once(&mut self) {
let t: Vec<_> = (0..self.height)
let c: Vec<BiomeChunk> = (0..self.chunks.len())
.into_par_iter()
.map(|y| {
let mut new_tiles = Vec::with_capacity(self.width);
for x in 0..self.width {
let kernel = self.get_kernel(x as i32, y as i32);
let r = kernel
.iter()
.filter_map(|f| *f)
.fold(vec![0.; self.biome_count], |a, b| {
return a.iter().zip(b).map(|v| v.0 + v.1).collect();
});
.map(|i| &self.chunks[i])
.map(|chunk| {
let tiles: Vec<_> = (0..Chunk::SIZE)
.into_par_iter()
.map(|y| {
let mut new_tiles = Vec::with_capacity(self.width);
for x in 0..Chunk::SIZE {
let kernel = self.get_kernel(x as i32, y as i32);
let r = kernel
.iter()
.filter_map(|f| *f)
.fold(vec![0.; self.biome_count], |a, b| {
return a.iter().zip(b).map(|v| v.0 + v.1).collect();
});
let sum: f32 = r.iter().sum();
if sum == 0. {
new_tiles.push(vec![0.; self.biome_count]);
continue;
}
new_tiles.push(r.iter().map(|f| f / sum).collect());
}
return new_tiles;
let sum: f32 = r.iter().sum();
if sum == 0. {
new_tiles.push(vec![0.; self.biome_count]);
continue;
}
new_tiles.push(r.iter().map(|f| f / sum).collect());
}
return new_tiles;
})
.flatten()
.collect();
return BiomeChunk {
tiles,
data: chunk.data,
};
})
.flatten()
.collect();
self.tiles = t;
self.chunks = c;
}
pub fn get_kernel(&self, x: i32, y: i32) -> [Option<&Vec<f32>>; 9] {
fn get_kernel(&self, x: i32, y: i32) -> [Option<&Vec<f32>>; 9] {
return [
self.get_biome(x - 1, y - 1),
self.get_biome(x, y - 1),
@@ -78,7 +107,37 @@ impl BiomeMap {
return None;
}
return Some(&self.tiles[x as usize + y as usize * self.width]);
let cx = x as usize / Chunk::SIZE;
let cy = y as usize / Chunk::SIZE;
let chunk = &self.chunks[cx + cy * Chunk::SIZE];
return Some(chunk.get_biome(x as usize - cx * Chunk::SIZE, y as usize - cy * Chunk::SIZE));
}
pub fn get_biome_data(&self, x: usize, y: usize) -> &BiomeData {
let cx = x / Chunk::SIZE;
let cy = y / Chunk::SIZE;
let chunk = &self.chunks[cx + cy * Chunk::SIZE];
return chunk.get_biome_data(x - cx * Chunk::SIZE, y - cy * Chunk::SIZE);
}
}
#[derive(Clone)]
pub struct BiomeChunk {
pub tiles: Vec<Vec<f32>>,
pub data: [BiomeData; Chunk::AREA],
}
impl BiomeChunk {
pub fn get_biome(&self, x: usize, y: usize) -> &Vec<f32> {
return &self.tiles[x as usize + y as usize * Chunk::SIZE];
}
pub fn get_biome_data(&self, x: usize, y: usize) -> &BiomeData {
return &self.data[x + y * Chunk::SIZE];
}
}
@@ -90,18 +149,27 @@ mod tests {
#[test]
fn biome_blend() {
let mut biome = BiomeMap::new(UVec2::ONE * 16, 8);
let w = biome.width / Chunk::SIZE;
let h = biome.height / Chunk::SIZE;
for y in 0..biome.height {
for x in 0..biome.width {
for y in 0..h {
for x in 0..w {
let mut b = vec![0.; biome.biome_count];
let i = x / Chunk::SIZE;
let z = y / Chunk::SIZE;
let idx = (i + z) % biome.biome_count;
let idx = (x + y) % biome.biome_count;
b[idx] = 1.;
biome.tiles.push(b);
biome.chunks.push(generate_chunk(x, y, b));
}
}
biome.blend(8);
}
fn generate_chunk(x: usize, y: usize, biome: Vec<f32>) -> BiomeChunk {
let chunk = BiomeChunk {
data: [BiomeData::default(); Chunk::AREA],
tiles: (0..Chunk::AREA).into_iter().map(|_| biome.clone()).collect(),
};
return chunk;
}
}

View File

@@ -1,12 +1,13 @@
use crate::hex_utils::SHORT_DIAGONAL;
use bevy::prelude::*;
use super::biome_map::BiomeData;
#[derive(Clone)]
pub struct Chunk {
pub heights: [f32; Chunk::AREA],
pub textures: [[u32; 2]; Chunk::AREA],
pub moisture: [f32; Chunk::AREA],
pub temperature: [f32; Chunk::AREA],
pub biome_data: [BiomeData; Chunk::AREA],
pub chunk_offset: IVec2,
}
@@ -15,8 +16,7 @@ impl Default for Chunk {
Self {
heights: [0.; Chunk::AREA],
textures: [[0; 2]; Chunk::AREA],
moisture: [0.; Chunk::AREA],
temperature: [0.; Chunk::AREA],
biome_data: [BiomeData::default(); Chunk::AREA],
chunk_offset: Default::default(),
}
}

View File

@@ -7,12 +7,13 @@ use super::chunk::Chunk;
#[derive(Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct GenerationConfig {
pub noise_scale: f64,
pub sea_level: f64,
pub border_size: f32,
pub biome_blend: usize,
pub moisture_layer: NoiseConfig,
pub temperature_layer: NoiseConfig,
pub continent_layer: NoiseConfig,
pub size: UVec2,
pub layers: Vec<GeneratorLayer>,
}
impl GenerationConfig {
@@ -24,7 +25,13 @@ impl GenerationConfig {
}
}
#[derive(Reflect, InspectorOptions, Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Default, Reflect, Clone, Debug)]
pub struct NoiseConfig {
pub scale: f64,
pub layers: Vec<GeneratorLayer>,
}
#[derive(Reflect, InspectorOptions, Serialize, Deserialize, Debug, Clone, Default)]
pub struct GeneratorLayer {
pub strength: f64,
pub min_value: f64,

View File

@@ -58,12 +58,12 @@ impl Map {
pub fn get_moisture(&self, pos: &HexCoord) -> f32 {
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return chunk.moisture[pos.to_chunk_local_index()];
return chunk.biome_data[pos.to_chunk_local_index()].moisture;
}
pub fn get_tempurature(&self, pos: &HexCoord) -> f32 {
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return chunk.temperature[pos.to_chunk_local_index()];
return chunk.biome_data[pos.to_chunk_local_index()].temperature;
}
pub fn get_center(&self) -> Vec3 {

View File

@@ -2,6 +2,7 @@ use bevy::ecs::schedule::States;
#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
pub enum GeneratorState {
Startup,
GenerateHeightmap,
SpawnMap,
Idle,