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 1acf806 commit c2131c2
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 14 deletions.
92 changes: 92 additions & 0 deletions src/pbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::prelude::*;
use bevy::math::Vec3;
use bevy::render::mesh::{Mesh, VertexAttributeValues};
use std::sync::{Arc, RwLock};

#[derive(Copy, Clone)]
/// Parameters for Proximity Based Shadowing
Expand Down Expand Up @@ -146,3 +147,94 @@ pub(crate) fn apply_pbs(
}
}
}

pub fn apply_pbs_with_connected_chunks<T, const N: usize>(
reg: &impl VoxelRegistry<Voxel = T>,
mesh: &mut Mesh,
metadata: &MeshMD<T>,
dims: Dimensions,
lower_bound: usize,
upper_bound: usize,
this_chunk: &[T],
north_chunk: Option<&Arc<RwLock<[T; N]>>>,
south_chunk: Option<&Arc<RwLock<[T; N]>>>,
east_chunk: Option<&Arc<RwLock<[T; N]>>>,
west_chunk: Option<&Arc<RwLock<[T; N]>>>,
) {
if let Some(pbs_value) = metadata.pbs {
for (index, quads) in metadata.vivi.vivi.iter().enumerate().skip(lower_bound) {
if index > upper_bound {
break;
}
for q in quads {
let mut close_voxels: Neighbors = [false; 6];
let face = face_from_u32(q & REVERSE_OFFSET_CONST);
let mut count = 0;
if let Some(neigbhor) = get_neighbor(index, face, dims) {
for j in 0..6 {
let tmp_face = Face::from(j);
if let Some(tmp) = get_neighbor(neigbhor, tmp_face, dims) {
let voxel = &this_chunk[tmp];
if reg.is_covering(voxel, tmp_face.opposite()) {
close_voxels[j] = true;
count += 1;
}
} else if let Some(neighboring_chunk) = match tmp_face {
Back => north_chunk,
Forward => south_chunk,
Right => east_chunk,
Left => west_chunk,
_ => continue,
} {
let tmp = get_neigbhor_across_chunk(dims, neigbhor, tmp_face);
let voxel = &neighboring_chunk.read().expect("g")[tmp];
if reg.is_covering(voxel, tmp_face.opposite()) {
close_voxels[j] = true;
count += 1;
}
}
}
} else {
if let Some(neighboring_chunk) = match face {
Back => north_chunk,
Forward => south_chunk,
Right => east_chunk,
Left => west_chunk,
_ => continue,
} {
let neigbhor = get_neigbhor_across_chunk(dims, index, face);
for j in 0..6 {
let tmp_face = Face::from(j);
if let Some(tmp) = get_neighbor(neigbhor, tmp_face, dims) {
let voxel = &neighboring_chunk.read().expect("gg")[tmp];
if reg.is_covering(voxel, tmp_face.opposite()) {
close_voxels[j] = true;
count += 1;
}
} else {
let tmp = index;
let voxel = &this_chunk[tmp];
if reg.is_covering(voxel, tmp_face.opposite()) {
close_voxels[j] = true;
count += 1;
}
}
}
}
}
if count != 0 {
apply_pbs_quad(
mesh,
&metadata.vivi,
index,
face,
close_voxels,
pbs_value,
reg.get_voxel_dimensions(),
dims,
);
}
}
}
}
}
28 changes: 14 additions & 14 deletions src/update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::pbs::*;
// use crate::pbs::*;
use crate::prelude::*;
use bevy::render::mesh::{Indices, VertexAttributeValues};

Expand Down Expand Up @@ -127,19 +127,19 @@ pub fn update_mesh<T: std::fmt::Debug>(
}

metadata.changed_voxels.clear();
if metadata.pbs.is_some() {
apply_pbs(
mesh,
&metadata.vivi,
metadata.dims,
min.checked_sub(metadata.dims.0 * metadata.dims.2 * 2)
.unwrap_or(0),
max.checked_add(metadata.dims.0 * metadata.dims.2 * 2)
.unwrap_or(usize::MAX),
metadata.pbs.unwrap(),
reg.get_voxel_dimensions(),
);
}
// if metadata.pbs.is_some() {
// apply_pbs(
// mesh,
// &metadata.vivi,
// metadata.dims,
// min.checked_sub(metadata.dims.0 * metadata.dims.2 * 2)
// .unwrap_or(0),
// max.checked_add(metadata.dims.0 * metadata.dims.2 * 2)
// .unwrap_or(usize::MAX),
// metadata.pbs.unwrap(),
// reg.get_voxel_dimensions(),
// );
// }
}

// The function removes all quads facing a voxel.
Expand Down

0 comments on commit c2131c2

Please sign in to comment.