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)]