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
//! Provides texture formats
//!

pub use self::mesh::*;
pub use self::mtl::*;
pub use self::texture::*;

use amethyst_assets::{AssetPrefab, Format, PrefabData, PrefabError, ProgressCounter};
use amethyst_core::specs::prelude::Entity;
use serde::de::DeserializeOwned;
use serde::Serialize;

use shape::InternalShape;
use {Mesh, ShapePrefab, Texture};

mod mesh;
mod mtl;
mod texture;

/// Internal mesh loading
///
/// ### Type parameters:
///
/// `V`: Vertex format to use for generated `Mesh`es, must be one of:
///     * `Vec<PosTex>`
///     * `Vec<PosNormTex>`
///     * `Vec<PosNormTangTex>`
///     * `ComboMeshCreator`
/// `M`: `Format` to use for loading `Mesh`es from file
#[derive(Deserialize, Serialize)]
pub enum MeshPrefab<V, M>
where
    M: Format<Mesh>,
    M::Options: DeserializeOwned + Serialize,
{
    /// Load an asset Mesh from file
    Asset(AssetPrefab<Mesh, M>),
    /// Generate a Mesh from basic type
    Shape(ShapePrefab<V>),
}

/// `PrefabData` for loading graphics, ie `Mesh` + `Material`
///
/// ### Type parameters:
///
/// `V`: Vertex format to use for generated `Mesh`es, must be one of:
///     * `Vec<PosTex>`
///     * `Vec<PosNormTex>`
///     * `Vec<PosNormTangTex>`
///     * `ComboMeshCreator`
/// - `M`: `Format` to use for loading `Mesh`es from file
/// - `T`: `Format` to use for loading `Texture`s from file
#[derive(Deserialize, Serialize)]
pub struct GraphicsPrefab<V, M = ObjFormat, T = TextureFormat>
where
    M: Format<Mesh>,
    M::Options: DeserializeOwned + Serialize,
    T: Format<Texture, Options = TextureMetadata>,
{
    mesh: MeshPrefab<V, M>,
    material: MaterialPrefab<T>,
}

impl<'a, V, M, T> PrefabData<'a> for GraphicsPrefab<V, M, T>
where
    M: Format<Mesh> + Clone,
    M::Options: Clone + DeserializeOwned + Serialize,
    T: Format<Texture, Options = TextureMetadata> + Sync + Clone,
    V: From<InternalShape> + Into<MeshData>,
{
    type SystemData = (
        <AssetPrefab<Mesh, M> as PrefabData<'a>>::SystemData,
        <MaterialPrefab<T> as PrefabData<'a>>::SystemData,
    );
    type Result = ();

    fn load_prefab(
        &self,
        entity: Entity,
        system_data: &mut <Self as PrefabData>::SystemData,
        entities: &[Entity],
    ) -> Result<(), PrefabError> {
        match self.mesh {
            MeshPrefab::Asset(ref m) => {
                m.load_prefab(entity, &mut system_data.0, entities)?;
            }
            MeshPrefab::Shape(ref s) => {
                s.load_prefab(entity, &mut system_data.0, entities)?;
            }
        }
        self.material
            .load_prefab(entity, &mut system_data.1, entities)?;
        Ok(())
    }

    fn trigger_sub_loading(
        &mut self,
        progress: &mut ProgressCounter,
        system_data: &mut Self::SystemData,
    ) -> Result<bool, PrefabError> {
        let mut ret = false;
        if match self.mesh {
            MeshPrefab::Asset(ref mut m) => m.trigger_sub_loading(progress, &mut system_data.0)?,
            MeshPrefab::Shape(ref mut s) => s.trigger_sub_loading(progress, &mut system_data.0)?,
        } {
            ret = true;
        }
        if self.material
            .trigger_sub_loading(progress, &mut system_data.1)?
        {
            ret = true;
        }
        Ok(ret)
    }
}