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
use amethyst_assets::{Format, Handle, PrefabData, PrefabError, ProgressCounter};
use amethyst_core::specs::prelude::{Entity, ReadExpect, Write, WriteStorage};

use super::{Texture, TextureMetadata, TexturePrefab};
use mtl::{Material, MaterialDefaults, MaterialTextureSet, TextureOffset};
use transparent::Transparent;

/// `PrefabData` for loading `Material`s
///
/// ### Type parameters:
///
/// - `F`: `Format` to use for loading `Texture`s
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct MaterialPrefab<F>
where
    F: Format<Texture, Options = TextureMetadata>,
{
    /// Diffuse map.
    pub albedo: Option<TexturePrefab<F>>,
    /// Diffuse texture offset
    pub albedo_offset: TextureOffset,
    /// Albedo texture animation index, for addition to `MaterialTextureSet`
    pub albedo_id: Option<u64>,
    /// Emission map.
    pub emission: Option<TexturePrefab<F>>,
    /// Emission texture offset
    pub emission_offset: TextureOffset,
    /// Emission texture animation index, for addition to `MaterialTextureSet`
    pub emission_id: Option<u64>,
    /// Normal map.
    pub normal: Option<TexturePrefab<F>>,
    /// Normal texture offset
    pub normal_offset: TextureOffset,
    /// Normal texture animation index, for addition to `MaterialTextureSet`
    pub normal_id: Option<u64>,
    /// Metallic map.
    pub metallic: Option<TexturePrefab<F>>,
    /// Metallic texture offset
    pub metallic_offset: TextureOffset,
    /// Metallic texture animation index, for addition to `MaterialTextureSet`
    pub metallic_id: Option<u64>,
    /// Roughness map.
    pub roughness: Option<TexturePrefab<F>>,
    /// Roughness texture offset
    pub roughness_offset: TextureOffset,
    /// Roughness texture animation index, for addition to `MaterialTextureSet`
    pub roughness_id: Option<u64>,
    /// Ambient occlusion map.
    pub ambient_occlusion: Option<TexturePrefab<F>>,
    /// Ambient occlusion texture offset
    pub ambient_occlusion_offset: TextureOffset,
    /// Ambient occlusion texture animation index, for addition to `MaterialTextureSet`
    pub ambient_occlusion_id: Option<u64>,
    /// Caveat map.
    pub caveat: Option<TexturePrefab<F>>,
    /// Caveat texture offset
    pub caveat_offset: TextureOffset,
    /// Caveat texture animation index, for addition to `MaterialTextureSet`
    pub caveat_id: Option<u64>,
    /// Set material as `Transparent`
    pub transparent: bool,
    /// Alpha cutoff: the value below which we do not draw the pixel
    pub alpha_cutoff: f32,
}

impl<F> Default for MaterialPrefab<F>
where
    F: Format<Texture, Options = TextureMetadata>,
{
    fn default() -> Self {
        MaterialPrefab {
            albedo: None,
            albedo_offset: TextureOffset::default(),
            albedo_id: None,
            emission: None,
            emission_offset: TextureOffset::default(),
            emission_id: None,
            normal: None,
            normal_offset: TextureOffset::default(),
            normal_id: None,
            metallic: None,
            metallic_offset: TextureOffset::default(),
            metallic_id: None,
            roughness: None,
            roughness_offset: TextureOffset::default(),
            roughness_id: None,
            ambient_occlusion: None,
            ambient_occlusion_offset: TextureOffset::default(),
            ambient_occlusion_id: None,
            caveat: None,
            caveat_offset: TextureOffset::default(),
            caveat_id: None,
            transparent: false,
            alpha_cutoff: 0.01,
        }
    }
}

fn load_handle<F>(
    entity: Entity,
    index: Option<u64>,
    prefab: &Option<TexturePrefab<F>>,
    texture_set: &mut MaterialTextureSet,
    tp_data: &mut <TexturePrefab<F> as PrefabData>::SystemData,
    def: &Handle<Texture>,
) -> Handle<Texture>
where
    F: Format<Texture, Options = TextureMetadata> + Sync + Clone,
{
    index
        .and_then(|i| texture_set.handle(i))
        .unwrap_or_else(|| {
            let handle = prefab
                .as_ref()
                .and_then(|tp| tp.load_prefab(entity, tp_data, &[]).ok());
            if let (&Some(ref index), &Some(ref handle)) = (&index, &handle) {
                texture_set.insert(*index, handle.clone());
            }
            handle.unwrap_or(def.clone())
        })
}

impl<'a, F> PrefabData<'a> for MaterialPrefab<F>
where
    F: Format<Texture, Options = TextureMetadata> + Sync + Clone,
{
    type SystemData = (
        WriteStorage<'a, Material>,
        ReadExpect<'a, MaterialDefaults>,
        Write<'a, MaterialTextureSet>,
        <TexturePrefab<F> as PrefabData<'a>>::SystemData,
        WriteStorage<'a, Transparent>,
    );
    type Result = ();

    fn load_prefab(
        &self,
        entity: Entity,
        system_data: &mut Self::SystemData,
        _: &[Entity],
    ) -> Result<(), PrefabError> {
        let &mut (
            ref mut material,
            ref mat_default,
            ref mut texture_set,
            ref mut tp_data,
            ref mut transparent,
        ) = system_data;
        let mtl = Material {
            albedo: load_handle(
                entity,
                self.albedo_id,
                &self.albedo,
                texture_set,
                tp_data,
                &mat_default.0.albedo,
            ),
            albedo_offset: self.albedo_offset.clone(),
            emission: load_handle(
                entity,
                self.emission_id,
                &self.emission,
                texture_set,
                tp_data,
                &mat_default.0.emission,
            ),
            emission_offset: self.emission_offset.clone(),
            normal: load_handle(
                entity,
                self.normal_id,
                &self.normal,
                texture_set,
                tp_data,
                &mat_default.0.normal,
            ),
            normal_offset: self.normal_offset.clone(),
            metallic: load_handle(
                entity,
                self.metallic_id,
                &self.metallic,
                texture_set,
                tp_data,
                &mat_default.0.metallic,
            ),
            metallic_offset: self.metallic_offset.clone(),
            roughness: load_handle(
                entity,
                self.roughness_id,
                &self.roughness,
                texture_set,
                tp_data,
                &mat_default.0.roughness,
            ),
            roughness_offset: self.roughness_offset.clone(),
            ambient_occlusion: load_handle(
                entity,
                self.ambient_occlusion_id,
                &self.ambient_occlusion,
                texture_set,
                tp_data,
                &mat_default.0.ambient_occlusion,
            ),
            ambient_occlusion_offset: self.ambient_occlusion_offset.clone(),
            caveat: load_handle(
                entity,
                self.caveat_id,
                &self.caveat,
                texture_set,
                tp_data,
                &mat_default.0.caveat,
            ),
            caveat_offset: self.caveat_offset.clone(),
            alpha_cutoff: self.alpha_cutoff,
        };
        material.insert(entity, mtl)?;
        if self.transparent {
            transparent.insert(entity, Transparent)?;
        }
        Ok(())
    }

    fn trigger_sub_loading(
        &mut self,
        progress: &mut ProgressCounter,
        system_data: &mut Self::SystemData,
    ) -> Result<bool, PrefabError> {
        let mut ret = false;
        if let Some(ref mut texture) = self.albedo {
            if texture.trigger_sub_loading(progress, &mut system_data.3)? {
                ret = true;
            }
        }
        if let Some(ref mut texture) = self.emission {
            if texture.trigger_sub_loading(progress, &mut system_data.3)? {
                ret = true;
            }
        }
        if let Some(ref mut texture) = self.normal {
            if texture.trigger_sub_loading(progress, &mut system_data.3)? {
                ret = true;
            }
        }
        if let Some(ref mut texture) = self.metallic {
            if texture.trigger_sub_loading(progress, &mut system_data.3)? {
                ret = true;
            }
        }
        if let Some(ref mut texture) = self.roughness {
            if texture.trigger_sub_loading(progress, &mut system_data.3)? {
                ret = true;
            }
        }
        if let Some(ref mut texture) = self.ambient_occlusion {
            if texture.trigger_sub_loading(progress, &mut system_data.3)? {
                ret = true;
            }
        }
        if let Some(ref mut texture) = self.caveat {
            if texture.trigger_sub_loading(progress, &mut system_data.3)? {
                ret = true;
            }
        }
        Ok(ret)
    }
}