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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
use ecs::sole::Single;
use crate::asset::{
Asset,
Assets,
Dependency as AssetDependency,
Event as AssetEvent,
Events as AssetEvents,
Handle as AssetHandle,
Label as AssetLabel,
};
use crate::builder;
use crate::color::{Color, Rgb};
use crate::ecs::Component;
use crate::reflection::Reflection;
use crate::texture::Texture;
pub mod asset;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Material
{
pub ambient: Color<f32>,
pub diffuse: Color<f32>,
pub specular: Color<f32>,
pub ambient_map: Option<AssetHandle<Texture>>,
pub diffuse_map: Option<AssetHandle<Texture>>,
pub specular_map: Option<AssetHandle<Texture>>,
pub shininess: f32,
pub loading_textures: Vec<(LoadingTextureName, AssetLabel<'static>)>,
}
impl Material
{
#[must_use]
pub fn builder() -> Builder
{
Builder::new()
}
pub fn textures(&self) -> impl Iterator<Item = &AssetHandle<Texture>>
{
[&self.ambient_map, &self.diffuse_map, &self.specular_map]
.into_iter()
.flatten()
}
}
impl Default for Material
{
fn default() -> Self
{
Self::builder().build()
}
}
impl Asset for Material
{
fn dependencies(&self) -> impl Iterator<Item = AssetDependency<'_>>
{
self.loading_textures.iter().map(|(_, texture_label)| {
AssetDependency::new::<Texture>(AssetLabel {
path: (&*texture_label.path).into(),
name: texture_label.name.as_deref().map(Into::into),
})
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LoadingTextureName
{
AmbientMap,
DiffuseMap,
SpecularMap,
}
/// [`Material`] builder.
#[derive(Debug, Clone)]
pub struct Builder
{
ambient: Color<f32>,
diffuse: Color<f32>,
specular: Color<f32>,
ambient_map: Option<AssetHandle<Texture>>,
diffuse_map: Option<AssetHandle<Texture>>,
specular_map: Option<AssetHandle<Texture>>,
shininess: f32,
loading_textures: Vec<(LoadingTextureName, AssetLabel<'static>)>,
}
impl Builder
{
#[must_use]
pub fn new() -> Self
{
Self {
ambient: Color::Rgb(Rgb::<f32>::white()),
diffuse: Color::Rgb(Rgb::<f32>::white()),
specular: Color::Rgb(Rgb::<f32>::white()),
ambient_map: None,
diffuse_map: None,
specular_map: None,
shininess: 32.0,
loading_textures: Vec::with_capacity(3),
}
}
#[must_use]
pub fn ambient(mut self, ambient: Color<f32>) -> Self
{
self.ambient = ambient;
self
}
#[must_use]
pub fn diffuse(mut self, diffuse: Color<f32>) -> Self
{
self.diffuse = diffuse;
self
}
#[must_use]
pub fn specular(mut self, specular: Color<f32>) -> Self
{
self.specular = specular;
self
}
#[must_use]
pub fn ambient_map(mut self, ambient_map: AssetHandle<Texture>) -> Self
{
self.ambient_map = Some(ambient_map);
self
}
#[must_use]
pub fn diffuse_map(mut self, diffuse_map: AssetHandle<Texture>) -> Self
{
self.diffuse_map = Some(diffuse_map);
self
}
#[must_use]
pub fn specular_map(mut self, specular_map: AssetHandle<Texture>) -> Self
{
self.specular_map = Some(specular_map);
self
}
#[must_use]
pub fn loading_texture(
mut self,
name: LoadingTextureName,
asset_label: AssetLabel<'static>,
) -> Self
{
self.loading_textures.push((name, asset_label));
self
}
#[must_use]
pub fn shininess(mut self, shininess: f32) -> Self
{
self.shininess = shininess;
self
}
/// Builds a new [`Material`].
#[must_use]
pub fn build(self) -> Material
{
Material {
ambient: self.ambient,
diffuse: self.diffuse,
specular: self.specular,
ambient_map: self.ambient_map,
diffuse_map: self.diffuse_map,
specular_map: self.specular_map,
shininess: self.shininess,
loading_textures: self.loading_textures,
}
}
}
impl Default for Builder
{
fn default() -> Self
{
Self::new()
}
}
builder! {
/// Material flags.
#[builder(name = FlagsBuilder, derives = (Debug, Clone))]
#[derive(Debug, Clone, Component, Reflection)]
#[non_exhaustive]
pub struct Flags
{
/// Whether to use material's ambient color instead of the global ambient color.
/// Default is `false`
pub use_ambient_color: bool,
}
}
impl Flags
{
#[must_use]
pub const fn builder() -> FlagsBuilder
{
FlagsBuilder::new()
}
}
impl Default for Flags
{
fn default() -> Self
{
Self::builder().build()
}
}
impl FlagsBuilder
{
#[must_use]
pub const fn new() -> Self
{
Self { use_ambient_color: false }
}
}
impl Default for FlagsBuilder
{
fn default() -> Self
{
Self::new()
}
}
pub(crate) fn set_stored_materials_texture_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::<Material>::from_id(*asset_id);
if !assets.is_loaded_and_has_type(&asset_handle) {
continue;
}
let Some(material_textures) = assets.get(&asset_handle).map(|mat| {
mat.loading_textures
.iter()
.map(|(tex_name, tex_label)| {
(
*tex_name,
assets.get_handle_to_loaded::<Texture>(tex_label.clone()),
)
})
.collect::<Vec<_>>()
}) else {
unreachable!();
};
let Some(material) = assets.get_mut(&asset_handle) else {
unreachable!();
};
for (tex_name, tex_asset) in material_textures {
let Some(tex_asset) = tex_asset else {
unreachable!();
};
let dst = match tex_name {
LoadingTextureName::AmbientMap => &mut material.ambient_map,
LoadingTextureName::DiffuseMap => &mut material.diffuse_map,
LoadingTextureName::SpecularMap => &mut material.specular_map,
};
*dst = Some(tex_asset);
}
}
}
|