-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate a more unique texture name for glb embedded textures #606
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4a7f4a
generate unique texture name for glb embedded textures
iche033 6c43c95
update test
iche033 24d8502
Merge branch 'gz-common5' into glb_tex_name
azeey 163222b
add includes
iche033 4b07788
Merge remote-tracking branch 'refs/remotes/origin/glb_tex_name' into …
iche033 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,9 @@ | |
* | ||
*/ | ||
|
||
#include <cstddef> | ||
#include <queue> | ||
#include <string> | ||
#include <unordered_set> | ||
|
||
#include "gz/common/graphics/Types.hh" | ||
|
@@ -69,10 +71,12 @@ class AssimpLoader::Implementation | |
/// \param[in] _scene the assimp scene | ||
/// \param[in] _matIdx index of the material in the scene | ||
/// \param[in] _path path where the mesh is located | ||
/// \param[in] _fileBaseName Base name of the mesh file. | ||
/// \return pointer to the converted common::Material | ||
public: MaterialPtr CreateMaterial(const aiScene* _scene, | ||
public: MaterialPtr CreateMaterial(const aiScene *_scene, | ||
unsigned _matIdx, | ||
const std::string& _path) const; | ||
const std::string &_path, | ||
const std::string &_fileBaseName) const; | ||
|
||
/// \brief Load a texture embedded in a mesh (i.e. for GLB format) | ||
/// into a gz::common::Image | ||
|
@@ -82,13 +86,15 @@ class AssimpLoader::Implementation | |
|
||
/// \brief Utility function to generate a texture name for both embedded | ||
/// and external textures | ||
/// \param[in] _prefix Prefix to add to the texture name | ||
/// \param[in] _scene the assimp scene | ||
/// \param[in] _mat the assimp material | ||
/// \param[in] _type the type of texture (i.e. Diffuse, Metal) | ||
/// \return the generated texture name | ||
public: std::string GenerateTextureName(const aiScene* _scene, | ||
aiMaterial* _mat, | ||
const std::string& _type) const; | ||
public: std::string GenerateTextureName(const std::string &_prefix, | ||
const aiScene* _scene, | ||
aiMaterial *_mat, | ||
const std::string &_type) const; | ||
|
||
/// \brief Function to parse texture information and load it if embedded | ||
/// \param[in] _scene the assimp scene | ||
|
@@ -320,7 +326,8 @@ void AssimpLoader::Implementation::RecursiveSkeletonCreate(const aiNode* _node, | |
|
||
////////////////////////////////////////////////// | ||
MaterialPtr AssimpLoader::Implementation::CreateMaterial( | ||
const aiScene* _scene, unsigned _matIdx, const std::string& _path) const | ||
const aiScene *_scene, unsigned _matIdx, const std::string &_path, | ||
const std::string &_fileBaseName) const | ||
{ | ||
MaterialPtr mat = std::make_shared<Material>(); | ||
aiColor4D color; | ||
|
@@ -386,8 +393,8 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial( | |
if (ret == AI_SUCCESS) | ||
{ | ||
// Check if the texture is embedded or not | ||
auto [texName, texData] = this->LoadTexture(_scene, | ||
texturePath, this->GenerateTextureName(_scene, assimpMat, "Diffuse")); | ||
auto [texName, texData] = this->LoadTexture(_scene, texturePath, | ||
this->GenerateTextureName(_fileBaseName, _scene, assimpMat, "Diffuse")); | ||
if (texData != nullptr) | ||
mat->SetTextureImage(texName, texData); | ||
else | ||
|
@@ -421,18 +428,19 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial( | |
if (ret == AI_SUCCESS) | ||
{ | ||
auto [texName, texData] = this->LoadTexture(_scene, texturePath, | ||
this->GenerateTextureName(_scene, assimpMat, "MetallicRoughness")); | ||
this->GenerateTextureName(_fileBaseName, _scene, assimpMat, | ||
"MetallicRoughness")); | ||
// Load it into a common::Image then split it | ||
auto texImg = texData != nullptr ? texData : | ||
std::make_shared<common::Image>(joinPaths(_path, texName)); | ||
auto [metalTexture, roughTexture] = | ||
this->SplitMetallicRoughnessMap(*texImg); | ||
pbr.SetMetalnessMap( | ||
this->GenerateTextureName(_scene, assimpMat, "Metalness"), | ||
metalTexture); | ||
this->GenerateTextureName(_fileBaseName, _scene, assimpMat, | ||
"Metalness"), metalTexture); | ||
pbr.SetRoughnessMap( | ||
this->GenerateTextureName(_scene, assimpMat, "Roughness"), | ||
roughTexture); | ||
this->GenerateTextureName(_fileBaseName, _scene, assimpMat, | ||
"Roughness"), roughTexture); | ||
} | ||
else | ||
{ | ||
|
@@ -441,15 +449,17 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial( | |
if (ret == AI_SUCCESS) | ||
{ | ||
auto [texName, texData] = this->LoadTexture(_scene, texturePath, | ||
this->GenerateTextureName(_scene, assimpMat, "Metalness")); | ||
this->GenerateTextureName(_fileBaseName, _scene, assimpMat, | ||
"Metalness")); | ||
pbr.SetMetalnessMap(texName, texData); | ||
} | ||
ret = assimpMat->GetTexture( | ||
aiTextureType_DIFFUSE_ROUGHNESS, 0, &texturePath); | ||
if (ret == AI_SUCCESS) | ||
{ | ||
auto [texName, texData] = this->LoadTexture(_scene, texturePath, | ||
this->GenerateTextureName(_scene, assimpMat, "Roughness")); | ||
this->GenerateTextureName(_fileBaseName, _scene, assimpMat, | ||
"Roughness")); | ||
pbr.SetRoughnessMap(texName, texData); | ||
} | ||
// Load lightmap only if it is not a glb/glTF mesh that contains a | ||
|
@@ -464,7 +474,8 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial( | |
if (ret == AI_SUCCESS) | ||
{ | ||
auto [texName, texData] = this->LoadTexture(_scene, texturePath, | ||
this->GenerateTextureName(_scene, assimpMat, "Lightmap")); | ||
this->GenerateTextureName(_fileBaseName, _scene, assimpMat, | ||
"Lightmap")); | ||
pbr.SetLightMap(texName, uvIdx, texData); | ||
} | ||
} | ||
|
@@ -473,15 +484,16 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial( | |
if (ret == AI_SUCCESS) | ||
{ | ||
auto [texName, texData] = this->LoadTexture(_scene, texturePath, | ||
this->GenerateTextureName(_scene, assimpMat, "Normal")); | ||
this->GenerateTextureName(_fileBaseName, _scene, assimpMat, "Normal")); | ||
// TODO(luca) different normal map spaces | ||
pbr.SetNormalMap(texName, NormalMapSpace::TANGENT, texData); | ||
} | ||
ret = assimpMat->GetTexture(aiTextureType_EMISSIVE, 0, &texturePath); | ||
if (ret == AI_SUCCESS) | ||
{ | ||
auto [texName, texData] = this->LoadTexture(_scene, texturePath, | ||
this->GenerateTextureName(_scene, assimpMat, "Emissive")); | ||
this->GenerateTextureName(_fileBaseName, _scene, assimpMat, | ||
"Emissive")); | ||
pbr.SetEmissiveMap(texName, texData); | ||
} | ||
#ifndef GZ_ASSIMP_PRE_5_2_0 | ||
|
@@ -600,15 +612,16 @@ ImagePtr AssimpLoader::Implementation::LoadEmbeddedTexture( | |
|
||
////////////////////////////////////////////////// | ||
std::string AssimpLoader::Implementation::GenerateTextureName( | ||
const aiScene* _scene, aiMaterial* _mat, const std::string& _type) const | ||
const std::string &_prefix, const aiScene *_scene, aiMaterial *_mat, | ||
const std::string &_type) const | ||
{ | ||
#ifdef GZ_ASSIMP_PRE_5_2_0 | ||
auto rootName = _scene->mRootNode->mName; | ||
#else | ||
auto rootName = _scene->mName; | ||
#endif | ||
return ToString(rootName) + "_" + ToString(_mat->GetName()) + | ||
"_" + _type; | ||
return _prefix + "_" + ToString(rootName) + "_" + | ||
ToString(_mat->GetName()) + "_" + _type; | ||
} | ||
|
||
////////////////////////////////////////////////// | ||
|
@@ -695,12 +708,18 @@ Mesh *AssimpLoader::Load(const std::string &_filename) | |
} | ||
auto& rootNode = scene->mRootNode; | ||
auto rootName = ToString(rootNode->mName); | ||
|
||
// compute assimp root node transform | ||
std::string extension = _filename.substr(_filename.rfind(".") + 1, | ||
_filename.size()); | ||
auto fileBaseName = common::basename(_filename); | ||
std::string extension; | ||
std::size_t extIdx = _filename.rfind("."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added. 163222b |
||
if (extIdx != std::string::npos) | ||
{ | ||
extension = _filename.substr(extIdx + 1, _filename.size()); | ||
fileBaseName = fileBaseName.substr(0, fileBaseName.rfind(extension) - 1); | ||
} | ||
std::transform(extension.begin(), extension.end(), | ||
extension.begin(), ::tolower); | ||
|
||
// compute assimp root node transform | ||
bool useIdentityRotation = (extension != "glb" && extension != "glTF"); | ||
auto transform = this->dataPtr->UpdatedRootNodeTransform(scene, | ||
useIdentityRotation); | ||
|
@@ -709,7 +728,8 @@ Mesh *AssimpLoader::Load(const std::string &_filename) | |
// Add the materials first | ||
for (unsigned _matIdx = 0; _matIdx < scene->mNumMaterials; ++_matIdx) | ||
{ | ||
auto mat = this->dataPtr->CreateMaterial(scene, _matIdx, path); | ||
auto mat = this->dataPtr->CreateMaterial(scene, _matIdx, path, | ||
fileBaseName); | ||
mesh->AddMaterial(mat); | ||
} | ||
// Create the skeleton | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is breaking API in
gz-common5
, you should targetmain
, isn't it ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function is in the private
AssimpLoader::Implementation
class and not exposed in header. So should be fine?