From 4bb419031c130402a5589ff698e28e230a0c123a Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Fri, 19 Aug 2022 01:45:47 +0300 Subject: SL-17653 Basic local gltf materials --- indra/newview/lltinygltfhelper.cpp | 245 +++++++++++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 indra/newview/lltinygltfhelper.cpp (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp new file mode 100644 index 0000000000..935f8e7794 --- /dev/null +++ b/indra/newview/lltinygltfhelper.cpp @@ -0,0 +1,245 @@ +/** + * @file lltinygltfhelper.cpp + * + * $LicenseInfo:firstyear=2022&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2022, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +#include "llviewerprecompiledheaders.h" + +#include "lltinygltfhelper.h" + +#include "llimage.h" +#include "llviewertexture.h" +#include "llviewertexturelist.h" + +void strip_alpha_channel(LLPointer& img) +{ + if (img->getComponents() == 4) + { + LLImageRaw* tmp = new LLImageRaw(img->getWidth(), img->getHeight(), 3); + tmp->copyUnscaled4onto3(img); + img = tmp; + } +} + +// copy red channel from src_img to dst_img +// PRECONDITIONS: +// dst_img must be 3 component +// src_img and dst_image must have the same dimensions +void copy_red_channel(LLPointer& src_img, LLPointer& dst_img) +{ + llassert(src_img->getWidth() == dst_img->getWidth() && src_img->getHeight() == dst_img->getHeight()); + llassert(dst_img->getComponents() == 3); + + U32 pixel_count = dst_img->getWidth() * dst_img->getHeight(); + U8* src = src_img->getData(); + U8* dst = dst_img->getData(); + S8 src_components = src_img->getComponents(); + + for (U32 i = 0; i < pixel_count; ++i) + { + dst[i * 3] = src[i * src_components]; + } +} + +void LLTinyGLTFHelper::initFetchedTextures(tinygltf::Material& material, + LLPointer& albedo_img, + LLPointer& normal_img, + LLPointer& mr_img, + LLPointer& emissive_img, + LLPointer& occlusion_img, + LLPointer& albedo_tex, + LLPointer& normal_tex, + LLPointer& mr_tex, + LLPointer& emissive_tex) +{ + if (albedo_img) + { + albedo_tex = LLViewerTextureManager::getFetchedTexture(albedo_img, FTType::FTT_LOCAL_FILE, true); + } + + if (normal_img) + { + strip_alpha_channel(normal_img); + normal_tex = LLViewerTextureManager::getFetchedTexture(normal_img, FTType::FTT_LOCAL_FILE, true); + } + + if (mr_img) + { + strip_alpha_channel(mr_img); + + if (occlusion_img && material.pbrMetallicRoughness.metallicRoughnessTexture.index != material.occlusionTexture.index) + { + // occlusion is a distinct texture from pbrMetallicRoughness + // pack into mr red channel + int occlusion_idx = material.occlusionTexture.index; + int mr_idx = material.pbrMetallicRoughness.metallicRoughnessTexture.index; + if (occlusion_idx != mr_idx) + { + //scale occlusion image to match resolution of mr image + occlusion_img->scale(mr_img->getWidth(), mr_img->getHeight()); + + copy_red_channel(occlusion_img, mr_img); + } + } + } + else if (occlusion_img) + { + //no mr but occlusion exists, make a white mr_img and copy occlusion red channel over + mr_img = new LLImageRaw(occlusion_img->getWidth(), occlusion_img->getHeight(), 3); + mr_img->clear(255, 255, 255); + copy_red_channel(occlusion_img, mr_img); + } + + if (mr_img) + { + mr_tex = LLViewerTextureManager::getFetchedTexture(mr_img, FTType::FTT_LOCAL_FILE, true); + } + + if (emissive_img) + { + strip_alpha_channel(emissive_img); + emissive_tex = LLViewerTextureManager::getFetchedTexture(emissive_img, FTType::FTT_LOCAL_FILE, true); + } +} + +void LLTinyGLTFHelper::setFromModel(LLGLTFMaterial* mat, tinygltf::Model& model) +{ + S32 index; + + auto& material_in = model.materials[0]; + + // get albedo texture + index = material_in.pbrMetallicRoughness.baseColorTexture.index; + if (index >= 0) + { + mat->mAlbedoId.set(model.images[index].uri); + } + else + { + mat->mAlbedoId.setNull(); + } + + // get normal map + index = material_in.normalTexture.index; + if (index >= 0) + { + mat->mNormalId.set(model.images[index].uri); + } + else + { + mat->mNormalId.setNull(); + } + + // get metallic-roughness texture + index = material_in.pbrMetallicRoughness.metallicRoughnessTexture.index; + if (index >= 0) + { + mat->mMetallicRoughnessId.set(model.images[index].uri); + } + else + { + mat->mMetallicRoughnessId.setNull(); + } + + // get emissive texture + index = material_in.emissiveTexture.index; + if (index >= 0) + { + mat->mEmissiveId.set(model.images[index].uri); + } + else + { + mat->mEmissiveId.setNull(); + } + + mat->setAlphaMode(material_in.alphaMode); + mat->mAlphaCutoff = llclamp((F32)material_in.alphaCutoff, 0.f, 1.f); + + mat->mAlbedoColor = getColor(material_in.pbrMetallicRoughness.baseColorFactor); + mat->mEmissiveColor = getColor(material_in.emissiveFactor); + + mat->mMetallicFactor = llclamp((F32)material_in.pbrMetallicRoughness.metallicFactor, 0.f, 1.f); + mat->mRoughnessFactor = llclamp((F32)material_in.pbrMetallicRoughness.roughnessFactor, 0.f, 1.f); + + mat->mDoubleSided = material_in.doubleSided; +} + +LLColor4 LLTinyGLTFHelper::getColor(const std::vector& in) +{ + LLColor4 out; + for (S32 i = 0; i < llmin((S32)in.size(), 4); ++i) + { + out.mV[i] = in[i]; + } + + return out; +} + +const tinygltf::Image * LLTinyGLTFHelper::getImageFromTextureIndex(const tinygltf::Model & model, S32 texture_index) +{ + if (texture_index >= 0) + { + S32 source_idx = model.textures[texture_index].source; + if (source_idx >= 0) + { + return &(model.images[source_idx]); + } + } + + return nullptr; +} + +LLImageRaw * LLTinyGLTFHelper::getTexture(const std::string & folder, const tinygltf::Model & model, S32 texture_index, std::string & name) +{ + const tinygltf::Image* image = getImageFromTextureIndex(model, texture_index); + LLImageRaw* rawImage = nullptr; + + if (image != nullptr && + image->bits == 8 && + !image->image.empty() && + image->component <= 4) + { + name = image->name; + rawImage = new LLImageRaw(&image->image[0], image->width, image->height, image->component); + rawImage->verticalFlip(); + } + + return rawImage; +} + +LLImageRaw * LLTinyGLTFHelper::getTexture(const std::string & folder, const tinygltf::Model & model, S32 texture_index) +{ + const tinygltf::Image* image = getImageFromTextureIndex(model, texture_index); + LLImageRaw* rawImage = nullptr; + + if (image != nullptr && + image->bits == 8 && + !image->image.empty() && + image->component <= 4) + { + rawImage = new LLImageRaw(&image->image[0], image->width, image->height, image->component); + rawImage->verticalFlip(); + } + + return rawImage; +} -- cgit v1.2.3 From 8f1d22686551f4d6783d03cd3685085ed7fcb96c Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Fri, 23 Sep 2022 11:19:56 -0500 Subject: SL-18134 Rename Albedo to Base Color to be more consistent with GLTF spec --- indra/newview/lltinygltfhelper.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index 935f8e7794..3125cacbd5 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -62,19 +62,19 @@ void copy_red_channel(LLPointer& src_img, LLPointer& dst } void LLTinyGLTFHelper::initFetchedTextures(tinygltf::Material& material, - LLPointer& albedo_img, + LLPointer& base_color_img, LLPointer& normal_img, LLPointer& mr_img, LLPointer& emissive_img, LLPointer& occlusion_img, - LLPointer& albedo_tex, + LLPointer& base_color_tex, LLPointer& normal_tex, LLPointer& mr_tex, LLPointer& emissive_tex) { - if (albedo_img) + if (base_color_img) { - albedo_tex = LLViewerTextureManager::getFetchedTexture(albedo_img, FTType::FTT_LOCAL_FILE, true); + base_color_tex = LLViewerTextureManager::getFetchedTexture(base_color_img, FTType::FTT_LOCAL_FILE, true); } if (normal_img) @@ -128,15 +128,15 @@ void LLTinyGLTFHelper::setFromModel(LLGLTFMaterial* mat, tinygltf::Model& model) auto& material_in = model.materials[0]; - // get albedo texture + // get base color texture index = material_in.pbrMetallicRoughness.baseColorTexture.index; if (index >= 0) { - mat->mAlbedoId.set(model.images[index].uri); + mat->mBaseColorId.set(model.images[index].uri); } else { - mat->mAlbedoId.setNull(); + mat->mBaseColorId.setNull(); } // get normal map @@ -175,7 +175,7 @@ void LLTinyGLTFHelper::setFromModel(LLGLTFMaterial* mat, tinygltf::Model& model) mat->setAlphaMode(material_in.alphaMode); mat->mAlphaCutoff = llclamp((F32)material_in.alphaCutoff, 0.f, 1.f); - mat->mAlbedoColor = getColor(material_in.pbrMetallicRoughness.baseColorFactor); + mat->mBaseColor= getColor(material_in.pbrMetallicRoughness.baseColorFactor); mat->mEmissiveColor = getColor(material_in.emissiveFactor); mat->mMetallicFactor = llclamp((F32)material_in.pbrMetallicRoughness.metallicFactor, 0.f, 1.f); -- cgit v1.2.3 From 9346b45188462056698083f4f83fe8fecbe19bdf Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Thu, 29 Sep 2022 22:38:40 +0300 Subject: SL-17653 Multi-material file support for local materials --- indra/newview/lltinygltfhelper.cpp | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index 3125cacbd5..c3dc10c2a0 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -122,17 +122,20 @@ void LLTinyGLTFHelper::initFetchedTextures(tinygltf::Material& material, } } -void LLTinyGLTFHelper::setFromModel(LLGLTFMaterial* mat, tinygltf::Model& model) +void LLTinyGLTFHelper::setFromModel(LLGLTFMaterial* mat, tinygltf::Model& model, S32 mat_index) { - S32 index; + if (model.materials.size() <= mat_index) + { + return; + } - auto& material_in = model.materials[0]; + tinygltf::Material& material_in = model.materials[mat_index]; // get base color texture - index = material_in.pbrMetallicRoughness.baseColorTexture.index; - if (index >= 0) + S32 tex_index = material_in.pbrMetallicRoughness.baseColorTexture.index; + if (tex_index >= 0) { - mat->mBaseColorId.set(model.images[index].uri); + mat->mBaseColorId.set(model.images[tex_index].uri); } else { @@ -140,10 +143,10 @@ void LLTinyGLTFHelper::setFromModel(LLGLTFMaterial* mat, tinygltf::Model& model) } // get normal map - index = material_in.normalTexture.index; - if (index >= 0) + tex_index = material_in.normalTexture.index; + if (tex_index >= 0) { - mat->mNormalId.set(model.images[index].uri); + mat->mNormalId.set(model.images[tex_index].uri); } else { @@ -151,10 +154,10 @@ void LLTinyGLTFHelper::setFromModel(LLGLTFMaterial* mat, tinygltf::Model& model) } // get metallic-roughness texture - index = material_in.pbrMetallicRoughness.metallicRoughnessTexture.index; - if (index >= 0) + tex_index = material_in.pbrMetallicRoughness.metallicRoughnessTexture.index; + if (tex_index >= 0) { - mat->mMetallicRoughnessId.set(model.images[index].uri); + mat->mMetallicRoughnessId.set(model.images[tex_index].uri); } else { @@ -162,10 +165,10 @@ void LLTinyGLTFHelper::setFromModel(LLGLTFMaterial* mat, tinygltf::Model& model) } // get emissive texture - index = material_in.emissiveTexture.index; - if (index >= 0) + tex_index = material_in.emissiveTexture.index; + if (tex_index >= 0) { - mat->mEmissiveId.set(model.images[index].uri); + mat->mEmissiveId.set(model.images[tex_index].uri); } else { -- cgit v1.2.3 From f6762c3de57434730a2cbf6d0241bf1db5c9346c Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Fri, 14 Oct 2022 17:35:48 -0500 Subject: SL-18105 Add to/from json capability to LLGLTFMaterial --- indra/newview/lltinygltfhelper.cpp | 65 -------------------------------------- 1 file changed, 65 deletions(-) (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index c3dc10c2a0..c80e87652a 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -122,71 +122,6 @@ void LLTinyGLTFHelper::initFetchedTextures(tinygltf::Material& material, } } -void LLTinyGLTFHelper::setFromModel(LLGLTFMaterial* mat, tinygltf::Model& model, S32 mat_index) -{ - if (model.materials.size() <= mat_index) - { - return; - } - - tinygltf::Material& material_in = model.materials[mat_index]; - - // get base color texture - S32 tex_index = material_in.pbrMetallicRoughness.baseColorTexture.index; - if (tex_index >= 0) - { - mat->mBaseColorId.set(model.images[tex_index].uri); - } - else - { - mat->mBaseColorId.setNull(); - } - - // get normal map - tex_index = material_in.normalTexture.index; - if (tex_index >= 0) - { - mat->mNormalId.set(model.images[tex_index].uri); - } - else - { - mat->mNormalId.setNull(); - } - - // get metallic-roughness texture - tex_index = material_in.pbrMetallicRoughness.metallicRoughnessTexture.index; - if (tex_index >= 0) - { - mat->mMetallicRoughnessId.set(model.images[tex_index].uri); - } - else - { - mat->mMetallicRoughnessId.setNull(); - } - - // get emissive texture - tex_index = material_in.emissiveTexture.index; - if (tex_index >= 0) - { - mat->mEmissiveId.set(model.images[tex_index].uri); - } - else - { - mat->mEmissiveId.setNull(); - } - - mat->setAlphaMode(material_in.alphaMode); - mat->mAlphaCutoff = llclamp((F32)material_in.alphaCutoff, 0.f, 1.f); - - mat->mBaseColor= getColor(material_in.pbrMetallicRoughness.baseColorFactor); - mat->mEmissiveColor = getColor(material_in.emissiveFactor); - - mat->mMetallicFactor = llclamp((F32)material_in.pbrMetallicRoughness.metallicFactor, 0.f, 1.f); - mat->mRoughnessFactor = llclamp((F32)material_in.pbrMetallicRoughness.roughnessFactor, 0.f, 1.f); - - mat->mDoubleSided = material_in.doubleSided; -} - LLColor4 LLTinyGLTFHelper::getColor(const std::vector& in) { LLColor4 out; -- cgit v1.2.3 From e6fb0a9397e77cf5d93765f03d1773527ceee303 Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Tue, 1 Nov 2022 18:51:00 +0200 Subject: SL-18391 Removed direct dependency of local materials onto tinygltf --- indra/newview/lltinygltfhelper.cpp | 124 +++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) (limited to 'indra/newview/lltinygltfhelper.cpp') 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& base_color_tex, + LLPointer& normal_tex, + LLPointer& mr_tex, + LLPointer& 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 base_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.pbrMetallicRoughness.baseColorTexture.index); + // get normal map + LLPointer normal_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.normalTexture.index); + // get metallic-roughness texture + LLPointer mr_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.pbrMetallicRoughness.metallicRoughnessTexture.index); + // get emissive texture + LLPointer emissive_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.emissiveTexture.index); + // get occlusion map if needed + LLPointer 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; + +} -- cgit v1.2.3 From 11c87378be68d9102d7dcf94d714b3c5c1923952 Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Fri, 4 Nov 2022 18:57:48 +0200 Subject: SL-18560 Make local materials save correctly from right-click menu --- indra/newview/lltinygltfhelper.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index cff26ea51f..58031d4204 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -186,11 +186,7 @@ bool LLTinyGLTFHelper::getMaterialFromFile( const std::string& filename, S32 mat_index, LLPointer < LLFetchedGLTFMaterial> material, - std::string& material_name, - LLPointer& base_color_tex, - LLPointer& normal_tex, - LLPointer& mr_tex, - LLPointer& emissive_tex) + std::string& material_name) { tinygltf::TinyGLTF loader; std::string error_msg; @@ -249,6 +245,11 @@ bool LLTinyGLTFHelper::getMaterialFromFile( occlusion_img = LLTinyGLTFHelper::getTexture(folder, model_in, material_in.occlusionTexture.index); } + LLPointer base_color_tex; + LLPointer normal_tex; + LLPointer mr_tex; + LLPointer emissive_tex; + // todo: pass it into local bitmaps? LLTinyGLTFHelper::initFetchedTextures(material_in, base_img, normal_img, mr_img, emissive_img, occlusion_img, @@ -258,7 +259,7 @@ bool LLTinyGLTFHelper::getMaterialFromFile( { base_color_tex->addTextureStats(64.f * 64.f, TRUE); material->mBaseColorId = base_color_tex->getID(); - material->mBaseColorTexture = base_color_tex; + material->mBaseColorTexture; } else { -- cgit v1.2.3 From 172366121aef566a11464d82968776dcf0a836d4 Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Fri, 4 Nov 2022 19:35:16 +0200 Subject: SL-18560 Mac build fix --- indra/newview/lltinygltfhelper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index 58031d4204..05587af9bc 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -259,7 +259,7 @@ bool LLTinyGLTFHelper::getMaterialFromFile( { base_color_tex->addTextureStats(64.f * 64.f, TRUE); material->mBaseColorId = base_color_tex->getID(); - material->mBaseColorTexture; + material->mBaseColorTexture = base_color_tex; } else { -- cgit v1.2.3 From 321c7895d00b0b88675c06b8f1cef063632fd686 Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Thu, 1 Dec 2022 21:54:49 +0200 Subject: SL-18741 Basic bulk upload for gltf materials #1 --- indra/newview/lltinygltfhelper.cpp | 53 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index 05587af9bc..838524e910 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -182,12 +182,62 @@ LLImageRaw * LLTinyGLTFHelper::getTexture(const std::string & folder, const tiny return rawImage; } +S32 LLTinyGLTFHelper::getMaterialCountFromFile(const std::string& filename) +{ + std::string exten = gDirUtilp->getExtension(filename); + S32 materials_in_file = 0; + + if (exten == "gltf" || exten == "glb") + { + tinygltf::TinyGLTF loader; + std::string error_msg; + std::string warn_msg; + + tinygltf::Model model_in; + + std::string filename_lc = filename; + 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. + bool decode_successful = false; + 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, error: Failed to decode" << error_msg + << ", warning:" << warn_msg + << " file: " << filename + << LL_ENDL; + return 0; + } + + if (model_in.materials.empty()) + { + // materials are missing + LL_WARNS("GLTF") << "Cannot load. File has no materials " << filename << LL_ENDL; + return 0; + } + materials_in_file = model_in.materials.size(); + } + return materials_in_file; +} + bool LLTinyGLTFHelper::getMaterialFromFile( const std::string& filename, S32 mat_index, - LLPointer < LLFetchedGLTFMaterial> material, + LLFetchedGLTFMaterial* material, std::string& material_name) { + llassert(material); + tinygltf::TinyGLTF loader; std::string error_msg; std::string warn_msg; @@ -304,5 +354,4 @@ bool LLTinyGLTFHelper::getMaterialFromFile( } return true; - } -- cgit v1.2.3 From dbc641ce52264d0b5a8e584a726f2df457f26f79 Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Fri, 16 Dec 2022 12:22:23 -0600 Subject: SL-18861 Optimize away alpha channel on GLTF material imports. --- indra/newview/lltinygltfhelper.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index 838524e910..611911014a 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -160,6 +160,7 @@ LLImageRaw * LLTinyGLTFHelper::getTexture(const std::string & folder, const tiny name = image->name; rawImage = new LLImageRaw(&image->image[0], image->width, image->height, image->component); rawImage->verticalFlip(); + rawImage->optimizeAwayAlpha(); } return rawImage; -- cgit v1.2.3 From d6841c07983a46ff805ed23a7318efbf9cca3b24 Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Thu, 9 Feb 2023 15:04:46 -0800 Subject: SL-19080: Update GLTF Material asset upload to v1.1, with stricter GLTF compliance and removal of unsupported features --- indra/newview/lltinygltfhelper.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index 611911014a..1a8e868d11 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -309,48 +309,48 @@ bool LLTinyGLTFHelper::getMaterialFromFile( if (base_color_tex) { base_color_tex->addTextureStats(64.f * 64.f, TRUE); - material->mBaseColorId = base_color_tex->getID(); + material->mTextureId[LLGLTFMaterial::GLTF_TEXTURE_INFO_BASE_COLOR] = base_color_tex->getID(); material->mBaseColorTexture = base_color_tex; } else { - material->mBaseColorId = LLUUID::null; + material->mTextureId[LLGLTFMaterial::GLTF_TEXTURE_INFO_BASE_COLOR] = LLUUID::null; material->mBaseColorTexture = nullptr; } if (normal_tex) { normal_tex->addTextureStats(64.f * 64.f, TRUE); - material->mNormalId = normal_tex->getID(); + material->mTextureId[LLGLTFMaterial::GLTF_TEXTURE_INFO_NORMAL] = normal_tex->getID(); material->mNormalTexture = normal_tex; } else { - material->mNormalId = LLUUID::null; + material->mTextureId[LLGLTFMaterial::GLTF_TEXTURE_INFO_NORMAL] = LLUUID::null; material->mNormalTexture = nullptr; } if (mr_tex) { mr_tex->addTextureStats(64.f * 64.f, TRUE); - material->mMetallicRoughnessId = mr_tex->getID(); + material->mTextureId[LLGLTFMaterial::GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS] = mr_tex->getID(); material->mMetallicRoughnessTexture = mr_tex; } else { - material->mMetallicRoughnessId = LLUUID::null; + material->mTextureId[LLGLTFMaterial::GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS] = LLUUID::null; material->mMetallicRoughnessTexture = nullptr; } if (emissive_tex) { emissive_tex->addTextureStats(64.f * 64.f, TRUE); - material->mEmissiveId = emissive_tex->getID(); + material->mTextureId[LLGLTFMaterial::GLTF_TEXTURE_INFO_EMISSIVE] = emissive_tex->getID(); material->mEmissiveTexture = emissive_tex; } else { - material->mEmissiveId = LLUUID::null; + material->mTextureId[LLGLTFMaterial::GLTF_TEXTURE_INFO_EMISSIVE] = LLUUID::null; material->mEmissiveTexture = nullptr; } -- cgit v1.2.3 From b9b913a60ada97fc61bf2176e443dc72c8359ccb Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Tue, 7 Mar 2023 09:38:43 -0600 Subject: SL-19349 Fix for hang on bulk upload of materials, add bulk upload option to material selector, incidental decruft. --- indra/newview/lltinygltfhelper.cpp | 53 ++++++++++---------------------------- 1 file changed, 13 insertions(+), 40 deletions(-) (limited to 'indra/newview/lltinygltfhelper.cpp') diff --git a/indra/newview/lltinygltfhelper.cpp b/indra/newview/lltinygltfhelper.cpp index 1a8e868d11..999be07dba 100644 --- a/indra/newview/lltinygltfhelper.cpp +++ b/indra/newview/lltinygltfhelper.cpp @@ -183,19 +183,16 @@ LLImageRaw * LLTinyGLTFHelper::getTexture(const std::string & folder, const tiny return rawImage; } -S32 LLTinyGLTFHelper::getMaterialCountFromFile(const std::string& filename) +bool LLTinyGLTFHelper::loadModel(const std::string& filename, tinygltf::Model& model_in) { std::string exten = gDirUtilp->getExtension(filename); - S32 materials_in_file = 0; - + if (exten == "gltf" || exten == "glb") { tinygltf::TinyGLTF loader; std::string error_msg; std::string warn_msg; - tinygltf::Model model_in; - std::string filename_lc = filename; LLStringUtil::toLower(filename_lc); @@ -217,57 +214,33 @@ S32 LLTinyGLTFHelper::getMaterialCountFromFile(const std::string& filename) << ", warning:" << warn_msg << " file: " << filename << LL_ENDL; - return 0; + return false; } if (model_in.materials.empty()) { // materials are missing LL_WARNS("GLTF") << "Cannot load. File has no materials " << filename << LL_ENDL; - return 0; + return false; } - materials_in_file = model_in.materials.size(); + + return true; } - return materials_in_file; + + + return false; } -bool LLTinyGLTFHelper::getMaterialFromFile( +bool LLTinyGLTFHelper::getMaterialFromModel( const std::string& filename, + const tinygltf::Model& model_in, S32 mat_index, LLFetchedGLTFMaterial* material, std::string& material_name) { llassert(material); - 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) + if (model_in.materials.size() <= mat_index) { // materials are missing LL_WARNS("GLTF") << "Cannot load Material, Material " << mat_index << " is missing, " << filename << LL_ENDL; @@ -276,7 +249,7 @@ bool LLTinyGLTFHelper::getMaterialFromFile( material->setFromModel(model_in, mat_index); - std::string folder = gDirUtilp->getDirName(filename_lc); + std::string folder = gDirUtilp->getDirName(filename); tinygltf::Material material_in = model_in.materials[mat_index]; material_name = material_in.name; -- cgit v1.2.3