@@ -2612,22 +2612,38 @@ void Mesh::load(const std::string& filepath, bool mergeCoincidentVerts, Material
26122612 f.v [2 ] = oldToNewMap[f.v [2 ]];
26132613 }
26142614 }
2615+
2616+ // get number of materials used and materials properties
2617+ if (material== nullptr || scene->mNumMaterials <= 1 )
2618+ {
2619+ std::unordered_set<int > materialIds = std::unordered_set<int >(_trisMtlIds.begin (), _trisMtlIds.end ());
26152620
2616- // set number of materials used
2617- const std::unordered_set<int > materialIds = std::unordered_set<int >(_trisMtlIds.begin (), _trisMtlIds.end ());
2618- nmtls = static_cast <int >(materialIds.size ());
2619-
2620- // get material related properties
2621- if (material != nullptr && scene->mNumMaterials > 1 )
2621+ // set number of materials used
2622+ nmtls = static_cast <int >(materialIds.size ());
2623+ }
2624+ else
26222625 {
2626+ std::unordered_set<int > materialIds; // does not preserve insertion order
2627+ std::vector<int > materialIdsWithTriangleOrder;
2628+
2629+ // build materialIds and materialIdsWithTriangleOrder
2630+ for (int id : _trisMtlIds)
2631+ {
2632+ if (materialIds.insert (id).second )
2633+ materialIdsWithTriangleOrder.push_back (id);
2634+ }
2635+
2636+ // set number of materials used
2637+ nmtls = static_cast <int >(materialIds.size ());
2638+
26232639 // get material properties from the first material as they are shared across all others
26242640 scene->mMaterials [1 ]->Get (AI_MATKEY_COLOR_AMBIENT, material->ambient );
26252641 scene->mMaterials [1 ]->Get (AI_MATKEY_COLOR_DIFFUSE, material->diffuse );
26262642 scene->mMaterials [1 ]->Get (AI_MATKEY_COLOR_SPECULAR, material->specular );
26272643 scene->mMaterials [1 ]->Get (AI_MATKEY_SHININESS, material->shininess );
26282644
26292645 // get textures from the next materials
2630- for (int id : materialIds )
2646+ for (int id : materialIdsWithTriangleOrder )
26312647 {
26322648 aiString diffuse;
26332649 if (scene->mMaterials [id + materialIdOffset]->Get (AI_MATKEY_TEXTURE_DIFFUSE (0 ), diffuse) == aiReturn_SUCCESS)
0 commit comments