Skip to content

Commit

Permalink
api changes|
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamkob12 committed Nov 8, 2023
1 parent 2ba945f commit 1acf806
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 64 deletions.
102 changes: 70 additions & 32 deletions src/adj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,74 @@ pub fn introduce_adjacent_chunks<T: std::fmt::Debug + Sized + Copy>(
}
}
update_mesh(main_mesh, main_md, reg);

if let Some(pbs) = main_md.pbs {
for index in iter_faces_of_chunk(dims, connection_side) {
if main_md
.vivi
.get_quad_index(connection_side, index)
.is_some()
{
let adj_voxel_index = get_neigbhor_across_chunk(dims, index, connection_side);
let mut closest_voxels = [false; 6];
for i in 0..6 {
let tmp_face = Face::from(i);
if let Some(neighbor) = get_neighbor(adj_voxel_index, tmp_face, dims) {
closest_voxels[i] =
reg.is_covering(&adjacent_chunk_grid[neighbor], tmp_face.opposite());
}
}
if closest_voxels != [false; 6] {
apply_pbs_quad(
main_mesh,
&main_md.vivi,
index,
connection_side,
closest_voxels,
pbs,
reg.get_voxel_dimensions(),
dims,
)
}
}
}
}
}

// if let Some(pbs) = main_md.pbs {
// for index in iter_faces_of_chunk(dims, connection_side) {
// if main_md
// .vivi
// .get_quad_index(connection_side, index)
// .is_some()
// {
// let adj_voxel_index = get_neigbhor_across_chunk(dims, index, connection_side);
// let mut closest_voxels = [false; 6];
// closest_voxels[connection_side.opposite() as usize] = true;
// for i in 0..6 {
// let tmp_face = Face::from(i);
// if let Some(neighbor) = get_neighbor(adj_voxel_index, tmp_face, dims) {
// closest_voxels[i] =
// reg.is_covering(&adjacent_chunk_grid[neighbor], tmp_face.opposite());
// }
// }
// if closest_voxels != [false; 6] {
// apply_pbs_quad(
// main_mesh,
// &main_md.vivi,
// index,
// connection_side,
// closest_voxels,
// pbs,
// voxel_dims,
// dims,
// )
// }
// } else if main_md.vivi.vivi[index].is_empty() {
// let adj_voxel_index = get_neigbhor_across_chunk(dims, index, connection_side);
// let mut closest_voxels = [false; 6];
// let adj_voxel = adjacent_chunk_grid[adj_voxel_index];
// if reg.is_covering(&adj_voxel, connection_side.opposite()) {
// for i in 0..6 {
// let face = Face::from(i);
// if let Some(neighbor) = get_neighbor(index, face, dims) {
// closest_voxels[i] = !main_md.vivi.vivi[neighbor].is_empty();
// }
// }
// closest_voxels[connection_side as usize] = true;
//
// for j in 0..6 {
// let face = Face::from(j);
// if let Some(neighbor) = get_neighbor(index, face, dims) {
// if
// /* !main_md.vivi.vivi[neighbor].is_empty() */
// main_md
// .vivi
// .get_quad_index(face.opposite(), neighbor)
// .is_some()
// {
// apply_pbs_quad(
// main_mesh,
// &main_md.vivi,
// neighbor,
// face.opposite(),
// closest_voxels,
// pbs,
// voxel_dims,
// dims,
// )
// }
// }
// }
// }
// }
// }
// }
2 changes: 1 addition & 1 deletion src/face.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
/// This enum represents all the faces of a cubic voxel.
pub enum Face {
Top,
Expand Down
6 changes: 3 additions & 3 deletions src/pbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ pub(crate) fn apply_pbs(
let face = face_from_u32(q & REVERSE_OFFSET_CONST);
let mut count = 0;
if let Some(neigbhor) = get_neighbor(i, face, dims) {
for i in 0..6 {
if let Some(tmp) = get_neighbor(neigbhor, Face::from(i), dims) {
for j in 0..6 {
if let Some(tmp) = get_neighbor(neigbhor, Face::from(j), dims) {
if !vivi.vivi[tmp].is_empty() {
close_voxels[i] = true;
close_voxels[j] = true;
count += 1;
}
}
Expand Down
46 changes: 18 additions & 28 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,45 +119,35 @@ pub fn extract_indices_data(mesh: &Mesh) -> Vec<[u32; 3]> {
}

pub fn iter_faces_of_chunk(dims: Dimensions, face: Face) -> impl Iterator<Item = usize> {
let all = 0..(dims.0 * dims.1 * dims.2);
let m = [dims.1 - 1, 0, dims.0 - 1, 0, dims.2 - 1, 0];
let i = match face {
Top | Bottom => 1,
Right | Left => 0,
Forward | Back => 2,
};
let all = all.filter(move |x| three_d_cords_arr(*x, dims)[i] == m[face as usize]);
all
(0..(dims.0 * dims.1 * dims.2))
.into_iter()
.filter(move |x| is_block_on_edge(dims, *x, face))
}

pub fn is_block_on_edge(dims: Dimensions, index: usize) -> Vec<Face> {
pub fn block_edges(dims: Dimensions, index: usize) -> Vec<Face> {
let mut to_return = vec![];
let m = [dims.1 - 1, 0, dims.0 - 1, 0, dims.2 - 1, 0];
for i in 0..6 {
let face = Face::from(i);
let j = match face {
Top | Bottom => 1,
Right | Left => 0,
Forward | Back => 2,
};
if three_d_cords_arr(index, dims)[j] == m[face as usize] {
if get_neighbor(index, face, dims).is_none() {
to_return.push(face);
}
}
to_return
}

pub fn is_block_on_edge(dims: Dimensions, index: usize, face: Face) -> bool {
get_neighbor(index, face, dims).is_none()
}

pub fn get_neigbhor_across_chunk(dims: Dimensions, index: usize, face: Face) -> usize {
for f in is_block_on_edge(dims, index) {
if f as usize == face as usize {
return match face {
Right => index - dims.0 + 1,
Left => index + dims.0 - 1,
Back => index - dims.0 * (dims.2 - 1),
Forward => index + dims.0 * (dims.2 - 1),
_ => panic!("Shouldn't happen"),
};
}
if is_block_on_edge(dims, index, face) {
return match face {
Right => index - dims.0 + 1,
Left => index + dims.0 - 1,
Back => index - dims.0 * (dims.2 - 1),
Forward => index + dims.0 * (dims.2 - 1),
_ => panic!("Shouldn't happen"),
};
}
panic!("`get_neigbhor_across_chunk` was called on a block that wasn't on the edge of a chunk");
}
Expand All @@ -180,7 +170,7 @@ pub fn get_neighbor(voxel: usize, face: Face, dims: Dimensions) -> Option<usize>
}

pub fn get_neigbhors_from_across_chunks(dims: Dimensions, index: usize) -> Vec<(Face, usize)> {
let edges = is_block_on_edge(dims, index);
let edges = block_edges(dims, index);
if edges.is_empty() {
return vec![];
}
Expand Down

0 comments on commit 1acf806

Please sign in to comment.