pub fn load_material(
    material: &gltf::Material,
    buffers: &Buffers,
    source: Arc<Source>,
    name: &str,
) -> Result<MaterialPrefab<TextureFormat>, GltfError> {
    let mut prefab = MaterialPrefab::default();
    prefab.albedo = Some(load_texture_with_factor(
        material.pbr_metallic_roughness().base_color_texture(),
        material.pbr_metallic_roughness().base_color_factor(),
        buffers,
        source.clone(),
        name,
    ).map(|(texture, _)| TexturePrefab::Data(texture))?);
    prefab.albedo_id = material.index().map(|i| i as u64 * 100);

    let (metallic, roughness) =
        load_texture_with_factor(
            material
                .pbr_metallic_roughness()
                .metallic_roughness_texture(),
            [
                material.pbr_metallic_roughness().metallic_factor(),
                material.pbr_metallic_roughness().roughness_factor(),
                1.0,
                1.0,
            ],
            buffers,
            source.clone(),
            name,
        ).map(|(texture, factors)| deconstruct_metallic_roughness(texture, factors[0], factors[1]))
            .map(|(metallic, roughness)| {
                (
                    TexturePrefab::Data(metallic.0),
                    TexturePrefab::Data(roughness.0),
                )
            })?;
    prefab.metallic = Some(metallic);
    prefab.metallic_id = material.index().map(|i| i as u64 * 10 + 1);
    prefab.roughness = Some(roughness);
    prefab.roughness_id = material.index().map(|i| i as u64 * 10 + 2);

    let em_factor = material.emissive_factor();
    prefab.emission = Some(load_texture_with_factor(
        material.emissive_texture(),
        [em_factor[0], em_factor[1], em_factor[2], 1.0],
        buffers,
        source.clone(),
        name,
    ).map(|(texture, _)| TexturePrefab::Data(texture))?);
    prefab.emission_id = material.index().map(|i| i as u64 * 10 + 3);

    // Can't use map/and_then because of Result returning from the load_texture function
    prefab.normal = match material.normal_texture() {
        Some(normal_texture) => {
            Some(
                load_texture(&normal_texture.texture(), buffers, source.clone(), name)
                    .map(|data| TexturePrefab::Data(data))?,
            )
        }

        None => None,
    };
    prefab.normal_id = material.index().map(|i| i as u64 * 10 + 4);

    // Can't use map/and_then because of Result returning from the load_texture function
    prefab.ambient_occlusion = match material.occlusion_texture() {
        Some(occlusion_texture) => {
            Some(
                load_texture(&occlusion_texture.texture(), buffers, source.clone(), name)
                    .map(|data| TexturePrefab::Data(data))?,
            )
        }

        None => None,
    };
    prefab.ambient_occlusion_id = material.index().map(|i| i as u64 * 10 + 5);
    prefab.transparent = if let AlphaMode::Blend = material.alpha_mode() {
        true
    } else {
        false
    };
    if let AlphaMode::Mask = material.alpha_mode() {
        prefab.alpha_cutoff = material.alpha_cutoff();
    }

    Ok(prefab)
}
// Load a single material, and transform into a format usable by the engine
Load a single material, and transform into a format usable by the engine
Load a single material and transform into a format usable by the engine
pub fn load_material(
    material: &gltf::Material,
    buffers: &Buffers,
    source: Arc<Source>,
    name: &str,
) -> Result<MaterialPrefab<TextureFormat>, GltfError> {
    let mut prefab = MaterialPrefab::default();
    prefab.albedo = Some(load_texture_with_factor(
        material.pbr_metallic_roughness().base_color_texture(),
        material.pbr_metallic_roughness().base_color_factor(),
        buffers,
        source.clone(),
        name,
    ).map(|(texture, _)| TexturePrefab::Data(texture))?);
    prefab.albedo_id = material.index().map(|i| i as u64 * 100);
    let (metallic, roughness) =
        load_texture_with_factor(
            material
                .pbr_metallic_roughness()
                .metallic_roughness_texture(),
            [
                material.pbr_metallic_roughness().metallic_factor(),
                material.pbr_metallic_roughness().roughness_factor(),
                1.0,
                1.0,
            ],
            buffers,
            source.clone(),
            name,
        ).map(|(texture, factors)| deconstruct_metallic_roughness(texture, factors[0], factors[1]))
            .map(|(metallic, roughness)| {
                (
                    TexturePrefab::Data(metallic.0),
                    TexturePrefab::Data(roughness.0),
                )
            })?;
    prefab.metallic = Some(metallic);
    prefab.metallic_id = material.index().map(|i| i as u64 * 10 + 1);
    prefab.roughness = Some(roughness);
    prefab.roughness_id = material.index().map(|i| i as u64 * 10 + 2);
    let em_factor = material.emissive_factor();
    prefab.emission = Some(load_texture_with_factor(
        material.emissive_texture(),
        [em_factor[0], em_factor[1], em_factor[2], 1.0],
        buffers,
        source.clone(),
        name,
    ).map(|(texture, _)| TexturePrefab::Data(texture))?);
    prefab.emission_id = material.index().map(|i| i as u64 * 10 + 3);
    // Can't use map/and_then because of Result returning from the load_texture function
    prefab.normal = match material.normal_texture() {
        Some(normal_texture) => {
            Some(
                load_texture(&normal_texture.texture(), buffers, source.clone(), name)
                    .map(|data| TexturePrefab::Data(data))?,
            )
        }
        None => None,
    };
    prefab.normal_id = material.index().map(|i| i as u64 * 10 + 4);
    // Can't use map/and_then because of Result returning from the load_texture function
    prefab.ambient_occlusion = match material.occlusion_texture() {
        Some(occlusion_texture) => {
            Some(
                load_texture(&occlusion_texture.texture(), buffers, source.clone(), name)
                    .map(|data| TexturePrefab::Data(data))?,
            )
        }
        None => None,
    };
    prefab.ambient_occlusion_id = material.index().map(|i| i as u64 * 10 + 5);
    prefab.transparent = if let AlphaMode::Blend = material.alpha_mode() {
        true
    } else {
        false
    };
    if let AlphaMode::Mask = material.alpha_mode() {
        prefab.alpha_cutoff = material.alpha_cutoff();
    }
    Ok(prefab)
}
pub fn load_material ( material : & gltf :: Material , buffers : & Buffers , source : Arc < Source > , name : & str , ) -> Result < MaterialPrefab < TextureFormat > , GltfError > { let mut prefab = MaterialPrefab :: default ( ) ; prefab . albedo = Some ( load_texture_with_factor ( material . pbr_metallic_roughness ( ) . base_color_texture ( ) , material . pbr_metallic_roughness ( ) . base_color_factor ( ) , buffers , source . clone ( ) , name , ) . map ( | ( texture , _ ) | TexturePrefab :: Data ( texture ) ) ? ) ; prefab . albedo_id = material . index ( ) . map ( | i | i as u64 * 100 ) ; let ( metallic , roughness ) = load_texture_with_factor ( material . pbr_metallic_roughness ( ) . metallic_roughness_texture ( ) , [ material . pbr_metallic_roughness ( ) . metallic_factor ( ) , material . pbr_metallic_roughness ( ) . roughness_factor ( ) , 1.0 , 1.0 , ] , buffers , source . clone ( ) , name , ) . map ( | ( texture , factors ) | deconstruct_metallic_roughness ( texture , factors [ 0 ] , factors [ 1 ] ) ) . map ( | ( metallic , roughness ) | { ( TexturePrefab :: Data ( metallic . 0 ) , TexturePrefab :: Data ( roughness . 0 ) , ) } ) ? ; prefab . metallic = Some ( metallic ) ; prefab . metallic_id = material . index ( ) . map ( | i | i as u64 * 10 + 1 ) ; prefab . roughness = Some ( roughness ) ; prefab . roughness_id = material . index ( ) . map ( | i | i as u64 * 10 + 2 ) ; let em_factor = material . emissive_factor ( ) ; prefab . emission = Some ( load_texture_with_factor ( material . emissive_texture ( ) , [ em_factor [ 0 ] , em_factor [ 1 ] , em_factor [ 2 ] , 1.0 ] , buffers , source . clone ( ) , name , ) . map ( | ( texture , _ ) | TexturePrefab :: Data ( texture ) ) ? ) ; prefab . emission_id = material . index ( ) . map ( | i | i as u64 * 10 + 3 ) ; // Can't use map/and_then because of Result returning from the load_texture function prefab . normal = match material . normal_texture ( ) { Some ( normal_texture ) => { Some ( load_texture ( & normal_texture . texture ( ) , buffers , source . clone ( ) , name ) . map ( | data | TexturePrefab :: Data ( data ) ) ? , ) } None => None , } ; prefab . normal_id = material . index ( ) . map ( | i | i as u64 * 10 + 4 ) ; // Can't use map/and_then because of Result returning from the load_texture function prefab . ambient_occlusion = match material . occlusion_texture ( ) { Some ( occlusion_texture ) => { Some ( load_texture ( & occlusion_texture . texture ( ) , buffers , source . clone ( ) , name ) . map ( | data | TexturePrefab :: Data ( data ) ) ? , ) } None => None , } ; prefab . ambient_occlusion_id = material . index ( ) . map ( | i | i as u64 * 10 + 5 ) ; prefab . transparent = if let AlphaMode :: Blend = material . alpha_mode ( ) { true } else { false } ; if let AlphaMode :: Mask = material . alpha_mode ( ) { prefab . alpha_cutoff = material . alpha_cutoff ( ) ; } Ok ( prefab ) }
Load a single material, and transform into a format usable by the engine
Load a single material and transform into a format usable by the engine
material
&gltf::Material
buffers
&Buffers
source
Arc<Source>
name
&str
material
&gltf::Material
buffers
&Buffers
source
Arc<Source>
name
&str