1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use crate::{
bounds::assert_in_bounds, IdentityVoxel, OrientedBlockFace, UnitQuadBuffer, UnorientedUnitQuad, Voxel, VoxelVisibility,
};
use ilattice::glam::UVec3;
use ilattice::prelude::Extent;
use ndshape::Shape;
pub fn visible_block_faces<T, S>(
voxels: &[T],
voxels_shape: &S,
min: [u32; 3],
max: [u32; 3],
faces: &[OrientedBlockFace; 6],
output: &mut UnitQuadBuffer,
) where
T: Voxel,
S: Shape<3, Coord = u32>,
{
visible_block_faces_with_voxel_view::<_, IdentityVoxel<T>, _>(
voxels,
voxels_shape,
min,
max,
faces,
output,
)
}
pub fn visible_block_faces_with_voxel_view<'a, T, V, S>(
voxels: &'a [T],
voxels_shape: &S,
min: [u32; 3],
max: [u32; 3],
faces: &[OrientedBlockFace; 6],
output: &mut UnitQuadBuffer,
) where
V: Voxel + From<&'a T>,
S: Shape<3, Coord = u32>,
{
assert_in_bounds(voxels, voxels_shape, min, max);
let min = UVec3::from(min).as_ivec3();
let max = UVec3::from(max).as_ivec3();
let extent = Extent::from_min_and_max(min, max);
let interior = extent.padded(-1);
let interior =
Extent::from_min_and_shape(interior.minimum.as_uvec3(), interior.shape.as_uvec3());
let kernel_strides =
faces.map(|face| voxels_shape.linearize(face.signed_normal().as_uvec3().to_array()));
for p in interior.iter3() {
let p_array = p.to_array();
let p_index = voxels_shape.linearize(p_array);
let p_voxel = V::from(unsafe { voxels.get_unchecked(p_index as usize) });
if let VoxelVisibility::Empty = p_voxel.get_visibility() {
continue;
}
for (face_index, face_stride) in kernel_strides.into_iter().enumerate() {
let neighbor_index = p_index.wrapping_add(face_stride);
let neighbor_voxel = V::from(unsafe { voxels.get_unchecked(neighbor_index as usize) });
let face_needs_mesh = match neighbor_voxel.get_visibility() {
VoxelVisibility::Empty => true,
VoxelVisibility::Translucent => p_voxel.get_visibility() == VoxelVisibility::Opaque,
VoxelVisibility::Opaque => false,
};
if face_needs_mesh {
output.groups[face_index].push(UnorientedUnitQuad { minimum: p_array });
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::RIGHT_HANDED_Y_UP_CONFIG;
use ndshape::{ConstShape, ConstShape3u32};
#[test]
#[should_panic]
fn panics_with_max_out_of_bounds_access() {
let samples = [EMPTY; SampleShape::SIZE as usize];
let mut buffer = UnitQuadBuffer::new();
visible_block_faces(
&samples,
&SampleShape {},
[0; 3],
[34, 33, 33],
&RIGHT_HANDED_Y_UP_CONFIG.faces,
&mut buffer,
);
}
#[test]
#[should_panic]
fn panics_with_min_out_of_bounds_access() {
let samples = [EMPTY; SampleShape::SIZE as usize];
let mut buffer = UnitQuadBuffer::new();
visible_block_faces(
&samples,
&SampleShape {},
[0, 34, 0],
[33; 3],
&RIGHT_HANDED_Y_UP_CONFIG.faces,
&mut buffer,
);
}
type SampleShape = ConstShape3u32<34, 34, 34>;
#[derive(Default, Clone, Copy, Eq, PartialEq)]
struct BoolVoxel(bool);
const EMPTY: BoolVoxel = BoolVoxel(false);
impl Voxel for BoolVoxel {
fn get_visibility(&self) -> VoxelVisibility {
if *self == EMPTY {
VoxelVisibility::Empty
} else {
VoxelVisibility::Opaque
}
}
}
}