From b2ac7b878f21f74da10750e44843fe5d40fc6dac Mon Sep 17 00:00:00 2001 From: arkeon Date: Tue, 27 May 2014 09:11:27 +0200 Subject: [PATCH] Correction on FBX material texture UV index (keep the previous method for "allMaterials" option, this should be change too since the materials indexes used are not the good ones, or just remove the object / material test --- code/FBXConverter.cpp | 171 +++++++++++++++++++++++++-------------- code/FBXImportSettings.h | 2 +- 2 files changed, 111 insertions(+), 62 deletions(-) diff --git a/code/FBXConverter.cpp b/code/FBXConverter.cpp index 1cbdbe9d4..89854fc8e 100644 --- a/code/FBXConverter.cpp +++ b/code/FBXConverter.cpp @@ -121,7 +121,7 @@ public: if(mat) { if (materials_converted.find(mat) == materials_converted.end()) { - ConvertMaterial(*mat); + ConvertMaterial(*mat, 0); } } } @@ -1381,7 +1381,7 @@ private: return; } - out->mMaterialIndex = ConvertMaterial(*mat); + out->mMaterialIndex = ConvertMaterial(*mat, &geo); materials_converted[mat] = out->mMaterialIndex; } @@ -1411,7 +1411,7 @@ private: // ------------------------------------------------------------------------------------------------ // Material -> aiMaterial - unsigned int ConvertMaterial(const Material& material) + unsigned int ConvertMaterial(const Material& material, const MeshGeometry* const mesh) { const PropertyTable& props = material.Props(); @@ -1440,8 +1440,8 @@ private: SetShadingPropertiesCommon(out_mat,props); // texture assignments - SetTextureProperties(out_mat,material.Textures()); - SetTextureProperties(out_mat,material.LayeredTextures()); + SetTextureProperties(out_mat,material.Textures(), mesh); + SetTextureProperties(out_mat,material.LayeredTextures(), mesh); return static_cast(materials.size() - 1); } @@ -1450,7 +1450,7 @@ private: // ------------------------------------------------------------------------------------------------ void TrySetTextureProperties(aiMaterial* out_mat, const TextureMap& textures, const std::string& propName, - aiTextureType target) + aiTextureType target, const MeshGeometry* const mesh) { TextureMap::const_iterator it = textures.find(propName); if(it == textures.end()) { @@ -1495,18 +1495,48 @@ private: std::find(materials.begin(),materials.end(),out_mat) )); - uvIndex = -1; - BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) { - const MeshGeometry* const mesh = dynamic_cast (v.first); - if(!mesh) { - continue; - } - const MatIndexArray& mats = mesh->GetMaterialIndices(); - if(std::find(mats.begin(),mats.end(),matIndex) == mats.end()) { - continue; - } + uvIndex = -1; + if (!mesh) + { + BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) { + const MeshGeometry* const mesh = dynamic_cast (v.first); + if(!mesh) { + continue; + } + const MatIndexArray& mats = mesh->GetMaterialIndices(); + if(std::find(mats.begin(),mats.end(),matIndex) == mats.end()) { + continue; + } + + int index = -1; + for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { + if(mesh->GetTextureCoords(i).empty()) { + break; + } + const std::string& name = mesh->GetTextureCoordChannelName(i); + if(name == uvSet) { + index = static_cast(i); + break; + } + } + if(index == -1) { + FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material"); + continue; + } + + if(uvIndex == -1) { + uvIndex = index; + } + else { + FBXImporter::LogWarn("the UV channel named " + uvSet + + " appears at different positions in meshes, results will be wrong"); + } + } + } + else + { int index = -1; for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { if(mesh->GetTextureCoords(i).empty()) { @@ -1520,17 +1550,12 @@ private: } if(index == -1) { FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material"); - continue; } if(uvIndex == -1) { uvIndex = index; } - else { - FBXImporter::LogWarn("the UV channel named " + uvSet + - " appears at different positions in meshes, results will be wrong"); - } - } + } if(uvIndex == -1) { FBXImporter::LogWarn("failed to resolve UV channel " + uvSet + ", using first UV channel"); @@ -1546,7 +1571,7 @@ private: // ------------------------------------------------------------------------------------------------ void TrySetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures, const std::string& propName, - aiTextureType target) + aiTextureType target, const MeshGeometry* const mesh) { LayeredTextureMap::const_iterator it = layeredTextures.find(propName); if(it == layeredTextures.end()) { @@ -1590,18 +1615,47 @@ private: std::find(materials.begin(),materials.end(),out_mat) )); - uvIndex = -1; - BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) { - const MeshGeometry* const mesh = dynamic_cast (v.first); - if(!mesh) { - continue; - } + uvIndex = -1; + if (!mesh) + { + BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) { + const MeshGeometry* const mesh = dynamic_cast (v.first); + if(!mesh) { + continue; + } - const MatIndexArray& mats = mesh->GetMaterialIndices(); - if(std::find(mats.begin(),mats.end(),matIndex) == mats.end()) { - continue; - } + const MatIndexArray& mats = mesh->GetMaterialIndices(); + if(std::find(mats.begin(),mats.end(),matIndex) == mats.end()) { + continue; + } + int index = -1; + for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { + if(mesh->GetTextureCoords(i).empty()) { + break; + } + const std::string& name = mesh->GetTextureCoordChannelName(i); + if(name == uvSet) { + index = static_cast(i); + break; + } + } + if(index == -1) { + FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material"); + continue; + } + + if(uvIndex == -1) { + uvIndex = index; + } + else { + FBXImporter::LogWarn("the UV channel named " + uvSet + + " appears at different positions in meshes, results will be wrong"); + } + } + } + else + { int index = -1; for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { if(mesh->GetTextureCoords(i).empty()) { @@ -1615,17 +1669,12 @@ private: } if(index == -1) { FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material"); - continue; } if(uvIndex == -1) { uvIndex = index; } - else { - FBXImporter::LogWarn("the UV channel named " + uvSet + - " appears at different positions in meshes, results will be wrong"); - } - } + } if(uvIndex == -1) { FBXImporter::LogWarn("failed to resolve UV channel " + uvSet + ", using first UV channel"); @@ -1638,33 +1687,33 @@ private: } // ------------------------------------------------------------------------------------------------ - void SetTextureProperties(aiMaterial* out_mat, const TextureMap& textures) + void SetTextureProperties(aiMaterial* out_mat, const TextureMap& textures, const MeshGeometry* const mesh) { - TrySetTextureProperties(out_mat, textures, "DiffuseColor", aiTextureType_DIFFUSE); - TrySetTextureProperties(out_mat, textures, "AmbientColor", aiTextureType_AMBIENT); - TrySetTextureProperties(out_mat, textures, "EmissiveColor", aiTextureType_EMISSIVE); - TrySetTextureProperties(out_mat, textures, "SpecularColor", aiTextureType_SPECULAR); - TrySetTextureProperties(out_mat, textures, "TransparentColor", aiTextureType_OPACITY); - TrySetTextureProperties(out_mat, textures, "ReflectionColor", aiTextureType_REFLECTION); - TrySetTextureProperties(out_mat, textures, "DisplacementColor", aiTextureType_DISPLACEMENT); - TrySetTextureProperties(out_mat, textures, "NormalMap", aiTextureType_NORMALS); - TrySetTextureProperties(out_mat, textures, "Bump", aiTextureType_HEIGHT); - TrySetTextureProperties(out_mat, textures, "ShininessExponent", aiTextureType_SHININESS); + TrySetTextureProperties(out_mat, textures, "DiffuseColor", aiTextureType_DIFFUSE, mesh); + TrySetTextureProperties(out_mat, textures, "AmbientColor", aiTextureType_AMBIENT, mesh); + TrySetTextureProperties(out_mat, textures, "EmissiveColor", aiTextureType_EMISSIVE, mesh); + TrySetTextureProperties(out_mat, textures, "SpecularColor", aiTextureType_SPECULAR, mesh); + TrySetTextureProperties(out_mat, textures, "TransparentColor", aiTextureType_OPACITY, mesh); + TrySetTextureProperties(out_mat, textures, "ReflectionColor", aiTextureType_REFLECTION, mesh); + TrySetTextureProperties(out_mat, textures, "DisplacementColor", aiTextureType_DISPLACEMENT, mesh); + TrySetTextureProperties(out_mat, textures, "NormalMap", aiTextureType_NORMALS, mesh); + TrySetTextureProperties(out_mat, textures, "Bump", aiTextureType_HEIGHT, mesh); + TrySetTextureProperties(out_mat, textures, "ShininessExponent", aiTextureType_SHININESS, mesh); } // ------------------------------------------------------------------------------------------------ - void SetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures) + void SetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures, const MeshGeometry* const mesh) { - TrySetTextureProperties(out_mat, layeredTextures, "DiffuseColor", aiTextureType_DIFFUSE); - TrySetTextureProperties(out_mat, layeredTextures, "AmbientColor", aiTextureType_AMBIENT); - TrySetTextureProperties(out_mat, layeredTextures, "EmissiveColor", aiTextureType_EMISSIVE); - TrySetTextureProperties(out_mat, layeredTextures, "SpecularColor", aiTextureType_SPECULAR); - TrySetTextureProperties(out_mat, layeredTextures, "TransparentColor", aiTextureType_OPACITY); - TrySetTextureProperties(out_mat, layeredTextures, "ReflectionColor", aiTextureType_REFLECTION); - TrySetTextureProperties(out_mat, layeredTextures, "DisplacementColor", aiTextureType_DISPLACEMENT); - TrySetTextureProperties(out_mat, layeredTextures, "NormalMap", aiTextureType_NORMALS); - TrySetTextureProperties(out_mat, layeredTextures, "Bump", aiTextureType_HEIGHT); - TrySetTextureProperties(out_mat, layeredTextures, "ShininessExponent", aiTextureType_SHININESS); + TrySetTextureProperties(out_mat, layeredTextures, "DiffuseColor", aiTextureType_DIFFUSE, mesh); + TrySetTextureProperties(out_mat, layeredTextures, "AmbientColor", aiTextureType_AMBIENT, mesh); + TrySetTextureProperties(out_mat, layeredTextures, "EmissiveColor", aiTextureType_EMISSIVE, mesh); + TrySetTextureProperties(out_mat, layeredTextures, "SpecularColor", aiTextureType_SPECULAR, mesh); + TrySetTextureProperties(out_mat, layeredTextures, "TransparentColor", aiTextureType_OPACITY, mesh); + TrySetTextureProperties(out_mat, layeredTextures, "ReflectionColor", aiTextureType_REFLECTION, mesh); + TrySetTextureProperties(out_mat, layeredTextures, "DisplacementColor", aiTextureType_DISPLACEMENT, mesh); + TrySetTextureProperties(out_mat, layeredTextures, "NormalMap", aiTextureType_NORMALS, mesh); + TrySetTextureProperties(out_mat, layeredTextures, "Bump", aiTextureType_HEIGHT, mesh); + TrySetTextureProperties(out_mat, layeredTextures, "ShininessExponent", aiTextureType_SHININESS, mesh); } diff --git a/code/FBXImportSettings.h b/code/FBXImportSettings.h index 47fc1d6a6..2020273d7 100644 --- a/code/FBXImportSettings.h +++ b/code/FBXImportSettings.h @@ -53,7 +53,7 @@ struct ImportSettings ImportSettings() : strictMode(true) , readAllLayers(true) - , readAllMaterials() + , readAllMaterials(false) , readMaterials(true) , readCameras(true) , readLights(true)