Parcourir la source

Merge pull request #287 from arkeon7/master

Correction on FBX material texture UV index
Alexander Gessler il y a 11 ans
Parent
commit
340b94f9a5
3 fichiers modifiés avec 113 ajouts et 64 suppressions
  1. 111 62
      code/FBXConverter.cpp
  2. 1 1
      code/FBXImportSettings.h
  3. 1 1
      include/assimp/quaternion.h

+ 111 - 62
code/FBXConverter.cpp

@@ -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.LayeredTextures());
+		SetTextureProperties(out_mat,material.Textures(), mesh);
+		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,18 +1495,48 @@ private:
 						std::find(materials.begin(),materials.end(),out_mat)
 						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<const MeshGeometry*> (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<const MeshGeometry*> (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<int>(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;
 						int index = -1;
 						for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
 						for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
 							if(mesh->GetTextureCoords(i).empty()) {
 							if(mesh->GetTextureCoords(i).empty()) {
@@ -1520,17 +1550,12 @@ private:
 						}
 						}
 						if(index == -1) {
 						if(index == -1) {
 							FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
 							FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
-							continue;
 						}
 						}
 
 
 						if(uvIndex == -1) {
 						if(uvIndex == -1) {
 							uvIndex = index;
 							uvIndex = index;
 						}
 						}
-						else {
-							FBXImporter::LogWarn("the UV channel named " + uvSet + 
-								" appears at different positions in meshes, results will be wrong");
-						}
-					}
+          }
 
 
 					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()) {
@@ -1590,18 +1615,47 @@ private:
 					std::find(materials.begin(),materials.end(),out_mat)
 					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<const MeshGeometry*> (v.first);
-					if(!mesh) {
-						continue;
-					}
+			  uvIndex = -1;
+        if (!mesh)
+        {					
+					BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) {
+						const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*> (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<int>(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;
 					int index = -1;
 					for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
 					for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
 						if(mesh->GetTextureCoords(i).empty()) {
 						if(mesh->GetTextureCoords(i).empty()) {
@@ -1615,17 +1669,12 @@ private:
 					}
 					}
 					if(index == -1) {
 					if(index == -1) {
 						FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
 						FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
-						continue;
 					}
 					}
 
 
 					if(uvIndex == -1) {
 					if(uvIndex == -1) {
 						uvIndex = index;
 						uvIndex = index;
 					}
 					}
-					else {
-						FBXImporter::LogWarn("the UV channel named " + uvSet + 
-							" appears at different positions in meshes, results will be wrong");
-					}
-				}
+        }
 
 
 				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, "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);
 	}
 	}
 
 
 
 

+ 1 - 1
code/FBXImportSettings.h

@@ -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)

+ 1 - 1
include/assimp/quaternion.h

@@ -55,7 +55,7 @@ template <typename TReal>
 class aiQuaterniont
 class aiQuaterniont
 {
 {
 public:
 public:
-	aiQuaterniont() : w(), x(), y(), z() {}
+	aiQuaterniont() : w(1.0), x(), y(), z() {}
 	aiQuaterniont(TReal pw, TReal px, TReal py, TReal pz) 
 	aiQuaterniont(TReal pw, TReal px, TReal py, TReal pz) 
 		: w(pw), x(px), y(py), z(pz) {}
 		: w(pw), x(px), y(py), z(pz) {}