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
use super::*;

pub use three_d_asset::VoxelGrid as CpuVoxelGrid;

///
#[deprecated = "renamed CpuVoxelGrid"]
pub type CpuVolume = CpuVoxelGrid;

///
/// A voxel grid inside a cube with a [material] type specified by the generic parameter.
///
pub struct VoxelGrid<M: Material>(Gm<Mesh, M>);

impl<M: Material + FromCpuVoxelGrid> VoxelGrid<M> {
    ///
    /// Constructs a [VoxelGrid] from a [CpuVoxelGrid], ie. constructs a [Gm] with a cube [Mesh] as geometry and
    /// a [material] type specified by the generic parameter which implement [FromCpuVoxelGrid].
    ///
    pub fn new(context: &Context, cpu_voxel_grid: &CpuVoxelGrid) -> ThreeDResult<Self> {
        let mut cube = CpuMesh::cube();
        cube.transform(&Mat4::from_nonuniform_scale(
            0.5 * cpu_voxel_grid.size.x,
            0.5 * cpu_voxel_grid.size.y,
            0.5 * cpu_voxel_grid.size.z,
        ))?;
        let gm = Gm::new(
            Mesh::new(&context, &cube).unwrap(),
            M::from_cpu_voxel_grid(context, cpu_voxel_grid)?,
        );
        Ok(Self(gm))
    }
}

impl<M: Material> std::ops::Deref for VoxelGrid<M> {
    type Target = Gm<Mesh, M>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<M: Material> std::ops::DerefMut for VoxelGrid<M> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<M: Material> Geometry for VoxelGrid<M> {
    fn aabb(&self) -> AxisAlignedBoundingBox {
        self.0.aabb()
    }

    fn render_with_material(
        &self,
        material: &dyn Material,
        camera: &Camera,
        lights: &[&dyn Light],
    ) -> ThreeDResult<()> {
        self.0.render_with_material(material, camera, lights)
    }
}

impl<M: Material> Object for VoxelGrid<M> {
    fn render(&self, camera: &Camera, lights: &[&dyn Light]) -> ThreeDResult<()> {
        self.0.render(camera, lights)
    }

    fn is_transparent(&self) -> bool {
        self.0.is_transparent()
    }
}