use bevy::mesh::Mesh;
use crate::chunk::{CHUNK_SIZE, Chunk};
use crate::voxel::VoxelType;
use super::ChunkNeighbors;
use super::greedy::{FaceDir, build_mesh, super_voxel};
pub fn generate_chunk_mesh_with_neighbors<V: VoxelType>(
chunk: &Chunk<V>,
neighbors: &ChunkNeighbors<V>,
step: usize,
) -> Mesh {
crate::profile_span!("voxel::mesh::neighbor");
let face_visible =
|chunk: &Chunk<V>, x: usize, y: usize, z: usize, face: &FaceDir, step: usize| -> bool {
is_face_visible(chunk, neighbors, x, y, z, face, step)
};
build_mesh(chunk, step, &face_visible)
}
#[inline]
fn is_face_visible<V: VoxelType>(
chunk: &Chunk<V>,
neighbors: &ChunkNeighbors<V>,
x: usize,
y: usize,
z: usize,
face: &FaceDir,
step: usize,
) -> bool {
let coords = [x, y, z];
let axis = face.axis;
let neighbor_on_axis = if face.positive {
coords[axis].wrapping_add(step)
} else {
coords[axis].wrapping_sub(step)
};
if neighbor_on_axis < CHUNK_SIZE {
let mut nc = [x, y, z];
nc[axis] = neighbor_on_axis;
let sv = super_voxel(chunk, nc[0], nc[1], nc[2], step);
return !sv.is_solid() || sv.color()[3] < 1.0;
}
let face_index = face_dir_to_index(face);
match &neighbors.faces[face_index] {
None => true, Some(slice) => {
let u_coord = coords[face.u_axis];
let v_coord = coords[face.v_axis];
for du in 0..step {
for dv in 0..step {
let u = u_coord + du;
let v = v_coord + dv;
if u >= CHUNK_SIZE || v >= CHUNK_SIZE {
return true; }
let slice_index = border_slice_index(face_index, u, v);
let neighbor = slice[slice_index];
if !neighbor.is_solid() || neighbor.color()[3] < 1.0 {
return true; }
}
}
false }
}
}
#[inline]
fn face_dir_to_index(face: &FaceDir) -> usize {
match (face.axis, face.positive) {
(1, true) => 0, (1, false) => 1, (0, true) => 2, (0, false) => 3, (2, true) => 4, (2, false) => 5, _ => unreachable!(),
}
}
#[inline]
fn border_slice_index(face_index: usize, u_coord: usize, v_coord: usize) -> usize {
match face_index {
0 | 1 => u_coord * CHUNK_SIZE + v_coord,
2 | 3 => v_coord * CHUNK_SIZE + u_coord,
4 | 5 => u_coord * CHUNK_SIZE + v_coord,
_ => unreachable!(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::voxel::DefaultVoxel;
const MAX: usize = CHUNK_SIZE - 1;
fn face_cases() -> [(usize, usize, (usize, usize, usize), (usize, usize, usize)); 6] {
[
(0, 1, (3, MAX, 7), (3, 0, 7)),
(1, 0, (3, 0, 7), (3, MAX, 7)),
(2, 3, (MAX, 3, 7), (0, 3, 7)),
(3, 2, (0, 3, 7), (MAX, 3, 7)),
(4, 5, (3, 7, MAX), (3, 7, 0)),
(5, 4, (3, 7, 0), (3, 7, MAX)),
]
}
fn single(cell: (usize, usize, usize)) -> Chunk<DefaultVoxel> {
let mut chunk = Chunk::new();
chunk.set(cell.0, cell.1, cell.2, DefaultVoxel::Rock);
chunk
}
fn mesh_vertices(
chunk: &Chunk<DefaultVoxel>,
face: usize,
slice_src: &Chunk<DefaultVoxel>,
opposite: usize,
) -> usize {
let mut neighbors = ChunkNeighbors::<DefaultVoxel>::default();
neighbors.faces[face] = Some(slice_src.border_slice(opposite));
generate_chunk_mesh_with_neighbors(chunk, &neighbors, 1).count_vertices()
}
#[test]
fn solid_neighbor_culls_the_touching_face_on_every_side() {
for (face, opposite, cell, touching) in face_cases() {
let vertices = mesh_vertices(&single(cell), face, &single(touching), opposite);
assert_eq!(
vertices, 20,
"face {face}: one of six quads (4 vertices) should be culled"
);
}
}
#[test]
fn neighbor_solid_at_a_different_cell_does_not_cull() {
for (face, opposite, cell, touching) in face_cases() {
let swapped = match face {
0 | 1 => (touching.2, touching.1, touching.0),
2 | 3 => (touching.0, touching.2, touching.1),
_ => (touching.1, touching.0, touching.2),
};
let vertices = mesh_vertices(&single(cell), face, &single(swapped), opposite);
assert_eq!(
vertices, 24,
"face {face}: misaligned neighbor must not cull"
);
}
}
#[test]
fn missing_or_empty_neighbor_keeps_all_faces() {
let chunk = single((0, 0, 0));
let none = ChunkNeighbors::<DefaultVoxel>::default();
assert_eq!(
generate_chunk_mesh_with_neighbors(&chunk, &none, 1).count_vertices(),
24
);
let empty = Chunk::<DefaultVoxel>::new();
assert_eq!(mesh_vertices(&chunk, 3, &empty, 2), 24);
}
}