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
mod camera;
mod light;
/// Contains model and material
/// # Usage
/// Check [Model](struct.Model.html) for more information about how to use this module.
pub mod model;

use crate::utils::*;
use crate::GltfData;
pub use camera::Camera;
pub use light::Light;
pub use model::{Material, Model};

use cgmath::*;
use gltf::scene::Node;

/// Contains cameras, models and lights of a scene.
#[derive(Default, Clone, Debug)]
pub struct Scene {
    /// List of models in the scene
    pub models: Vec<Model>,
    /// List of cameras in the scene
    pub cameras: Vec<Camera>,
    /// List of lights in the scene
    pub lights: Vec<Light>,
}

impl Scene {
    pub(crate) fn load(gltf_scene: gltf::Scene, data: &GltfData, col: &mut Collection) -> Self {
        let mut scene = Self::default();
        for node in gltf_scene.nodes() {
            scene.read_node(&node, &One::one(), data, col);
        }
        scene
    }

    fn read_node(
        &mut self,
        node: &Node,
        parent_transform: &Matrix4<f32>,
        data: &GltfData,
        col: &mut Collection,
    ) {
        // Compute transform of the current node
        let transform = parent_transform * transform_to_matrix(node.transform());

        // Recurse on children
        for child in node.children() {
            self.read_node(&child, &transform, data, col);
        }

        // Load camera
        if let Some(camera) = node.camera() {
            self.cameras.push(Camera::load(camera, &transform));
        }

        // Load light
        if let Some(light) = node.light() {
            self.lights.push(Light::load(light, &transform));
        }

        // Load model
        if let Some(mesh) = node.mesh() {
            for primitive in mesh.primitives() {
                self.models
                    .push(Model::load(primitive, &transform, data, col));
            }
        }
    }
}