RemoveRedundantMaterials: avoid crash when all materials are redundant

pull/5439/head
Stephen Gold 2024-01-25 15:05:17 -08:00
parent ac29847d56
commit cbe3bbd723
1 changed files with 30 additions and 28 deletions

View File

@ -3,7 +3,7 @@
Open Asset Import Library (assimp) Open Asset Import Library (assimp)
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
Copyright (c) 2006-2022, assimp team Copyright (c) 2006-2024, assimp team
All rights reserved. All rights reserved.
@ -153,36 +153,38 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene) {
} }
// If the new material count differs from the original, // If the new material count differs from the original,
// we need to rebuild the material list and remap mesh material indexes. // we need to rebuild the material list and remap mesh material indexes.
if(iNewNum < 1)
throw DeadlyImportError("No materials remaining");
if (iNewNum != pScene->mNumMaterials) { if (iNewNum != pScene->mNumMaterials) {
ai_assert(iNewNum > 0); aiMaterial** ppcMaterials;
aiMaterial** ppcMaterials = new aiMaterial*[iNewNum]; if (iNewNum == 0) {
::memset(ppcMaterials,0,sizeof(void*)*iNewNum); ppcMaterials = nullptr;
for (unsigned int p = 0; p < pScene->mNumMaterials;++p) } else {
{ ppcMaterials = new aiMaterial*[iNewNum];
// if the material is not referenced ... remove it ::memset(ppcMaterials,0,sizeof(void*)*iNewNum);
if (!abReferenced[p]) { for (unsigned int p = 0; p < pScene->mNumMaterials;++p)
continue; {
} // if the material is not referenced ... remove it
if (!abReferenced[p]) {
// generate new names for modified materials that had no names continue;
const unsigned int idx = aiMappingTable[p]; }
if (ppcMaterials[idx]) {
aiString sz; // generate new names for modified materials that had no names
if( ppcMaterials[idx]->Get(AI_MATKEY_NAME, sz) != AI_SUCCESS ) { const unsigned int idx = aiMappingTable[p];
sz.length = ::ai_snprintf(sz.data,MAXLEN,"JoinedMaterial_#%u",p); if (ppcMaterials[idx]) {
((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME); aiString sz;
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];
} }
} else {
ppcMaterials[idx] = pScene->mMaterials[p];
} }
} // update all material indices
// update all material indices for (unsigned int p = 0; p < pScene->mNumMeshes;++p) {
for (unsigned int p = 0; p < pScene->mNumMeshes;++p) { aiMesh* mesh = pScene->mMeshes[p];
aiMesh* mesh = pScene->mMeshes[p]; ai_assert(nullptr != mesh);
ai_assert(nullptr != mesh); mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex];
mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex]; }
} }
// delete the old material list // delete the old material list
delete[] pScene->mMaterials; delete[] pScene->mMaterials;