From 9905fc076c74e7a79297b39ea7c8d627604cfb51 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 9 Oct 2019 16:44:06 -0700 Subject: [PATCH] Materials of baked models fbx are loading correctly, still using the name/id of the mateiral in the part struct --- libraries/fbx/src/FBXSerializer.cpp | 35 +++++++++++++++++++----- libraries/fbx/src/FBXSerializer_Mesh.cpp | 8 +++++- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/libraries/fbx/src/FBXSerializer.cpp b/libraries/fbx/src/FBXSerializer.cpp index 2fa90af9db..c35a23ef3a 100644 --- a/libraries/fbx/src/FBXSerializer.cpp +++ b/libraries/fbx/src/FBXSerializer.cpp @@ -1480,6 +1480,12 @@ HFMModel* FBXSerializer::extractHFMModel(const hifi::VariantHash& mapping, const // meshShapes will be added to hfmModel at the very end std::vector meshShapes; meshShapes.reserve(instanceModelIDs.size() * mesh.parts.size()); + if (instanceModelIDs.size() > 1) { + qCDebug(modelformat) << "Mesh " << meshID << " made of " << mesh.parts.size() << " parts is instanced " << instanceModelIDs.size() << " times!!!"; + } + if (mesh.parts.size() < 1) { + qCDebug(modelformat) << "Mesh " << meshID << " made of " << mesh.parts.size() << " parts !!!!! "; + } for (const QString& modelID : instanceModelIDs) { // The transform node has the same indexing order as the joints const uint32_t transformIndex = (uint32_t)modelIDs.indexOf(modelID); @@ -1500,6 +1506,14 @@ HFMModel* FBXSerializer::extractHFMModel(const hifi::VariantHash& mapping, const shape.mesh = meshIndex; shape.meshPart = i; shape.transform = transformIndex; + + auto matName = mesh.parts[i].materialID; + auto materialIt = materialNameToID.find(matName.toStdString()); + if (materialIt != materialNameToID.end()) { + shape.material = materialIt->second; + } else { + qCDebug(modelformat) << "Unknown material ? " << matName; + } shape.transformedExtents.reset(); // compute the shape extents from the transformed vertices @@ -1546,14 +1560,21 @@ HFMModel* FBXSerializer::extractHFMModel(const hifi::VariantHash& mapping, const } // For baked models with FBX_DRACO_MESH_VERSION >= 2, get materials from extracted.materialIDPerMeshPart if (!extracted.materialIDPerMeshPart.empty()) { - for (uint32_t i = 0; i < (uint32_t)extracted.materialIDPerMeshPart.size(); ++i) { - hfm::Shape& shape = partShapes[i]; - const std::string& materialID = extracted.materialIDPerMeshPart[i]; - auto materialIt = materialNameToID.find(materialID); - if (materialIt != materialNameToID.end()) { - shape.material = materialIt->second; + /* if (partShapes.size() == extracted.materialIDPerMeshPart.size()) { + for (uint32_t i = 0; i < (uint32_t)extracted.materialIDPerMeshPart.size(); ++i) { + hfm::Shape& shape = partShapes[i]; + const std::string& materialID = extracted.materialIDPerMeshPart[i]; + auto materialIt = materialNameToID.find(materialID); + if (materialIt != materialNameToID.end()) { + shape.material = materialIt->second; + } } - } + } else { + for (int p = 0; p < mesh.parts.size(); p++) { + qCDebug(modelformat) << "mesh.parts[" << p <<"] is " << mesh.parts[p].materialID; + } + qCDebug(modelformat) << "partShapes is not the same size as materialIDPerMeshPart ?"; + }*/ } // find the clusters with which the mesh is associated diff --git a/libraries/fbx/src/FBXSerializer_Mesh.cpp b/libraries/fbx/src/FBXSerializer_Mesh.cpp index 7c6be5740a..37f2c9ec1b 100644 --- a/libraries/fbx/src/FBXSerializer_Mesh.cpp +++ b/libraries/fbx/src/FBXSerializer_Mesh.cpp @@ -492,9 +492,15 @@ ExtractedMesh FBXSerializer::extractMesh(const FBXNode& object, unsigned int& me int& partIndexPlusOne = materialTextureParts[materialTexture]; if (partIndexPlusOne == 0) { data.extracted.mesh.parts.resize(data.extracted.mesh.parts.size() + 1); + HFMMeshPart& part = data.extracted.mesh.parts.back(); // Figure out if this is the older way of defining the per-part material for baked FBX - if (dracoMeshNodeVersion < 2) { + if (dracoMeshNodeVersion >= 2) { + // Define the materialID now + if (materialID < dracoMaterialList.size()) { + part.materialID = QString(dracoMaterialList[materialID].c_str()); + } + } else { // Define the materialID later, based on the order of first appearance of the materials in the _connectionChildMap data.extracted.partMaterialTextures.append(materialTexture); }