trying to re-implement chunk_manager

This commit is contained in:
2026-03-19 21:52:58 +03:30
parent adebff1322
commit 45c122ad28
15 changed files with 216 additions and 566 deletions

View File

@@ -16,7 +16,7 @@ pub struct Chunk {
impl Chunk {
/// pos_z and pos_x are `world` coordinates of the chunk, inside the world
pub fn new(world_pos_x: f64, world_pos_y: f64, world_pos_z: f64, size: Vector3i) -> Self {
pub fn new(world_pos_x: f64, world_pos_y: f64, world_pos_z: f64) -> Self {
Chunk {
voxels: vec![0u32; BITPACKED_SIZE],
world_position: (world_pos_x, world_pos_y, world_pos_z),
@@ -24,10 +24,14 @@ impl Chunk {
}
}
#[inline]
pub fn get_voxel_index(&self, local_pos: Vector3i, chunk_size: Vector3i) -> usize {
((local_pos.x * chunk_size.y * chunk_size.z) + (local_pos.y * chunk_size.x) + local_pos.z)
as usize
// #[inline]
// pub fn get_voxel_index(&self, local_pos: Vector3i, chunk_size: Vector3i) -> usize {
// ((local_pos.x * chunk_size.y * chunk_size.z) + (local_pos.y * chunk_size.x) + local_pos.z)
// as usize
// }
#[inline(always)]
fn get_voxel_index(local: Vector3i, chunk_size: Vector3i) -> usize {
((local.x << 10) | (local.y << 5) | local.z) as usize
}
#[inline]
@@ -48,16 +52,15 @@ impl Chunk {
local_pos
);
let index = self.get_voxel_index(local_pos, chunk_size);
let index = Self::get_voxel_index(local_pos, chunk_size);
debug_assert!(
index < ARR_SIZE,
index < BITPACKED_SIZE,
"Voxel index {:?} out of chunk bounds",
index
);
// Get the u32 containing this bit and the bit position within it
let word_index = index / chunk_size.x as usize;
let bit_index = index % chunk_size.x as usize;
let word_index = index >> 5; // divide by 32
let bit_index = index & 31; // modulo 32
(self.voxels[word_index] & (1 << bit_index)) != 0
}
@@ -68,10 +71,10 @@ impl Chunk {
"Voxel position {:?} out of chunk bounds",
voxel_pos
);
let index = self.get_voxel_index(voxel_pos, chunk_size);
let index = Self::get_voxel_index(voxel_pos, chunk_size);
let word_index = index / chunk_size.x as usize;
let bit_index = index % chunk_size.x as usize;
let word_index = index >> 5;
let bit_index = index & 31;
if is_solid {
self.voxels[word_index] |= 1 << bit_index;
@@ -80,29 +83,4 @@ impl Chunk {
}
self.modified = true;
}
pub fn fill(&mut self, is_solid: bool) {
let fill_value = if is_solid { u32::MAX } else { 0 };
self.voxels.fill(fill_value);
self.modified = true;
}
pub fn get_position(&self) -> (f64, f64, f64) {
self.world_position
}
pub fn get_world_bounds(&self, chunk_size: Vector3i) -> (Vector3, Vector3) {
let (x, y, z) = self.world_position;
let min = Vector3::new(
(x * chunk_size.x as f64) as f32,
(y * chunk_size.y as f64) as f32,
(z * chunk_size.z as f64) as f32,
);
let max = Vector3::new(
((x + 1.0) * chunk_size.x as f64) as f32,
((y + 1.0) * chunk_size.y as f64) as f32,
((z + 1.0) * chunk_size.z as f64) as f32,
);
(min, max)
}
}