summaryrefslogtreecommitdiff
path: root/engine/src/model.rs
blob: c231a9f049eece467ebd5917fff63e6654325301 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::asset::{
    Asset,
    Assets,
    Dependency as AssetDependency,
    Event as AssetEvent,
    Events as AssetEvents,
    Handle as AssetHandle,
    Label as AssetLabel,
};
use crate::ecs::sole::Single;
use crate::ecs::Component;
use crate::material::Material;
use crate::mesh::Mesh;

pub mod asset;

#[derive(Debug, Default)]
#[non_exhaustive]
pub struct Model {}

impl Asset for Model
{
    fn dependencies(&self) -> impl Iterator<Item = AssetDependency<'_>>
    {
        [].into_iter()
    }
}

#[derive(Debug, Clone, Component)]
#[allow(clippy::module_name_repetitions)]
pub struct ModelMesh
{
    pub asset: AssetHandle<Mesh>,
}

#[derive(Debug, Clone, Component)]
#[allow(clippy::module_name_repetitions)]
pub struct ModelMaterial
{
    pub asset: AssetHandle<MaterialDescription>,
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct MaterialDescription
{
    pub asset: AssetHandle<Material>,
    pub loading_material: Option<AssetLabel<'static>>,
}

impl MaterialDescription
{
    #[must_use]
    pub fn new(asset: AssetHandle<Material>) -> Self
    {
        Self { asset, loading_material: None }
    }

    #[must_use]
    pub fn with_loading_material(mut self, loading_material: AssetLabel<'static>)
        -> Self
    {
        self.loading_material = Some(loading_material);
        self
    }
}

impl Asset for MaterialDescription
{
    fn dependencies(&self) -> impl Iterator<Item = AssetDependency<'_>>
    {
        self.loading_material.iter().map(|material_label| {
            AssetDependency::new::<Material>(AssetLabel {
                path: (&*material_label.path).into(),
                name: material_label.name.as_deref().map(Into::into),
            })
        })
    }
}

pub(crate) fn set_stored_mat_descs_handles(
    mut assets: Single<Assets>,
    asset_events: Single<AssetEvents>,
)
{
    let assets = assets.get_mut().unwrap();
    let asset_events = asset_events.get().unwrap();

    for AssetEvent::Stored(asset_id, ..) in asset_events.iter_last_tick() {
        let asset_handle = AssetHandle::<MaterialDescription>::from_id(*asset_id);

        if !assets.is_loaded_and_has_type(&asset_handle) {
            continue;
        }

        let material_asset = {
            let Some(material_desc) = assets.get(&asset_handle) else {
                unreachable!();
            };

            if let Some(material_label) = &material_desc.loading_material {
                let Some(material_asset) =
                    assets.get_handle_to_loaded::<Material>(material_label.clone())
                else {
                    unreachable!();
                };

                Some(material_asset)
            } else {
                None
            }
        };

        let Some(material_asset) = material_asset else {
            continue;
        };

        let Some(material_desc) = assets.get_mut(&asset_handle) else {
            unreachable!();
        };

        material_desc.asset = material_asset;
    }
}