blender_mesh/create_mesh/
pbr_cube_without_textures.rs

1use crate::vertex_attributes::IndexedAttribute;
2use crate::{
3    BlenderMesh, BoundingBox, MaterialInput, MultiIndexedVertexAttributes, PrincipledBSDF,
4    VertexAttribute,
5};
6use std::collections::HashMap;
7
8impl BlenderMesh {
9    /// Create a default Blender cube with uniform PBR texture inputs.
10    ///
11    /// A 2x2x2 cube centered about the origin.
12    pub fn pbr_cube_without_textures() -> Self {
13        let mut materials = HashMap::with_capacity(1);
14        materials.insert(
15            "Default".to_string(),
16            PrincipledBSDF {
17                base_color: MaterialInput::Uniform([0.4, 0.5, 0.6]),
18                roughness: MaterialInput::Uniform(0.2),
19                metallic: MaterialInput::Uniform(0.3),
20                normal_map: None,
21            },
22        );
23
24        let multi_indexed_vertex_attributes = MultiIndexedVertexAttributes {
25            vertices_in_each_face: vec![4, 4, 4, 4, 4, 4],
26            positions: IndexedAttribute::new(
27                vec![
28                    0, 1, 2, 3, 4, 7, 6, 5, 0, 4, 5, 1, 1, 5, 6, 2, 2, 6, 7, 3, 4, 0, 3, 7,
29                ],
30                VertexAttribute::new(
31                    vec![
32                        1., 1., -1., 1., -1., -1., -1., -1., -1., -1., 1., -1., 1., 1., 1., 1.,
33                        -1., 1., -1., -1., 1., -1., 1., 1.,
34                    ],
35                    3,
36                )
37                .unwrap(),
38            ),
39            normals: Some(IndexedAttribute::new(
40                vec![
41                    0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
42                ],
43                VertexAttribute::new(
44                    vec![
45                        0., 0., -1., 0., 0., 1., 1., 0., 0., 0., -1., 0., -1., 0., 0., 0., 1., 0.,
46                    ],
47                    3,
48                )
49                .unwrap(),
50            )),
51            uvs: None,
52            bone_influences: None,
53        };
54
55        Self {
56            name: "CubeWithoutTextures".to_string(),
57            armature_name: None,
58            bounding_box: BoundingBox {
59                min_corner: [-1.; 3].into(),
60                max_corner: [1.; 3].into(),
61            },
62            multi_indexed_vertex_attributes,
63            materials,
64            custom_properties: Default::default(),
65        }
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use crate::CreateSingleIndexConfig;
73
74    /// Verify that we can combine the positions and normals into a single buffer
75    #[test]
76    fn generate_vertex_buffer() {
77        let mut mesh = BlenderMesh::pbr_cube_without_textures();
78        mesh.combine_vertex_indices(&CreateSingleIndexConfig {
79            bone_influences_per_vertex: None,
80            calculate_face_tangents: false,
81        });
82    }
83}