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(),
|
||||
(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_idx = math::to_1d_index(noise_pos, noise_dims);
|
||||
let val = self.noise[noise_idx];
|
||||
noise_vals.push(val);
|
||||
block_sign += val.signum();
|
||||
}
|
||||
// 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) + block_pos;
|
||||
let noise_idx = math::to_1d_index(noise_pos, noise_dims);
|
||||
let val = self.noise[noise_idx];
|
||||
noise_vals.push(val);
|
||||
block_sign += val.signum();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If all the corners are negative, then all the interpolated values
|
||||
// will be negative too. In that case we can just fill with empty.
|
||||
if block_sign == -8.0 {
|
||||
block.resize(512, Voxel::Empty);
|
||||
} else {
|
||||
let mut vals = [0.0f32; 512];
|
||||
math::tri_lerp_block(&noise_vals, &[8, 8, 8], &mut vals);
|
||||
// If all the corners are negative, then all the interpolated values
|
||||
// will be negative too. In that case we can just fill with empty.
|
||||
if block_sign == -8.0 {
|
||||
block.resize(512, Voxel::Empty);
|
||||
} else {
|
||||
let mut vals = [0.0f32; 512];
|
||||
math::tri_lerp_block(&noise_vals, &[8, 8, 8], &mut vals);
|
||||
|
||||
// TODO: Better voxel colours
|
||||
let mut idx = 0;
|
||||
for z in 0..8 {
|
||||
for y in 0..8 {
|
||||
for x in 0..8 {
|
||||
let val = vals[idx];
|
||||
idx += 1;
|
||||
// TODO: Better voxel colours
|
||||
let mut idx = 0;
|
||||
for z in 0..8 {
|
||||
for y in 0..8 {
|
||||
for x in 0..8 {
|
||||
let val = vals[idx];
|
||||
idx += 1;
|
||||
|
||||
if val > 0.0 {
|
||||
let r = ((x + 1) * 32 - 1) as u8;
|
||||
let g = ((y + 1) * 32 - 1) as u8;
|
||||
let b = ((z + 1) * 32 - 1) as u8;
|
||||
block.push(Voxel::Color(r, g, b));
|
||||
} else {
|
||||
block.push(Voxel::Empty);
|
||||
}
|
||||
if val > 0.0 {
|
||||
let r = ((x + 1) * 32 - 1) as u8;
|
||||
let g = ((y + 1) * 32 - 1) as u8;
|
||||
let b = ((z + 1) * 32 - 1) as u8;
|
||||
block.push(Voxel::Color(r, g, b));
|
||||
} else {
|
||||
block.push(Voxel::Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
block.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue