fix: reduce gltf2 export time (#5972)

* fix: reduce gltf2 export time

* fix: use std::less<> in std::map

---------

Co-authored-by: huangxinchuang <huangxinchuang@pointcode.com>
Co-authored-by: Kim Kulling <kimkulling@users.noreply.github.com>
This commit is contained in:
1323236654
2025-01-26 04:50:33 +08:00
committed by GitHub
parent 2aac984fa3
commit bc47836a8f
3 changed files with 16 additions and 9 deletions

View File

@@ -1267,6 +1267,7 @@ private:
size_t mBodyOffset;
size_t mBodyLength;
IdMap mUsedIds;
std::map<std::string, int, std::less<>> mUsedNamesMap;
Ref<Buffer> mBodyBuffer;
};

View File

@@ -2195,8 +2195,10 @@ inline std::string Asset::FindUniqueID(const std::string &str, const char *suffi
std::string id = str;
if (!id.empty()) {
if (mUsedIds.find(id) == mUsedIds.end())
if (mUsedIds.find(id) == mUsedIds.end()){
mUsedNamesMap[id] = 0;
return id;
}
id += "_";
}
@@ -2205,17 +2207,13 @@ inline std::string Asset::FindUniqueID(const std::string &str, const char *suffi
Asset::IdMap::iterator it = mUsedIds.find(id);
if (it == mUsedIds.end()) {
mUsedNamesMap[id] = 0;
return id;
}
std::vector<char> buffer;
buffer.resize(id.size() + 16);
int offset = ai_snprintf(buffer.data(), buffer.size(), "%s_", id.c_str());
for (int i = 0; it != mUsedIds.end(); ++i) {
ai_snprintf(buffer.data() + offset, buffer.size() - offset, "%d", i);
id = buffer.data();
it = mUsedIds.find(id);
}
auto key = id;
id += "_" + std::to_string(mUsedNamesMap[key]);
mUsedNamesMap[key] = mUsedNamesMap[key] + 1;
return id;
}

View File

@@ -1499,6 +1499,10 @@ unsigned int glTF2Exporter::ExportNodeHierarchy(const aiNode *n) {
Ref<Node> node = mAsset->nodes.Create(mAsset->FindUniqueID(n->mName.C_Str(), "node"));
node->name = n->mName.C_Str();
if(n->mNumChildren > 0)
node->children.reserve(n->mNumChildren);
if(n->mNumMeshes > 0)
node->meshes.reserve(n->mNumMeshes);
if (!n->mTransformation.IsIdentity(configEpsilon)) {
node->matrix.isPresent = true;
@@ -1527,6 +1531,10 @@ unsigned int glTF2Exporter::ExportNode(const aiNode *n, Ref<Node> &parent) {
node->parent = parent;
node->name = name;
if(n->mNumChildren > 0)
node->children.reserve(n->mNumChildren);
if(n->mNumMeshes > 0)
node->meshes.reserve(n->mNumMeshes);
ExportNodeExtras(n->mMetaData, node->extras);