easy_gltf/scene/
mod.rs

1mod camera;
2mod light;
3/// Contains model and material
4/// # Usage
5/// Check [Model](struct.Model.html) for more information about how to use this module.
6pub mod model;
7
8use crate::GltfData;
9use crate::utils::transform_to_matrix;
10pub use camera::{Camera, Projection};
11pub use light::Light;
12pub use model::{Material, Model};
13
14use cgmath::*;
15use gltf::scene::Node;
16
17/// Contains cameras, models and lights of a scene.
18#[derive(Default, Clone, Debug)]
19pub struct Scene {
20    #[cfg(feature = "names")]
21    /// Scene name. Requires the `names` feature.
22    pub name: Option<String>,
23    #[cfg(feature = "extras")]
24    /// Scene extra data. Requires the `extras` feature.
25    pub extras: gltf::json::extras::Extras,
26    /// List of models in the scene
27    pub models: Vec<Model>,
28    /// List of cameras in the scene
29    pub cameras: Vec<Camera>,
30    /// List of lights in the scene
31    pub lights: Vec<Light>,
32}
33
34impl Scene {
35    pub(crate) fn load(gltf_scene: gltf::Scene, data: &mut GltfData) -> Self {
36        let mut scene = Self::default();
37
38        #[cfg(feature = "names")]
39        {
40            scene.name = gltf_scene.name().map(String::from);
41        }
42        #[cfg(feature = "extras")]
43        {
44            scene.extras = gltf_scene.extras().clone();
45        }
46
47        for node in gltf_scene.nodes() {
48            scene.read_node(&node, &One::one(), data);
49        }
50        scene
51    }
52
53    fn read_node(&mut self, node: &Node, parent_transform: &Matrix4<f32>, data: &mut GltfData) {
54        // Compute transform of the current node
55        let transform = parent_transform * transform_to_matrix(node.transform());
56
57        // Recurse on children
58        for child in node.children() {
59            self.read_node(&child, &transform, data);
60        }
61
62        // Load camera
63        if let Some(camera) = node.camera() {
64            self.cameras.push(Camera::load(camera, &transform));
65        }
66
67        // Load light
68        if let Some(light) = node.light() {
69            self.lights.push(Light::load(light, &transform));
70        }
71
72        // Load model
73        if let Some(mesh) = node.mesh() {
74            for (i, primitive) in mesh.primitives().enumerate() {
75                self.models
76                    .push(Model::load(&mesh, i, primitive, &transform, data));
77            }
78        }
79    }
80}