diff --git a/src/pbs.rs b/src/pbs.rs index cf5ad7f..54bb94c 100644 --- a/src/pbs.rs +++ b/src/pbs.rs @@ -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 @@ -146,3 +147,94 @@ pub(crate) fn apply_pbs( } } } + +pub fn apply_pbs_with_connected_chunks( + reg: &impl VoxelRegistry, + mesh: &mut Mesh, + metadata: &MeshMD, + dims: Dimensions, + lower_bound: usize, + upper_bound: usize, + this_chunk: &[T], + north_chunk: Option<&Arc>>, + south_chunk: Option<&Arc>>, + east_chunk: Option<&Arc>>, + west_chunk: Option<&Arc>>, +) { + 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, + ); + } + } + } + } +} diff --git a/src/update.rs b/src/update.rs index bf2cb0c..fb77fd3 100644 --- a/src/update.rs +++ b/src/update.rs @@ -1,4 +1,4 @@ -use crate::pbs::*; +// use crate::pbs::*; use crate::prelude::*; use bevy::render::mesh::{Indices, VertexAttributeValues}; @@ -127,19 +127,19 @@ pub fn update_mesh( } 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.