From ca08c4a20948c5fa96be43015edbf726c81b6d12 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 27 Mar 2019 11:20:16 +0100 Subject: [PATCH 1/6] FBX: Fix for loading taking a very long time on models with many duplicate names (issue_2390) --- code/FBXConverter.cpp | 9 +++++---- code/FBXConverter.h | 8 +++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/code/FBXConverter.cpp b/code/FBXConverter.cpp index a61ae4006..692f081da 100644 --- a/code/FBXConverter.cpp +++ b/code/FBXConverter.cpp @@ -410,16 +410,17 @@ namespace Assimp { void FBXConverter::GetUniqueName(const std::string &name, std::string &uniqueName) { - int i = 0; uniqueName = name; - while (mNodeNames.find(uniqueName) != mNodeNames.end()) + int i = 0; + auto it = mNodeNameInstances.find(uniqueName); + if (it != mNodeNameInstances.end()) { - ++i; + i = it->second + 1; std::stringstream ext; ext << name << std::setfill('0') << std::setw(3) << i; uniqueName = ext.str(); } - mNodeNames.insert(uniqueName); + mNodeNameInstances[name] = i; } diff --git a/code/FBXConverter.h b/code/FBXConverter.h index 398baa445..1ed88406e 100644 --- a/code/FBXConverter.h +++ b/code/FBXConverter.h @@ -58,6 +58,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include struct aiScene; struct aiNode; @@ -74,8 +75,6 @@ namespace FBX { class Document; -using NodeNameCache = std::set; - /** * Convert a FBX #Document to #aiScene * @param out Empty scene to be populated @@ -444,7 +443,10 @@ private: typedef std::map NodeAnimBitMap; NodeAnimBitMap node_anim_chain_bits; - NodeNameCache mNodeNames; + // number of nodes with the same name + typedef std::unordered_map NodeNameMap; + NodeNameMap mNodeNameInstances; + double anim_fps; aiScene* const out; From 89b3de947366e04c40a1e145356b2c9f0a671c40 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 29 Mar 2019 16:24:45 +0100 Subject: [PATCH 2/6] Update FBXConverter.h Introduce using instead of typedef. --- code/FBXConverter.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/code/FBXConverter.h b/code/FBXConverter.h index 1ed88406e..8c31fba31 100644 --- a/code/FBXConverter.h +++ b/code/FBXConverter.h @@ -418,7 +418,6 @@ private: void TransferDataToScene(); private: - // 0: not assigned yet, others: index is value - 1 unsigned int defaultMaterialIndex; @@ -428,23 +427,22 @@ private: std::vector lights; std::vector cameras; std::vector textures; - - typedef std::map MaterialMap; + using MaterialMap = std::map; MaterialMap materials_converted; - typedef std::map VideoMap; + using VideoMap = std::map; VideoMap textures_converted; - typedef std::map > MeshMap; + using MeshMap = std::map >; MeshMap meshes_converted; // fixed node name -> which trafo chain components have animations? - typedef std::map NodeAnimBitMap; + using NodeAnimBitMap = std::map ; NodeAnimBitMap node_anim_chain_bits; // number of nodes with the same name - typedef std::unordered_map NodeNameMap; + using NodeNameMap = std::unordered_map ; NodeNameMap mNodeNameInstances; double anim_fps; From 0505dd72661b28b53589ff1cf58074b862064399 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 3 Apr 2019 12:19:23 +0200 Subject: [PATCH 3/6] issue 2390: FBXConverter::GetUniqueName now uses a map for keeping track of duplicate node name count and a set for registering names that have been taken.This is required because the model might contain a several nodes called "nodename" and another one called "nodename001". In that case we can't add "001" to the second node called "nodename". --- code/FBXConverter.cpp | 16 ++++++++++------ code/FBXConverter.h | 8 ++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/code/FBXConverter.cpp b/code/FBXConverter.cpp index 692f081da..a654bc870 100644 --- a/code/FBXConverter.cpp +++ b/code/FBXConverter.cpp @@ -412,18 +412,22 @@ namespace Assimp { { uniqueName = name; int i = 0; - auto it = mNodeNameInstances.find(uniqueName); + auto it = mNodeNameInstances.find(name); // duplicate node name instance count if (it != mNodeNameInstances.end()) { - i = it->second + 1; - std::stringstream ext; - ext << name << std::setfill('0') << std::setw(3) << i; - uniqueName = ext.str(); + i = it->second; + while (mNodeNames.find(uniqueName) != mNodeNames.end()) + { + i++; + std::stringstream ext; + ext << name << std::setfill('0') << std::setw(3) << i; + uniqueName = ext.str(); + } } mNodeNameInstances[name] = i; + mNodeNames.insert(uniqueName); } - const char* FBXConverter::NameTransformationComp(TransformationComp comp) { switch (comp) { case TransformationComp_Translation: diff --git a/code/FBXConverter.h b/code/FBXConverter.h index 8c31fba31..162cf6e94 100644 --- a/code/FBXConverter.h +++ b/code/FBXConverter.h @@ -59,6 +59,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include struct aiScene; struct aiNode; @@ -442,8 +443,11 @@ private: NodeAnimBitMap node_anim_chain_bits; // number of nodes with the same name - using NodeNameMap = std::unordered_map ; - NodeNameMap mNodeNameInstances; + typedef std::unordered_map NodeAnimNameMap; + NodeAnimNameMap mNodeNameInstances; + + typedef std::unordered_set NodeNameCache; + NodeNameCache mNodeNames; double anim_fps; From 6d1514dd1005fcb7176ae2241e4afd945e975c2d Mon Sep 17 00:00:00 2001 From: Matias Lavik Date: Wed, 3 Apr 2019 20:19:44 +0200 Subject: [PATCH 4/6] replaced "typedef" with "using" (I didn't notice Kim Kulling's previous commit until now) --- code/FBXConverter.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/FBXConverter.h b/code/FBXConverter.h index 162cf6e94..50637468b 100644 --- a/code/FBXConverter.h +++ b/code/FBXConverter.h @@ -443,10 +443,10 @@ private: NodeAnimBitMap node_anim_chain_bits; // number of nodes with the same name - typedef std::unordered_map NodeAnimNameMap; + using NodeAnimNameMap = std::unordered_map; NodeAnimNameMap mNodeNameInstances; - typedef std::unordered_set NodeNameCache; + using NodeNameCache = std::unordered_set; NodeNameCache mNodeNames; double anim_fps; From 20acce1c5ef55cd94cd078388ee2d9bdfcb375d4 Mon Sep 17 00:00:00 2001 From: Matias Date: Thu, 4 Apr 2019 16:31:34 +0200 Subject: [PATCH 5/6] Suggested fix for issue : If available, use "Scaling" and "Translation" instead of "ModelUVScaling" and "ModelUVTranslation". This seems to be what 3DS Max and FBX SDK use. --- code/FBXMaterial.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/code/FBXMaterial.cpp b/code/FBXMaterial.cpp index f5f6fda03..fa3778d67 100644 --- a/code/FBXMaterial.cpp +++ b/code/FBXMaterial.cpp @@ -206,6 +206,20 @@ Texture::Texture(uint64_t id, const Element& element, const Document& doc, const props = GetPropertyTable(doc,"Texture.FbxFileTexture",element,sc); + // 3DS Max and FBX SDK use "Scaling" and "Translation" instead of "ModelUVScaling" and "ModelUVTranslation". Use these properties if available. + bool ok; + const aiVector3D& scaling = PropertyGet(*props, "Scaling", ok); + if (ok) { + uvScaling.x = scaling.x; + uvScaling.y = scaling.y; + } + + const aiVector3D& trans = PropertyGet(*props, "Translation", ok); + if (ok) { + uvTrans.x = trans.x; + uvTrans.y = trans.y; + } + // resolve video links if(doc.Settings().readTextures) { const std::vector& conns = doc.GetConnectionsByDestinationSequenced(ID()); From c321fb6a4445efc8e92da6272e857bc219ddab9b Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sat, 6 Apr 2019 08:59:30 +0200 Subject: [PATCH 6/6] Update FBXConverter.cpp FBX: Add missind initializers. --- code/FBXConverter.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/code/FBXConverter.cpp b/code/FBXConverter.cpp index 7eaba02c0..09ae06a64 100644 --- a/code/FBXConverter.cpp +++ b/code/FBXConverter.cpp @@ -78,6 +78,16 @@ namespace Assimp { FBXConverter::FBXConverter(aiScene* out, const Document& doc) : defaultMaterialIndex() + , lights() + , cameras() + , textures() + , materials_converted() + , textures_converted() + , meshes_converted() + , node_anim_chain_bits() + , mNodeNameInstances() + , mNodeNames() + , anim_fps() , out(out) , doc(doc) { // animations need to be converted first since this will