Biome blending

Dithering to biome edges
This commit is contained in:
2024-07-07 10:51:02 -04:00
parent 8e92df8008
commit ba2dcf7129
7 changed files with 68 additions and 19 deletions

View File

@@ -1,4 +1,5 @@
use bevy::math::{UVec2, Vec3};
use noise::NoiseFn;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use super::chunk::Chunk;
@@ -120,6 +121,15 @@ impl BiomeMap {
return Some(chunk.get_biome(x as usize - cx * Chunk::SIZE, y as usize - cy * Chunk::SIZE));
}
pub fn get_biome_id(&self, x: usize, y: usize) -> usize {
let cx = (x as f32 / Chunk::SIZE as f32).floor() as usize;
let cy = (y as f32 / Chunk::SIZE as f32).floor() as usize;
let chunk = &self.chunks[cx + cy * self.size.x as usize];
return chunk.get_biome_id(x - (cx * Chunk::SIZE), y - (cy * Chunk::SIZE));
}
pub fn get_biome_data(&self, x: usize, y: usize) -> &BiomeData {
let cx = (x as f32 / Chunk::SIZE as f32).floor() as usize;
let cy = (y as f32 / Chunk::SIZE as f32).floor() as usize;
@@ -145,6 +155,37 @@ impl BiomeChunk {
pub fn get_biome_data(&self, x: usize, y: usize) -> &BiomeData {
return &self.data[x + y * Chunk::SIZE];
}
pub fn get_biome_id(&self, x: usize, y: usize) -> usize {
let b = self.get_biome(x, y);
let mut max = 0.;
let mut idx = 0;
for i in 0..b.len() {
let blend = b[i];
if blend > max {
max = blend;
idx = i;
}
}
return idx;
}
pub fn get_biome_id_dithered(&self, x: usize, y: usize, noise: &impl NoiseFn<f64, 2>, scale: f64) -> usize {
let cur_id = self.get_biome_id(x, y);
let b = self.get_biome(x, y);
let n = (noise.get([x as f64 / scale, y as f64 / scale]) as f32) * b[cur_id];
for i in 0..b.len() {
let blend = b[i];
if blend == 0. {
continue;
}
if n < blend {
return i;
}
}
return cur_id;
}
}
#[cfg(test)]

View File

@@ -8,6 +8,7 @@ pub struct Chunk {
pub heights: [f32; Chunk::AREA],
pub textures: [[u32; 2]; Chunk::AREA],
pub biome_data: [BiomeData; Chunk::AREA],
pub biome_id: [usize; Chunk::AREA],
pub chunk_offset: IVec2,
}
@@ -17,6 +18,7 @@ impl Default for Chunk {
heights: [0.; Chunk::AREA],
textures: [[0; 2]; Chunk::AREA],
biome_data: [BiomeData::default(); Chunk::AREA],
biome_id: [0; Chunk::AREA],
chunk_offset: Default::default(),
}
}

View File

@@ -10,9 +10,10 @@ pub struct GenerationConfig {
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 biome_dither: f64,
pub moisture_noise: NoiseConfig,
pub temperature_noise: NoiseConfig,
pub continent_noise: NoiseConfig,
pub size: UVec2,
}