From 8e8757f800ea06653c645b12061059b6997b8afb Mon Sep 17 00:00:00 2001 From: tomacd Date: Thu, 27 Oct 2016 19:23:43 +0200 Subject: [PATCH] Keep original materials names after RemoveRedundant materials process and export to collada --- code/ColladaExporter.cpp | 19 +++++++++++++++++-- code/RemoveRedundantMaterials.cpp | 8 +++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/code/ColladaExporter.cpp b/code/ColladaExporter.cpp index a3bbc56..eb94b4e 100644 --- a/code/ColladaExporter.cpp +++ b/code/ColladaExporter.cpp @@ -636,9 +636,24 @@ void ColladaExporter::WriteMaterials() const aiMaterial* mat = mScene->mMaterials[a]; aiString name; - if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS ) + if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS ) { name = "mat"; - materials[a].name = std::string( "m") + to_string(a) + name.C_Str(); + materials[a].name = std::string( "m") + to_string(a) + name.C_Str(); + } else { + // try to use the material's name if no other material has already taken it, else append # + std::string testName = name.C_Str(); + size_t materialCountWithThisName = 0; + for( size_t i = 0; i < a; i ++ ) { + if( materials[i].name == testName ) { + materialCountWithThisName ++; + } + } + if( materialCountWithThisName == 0 ) { + materials[a].name = name.C_Str(); + } else { + materials[a].name = std::string(name.C_Str()) + to_string(materialCountWithThisName); + } + } for( std::string::iterator it = materials[a].name.begin(); it != materials[a].name.end(); ++it ) { if( !isalnum_C( *it ) ) { *it = '_'; diff --git a/code/RemoveRedundantMaterials.cpp b/code/RemoveRedundantMaterials.cpp index a3e61ae..a9954b9 100644 --- a/code/RemoveRedundantMaterials.cpp +++ b/code/RemoveRedundantMaterials.cpp @@ -177,12 +177,14 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene) continue; } - // generate new names for all modified materials + // generate new names for modified materials that had no names const unsigned int idx = aiMappingTable[p]; if (ppcMaterials[idx]) { aiString sz; - sz.length = ::ai_snprintf(sz.data,MAXLEN,"JoinedMaterial_#%u",p); - ((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME); + if( ppcMaterials[idx]->Get(AI_MATKEY_NAME, sz) != AI_SUCCESS ) { + sz.length = ::ai_snprintf(sz.data,MAXLEN,"JoinedMaterial_#%u",p); + ((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME); + } } else { ppcMaterials[idx] = pScene->mMaterials[p]; } -- 2.7.4