fix(obj): assign correct type to textures that have already been loaded. implement .mtl transparency keyword

This commit is contained in:
2023-11-03 02:43:05 +02:00
parent 8decddbf14
commit b403495da2
+11 -2
View File
@@ -48,6 +48,7 @@ enum MtlToken {
SpecularExponent, SpecularExponent,
Refraction, Refraction,
Opacity, Opacity,
Transparency,
AmbientTexture, AmbientTexture,
DiffuseTexture, DiffuseTexture,
SpecularTexture, SpecularTexture,
@@ -70,6 +71,7 @@ impl MtlToken {
"Ns" => Some(MtlToken::SpecularExponent), "Ns" => Some(MtlToken::SpecularExponent),
"Ni" => Some(MtlToken::Refraction), "Ni" => Some(MtlToken::Refraction),
"d" => Some(MtlToken::Opacity), "d" => Some(MtlToken::Opacity),
"Tr" => Some(MtlToken::Transparency),
"map_Ka" => Some(MtlToken::AmbientTexture), "map_Ka" => Some(MtlToken::AmbientTexture),
"map_Kd" => Some(MtlToken::DiffuseTexture), "map_Kd" => Some(MtlToken::DiffuseTexture),
"map_Ks" => Some(MtlToken::SpecularTexture), "map_Ks" => Some(MtlToken::SpecularTexture),
@@ -142,6 +144,10 @@ fn parse_mtl(path: &PathBuf, obj_textures: &mut HashMap<String, Texture>) -> Res
Some(MtlToken::Opacity) => { Some(MtlToken::Opacity) => {
opacity = iter.next().unwrap().parse::<f32>().unwrap(); opacity = iter.next().unwrap().parse::<f32>().unwrap();
} }
Some(MtlToken::Transparency) => {
// it's just opposite of opacity so we subtract it from 1.0
opacity = 1.0 - iter.next().unwrap().parse::<f32>().unwrap();
}
Some(MtlToken::DiffuseTexture) Some(MtlToken::DiffuseTexture)
| Some(MtlToken::AmbientTexture) | Some(MtlToken::AmbientTexture)
| Some(MtlToken::SpecularTexture) | Some(MtlToken::SpecularTexture)
@@ -149,8 +155,11 @@ fn parse_mtl(path: &PathBuf, obj_textures: &mut HashMap<String, Texture>) -> Res
let tex_type = TextureType::from_material_str(token).unwrap(); let tex_type = TextureType::from_material_str(token).unwrap();
let name = iter.next().unwrap().to_string(); let name = iter.next().unwrap().to_string();
let mat = if obj_textures.contains_key(&name) { let tex = if obj_textures.contains_key(&name) {
obj_textures.get(&name).unwrap().clone() let mut tex = obj_textures.get(&name).unwrap().clone();
tex.typ = tex_type;
tex
} else { } else {
let path = path.parent().unwrap().join(&name); let path = path.parent().unwrap().join(&name);
let tex = match Texture::new(path, tex_type) { let tex = match Texture::new(path, tex_type) {