@@ -565,7 +565,7 @@ bool LLGLTFLoader::addJointToModelSkin(LLMeshSkinInfo& skin_info, S32 gltf_skin_
565
565
return true ;
566
566
}
567
567
568
- LLImportMaterial LLGLTFLoader::processMaterial (S32 material_index)
568
+ LLGLTFLoader::LLGLTFImportMaterial LLGLTFLoader::processMaterial (S32 material_index, S32 fallback_index )
569
569
{
570
570
// Check cache first
571
571
auto cached = mMaterialCache .find (material_index);
@@ -577,6 +577,9 @@ LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index)
577
577
LLImportMaterial impMat;
578
578
impMat.mDiffuseColor = LLColor4::white; // Default color
579
579
580
+ // Generate material name
581
+ std::string materialName = generateMaterialName (material_index, fallback_index);
582
+
580
583
// Process material if available
581
584
if (material_index >= 0 && material_index < mGLTFAsset .mMaterials .size ())
582
585
{
@@ -602,39 +605,36 @@ LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index)
602
605
impMat.mDiffuseMapLabel = material->mName .empty () ? filename : material->mName ;
603
606
604
607
// Check if the texture is already loaded
605
- if (texIndex < mGLTFAsset .mTextures .size ())
608
+ S32 sourceIndex;
609
+ if (validateTextureIndex (texIndex, sourceIndex))
606
610
{
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 ())
609
613
{
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;
620
620
}
621
621
}
622
622
}
623
623
}
624
624
}
625
625
626
+ // Create cached material with both material and name
627
+ LLGLTFImportMaterial cachedMat (impMat, materialName);
628
+
626
629
// Cache the processed material
627
- mMaterialCache [material_index] = impMat ;
628
- return impMat ;
630
+ mMaterialCache [material_index] = cachedMat ;
631
+ return cachedMat ;
629
632
}
630
633
631
634
std::string LLGLTFLoader::processTexture (S32 texture_index, const std::string& texture_type, const std::string& material_name)
632
635
{
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))
638
638
return " " ;
639
639
640
640
LL::GLTF::Image& image = mGLTFAsset .mImages [sourceIndex];
@@ -669,6 +669,18 @@ std::string LLGLTFLoader::processTexture(S32 texture_index, const std::string& t
669
669
return " " ;
670
670
}
671
671
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
+
672
684
std::string LLGLTFLoader::generateMaterialName (S32 material_index, S32 fallback_index)
673
685
{
674
686
if (material_index >= 0 && material_index < mGLTFAsset .mMaterials .size ())
@@ -750,7 +762,10 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const std::string& bas
750
762
std::vector<GLTFVertex> vertices;
751
763
752
764
// 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;
754
769
755
770
if (prim.getIndexCount () % 3 != 0 )
756
771
{
@@ -933,10 +948,6 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const std::string& bas
933
948
}
934
949
}
935
950
936
- // Generate material name using helper method
937
- std::string materialName = generateMaterialName (prim.mMaterial , pModel->getNumVolumeFaces () - 1 );
938
- mats[materialName] = impMat;
939
-
940
951
// Indices handling
941
952
if (faceVertices.size () >= VERTICIES_LIMIT)
942
953
{
0 commit comments