1515 *
1616 */
1717
18+ #include < cstddef>
1819#include < queue>
20+ #include < string>
1921#include < unordered_set>
2022
2123#include " gz/common/graphics/Types.hh"
@@ -69,10 +71,12 @@ class AssimpLoader::Implementation
6971 // / \param[in] _scene the assimp scene
7072 // / \param[in] _matIdx index of the material in the scene
7173 // / \param[in] _path path where the mesh is located
74+ // / \param[in] _fileBaseName Base name of the mesh file.
7275 // / \return pointer to the converted common::Material
73- public: MaterialPtr CreateMaterial (const aiScene* _scene,
76+ public: MaterialPtr CreateMaterial (const aiScene * _scene,
7477 unsigned _matIdx,
75- const std::string& _path) const ;
78+ const std::string &_path,
79+ const std::string &_fileBaseName) const ;
7680
7781 // / \brief Load a texture embedded in a mesh (i.e. for GLB format)
7882 // / into a gz::common::Image
@@ -82,13 +86,15 @@ class AssimpLoader::Implementation
8286
8387 // / \brief Utility function to generate a texture name for both embedded
8488 // / and external textures
89+ // / \param[in] _prefix Prefix to add to the texture name
8590 // / \param[in] _scene the assimp scene
8691 // / \param[in] _mat the assimp material
8792 // / \param[in] _type the type of texture (i.e. Diffuse, Metal)
8893 // / \return the generated texture name
89- public: std::string GenerateTextureName (const aiScene* _scene,
90- aiMaterial* _mat,
91- const std::string& _type) const ;
94+ public: std::string GenerateTextureName (const std::string &_prefix,
95+ const aiScene* _scene,
96+ aiMaterial *_mat,
97+ const std::string &_type) const ;
9298
9399 // / \brief Function to parse texture information and load it if embedded
94100 // / \param[in] _scene the assimp scene
@@ -320,7 +326,8 @@ void AssimpLoader::Implementation::RecursiveSkeletonCreate(const aiNode* _node,
320326
321327// ////////////////////////////////////////////////
322328MaterialPtr AssimpLoader::Implementation::CreateMaterial (
323- const aiScene* _scene, unsigned _matIdx, const std::string& _path) const
329+ const aiScene *_scene, unsigned _matIdx, const std::string &_path,
330+ const std::string &_fileBaseName) const
324331{
325332 MaterialPtr mat = std::make_shared<Material>();
326333 aiColor4D color;
@@ -386,8 +393,8 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial(
386393 if (ret == AI_SUCCESS)
387394 {
388395 // Check if the texture is embedded or not
389- auto [texName, texData] = this ->LoadTexture (_scene,
390- texturePath, this ->GenerateTextureName (_scene, assimpMat, " Diffuse" ));
396+ auto [texName, texData] = this ->LoadTexture (_scene, texturePath,
397+ this ->GenerateTextureName (_fileBaseName, _scene, assimpMat, " Diffuse" ));
391398 if (texData != nullptr )
392399 mat->SetTextureImage (texName, texData);
393400 else
@@ -421,18 +428,19 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial(
421428 if (ret == AI_SUCCESS)
422429 {
423430 auto [texName, texData] = this ->LoadTexture (_scene, texturePath,
424- this ->GenerateTextureName (_scene, assimpMat, " MetallicRoughness" ));
431+ this ->GenerateTextureName (_fileBaseName, _scene, assimpMat,
432+ " MetallicRoughness" ));
425433 // Load it into a common::Image then split it
426434 auto texImg = texData != nullptr ? texData :
427435 std::make_shared<common::Image>(joinPaths (_path, texName));
428436 auto [metalTexture, roughTexture] =
429437 this ->SplitMetallicRoughnessMap (*texImg);
430438 pbr.SetMetalnessMap (
431- this ->GenerateTextureName (_scene, assimpMat, " Metalness " ) ,
432- metalTexture);
439+ this ->GenerateTextureName (_fileBaseName, _scene, assimpMat ,
440+ " Metalness " ), metalTexture);
433441 pbr.SetRoughnessMap (
434- this ->GenerateTextureName (_scene, assimpMat, " Roughness " ) ,
435- roughTexture);
442+ this ->GenerateTextureName (_fileBaseName, _scene, assimpMat ,
443+ " Roughness " ), roughTexture);
436444 }
437445 else
438446 {
@@ -441,15 +449,17 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial(
441449 if (ret == AI_SUCCESS)
442450 {
443451 auto [texName, texData] = this ->LoadTexture (_scene, texturePath,
444- this ->GenerateTextureName (_scene, assimpMat, " Metalness" ));
452+ this ->GenerateTextureName (_fileBaseName, _scene, assimpMat,
453+ " Metalness" ));
445454 pbr.SetMetalnessMap (texName, texData);
446455 }
447456 ret = assimpMat->GetTexture (
448457 aiTextureType_DIFFUSE_ROUGHNESS, 0 , &texturePath);
449458 if (ret == AI_SUCCESS)
450459 {
451460 auto [texName, texData] = this ->LoadTexture (_scene, texturePath,
452- this ->GenerateTextureName (_scene, assimpMat, " Roughness" ));
461+ this ->GenerateTextureName (_fileBaseName, _scene, assimpMat,
462+ " Roughness" ));
453463 pbr.SetRoughnessMap (texName, texData);
454464 }
455465 // Load lightmap only if it is not a glb/glTF mesh that contains a
@@ -464,7 +474,8 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial(
464474 if (ret == AI_SUCCESS)
465475 {
466476 auto [texName, texData] = this ->LoadTexture (_scene, texturePath,
467- this ->GenerateTextureName (_scene, assimpMat, " Lightmap" ));
477+ this ->GenerateTextureName (_fileBaseName, _scene, assimpMat,
478+ " Lightmap" ));
468479 pbr.SetLightMap (texName, uvIdx, texData);
469480 }
470481 }
@@ -473,15 +484,16 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial(
473484 if (ret == AI_SUCCESS)
474485 {
475486 auto [texName, texData] = this ->LoadTexture (_scene, texturePath,
476- this ->GenerateTextureName (_scene, assimpMat, " Normal" ));
487+ this ->GenerateTextureName (_fileBaseName, _scene, assimpMat, " Normal" ));
477488 // TODO(luca) different normal map spaces
478489 pbr.SetNormalMap (texName, NormalMapSpace::TANGENT, texData);
479490 }
480491 ret = assimpMat->GetTexture (aiTextureType_EMISSIVE, 0 , &texturePath);
481492 if (ret == AI_SUCCESS)
482493 {
483494 auto [texName, texData] = this ->LoadTexture (_scene, texturePath,
484- this ->GenerateTextureName (_scene, assimpMat, " Emissive" ));
495+ this ->GenerateTextureName (_fileBaseName, _scene, assimpMat,
496+ " Emissive" ));
485497 pbr.SetEmissiveMap (texName, texData);
486498 }
487499#ifndef GZ_ASSIMP_PRE_5_2_0
@@ -600,15 +612,16 @@ ImagePtr AssimpLoader::Implementation::LoadEmbeddedTexture(
600612
601613// ////////////////////////////////////////////////
602614std::string AssimpLoader::Implementation::GenerateTextureName (
603- const aiScene* _scene, aiMaterial* _mat, const std::string& _type) const
615+ const std::string &_prefix, const aiScene *_scene, aiMaterial *_mat,
616+ const std::string &_type) const
604617{
605618#ifdef GZ_ASSIMP_PRE_5_2_0
606619 auto rootName = _scene->mRootNode ->mName ;
607620#else
608621 auto rootName = _scene->mName ;
609622#endif
610- return ToString (rootName) + " _" + ToString (_mat-> GetName ()) +
611- " _" + _type;
623+ return _prefix + " _" + ToString (rootName) + " _ " +
624+ ToString (_mat-> GetName ()) + " _" + _type;
612625}
613626
614627// ////////////////////////////////////////////////
@@ -695,12 +708,18 @@ Mesh *AssimpLoader::Load(const std::string &_filename)
695708 }
696709 auto & rootNode = scene->mRootNode ;
697710 auto rootName = ToString (rootNode->mName );
698-
699- // compute assimp root node transform
700- std::string extension = _filename.substr (_filename.rfind (" ." ) + 1 ,
701- _filename.size ());
711+ auto fileBaseName = common::basename (_filename);
712+ std::string extension;
713+ std::size_t extIdx = _filename.rfind (" ." );
714+ if (extIdx != std::string::npos)
715+ {
716+ extension = _filename.substr (extIdx + 1 , _filename.size ());
717+ fileBaseName = fileBaseName.substr (0 , fileBaseName.rfind (extension) - 1 );
718+ }
702719 std::transform (extension.begin (), extension.end (),
703720 extension.begin (), ::tolower);
721+
722+ // compute assimp root node transform
704723 bool useIdentityRotation = (extension != " glb" && extension != " glTF" );
705724 auto transform = this ->dataPtr ->UpdatedRootNodeTransform (scene,
706725 useIdentityRotation);
@@ -709,7 +728,8 @@ Mesh *AssimpLoader::Load(const std::string &_filename)
709728 // Add the materials first
710729 for (unsigned _matIdx = 0 ; _matIdx < scene->mNumMaterials ; ++_matIdx)
711730 {
712- auto mat = this ->dataPtr ->CreateMaterial (scene, _matIdx, path);
731+ auto mat = this ->dataPtr ->CreateMaterial (scene, _matIdx, path,
732+ fileBaseName);
713733 mesh->AddMaterial (mat);
714734 }
715735 // Create the skeleton
0 commit comments