summaryrefslogtreecommitdiff
path: root/engine/src/model.rs
blob: 4c88f8f8464d5bd76b8b543721707cd4a56b4b8b (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::borrow::Cow;
use std::collections::HashMap;

use ecs::Component;

use crate::asset::Handle as AssetHandle;
use crate::material::Material;
use crate::material::asset::Map as MaterialAssetMap;
use crate::mesh::Mesh;

pub mod asset;

#[derive(Debug, Clone, Component)]
#[non_exhaustive]
pub struct Model
{
    pub spec_asset: AssetHandle<Spec>,
}

impl Model
{
    pub fn new(asset_handle: AssetHandle<Spec>) -> Self
    {
        Self { spec_asset: asset_handle }
    }
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Spec
{
    pub mesh_asset: Option<AssetHandle<Mesh>>,
    pub materials: Materials,
    pub material_names: Vec<Cow<'static, str>>,
}

impl Spec
{
    pub fn builder() -> SpecBuilder
    {
        SpecBuilder::default()
    }
}

#[derive(Debug, Default, Clone)]
pub struct SpecBuilder
{
    mesh_asset: Option<AssetHandle<Mesh>>,
    materials: Materials,
    material_names: Vec<Cow<'static, str>>,
}

impl SpecBuilder
{
    pub fn mesh(mut self, asset: AssetHandle<Mesh>) -> Self
    {
        self.mesh_asset = Some(asset);

        self
    }

    pub fn materials(mut self, materials: Materials) -> Self
    {
        self.materials = materials;

        self
    }

    pub fn material_name(mut self, material_name: impl Into<Cow<'static, str>>) -> Self
    {
        self.material_names.push(material_name.into());

        self
    }

    pub fn material_names<MaterialName>(
        mut self,
        material_names: impl IntoIterator<Item = MaterialName>,
    ) -> Self
    where
        MaterialName: Into<Cow<'static, str>>,
    {
        self.material_names
            .extend(material_names.into_iter().map(|mat_name| mat_name.into()));

        self
    }

    #[tracing::instrument(skip_all)]
    pub fn build(self) -> Spec
    {
        if !self.materials.is_empty() && self.material_names.is_empty() {
            tracing::warn!("Model spec will have materials but no material names");
        }

        if self.materials.is_empty() && !self.material_names.is_empty() {
            tracing::warn!("Model spec will have material names but no materials");
        }

        Spec {
            mesh_asset: self.mesh_asset,
            materials: self.materials,
            material_names: self.material_names,
        }
    }
}

#[derive(Debug, Clone)]
pub enum Materials
{
    Direct(HashMap<Cow<'static, str>, AssetHandle<Material>>),
    Maps(Vec<AssetHandle<MaterialAssetMap>>),
}

impl Materials
{
    pub fn direct<MaterialName>(
        material_assets: impl IntoIterator<Item = (MaterialName, AssetHandle<Material>)>,
    ) -> Self
    where
        MaterialName: Into<Cow<'static, str>>,
    {
        Self::Direct(
            material_assets
                .into_iter()
                .map(|(material_name, mat_asset)| (material_name.into(), mat_asset))
                .collect(),
        )
    }

    pub fn is_empty(&self) -> bool
    {
        match self {
            Self::Direct(material_assets) => material_assets.is_empty(),
            Self::Maps(material_asset_map_assets) => material_asset_map_assets.is_empty(),
        }
    }

    pub fn len(&self) -> usize
    {
        match self {
            Self::Direct(material_assets) => material_assets.len(),
            Self::Maps(material_asset_map_assets) => material_asset_map_assets.len(),
        }
    }
}

impl Default for Materials
{
    fn default() -> Self
    {
        Self::Maps(Vec::new())
    }
}