diff options
-rw-r--r-- | indra/newview/lllocalgltfmaterials.cpp | 145 | ||||
-rw-r--r-- | indra/newview/lltinygltfhelper.cpp | 124 | ||||
-rw-r--r-- | indra/newview/lltinygltfhelper.h | 13 |
3 files changed, 161 insertions, 121 deletions
diff --git a/indra/newview/lllocalgltfmaterials.cpp b/indra/newview/lllocalgltfmaterials.cpp index f194226fa5..3912ed5e30 100644 --- a/indra/newview/lllocalgltfmaterials.cpp +++ b/indra/newview/lllocalgltfmaterials.cpp @@ -47,7 +47,6 @@ #include "llscrolllistctrl.h" #include "lltinygltfhelper.h" #include "llviewertexture.h" -#include "tinygltf/tiny_gltf.h" /*=======================================*/ /* Formal declarations, constants, etc. */ @@ -83,7 +82,7 @@ LLLocalGLTFMaterial::LLLocalGLTFMaterial(std::string filename, S32 index) } else { - LL_WARNS() << "File of no valid extension given, local material creation aborted." << "\n" + LL_WARNS("GLTF") << "File of no valid extension given, local material creation aborted." << "\n" << "Filename: " << mFilename << LL_ENDL; return; // no valid extension. } @@ -180,7 +179,7 @@ bool LLLocalGLTFMaterial::updateSelf() } else { - LL_WARNS() << "During the update process the following file was found" << "\n" + LL_WARNS("GLTF") << "During the update process the following file was found" << "\n" << "but could not be opened or decoded for " << LL_LOCAL_UPDATE_RETRIES << " attempts." << "\n" << "Filename: " << mFilename << "\n" << "Disabling further update attempts for this file." << LL_ENDL; @@ -199,7 +198,7 @@ bool LLLocalGLTFMaterial::updateSelf() else { - LL_WARNS() << "During the update process, the following file was not found." << "\n" + LL_WARNS("GLTF") << "During the update process, the following file was not found." << "\n" << "Filename: " << mFilename << "\n" << "Disabling further update attempts for this file." << LL_ENDL; @@ -234,120 +233,24 @@ bool LLLocalGLTFMaterial::loadMaterial() case ET_MATERIAL_GLTF: case ET_MATERIAL_GLB: { - tinygltf::TinyGLTF loader; - std::string error_msg; - std::string warn_msg; - - tinygltf::Model model_in; - std::string filename_lc = mFilename; LLStringUtil::toLower(filename_lc); - - // Load a tinygltf model fom a file. Assumes that the input filename has already been - // been sanitized to one of (.gltf , .glb) extensions, so does a simple find to distinguish. - if (std::string::npos == filename_lc.rfind(".gltf")) - { // file is binary - decode_successful = loader.LoadBinaryFromFile(&model_in, &error_msg, &warn_msg, filename_lc); - } - else - { // file is ascii - decode_successful = loader.LoadASCIIFromFile(&model_in, &error_msg, &warn_msg, filename_lc); - } - - if (!decode_successful) - { - LL_WARNS() << "Cannot load Material, error: " << error_msg - << ", warning:" << warn_msg - << " file: " << mFilename - << LL_ENDL; - break; - } - - if (model_in.materials.size() <= mMaterialIndex) - { - // materials are missing - LL_WARNS() << "Cannot load Material, Material " << mMaterialIndex << " is missing, " << mFilename << LL_ENDL; - decode_successful = false; - break; - } - - // sets everything, but textures will have inaccurate ids - mGLTFMaterial->setFromModel(model_in, mMaterialIndex); - - std::string folder = gDirUtilp->getDirName(filename_lc); - tinygltf::Material material_in = model_in.materials[mMaterialIndex]; - - if (!material_in.name.empty()) - { - mShortName = gDirUtilp->getBaseFileName(filename_lc, true) + " (" + material_in.name + ")"; - } - - // get base color texture - LLPointer<LLImageRaw> base_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.pbrMetallicRoughness.baseColorTexture.index); - // get normal map - LLPointer<LLImageRaw> normal_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.normalTexture.index); - // get metallic-roughness texture - LLPointer<LLImageRaw> mr_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.pbrMetallicRoughness.metallicRoughnessTexture.index); - // get emissive texture - LLPointer<LLImageRaw> emissive_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.emissiveTexture.index); - // get occlusion map if needed - LLPointer<LLImageRaw> occlusion_img; - if (material_in.occlusionTexture.index != material_in.pbrMetallicRoughness.metallicRoughnessTexture.index) - { - occlusion_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.occlusionTexture.index); - } - - // todo: pass it into local bitmaps? - LLTinyGLTFHelper::initFetchedTextures(material_in, - base_img, normal_img, mr_img, emissive_img, occlusion_img, - mBaseColorFetched, mNormalFetched, mMRFetched, mEmissiveFetched); - - if (mBaseColorFetched) - { - mBaseColorFetched->addTextureStats(64.f * 64.f, TRUE); - mGLTFMaterial->mBaseColorId = mBaseColorFetched->getID(); - mGLTFMaterial->mBaseColorTexture = mBaseColorFetched; - } - else - { - mGLTFMaterial->mBaseColorId = LLUUID::null; - mGLTFMaterial->mBaseColorTexture = nullptr; - } - - if (mNormalFetched) - { - mNormalFetched->addTextureStats(64.f * 64.f, TRUE); - mGLTFMaterial->mNormalId = mNormalFetched->getID(); - mGLTFMaterial->mNormalTexture = mBaseColorFetched; - } - else - { - mGLTFMaterial->mNormalId = LLUUID::null; - mGLTFMaterial->mNormalTexture = nullptr; - } - - if (mMRFetched) - { - mMRFetched->addTextureStats(64.f * 64.f, TRUE); - mGLTFMaterial->mMetallicRoughnessId = mMRFetched->getID(); - mGLTFMaterial->mMetallicRoughnessTexture = mBaseColorFetched; - } - else - { - mGLTFMaterial->mMetallicRoughnessId = LLUUID::null; - mGLTFMaterial->mMetallicRoughnessTexture = nullptr; - } - - if (mEmissiveFetched) - { - mEmissiveFetched->addTextureStats(64.f * 64.f, TRUE); - mGLTFMaterial->mEmissiveId = mEmissiveFetched->getID(); - mGLTFMaterial->mEmissiveTexture = mBaseColorFetched; - } - else + std::string material_name; + + // Might be a good idea to make these textures into local textures + LLTinyGLTFHelper::getMaterialFromFile( + mFilename, + mMaterialIndex, + mGLTFMaterial, + material_name, + mBaseColorFetched, + mNormalFetched, + mMRFetched, + mEmissiveFetched); + + if (!material_name.empty()) { - mGLTFMaterial->mEmissiveId = LLUUID::null; - mGLTFMaterial->mEmissiveTexture = nullptr; + mShortName = gDirUtilp->getBaseFileName(filename_lc, true) + " (" + material_name + ")"; } break; @@ -359,9 +262,9 @@ bool LLLocalGLTFMaterial::loadMaterial() // accessing mFilename and any other object properties might very well crash the viewer. // getting here should be impossible, or there's been a pretty serious bug. - LL_WARNS() << "During a decode attempt, the following local material had no properly assigned extension." << LL_ENDL; - LL_WARNS() << "Filename: " << mFilename << LL_ENDL; - LL_WARNS() << "Disabling further update attempts for this file." << LL_ENDL; + LL_WARNS("GLTF") << "During a decode attempt, the following local material had no properly assigned extension." << LL_ENDL; + LL_WARNS("GLTF") << "Filename: " << mFilename << LL_ENDL; + LL_WARNS("GLTF") << "Disabling further update attempts for this file." << LL_ENDL; mLinkStatus = LS_BROKEN; } } @@ -461,7 +364,7 @@ S32 LLLocalGLTFMaterialMgr::addUnit(const std::string& filename) if (!decode_successful) { - LL_WARNS() << "Cannot load, error: Failed to decode" << error_msg + LL_WARNS("GLTF") << "Cannot load, error: Failed to decode" << error_msg << ", warning:" << warn_msg << " file: " << filename << LL_ENDL; @@ -471,7 +374,7 @@ S32 LLLocalGLTFMaterialMgr::addUnit(const std::string& filename) if (model_in.materials.empty()) { // materials are missing - LL_WARNS() << "Cannot load. File has no materials " << filename << LL_ENDL; + LL_WARNS("GLTF") << "Cannot load. File has no materials " << filename << LL_ENDL; return 0; } materials_in_file = model_in.materials.size(); @@ -492,7 +395,7 @@ S32 LLLocalGLTFMaterialMgr::addUnit(const std::string& filename) } else { - LL_WARNS() << "Attempted to add invalid or unreadable image file, attempt cancelled.\n" + LL_WARNS("GLTF") << "Attempted to add invalid or unreadable image file, attempt cancelled.\n" << "Filename: " << filename << LL_ENDL; LLSD notif_args; diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index c80e87652a..cff26ea51f 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -181,3 +181,127 @@ LLImageRaw * LLTinyGLTFHelper::getTexture(const std::string & folder, const tiny return rawImage; } + +bool LLTinyGLTFHelper::getMaterialFromFile( + const std::string& filename, + S32 mat_index, + LLPointer < LLFetchedGLTFMaterial> material, + std::string& material_name, + LLPointer<LLViewerFetchedTexture>& base_color_tex, + LLPointer<LLViewerFetchedTexture>& normal_tex, + LLPointer<LLViewerFetchedTexture>& mr_tex, + LLPointer<LLViewerFetchedTexture>& emissive_tex) +{ + tinygltf::TinyGLTF loader; + std::string error_msg; + std::string warn_msg; + tinygltf::Model model_in; + std::string filename_lc = filename; + bool decode_successful = true; + + LLStringUtil::toLower(filename_lc); + + // Load a tinygltf model fom a file. Assumes that the input filename has already been + // been sanitized to one of (.gltf , .glb) extensions, so does a simple find to distinguish. + if (std::string::npos == filename_lc.rfind(".gltf")) + { // file is binary + decode_successful = loader.LoadBinaryFromFile(&model_in, &error_msg, &warn_msg, filename_lc); + } + else + { // file is ascii + decode_successful = loader.LoadASCIIFromFile(&model_in, &error_msg, &warn_msg, filename_lc); + } + + if (!decode_successful) + { + LL_WARNS("GLTF") << "Cannot load Material, error: " << error_msg + << ", warning:" << warn_msg + << " file: " << filename + << LL_ENDL; + return false; + } + else if (model_in.materials.size() <= mat_index) + { + // materials are missing + LL_WARNS("GLTF") << "Cannot load Material, Material " << mat_index << " is missing, " << filename << LL_ENDL; + return false; + } + + material->setFromModel(model_in, mat_index); + + std::string folder = gDirUtilp->getDirName(filename_lc); + tinygltf::Material material_in = model_in.materials[mat_index]; + + material_name = material_in.name; + + // get base color texture + LLPointer<LLImageRaw> base_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.pbrMetallicRoughness.baseColorTexture.index); + // get normal map + LLPointer<LLImageRaw> normal_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.normalTexture.index); + // get metallic-roughness texture + LLPointer<LLImageRaw> mr_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.pbrMetallicRoughness.metallicRoughnessTexture.index); + // get emissive texture + LLPointer<LLImageRaw> emissive_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.emissiveTexture.index); + // get occlusion map if needed + LLPointer<LLImageRaw> occlusion_img; + if (material_in.occlusionTexture.index != material_in.pbrMetallicRoughness.metallicRoughnessTexture.index) + { + occlusion_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.occlusionTexture.index); + } + + // todo: pass it into local bitmaps? + LLTinyGLTFHelper::initFetchedTextures(material_in, + base_img, normal_img, mr_img, emissive_img, occlusion_img, + base_color_tex, normal_tex, mr_tex, emissive_tex); + + if (base_color_tex) + { + base_color_tex->addTextureStats(64.f * 64.f, TRUE); + material->mBaseColorId = base_color_tex->getID(); + material->mBaseColorTexture = base_color_tex; + } + else + { + material->mBaseColorId = LLUUID::null; + material->mBaseColorTexture = nullptr; + } + + if (normal_tex) + { + normal_tex->addTextureStats(64.f * 64.f, TRUE); + material->mNormalId = normal_tex->getID(); + material->mNormalTexture = normal_tex; + } + else + { + material->mNormalId = LLUUID::null; + material->mNormalTexture = nullptr; + } + + if (mr_tex) + { + mr_tex->addTextureStats(64.f * 64.f, TRUE); + material->mMetallicRoughnessId = mr_tex->getID(); + material->mMetallicRoughnessTexture = mr_tex; + } + else + { + material->mMetallicRoughnessId = LLUUID::null; + material->mMetallicRoughnessTexture = nullptr; + } + + if (emissive_tex) + { + emissive_tex->addTextureStats(64.f * 64.f, TRUE); + material->mEmissiveId = emissive_tex->getID(); + material->mEmissiveTexture = emissive_tex; + } + else + { + material->mEmissiveId = LLUUID::null; + material->mEmissiveTexture = nullptr; + } + + return true; + +} diff --git a/indra/newview/lltinygltfhelper.h b/indra/newview/lltinygltfhelper.h index 9c2e5afc17..48a6985641 100644 --- a/indra/newview/lltinygltfhelper.h +++ b/indra/newview/lltinygltfhelper.h @@ -27,6 +27,7 @@ #pragma once #include "llgltfmaterial.h" +#include "llgltfmateriallist.h" #include "llpointer.h" #include "tinygltf/tiny_gltf.h" @@ -40,6 +41,18 @@ namespace LLTinyGLTFHelper LLImageRaw* getTexture(const std::string& folder, const tinygltf::Model& model, S32 texture_index, std::string& name); LLImageRaw* getTexture(const std::string& folder, const tinygltf::Model& model, S32 texture_index); + LLImageRaw* getTexture(const std::string& folder, const tinygltf::Model& model, S32 texture_index); + + bool getMaterialFromFile( + const std::string& filename, + S32 mat_index, + LLPointer < LLFetchedGLTFMaterial> material, + std::string& material_name, + LLPointer<LLViewerFetchedTexture>& base_color_tex, + LLPointer<LLViewerFetchedTexture>& normal_tex, + LLPointer<LLViewerFetchedTexture>& mr_tex, + LLPointer<LLViewerFetchedTexture>& emissive_tex); + void initFetchedTextures(tinygltf::Material& material, LLPointer<LLImageRaw>& base_color_img, LLPointer<LLImageRaw>& normal_img, |