Allow for arbitrary world chunk sizes
This commit is contained in:
parent
27b61c624d
commit
f57bf565e4
|
@ -68,13 +68,16 @@ impl App {
|
||||||
0.25,
|
0.25,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut world = voxel::world::WorldManager::new(voxel::world::GenerationSettings {
|
let mut world = voxel::world::WorldManager::new(
|
||||||
seed: 0,
|
voxel::world::GenerationSettings {
|
||||||
frequency: 0.04,
|
seed: 0,
|
||||||
octaves: 3,
|
frequency: 0.04,
|
||||||
gain: 0.5,
|
octaves: 3,
|
||||||
lacunarity: 2.0,
|
gain: 0.5,
|
||||||
});
|
lacunarity: 2.0,
|
||||||
|
},
|
||||||
|
glam::uvec3(16, 32, 32),
|
||||||
|
);
|
||||||
|
|
||||||
let mut renderer = voxel::VoxelRenderer::new(&self.render_ctx, &camera_controller);
|
let mut renderer = voxel::VoxelRenderer::new(&self.render_ctx, &camera_controller);
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,13 @@ pub struct Chunk {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Chunk {
|
impl Chunk {
|
||||||
fn get_block(&mut self, pos: glam::UVec3) -> Vec<Voxel> {
|
fn get_block(&mut self, pos: glam::UVec3, chunk_dims: glam::UVec3) -> Vec<Voxel> {
|
||||||
let chunk_dims = glam::uvec3(32, 32, 32);
|
|
||||||
let noise_dims = chunk_dims + glam::uvec3(1, 1, 1);
|
let noise_dims = chunk_dims + glam::uvec3(1, 1, 1);
|
||||||
let block_idx = math::to_1d_index(pos, chunk_dims);
|
let block_idx = math::to_1d_index(pos, chunk_dims);
|
||||||
assert_eq!(self.blocks.len(), 32768);
|
assert_eq!(
|
||||||
|
self.blocks.len(),
|
||||||
|
(chunk_dims.x * chunk_dims.y * chunk_dims.z) as usize
|
||||||
|
);
|
||||||
let block = &mut self.blocks[block_idx];
|
let block = &mut self.blocks[block_idx];
|
||||||
|
|
||||||
if block.is_empty() {
|
if block.is_empty() {
|
||||||
|
@ -47,6 +49,7 @@ impl Chunk {
|
||||||
let mut vals = [0.0f32; 512];
|
let mut vals = [0.0f32; 512];
|
||||||
math::tri_lerp_block(&noise_vals, &[8, 8, 8], &mut vals);
|
math::tri_lerp_block(&noise_vals, &[8, 8, 8], &mut vals);
|
||||||
|
|
||||||
|
// TODO: Better voxel colours
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
for z in 0..8 {
|
for z in 0..8 {
|
||||||
for y in 0..8 {
|
for y in 0..8 {
|
||||||
|
@ -83,13 +86,22 @@ pub struct GenerationSettings {
|
||||||
|
|
||||||
pub struct WorldManager {
|
pub struct WorldManager {
|
||||||
settings: GenerationSettings,
|
settings: GenerationSettings,
|
||||||
|
chunk_dims: glam::UVec3,
|
||||||
chunks: HashMap<glam::IVec3, Chunk>,
|
chunks: HashMap<glam::IVec3, Chunk>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WorldManager {
|
impl WorldManager {
|
||||||
pub fn new(settings: GenerationSettings) -> Self {
|
pub fn new(settings: GenerationSettings, chunk_dims: glam::UVec3) -> Self {
|
||||||
let chunks = HashMap::new();
|
let chunks = HashMap::new();
|
||||||
Self { settings, chunks }
|
Self {
|
||||||
|
settings,
|
||||||
|
chunk_dims,
|
||||||
|
chunks,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_chunk_dims(&self) -> glam::UVec3 {
|
||||||
|
self.chunk_dims
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_block(&mut self, chunk_pos: glam::IVec3, local_pos: glam::UVec3) -> Vec<Voxel> {
|
pub fn get_block(&mut self, chunk_pos: glam::IVec3, local_pos: glam::UVec3) -> Vec<Voxel> {
|
||||||
|
@ -101,20 +113,20 @@ impl WorldManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
let chunk = self.chunks.get_mut(&chunk_pos).unwrap();
|
let chunk = self.chunks.get_mut(&chunk_pos).unwrap();
|
||||||
chunk.get_block(local_pos)
|
chunk.get_block(local_pos, self.chunk_dims)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_chunk(&mut self, pos: glam::IVec3) -> Chunk {
|
fn gen_chunk(&mut self, pos: glam::IVec3) -> Chunk {
|
||||||
// We use dimensions of 33 because the corners on the last chunk block of each
|
// We use dimensions of `chunk_dims + 1` because the corners on the last chunk
|
||||||
// axis step outside of our 0..32 bounds, sharing a value with the neighbouring
|
// block of each axis step outside of our 0..N bounds, sharing a value with the
|
||||||
// chunk
|
// neighbouring chunk
|
||||||
let noise = simdnoise::NoiseBuilder::fbm_3d_offset(
|
let noise = simdnoise::NoiseBuilder::fbm_3d_offset(
|
||||||
pos.x as f32,
|
pos.x as f32,
|
||||||
33,
|
self.chunk_dims.x as usize + 1,
|
||||||
pos.y as f32,
|
pos.y as f32,
|
||||||
33,
|
self.chunk_dims.y as usize + 1,
|
||||||
pos.z as f32,
|
pos.z as f32,
|
||||||
33,
|
self.chunk_dims.z as usize + 1,
|
||||||
)
|
)
|
||||||
.with_seed(self.settings.seed)
|
.with_seed(self.settings.seed)
|
||||||
.with_freq(self.settings.frequency)
|
.with_freq(self.settings.frequency)
|
||||||
|
@ -124,8 +136,8 @@ impl WorldManager {
|
||||||
.generate()
|
.generate()
|
||||||
.0;
|
.0;
|
||||||
|
|
||||||
let mut blocks = Vec::with_capacity(32768);
|
let num_blocks = self.chunk_dims.x * self.chunk_dims.y * self.chunk_dims.z;
|
||||||
blocks.resize(blocks.capacity(), vec![]);
|
let blocks = vec![vec![]; num_blocks as usize];
|
||||||
Chunk { pos, noise, blocks }
|
Chunk { pos, noise, blocks }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue