Skip to content

Commit b046a8b

Browse files
committed
#4204 Cache material names, centralize texture index validation
1 parent 5a50be4 commit b046a8b

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

indra/newview/gltf/llgltfloader.cpp

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ bool LLGLTFLoader::addJointToModelSkin(LLMeshSkinInfo& skin_info, S32 gltf_skin_
565565
return true;
566566
}
567567

568-
LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index)
568+
LLGLTFLoader::LLGLTFImportMaterial LLGLTFLoader::processMaterial(S32 material_index, S32 fallback_index)
569569
{
570570
// Check cache first
571571
auto cached = mMaterialCache.find(material_index);
@@ -577,6 +577,9 @@ LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index)
577577
LLImportMaterial impMat;
578578
impMat.mDiffuseColor = LLColor4::white; // Default color
579579

580+
// Generate material name
581+
std::string materialName = generateMaterialName(material_index, fallback_index);
582+
580583
// Process material if available
581584
if (material_index >= 0 && material_index < mGLTFAsset.mMaterials.size())
582585
{
@@ -602,39 +605,36 @@ LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index)
602605
impMat.mDiffuseMapLabel = material->mName.empty() ? filename : material->mName;
603606

604607
// Check if the texture is already loaded
605-
if (texIndex < mGLTFAsset.mTextures.size())
608+
S32 sourceIndex;
609+
if (validateTextureIndex(texIndex, sourceIndex))
606610
{
607-
S32 sourceIndex = mGLTFAsset.mTextures[texIndex].mSource;
608-
if (sourceIndex >= 0 && sourceIndex < mGLTFAsset.mImages.size())
611+
LL::GLTF::Image& image = mGLTFAsset.mImages[sourceIndex];
612+
if (image.mTexture.notNull())
609613
{
610-
LL::GLTF::Image& image = mGLTFAsset.mImages[sourceIndex];
611-
if (image.mTexture.notNull())
612-
{
613-
impMat.setDiffuseMap(image.mTexture->getID());
614-
LL_INFOS("GLTF_IMPORT") << "Using existing texture ID: " << image.mTexture->getID().asString() << LL_ENDL;
615-
}
616-
else
617-
{
618-
LL_INFOS("GLTF_IMPORT") << "Texture needs loading: " << impMat.mDiffuseMapFilename << LL_ENDL;
619-
}
614+
impMat.setDiffuseMap(image.mTexture->getID());
615+
LL_INFOS("GLTF_IMPORT") << "Using existing texture ID: " << image.mTexture->getID().asString() << LL_ENDL;
616+
}
617+
else
618+
{
619+
LL_INFOS("GLTF_IMPORT") << "Texture needs loading: " << impMat.mDiffuseMapFilename << LL_ENDL;
620620
}
621621
}
622622
}
623623
}
624624
}
625625

626+
// Create cached material with both material and name
627+
LLGLTFImportMaterial cachedMat(impMat, materialName);
628+
626629
// Cache the processed material
627-
mMaterialCache[material_index] = impMat;
628-
return impMat;
630+
mMaterialCache[material_index] = cachedMat;
631+
return cachedMat;
629632
}
630633

631634
std::string LLGLTFLoader::processTexture(S32 texture_index, const std::string& texture_type, const std::string& material_name)
632635
{
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())
636+
S32 sourceIndex;
637+
if (!validateTextureIndex(texture_index, sourceIndex))
638638
return "";
639639

640640
LL::GLTF::Image& image = mGLTFAsset.mImages[sourceIndex];
@@ -669,6 +669,18 @@ std::string LLGLTFLoader::processTexture(S32 texture_index, const std::string& t
669669
return "";
670670
}
671671

672+
bool LLGLTFLoader::validateTextureIndex(S32 texture_index, S32& source_index)
673+
{
674+
if (texture_index < 0 || texture_index >= mGLTFAsset.mTextures.size())
675+
return false;
676+
677+
source_index = mGLTFAsset.mTextures[texture_index].mSource;
678+
if (source_index < 0 || source_index >= mGLTFAsset.mImages.size())
679+
return false;
680+
681+
return true;
682+
}
683+
672684
std::string LLGLTFLoader::generateMaterialName(S32 material_index, S32 fallback_index)
673685
{
674686
if (material_index >= 0 && material_index < mGLTFAsset.mMaterials.size())
@@ -750,7 +762,10 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const std::string& bas
750762
std::vector<GLTFVertex> vertices;
751763

752764
// Use cached material processing
753-
LLImportMaterial impMat = processMaterial(prim.mMaterial);
765+
LLGLTFImportMaterial cachedMat = processMaterial(prim.mMaterial, pModel->getNumVolumeFaces() - 1);
766+
LLImportMaterial impMat = cachedMat;
767+
std::string materialName = cachedMat.name;
768+
mats[materialName] = impMat;
754769

755770
if (prim.getIndexCount() % 3 != 0)
756771
{
@@ -933,10 +948,6 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const std::string& bas
933948
}
934949
}
935950

936-
// Generate material name using helper method
937-
std::string materialName = generateMaterialName(prim.mMaterial, pModel->getNumVolumeFaces() - 1);
938-
mats[materialName] = impMat;
939-
940951
// Indices handling
941952
if (faceVertices.size() >= VERTICIES_LIMIT)
942953
{

indra/newview/gltf/llgltfloader.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ class LLGLTFLoader : public LLModelLoader
7171
typedef std::map <S32, JointNodeData> joints_data_map_t;
7272
typedef std::map <std::string, S32> joints_name_to_node_map_t;
7373

74+
class LLGLTFImportMaterial : public LLImportMaterial
75+
{
76+
public:
77+
std::string name;
78+
LLGLTFImportMaterial() = default;
79+
LLGLTFImportMaterial(const LLImportMaterial& mat, const std::string& n) : LLImportMaterial(mat), name(n) {}
80+
};
81+
7482
LLGLTFLoader(std::string filename,
7583
S32 lod,
7684
LLModelLoader::load_callback_t load_cb,
@@ -131,16 +139,18 @@ class LLGLTFLoader : public LLModelLoader
131139
// per skin joint count, needs to be tracked for the sake of limits check.
132140
std::vector<S32> mValidJointsCount;
133141

134-
// Material cache to avoid duplicate processing
135-
std::map<S32, LLImportMaterial> mMaterialCache;
142+
// Cached material information
143+
typedef std::map<S32, LLGLTFImportMaterial> MaterialCache;
144+
MaterialCache mMaterialCache;
136145

137146
private:
138147
bool parseMeshes();
139148
void computeCombinedNodeTransform(const LL::GLTF::Asset& asset, S32 node_index, glm::mat4& combined_transform) const;
140149
void processNodeHierarchy(S32 node_idx, std::map<std::string, S32>& mesh_name_counts, U32 submodel_limit, const LLVolumeParams& volume_params);
141150
bool addJointToModelSkin(LLMeshSkinInfo& skin_info, S32 gltf_skin_idx, size_t gltf_joint_idx);
142-
LLImportMaterial processMaterial(S32 material_index);
151+
LLGLTFImportMaterial processMaterial(S32 material_index, S32 fallback_index);
143152
std::string processTexture(S32 texture_index, const std::string& texture_type, const std::string& material_name);
153+
bool validateTextureIndex(S32 texture_index, S32& source_index);
144154
std::string generateMaterialName(S32 material_index, S32 fallback_index = -1);
145155
bool populateModelFromMesh(LLModel* pModel, const std::string& base_name, const LL::GLTF::Mesh &mesh, const LL::GLTF::Node &node, material_map& mats);
146156
void populateJointsFromSkin(S32 skin_idx);

0 commit comments

Comments
 (0)