@@ -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
631634std::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+
672684std::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 {
0 commit comments