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
pull/287/head
arkeon 2014-05-27 09:11:27 +02:00
parent 62554a055d
commit b2ac7b878f
2 changed files with 111 additions and 62 deletions

View File

@ -121,7 +121,7 @@ public:
if(mat) { if(mat) {
if (materials_converted.find(mat) == materials_converted.end()) { if (materials_converted.find(mat) == materials_converted.end()) {
ConvertMaterial(*mat); ConvertMaterial(*mat, 0);
} }
} }
} }
@ -1381,7 +1381,7 @@ private:
return; return;
} }
out->mMaterialIndex = ConvertMaterial(*mat); out->mMaterialIndex = ConvertMaterial(*mat, &geo);
materials_converted[mat] = out->mMaterialIndex; materials_converted[mat] = out->mMaterialIndex;
} }
@ -1411,7 +1411,7 @@ private:
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Material -> aiMaterial // Material -> aiMaterial
unsigned int ConvertMaterial(const Material& material) unsigned int ConvertMaterial(const Material& material, const MeshGeometry* const mesh)
{ {
const PropertyTable& props = material.Props(); const PropertyTable& props = material.Props();
@ -1440,8 +1440,8 @@ private:
SetShadingPropertiesCommon(out_mat,props); SetShadingPropertiesCommon(out_mat,props);
// texture assignments // texture assignments
SetTextureProperties(out_mat,material.Textures()); SetTextureProperties(out_mat,material.Textures(), mesh);
SetTextureProperties(out_mat,material.LayeredTextures()); SetTextureProperties(out_mat,material.LayeredTextures(), mesh);
return static_cast<unsigned int>(materials.size() - 1); return static_cast<unsigned int>(materials.size() - 1);
} }
@ -1450,7 +1450,7 @@ private:
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void TrySetTextureProperties(aiMaterial* out_mat, const TextureMap& textures, void TrySetTextureProperties(aiMaterial* out_mat, const TextureMap& textures,
const std::string& propName, const std::string& propName,
aiTextureType target) aiTextureType target, const MeshGeometry* const mesh)
{ {
TextureMap::const_iterator it = textures.find(propName); TextureMap::const_iterator it = textures.find(propName);
if(it == textures.end()) { if(it == textures.end()) {
@ -1495,7 +1495,10 @@ private:
std::find(materials.begin(),materials.end(),out_mat) std::find(materials.begin(),materials.end(),out_mat)
)); ));
uvIndex = -1; uvIndex = -1;
if (!mesh)
{
BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) { BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) {
const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*> (v.first); const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*> (v.first);
if(!mesh) { if(!mesh) {
@ -1531,6 +1534,28 @@ private:
" appears at different positions in meshes, results will be wrong"); " 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()) {
break;
}
const std::string& name = mesh->GetTextureCoordChannelName(i);
if(name == uvSet) {
index = static_cast<int>(i);
break;
}
}
if(index == -1) {
FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
}
if(uvIndex == -1) {
uvIndex = index;
}
}
if(uvIndex == -1) { if(uvIndex == -1) {
FBXImporter::LogWarn("failed to resolve UV channel " + uvSet + ", using first UV channel"); 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, void TrySetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures,
const std::string& propName, const std::string& propName,
aiTextureType target) aiTextureType target, const MeshGeometry* const mesh)
{ {
LayeredTextureMap::const_iterator it = layeredTextures.find(propName); LayeredTextureMap::const_iterator it = layeredTextures.find(propName);
if(it == layeredTextures.end()) { if(it == layeredTextures.end()) {
@ -1591,6 +1616,8 @@ private:
)); ));
uvIndex = -1; uvIndex = -1;
if (!mesh)
{
BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) { BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) {
const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*> (v.first); const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*> (v.first);
if(!mesh) { if(!mesh) {
@ -1626,6 +1653,28 @@ private:
" appears at different positions in meshes, results will be wrong"); " 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()) {
break;
}
const std::string& name = mesh->GetTextureCoordChannelName(i);
if(name == uvSet) {
index = static_cast<int>(i);
break;
}
}
if(index == -1) {
FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
}
if(uvIndex == -1) {
uvIndex = index;
}
}
if(uvIndex == -1) { if(uvIndex == -1) {
FBXImporter::LogWarn("failed to resolve UV channel " + uvSet + ", using first UV channel"); 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, "DiffuseColor", aiTextureType_DIFFUSE, mesh);
TrySetTextureProperties(out_mat, textures, "AmbientColor", aiTextureType_AMBIENT); TrySetTextureProperties(out_mat, textures, "AmbientColor", aiTextureType_AMBIENT, mesh);
TrySetTextureProperties(out_mat, textures, "EmissiveColor", aiTextureType_EMISSIVE); TrySetTextureProperties(out_mat, textures, "EmissiveColor", aiTextureType_EMISSIVE, mesh);
TrySetTextureProperties(out_mat, textures, "SpecularColor", aiTextureType_SPECULAR); TrySetTextureProperties(out_mat, textures, "SpecularColor", aiTextureType_SPECULAR, mesh);
TrySetTextureProperties(out_mat, textures, "TransparentColor", aiTextureType_OPACITY); TrySetTextureProperties(out_mat, textures, "TransparentColor", aiTextureType_OPACITY, mesh);
TrySetTextureProperties(out_mat, textures, "ReflectionColor", aiTextureType_REFLECTION); TrySetTextureProperties(out_mat, textures, "ReflectionColor", aiTextureType_REFLECTION, mesh);
TrySetTextureProperties(out_mat, textures, "DisplacementColor", aiTextureType_DISPLACEMENT); TrySetTextureProperties(out_mat, textures, "DisplacementColor", aiTextureType_DISPLACEMENT, mesh);
TrySetTextureProperties(out_mat, textures, "NormalMap", aiTextureType_NORMALS); TrySetTextureProperties(out_mat, textures, "NormalMap", aiTextureType_NORMALS, mesh);
TrySetTextureProperties(out_mat, textures, "Bump", aiTextureType_HEIGHT); TrySetTextureProperties(out_mat, textures, "Bump", aiTextureType_HEIGHT, mesh);
TrySetTextureProperties(out_mat, textures, "ShininessExponent", aiTextureType_SHININESS); 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, "DiffuseColor", aiTextureType_DIFFUSE, mesh);
TrySetTextureProperties(out_mat, layeredTextures, "AmbientColor", aiTextureType_AMBIENT); TrySetTextureProperties(out_mat, layeredTextures, "AmbientColor", aiTextureType_AMBIENT, mesh);
TrySetTextureProperties(out_mat, layeredTextures, "EmissiveColor", aiTextureType_EMISSIVE); TrySetTextureProperties(out_mat, layeredTextures, "EmissiveColor", aiTextureType_EMISSIVE, mesh);
TrySetTextureProperties(out_mat, layeredTextures, "SpecularColor", aiTextureType_SPECULAR); TrySetTextureProperties(out_mat, layeredTextures, "SpecularColor", aiTextureType_SPECULAR, mesh);
TrySetTextureProperties(out_mat, layeredTextures, "TransparentColor", aiTextureType_OPACITY); TrySetTextureProperties(out_mat, layeredTextures, "TransparentColor", aiTextureType_OPACITY, mesh);
TrySetTextureProperties(out_mat, layeredTextures, "ReflectionColor", aiTextureType_REFLECTION); TrySetTextureProperties(out_mat, layeredTextures, "ReflectionColor", aiTextureType_REFLECTION, mesh);
TrySetTextureProperties(out_mat, layeredTextures, "DisplacementColor", aiTextureType_DISPLACEMENT); TrySetTextureProperties(out_mat, layeredTextures, "DisplacementColor", aiTextureType_DISPLACEMENT, mesh);
TrySetTextureProperties(out_mat, layeredTextures, "NormalMap", aiTextureType_NORMALS); TrySetTextureProperties(out_mat, layeredTextures, "NormalMap", aiTextureType_NORMALS, mesh);
TrySetTextureProperties(out_mat, layeredTextures, "Bump", aiTextureType_HEIGHT); TrySetTextureProperties(out_mat, layeredTextures, "Bump", aiTextureType_HEIGHT, mesh);
TrySetTextureProperties(out_mat, layeredTextures, "ShininessExponent", aiTextureType_SHININESS); TrySetTextureProperties(out_mat, layeredTextures, "ShininessExponent", aiTextureType_SHININESS, mesh);
} }

View File

@ -53,7 +53,7 @@ struct ImportSettings
ImportSettings() ImportSettings()
: strictMode(true) : strictMode(true)
, readAllLayers(true) , readAllLayers(true)
, readAllMaterials() , readAllMaterials(false)
, readMaterials(true) , readMaterials(true)
, readCameras(true) , readCameras(true)
, readLights(true) , readLights(true)