three_d/renderer/object/
voxel_grid.rs

1use super::*;
2
3pub use three_d_asset::VoxelGrid as CpuVoxelGrid;
4
5///
6/// A voxel grid inside a cube with a [material] type specified by the generic parameter.
7///
8pub struct VoxelGrid<M: Material>(Gm<Mesh, M>);
9
10impl<M: Material + FromCpuVoxelGrid> VoxelGrid<M> {
11    ///
12    /// Constructs a [VoxelGrid] from a [CpuVoxelGrid], ie. constructs a [Gm] with a cube [Mesh] as geometry and
13    /// a [material] type specified by the generic parameter which implement [FromCpuVoxelGrid].
14    ///
15    pub fn new(context: &Context, cpu_voxel_grid: &CpuVoxelGrid) -> Self {
16        let mut cube = CpuMesh::cube();
17        cube.transform(Mat4::from_nonuniform_scale(
18            0.5 * cpu_voxel_grid.size.x,
19            0.5 * cpu_voxel_grid.size.y,
20            0.5 * cpu_voxel_grid.size.z,
21        ))
22        .expect("Invalid size for VoxelGrid");
23        let gm = Gm::new(
24            Mesh::new(context, &cube),
25            M::from_cpu_voxel_grid(context, cpu_voxel_grid),
26        );
27        Self(gm)
28    }
29}
30
31impl<'a, M: Material> IntoIterator for &'a VoxelGrid<M> {
32    type Item = &'a dyn Object;
33    type IntoIter = std::iter::Once<&'a dyn Object>;
34
35    fn into_iter(self) -> Self::IntoIter {
36        std::iter::once(self)
37    }
38}
39
40use std::ops::Deref;
41impl<M: Material> Deref for VoxelGrid<M> {
42    type Target = Gm<Mesh, M>;
43    fn deref(&self) -> &Self::Target {
44        &self.0
45    }
46}
47
48impl<M: Material> std::ops::DerefMut for VoxelGrid<M> {
49    fn deref_mut(&mut self) -> &mut Self::Target {
50        &mut self.0
51    }
52}
53
54impl<M: Material> Geometry for VoxelGrid<M> {
55    impl_geometry_body!(deref);
56
57    fn animate(&mut self, time: f32) {
58        self.0.animate(time)
59    }
60}
61
62impl<M: Material> Object for VoxelGrid<M> {
63    impl_object_body!(deref);
64}