Skip to content

Commit d033bba

Browse files
committed
#4204 Refactor material name and texture handling in GLTF loader
1 parent 63134f7 commit d033bba

File tree

2 files changed

+75
-64
lines changed

2 files changed

+75
-64
lines changed

indra/newview/gltf/llgltfloader.cpp

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -594,67 +594,30 @@ LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index)
594594
if (material->mPbrMetallicRoughness.mBaseColorTexture.mIndex >= 0)
595595
{
596596
S32 texIndex = material->mPbrMetallicRoughness.mBaseColorTexture.mIndex;
597-
if (texIndex < mGLTFAsset.mTextures.size())
597+
std::string filename = processTexture(texIndex, "base_color", material->mName);
598+
599+
if (!filename.empty())
598600
{
599-
S32 sourceIndex = mGLTFAsset.mTextures[texIndex].mSource;
600-
if (sourceIndex >= 0 && sourceIndex < mGLTFAsset.mImages.size())
601-
{
602-
LL::GLTF::Image& image = mGLTFAsset.mImages[sourceIndex];
601+
impMat.mDiffuseMapFilename = filename;
602+
impMat.mDiffuseMapLabel = material->mName.empty() ? filename : material->mName;
603603

604-
// Use URI as texture file name
605-
if (!image.mUri.empty())
604+
// Check if the texture is already loaded
605+
if (texIndex < mGLTFAsset.mTextures.size())
606+
{
607+
S32 sourceIndex = mGLTFAsset.mTextures[texIndex].mSource;
608+
if (sourceIndex >= 0 && sourceIndex < mGLTFAsset.mImages.size())
606609
{
607-
// URI might be a remote URL or a local path
608-
std::string filename = image.mUri;
609-
610-
// Extract just the filename from the URI
611-
size_t pos = filename.find_last_of("/\\");
612-
if (pos != std::string::npos)
613-
{
614-
filename = filename.substr(pos + 1);
615-
}
616-
617-
// Store the texture filename
618-
impMat.mDiffuseMapFilename = filename;
619-
impMat.mDiffuseMapLabel = material->mName.empty() ? filename : material->mName;
620-
621-
LL_INFOS("GLTF_IMPORT") << "Found texture: " << impMat.mDiffuseMapFilename
622-
<< " for material: " << material->mName << LL_ENDL;
623-
624-
LLSD args;
625-
args["Message"] = "TextureFound";
626-
args["TEXTURE_NAME"] = impMat.mDiffuseMapFilename;
627-
args["MATERIAL_NAME"] = material->mName;
628-
mWarningsArray.append(args);
629-
630-
// If the image has a texture loaded already, use it
610+
LL::GLTF::Image& image = mGLTFAsset.mImages[sourceIndex];
631611
if (image.mTexture.notNull())
632612
{
633613
impMat.setDiffuseMap(image.mTexture->getID());
634614
LL_INFOS("GLTF_IMPORT") << "Using existing texture ID: " << image.mTexture->getID().asString() << LL_ENDL;
635615
}
636616
else
637617
{
638-
// Texture will be loaded later through the callback system
639618
LL_INFOS("GLTF_IMPORT") << "Texture needs loading: " << impMat.mDiffuseMapFilename << LL_ENDL;
640619
}
641620
}
642-
else if (image.mTexture.notNull())
643-
{
644-
// No URI but we have a texture, use it directly
645-
impMat.setDiffuseMap(image.mTexture->getID());
646-
LL_INFOS("GLTF_IMPORT") << "Using existing texture ID without URI: " << image.mTexture->getID().asString() << LL_ENDL;
647-
}
648-
else if (image.mBufferView >= 0)
649-
{
650-
// For embedded textures (no URI but has buffer data)
651-
std::string temp_filename = extractTextureToTempFile(texIndex, "base_color");
652-
if (!temp_filename.empty())
653-
{
654-
impMat.mDiffuseMapFilename = temp_filename;
655-
impMat.mDiffuseMapLabel = material->mName.empty() ? temp_filename : material->mName;
656-
}
657-
}
658621
}
659622
}
660623
}
@@ -665,6 +628,66 @@ LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index)
665628
return impMat;
666629
}
667630

631+
std::string LLGLTFLoader::processTexture(S32 texture_index, const std::string& texture_type, const std::string& material_name)
632+
{
633+
if (texture_index < 0 || texture_index >= mGLTFAsset.mTextures.size())
634+
return "";
635+
636+
S32 sourceIndex = mGLTFAsset.mTextures[texture_index].mSource;
637+
if (sourceIndex < 0 || sourceIndex >= mGLTFAsset.mImages.size())
638+
return "";
639+
640+
LL::GLTF::Image& image = mGLTFAsset.mImages[sourceIndex];
641+
642+
// Process URI-based textures
643+
if (!image.mUri.empty())
644+
{
645+
std::string filename = image.mUri;
646+
size_t pos = filename.find_last_of("/\\");
647+
if (pos != std::string::npos)
648+
{
649+
filename = filename.substr(pos + 1);
650+
}
651+
652+
LL_INFOS("GLTF_IMPORT") << "Found texture: " << filename << " for material: " << material_name << LL_ENDL;
653+
654+
LLSD args;
655+
args["Message"] = "TextureFound";
656+
args["TEXTURE_NAME"] = filename;
657+
args["MATERIAL_NAME"] = material_name;
658+
mWarningsArray.append(args);
659+
660+
return filename;
661+
}
662+
663+
// Process embedded textures
664+
if (image.mBufferView >= 0)
665+
{
666+
return extractTextureToTempFile(texture_index, texture_type);
667+
}
668+
669+
return "";
670+
}
671+
672+
std::string LLGLTFLoader::generateMaterialName(S32 material_index, S32 fallback_index)
673+
{
674+
if (material_index >= 0 && material_index < mGLTFAsset.mMaterials.size())
675+
{
676+
LL::GLTF::Material* material = &mGLTFAsset.mMaterials[material_index];
677+
std::string materialName = material->mName;
678+
679+
if (materialName.empty())
680+
{
681+
materialName = "mat" + std::to_string(material_index);
682+
}
683+
return materialName;
684+
}
685+
else
686+
{
687+
return fallback_index >= 0 ? "mat_default" + std::to_string(fallback_index) : "mat_default";
688+
}
689+
}
690+
668691
bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const std::string& base_name, const LL::GLTF::Mesh& mesh, const LL::GLTF::Node& nodeno, material_map& mats)
669692
{
670693
// Set the requested label for the floater display and uploading
@@ -910,22 +933,8 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const std::string& bas
910933
}
911934
}
912935

913-
// Create a unique material name for this primitive
914-
std::string materialName;
915-
if (prim.mMaterial >= 0 && prim.mMaterial < mGLTFAsset.mMaterials.size())
916-
{
917-
LL::GLTF::Material* material = &mGLTFAsset.mMaterials[prim.mMaterial];
918-
materialName = material->mName;
919-
920-
if (materialName.empty())
921-
{
922-
materialName = "mat" + std::to_string(prim.mMaterial);
923-
}
924-
}
925-
else
926-
{
927-
materialName = "mat_default" + std::to_string(pModel->getNumVolumeFaces() - 1);
928-
}
936+
// Generate material name using helper method
937+
std::string materialName = generateMaterialName(prim.mMaterial, pModel->getNumVolumeFaces() - 1);
929938
mats[materialName] = impMat;
930939

931940
// Indices handling

indra/newview/gltf/llgltfloader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class LLGLTFLoader : public LLModelLoader
140140
void processNodeHierarchy(S32 node_idx, std::map<std::string, S32>& mesh_name_counts, U32 submodel_limit, const LLVolumeParams& volume_params);
141141
bool addJointToModelSkin(LLMeshSkinInfo& skin_info, S32 gltf_skin_idx, size_t gltf_joint_idx);
142142
LLImportMaterial processMaterial(S32 material_index);
143+
std::string processTexture(S32 texture_index, const std::string& texture_type, const std::string& material_name);
144+
std::string generateMaterialName(S32 material_index, S32 fallback_index = -1);
143145
bool populateModelFromMesh(LLModel* pModel, const std::string& base_name, const LL::GLTF::Mesh &mesh, const LL::GLTF::Node &node, material_map& mats);
144146
void populateJointsFromSkin(S32 skin_idx);
145147
void populateJointGroups();

0 commit comments

Comments
 (0)