Extract chunk block generation out to it's own function
This commit is contained in:
parent
42fdf48660
commit
e1d621f573
|
@ -22,56 +22,62 @@ impl Chunk {
|
||||||
self.blocks.len(),
|
self.blocks.len(),
|
||||||
(chunk_dims.x * chunk_dims.y * chunk_dims.z) as usize
|
(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];
|
let block = &mut self.blocks[block_idx];
|
||||||
|
|
||||||
if block.is_empty() {
|
// Extract relevant noise values from the chunk
|
||||||
// Extract relevant noise values from the chunk
|
let mut noise_vals = Vec::new();
|
||||||
let mut noise_vals = Vec::new();
|
let mut block_sign = 0.0;
|
||||||
|
for z in 0..2 {
|
||||||
let mut block_sign = 0.0;
|
for y in 0..2 {
|
||||||
for z in 0..2 {
|
for x in 0..2 {
|
||||||
for y in 0..2 {
|
let noise_pos = glam::uvec3(x, y, z) + block_pos;
|
||||||
for x in 0..2 {
|
let noise_idx = math::to_1d_index(noise_pos, noise_dims);
|
||||||
let noise_pos = glam::uvec3(x, y, z) + pos;
|
let val = self.noise[noise_idx];
|
||||||
let noise_idx = math::to_1d_index(noise_pos, noise_dims);
|
noise_vals.push(val);
|
||||||
let val = self.noise[noise_idx];
|
block_sign += val.signum();
|
||||||
noise_vals.push(val);
|
|
||||||
block_sign += val.signum();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If all the corners are negative, then all the interpolated values
|
// If all the corners are negative, then all the interpolated values
|
||||||
// will be negative too. In that case we can just fill with empty.
|
// will be negative too. In that case we can just fill with empty.
|
||||||
if block_sign == -8.0 {
|
if block_sign == -8.0 {
|
||||||
block.resize(512, Voxel::Empty);
|
block.resize(512, Voxel::Empty);
|
||||||
} else {
|
} else {
|
||||||
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
|
// 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 {
|
||||||
for x in 0..8 {
|
for x in 0..8 {
|
||||||
let val = vals[idx];
|
let val = vals[idx];
|
||||||
idx += 1;
|
idx += 1;
|
||||||
|
|
||||||
if val > 0.0 {
|
if val > 0.0 {
|
||||||
let r = ((x + 1) * 32 - 1) as u8;
|
let r = ((x + 1) * 32 - 1) as u8;
|
||||||
let g = ((y + 1) * 32 - 1) as u8;
|
let g = ((y + 1) * 32 - 1) as u8;
|
||||||
let b = ((z + 1) * 32 - 1) as u8;
|
let b = ((z + 1) * 32 - 1) as u8;
|
||||||
block.push(Voxel::Color(r, g, b));
|
block.push(Voxel::Color(r, g, b));
|
||||||
} else {
|
} else {
|
||||||
block.push(Voxel::Empty);
|
block.push(Voxel::Empty);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
block.to_owned()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue