diff options
-rw-r--r-- | indra/newview/gltf/llgltfloader.cpp | 63 | ||||
-rw-r--r-- | indra/newview/gltf/llgltfloader.h | 16 |
2 files changed, 50 insertions, 29 deletions
diff --git a/indra/newview/gltf/llgltfloader.cpp b/indra/newview/gltf/llgltfloader.cpp index 3d46db9e6a..3019a12446 100644 --- a/indra/newview/gltf/llgltfloader.cpp +++ b/indra/newview/gltf/llgltfloader.cpp @@ -565,7 +565,7 @@ bool LLGLTFLoader::addJointToModelSkin(LLMeshSkinInfo& skin_info, S32 gltf_skin_ return true; } -LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index) +LLGLTFLoader::LLGLTFImportMaterial LLGLTFLoader::processMaterial(S32 material_index, S32 fallback_index) { // Check cache first auto cached = mMaterialCache.find(material_index); @@ -577,6 +577,9 @@ LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index) LLImportMaterial impMat; impMat.mDiffuseColor = LLColor4::white; // Default color + // Generate material name + std::string materialName = generateMaterialName(material_index, fallback_index); + // Process material if available if (material_index >= 0 && material_index < mGLTFAsset.mMaterials.size()) { @@ -602,39 +605,36 @@ LLImportMaterial LLGLTFLoader::processMaterial(S32 material_index) impMat.mDiffuseMapLabel = material->mName.empty() ? filename : material->mName; // Check if the texture is already loaded - if (texIndex < mGLTFAsset.mTextures.size()) + S32 sourceIndex; + if (validateTextureIndex(texIndex, sourceIndex)) { - S32 sourceIndex = mGLTFAsset.mTextures[texIndex].mSource; - if (sourceIndex >= 0 && sourceIndex < mGLTFAsset.mImages.size()) + LL::GLTF::Image& image = mGLTFAsset.mImages[sourceIndex]; + if (image.mTexture.notNull()) { - LL::GLTF::Image& image = mGLTFAsset.mImages[sourceIndex]; - if (image.mTexture.notNull()) - { - impMat.setDiffuseMap(image.mTexture->getID()); - LL_INFOS("GLTF_IMPORT") << "Using existing texture ID: " << image.mTexture->getID().asString() << LL_ENDL; - } - else - { - LL_INFOS("GLTF_IMPORT") << "Texture needs loading: " << impMat.mDiffuseMapFilename << LL_ENDL; - } + impMat.setDiffuseMap(image.mTexture->getID()); + LL_INFOS("GLTF_IMPORT") << "Using existing texture ID: " << image.mTexture->getID().asString() << LL_ENDL; + } + else + { + LL_INFOS("GLTF_IMPORT") << "Texture needs loading: " << impMat.mDiffuseMapFilename << LL_ENDL; } } } } } + // Create cached material with both material and name + LLGLTFImportMaterial cachedMat(impMat, materialName); + // Cache the processed material - mMaterialCache[material_index] = impMat; - return impMat; + mMaterialCache[material_index] = cachedMat; + return cachedMat; } std::string LLGLTFLoader::processTexture(S32 texture_index, const std::string& texture_type, const std::string& material_name) { - if (texture_index < 0 || texture_index >= mGLTFAsset.mTextures.size()) - return ""; - - S32 sourceIndex = mGLTFAsset.mTextures[texture_index].mSource; - if (sourceIndex < 0 || sourceIndex >= mGLTFAsset.mImages.size()) + S32 sourceIndex; + if (!validateTextureIndex(texture_index, sourceIndex)) return ""; LL::GLTF::Image& image = mGLTFAsset.mImages[sourceIndex]; @@ -669,6 +669,18 @@ std::string LLGLTFLoader::processTexture(S32 texture_index, const std::string& t return ""; } +bool LLGLTFLoader::validateTextureIndex(S32 texture_index, S32& source_index) +{ + if (texture_index < 0 || texture_index >= mGLTFAsset.mTextures.size()) + return false; + + source_index = mGLTFAsset.mTextures[texture_index].mSource; + if (source_index < 0 || source_index >= mGLTFAsset.mImages.size()) + return false; + + return true; +} + std::string LLGLTFLoader::generateMaterialName(S32 material_index, S32 fallback_index) { if (material_index >= 0 && material_index < mGLTFAsset.mMaterials.size()) @@ -750,7 +762,10 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const std::string& bas std::vector<GLTFVertex> vertices; // Use cached material processing - LLImportMaterial impMat = processMaterial(prim.mMaterial); + LLGLTFImportMaterial cachedMat = processMaterial(prim.mMaterial, pModel->getNumVolumeFaces() - 1); + LLImportMaterial impMat = cachedMat; + std::string materialName = cachedMat.name; + mats[materialName] = impMat; if (prim.getIndexCount() % 3 != 0) { @@ -933,10 +948,6 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const std::string& bas } } - // Generate material name using helper method - std::string materialName = generateMaterialName(prim.mMaterial, pModel->getNumVolumeFaces() - 1); - mats[materialName] = impMat; - // Indices handling if (faceVertices.size() >= VERTICIES_LIMIT) { diff --git a/indra/newview/gltf/llgltfloader.h b/indra/newview/gltf/llgltfloader.h index 46506cdf50..e8b91996c7 100644 --- a/indra/newview/gltf/llgltfloader.h +++ b/indra/newview/gltf/llgltfloader.h @@ -71,6 +71,14 @@ class LLGLTFLoader : public LLModelLoader typedef std::map <S32, JointNodeData> joints_data_map_t; typedef std::map <std::string, S32> joints_name_to_node_map_t; + class LLGLTFImportMaterial : public LLImportMaterial + { + public: + std::string name; + LLGLTFImportMaterial() = default; + LLGLTFImportMaterial(const LLImportMaterial& mat, const std::string& n) : LLImportMaterial(mat), name(n) {} + }; + LLGLTFLoader(std::string filename, S32 lod, LLModelLoader::load_callback_t load_cb, @@ -131,16 +139,18 @@ protected: // per skin joint count, needs to be tracked for the sake of limits check. std::vector<S32> mValidJointsCount; - // Material cache to avoid duplicate processing - std::map<S32, LLImportMaterial> mMaterialCache; + // Cached material information + typedef std::map<S32, LLGLTFImportMaterial> MaterialCache; + MaterialCache mMaterialCache; private: bool parseMeshes(); void computeCombinedNodeTransform(const LL::GLTF::Asset& asset, S32 node_index, glm::mat4& combined_transform) const; void processNodeHierarchy(S32 node_idx, std::map<std::string, S32>& mesh_name_counts, U32 submodel_limit, const LLVolumeParams& volume_params); bool addJointToModelSkin(LLMeshSkinInfo& skin_info, S32 gltf_skin_idx, size_t gltf_joint_idx); - LLImportMaterial processMaterial(S32 material_index); + LLGLTFImportMaterial processMaterial(S32 material_index, S32 fallback_index); std::string processTexture(S32 texture_index, const std::string& texture_type, const std::string& material_name); + bool validateTextureIndex(S32 texture_index, S32& source_index); std::string generateMaterialName(S32 material_index, S32 fallback_index = -1); bool populateModelFromMesh(LLModel* pModel, const std::string& base_name, const LL::GLTF::Mesh &mesh, const LL::GLTF::Node &node, material_map& mats); void populateJointsFromSkin(S32 skin_idx); |