Fix regression of importing only a single material for models with split objects enabled

#1483
This commit is contained in:
Wojtek Figat
2023-09-22 19:43:00 +02:00
parent 0948f7f18e
commit 248304a78f
3 changed files with 22 additions and 7 deletions

View File

@@ -566,12 +566,17 @@ bool ImportMaterials(ImportedModelData& result, AssimpImporterData& data, String
return false;
}
bool IsMeshInvalid(const aiMesh* aMesh)
{
return aMesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE || aMesh->mNumVertices == 0 || aMesh->mNumFaces == 0 || aMesh->mFaces[0].mNumIndices != 3;
}
bool ImportMesh(int32 i, ImportedModelData& result, AssimpImporterData& data, String& errorMsg)
{
const auto aMesh = data.Scene->mMeshes[i];
// Skip invalid meshes
if (aMesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE || aMesh->mNumVertices == 0 || aMesh->mNumFaces == 0 || aMesh->mFaces[0].mNumIndices != 3)
if (IsMeshInvalid(aMesh))
return false;
// Skip unused meshes

View File

@@ -923,8 +923,6 @@ bool ImportMesh(int32 index, ImportedModelData& result, OpenFbxImporterData& dat
const auto aMesh = data.Scene->getMesh(index);
const auto aGeometry = aMesh->getGeometry();
const auto trianglesCount = aGeometry->getVertexCount() / 3;
// Skip invalid meshes
if (IsMeshInvalid(aMesh))
return false;
@@ -1272,6 +1270,22 @@ bool ModelTool::ImportDataOpenFBX(const char* path, ImportedModelData& data, Opt
const auto meshIndex = Math::Clamp<int32>(options.ObjectIndex, 0, meshCount - 1);
if (ImportMesh(meshIndex, data, *context, errorMsg))
return true;
// Let the firstly imported mesh import all materials from all meshes (index 0 is importing all following ones before itself during splitting - see code above)
if (options.ObjectIndex == 1)
{
for (int32 i = 0; i < meshCount; i++)
{
const auto aMesh = context->Scene->getMesh(i);
if (i == 1 || IsMeshInvalid(aMesh))
continue;
for (int32 j = 0; j < aMesh->getMaterialCount(); j++)
{
const ofbx::Material* aMaterial = aMesh->getMaterial(j);
context->AddMaterial(data, aMaterial);
}
}
}
}
else
{

View File

@@ -910,10 +910,6 @@ bool ModelTool::ImportModel(const String& path, ModelData& meshData, Options& op
{
auto& texture = data.Textures[i];
// When splitting imported meshes allow only the first mesh to import assets (mesh[0] is imported after all following ones so import assets during mesh[1])
if (!options.SplitObjects && options.ObjectIndex != 1 && options.ObjectIndex != -1)
continue;
// Auto-import textures
if (autoImportOutput.IsEmpty() || (data.Types & ImportDataTypes::Textures) == ImportDataTypes::None || texture.FilePath.IsEmpty())
continue;