Extract chunk block generation out to it's own function

This commit is contained in:
Jarrod Doyle 2023-07-28 11:45:24 +01:00
parent 42fdf48660
commit e1d621f573
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 44 additions and 38 deletions

View File

@ -22,17 +22,26 @@ impl Chunk {
self.blocks.len(),
(chunk_dims.x * chunk_dims.y * chunk_dims.z) as usize
);
let block = &self.blocks[block_idx];
if !block.is_empty() {
block.to_owned()
} else {
self.gen_block(pos, block_idx, noise_dims);
self.blocks[block_idx].to_owned()
}
}
fn gen_block(&mut self, block_pos: glam::UVec3, block_idx: usize, noise_dims: glam::UVec3) {
let block = &mut self.blocks[block_idx];
if block.is_empty() {
// Extract relevant noise values from the chunk
let mut noise_vals = Vec::new();
let mut block_sign = 0.0;
for z in 0..2 {
for y in 0..2 {
for x in 0..2 {
let noise_pos = glam::uvec3(x, y, z) + pos;
let noise_pos = glam::uvec3(x, y, z) + block_pos;
let noise_idx = math::to_1d_index(noise_pos, noise_dims);
let val = self.noise[noise_idx];
noise_vals.push(val);
@ -70,9 +79,6 @@ impl Chunk {
}
}
}
block.to_owned()
}
}
#[derive(Debug, Clone, Copy)]