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/llprimitive/llgltfmaterial.cpp | 199 +++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 indra/llprimitive/llgltfmaterial.cpp (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp new file mode 100644 index 0000000000..369a1786a3 --- /dev/null +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -0,0 +1,199 @@ +/** + * @file llgltfmaterial.cpp + * @brief Material definition + * + * $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 "linden_common.h" + +#include "llgltfmaterial.h" + +#include "tiny_gltf.h" + +bool LLGLTFMaterial::fromJSON(const std::string& json, std::string& warn_msg, std::string& error_msg) +{ + tinygltf::TinyGLTF gltf; + + tinygltf::Model model_in; + + if (gltf.LoadASCIIFromString(&model_in, &error_msg, &warn_msg, json.c_str(), json.length(), "")) + { + setFromModel(model_in, 0); + + //DEBUG generate json and print + LL_INFOS() << asJSON(true) << LL_ENDL; + + return true; + } + + return false; +} + +std::string LLGLTFMaterial::asJSON(bool prettyprint) const +{ + tinygltf::TinyGLTF gltf; + tinygltf::Model model_out; + + std::ostringstream str; + + writeToModel(model_out, 0); + + gltf.WriteGltfSceneToStream(&model_out, str, prettyprint, false); + + return str.str(); +} + +void LLGLTFMaterial::setFromModel(const tinygltf::Model& model, S32 mat_index) +{ + if (model.materials.size() <= mat_index) + { + return; + } + + const tinygltf::Material& material_in = model.materials[mat_index]; + + // get base color texture + S32 tex_index = material_in.pbrMetallicRoughness.baseColorTexture.index; + if (tex_index >= 0) + { + mBaseColorId.set(model.images[tex_index].uri); + } + else + { + mBaseColorId.setNull(); + } + + // get normal map + tex_index = material_in.normalTexture.index; + if (tex_index >= 0) + { + mNormalId.set(model.images[tex_index].uri); + } + else + { + mNormalId.setNull(); + } + + // get metallic-roughness texture + tex_index = material_in.pbrMetallicRoughness.metallicRoughnessTexture.index; + if (tex_index >= 0) + { + mMetallicRoughnessId.set(model.images[tex_index].uri); + } + else + { + mMetallicRoughnessId.setNull(); + } + + // get emissive texture + tex_index = material_in.emissiveTexture.index; + if (tex_index >= 0) + { + mEmissiveId.set(model.images[tex_index].uri); + } + else + { + mEmissiveId.setNull(); + } + + setAlphaMode(material_in.alphaMode); + mAlphaCutoff = llclamp((F32)material_in.alphaCutoff, 0.f, 1.f); + + mBaseColor.set(material_in.pbrMetallicRoughness.baseColorFactor); + mEmissiveColor.set(material_in.emissiveFactor); + + mMetallicFactor = llclamp((F32)material_in.pbrMetallicRoughness.metallicFactor, 0.f, 1.f); + mRoughnessFactor = llclamp((F32)material_in.pbrMetallicRoughness.roughnessFactor, 0.f, 1.f); + + mDoubleSided = material_in.doubleSided; +} + +void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const +{ + if (model.materials.size() < mat_index+1) + { + model.materials.resize(mat_index + 1); + } + + tinygltf::Material& material_out = model.materials[mat_index]; + + // set base color texture + if (mBaseColorId.notNull()) + { + U32 idx = model.images.size(); + model.images.resize(idx + 1); + model.textures.resize(idx + 1); + + material_out.pbrMetallicRoughness.baseColorTexture.index = idx; + model.textures[idx].source = idx; + model.images[idx].uri = mBaseColorId.asString(); + } + + // set normal texture + if (mNormalId.notNull()) + { + U32 idx = model.images.size(); + model.images.resize(idx + 1); + model.textures.resize(idx + 1); + + material_out.normalTexture.index = idx; + model.textures[idx].source = idx; + model.images[idx].uri = mNormalId.asString(); + } + + // set metallic-roughness texture + if (mMetallicRoughnessId.notNull()) + { + U32 idx = model.images.size(); + model.images.resize(idx + 1); + model.textures.resize(idx + 1); + + material_out.pbrMetallicRoughness.metallicRoughnessTexture.index = idx; + model.textures[idx].source = idx; + model.images[idx].uri = mMetallicRoughnessId.asString(); + } + + // set emissive texture + if (mEmissiveId.notNull()) + { + U32 idx = model.images.size(); + model.images.resize(idx + 1); + model.textures.resize(idx + 1); + + material_out.emissiveTexture.index = idx; + model.textures[idx].source = idx; + model.images[idx].uri = mEmissiveId.asString(); + } + + material_out.alphaMode = getAlphaMode(); + material_out.alphaCutoff = mAlphaCutoff; + + mBaseColor.write(material_out.pbrMetallicRoughness.baseColorFactor); + mEmissiveColor.write(material_out.emissiveFactor); + + material_out.pbrMetallicRoughness.metallicFactor = mMetallicFactor; + material_out.pbrMetallicRoughness.roughnessFactor = mRoughnessFactor; + + material_out.doubleSided = mDoubleSided; +} + -- cgit v1.2.3 From 44687a78625d6ed21a82356b07e9927f8d45f629 Mon Sep 17 00:00:00 2001 From: Brad Kittenbrink Date: Mon, 17 Oct 2022 15:47:15 -0700 Subject: WIP for SL-17697 live editing now computes diffs of changed material properties in tinygltf schema json --- indra/llprimitive/llgltfmaterial.cpp | 94 ++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 369a1786a3..951b768678 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -197,3 +197,97 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const material_out.doubleSided = mDoubleSided; } +void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model & model, S32 mat_index, LLGLTFMaterial const * base_material) const +{ + if (model.materials.size() < mat_index+1) + { + model.materials.resize(mat_index + 1); + } + + tinygltf::Material& material_out = model.materials[mat_index]; + + // TODO - fix handling of resetting to null/default values + + // set base color texture + if (mBaseColorId.notNull() && mBaseColorId != base_material->mBaseColorId) + { + U32 idx = model.images.size(); + model.images.resize(idx + 1); + model.textures.resize(idx + 1); + + material_out.pbrMetallicRoughness.baseColorTexture.index = idx; + model.textures[idx].source = idx; + model.images[idx].uri = mBaseColorId.asString(); + } + + // set normal texture + if (mNormalId.notNull() && mNormalId != base_material->mNormalId) + { + U32 idx = model.images.size(); + model.images.resize(idx + 1); + model.textures.resize(idx + 1); + + material_out.normalTexture.index = idx; + model.textures[idx].source = idx; + model.images[idx].uri = mNormalId.asString(); + } + + // set metallic-roughness texture + if (mMetallicRoughnessId.notNull() && mMetallicRoughnessId != base_material->mMetallicRoughnessId) + { + U32 idx = model.images.size(); + model.images.resize(idx + 1); + model.textures.resize(idx + 1); + + material_out.pbrMetallicRoughness.metallicRoughnessTexture.index = idx; + model.textures[idx].source = idx; + model.images[idx].uri = mMetallicRoughnessId.asString(); + } + + // set emissive texture + if (mEmissiveId.notNull() && mEmissiveId != base_material->mEmissiveId) + { + U32 idx = model.images.size(); + model.images.resize(idx + 1); + model.textures.resize(idx + 1); + + material_out.emissiveTexture.index = idx; + model.textures[idx].source = idx; + model.images[idx].uri = mEmissiveId.asString(); + } + + if(mAlphaMode != base_material->mAlphaMode) + { + material_out.alphaMode = getAlphaMode(); + } + + if(mAlphaCutoff != base_material->mAlphaCutoff) + { + material_out.alphaCutoff = mAlphaCutoff; + } + + if(mBaseColor != base_material->mBaseColor) + { + mBaseColor.write(material_out.pbrMetallicRoughness.baseColorFactor); + } + + if(mEmissiveColor != base_material->mEmissiveColor) + { + mEmissiveColor.write(material_out.emissiveFactor); + } + + if(mMetallicFactor != base_material->mMetallicFactor) + { + material_out.pbrMetallicRoughness.metallicFactor = mMetallicFactor; + } + + if(mRoughnessFactor != base_material->mRoughnessFactor) + { + material_out.pbrMetallicRoughness.roughnessFactor = mRoughnessFactor; + } + + if(mDoubleSided != base_material->mDoubleSided) + { + material_out.doubleSided = mDoubleSided; + } +} -- cgit v1.2.3 From de4c018499ddaebbe466fb5a8938554a2d4a3b19 Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Wed, 19 Oct 2022 14:41:17 -0500 Subject: SL-18105 Hook up render pipe directly to LLTextureEntry::mGLTFMaterial and add LLViewerFetchedTextures to LLFetchedGLTFMaterial. Lower reflection probe resolution to 128x128 per side. --- indra/llprimitive/llgltfmaterial.cpp | 187 ++++++++++++++++++++++++++++++++--- 1 file changed, 171 insertions(+), 16 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 951b768678..19081cd76f 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -28,12 +28,44 @@ #include "llgltfmaterial.h" -#include "tiny_gltf.h" +#include "tinygltf/tiny_gltf.h" + +LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) +{ + *this = rhs; +} + +LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) +{ + //have to do a manual operator= because of LLRefCount + mBaseColorId = rhs.mBaseColorId; + mNormalId = rhs.mNormalId; + mMetallicRoughnessId = rhs.mMetallicRoughnessId; + mEmissiveId = rhs.mEmissiveId; + + mBaseColor = rhs.mBaseColor; + mEmissiveColor = rhs.mEmissiveColor; + + mMetallicFactor = rhs.mMetallicFactor; + mRoughnessFactor = rhs.mRoughnessFactor; + mAlphaCutoff = rhs.mAlphaCutoff; + + mDoubleSided = rhs.mDoubleSided; + mAlphaMode = rhs.mAlphaMode; + + for (S32 i = 0; i < 3; ++i) + { + mTextureTransform[i] = rhs.mTextureTransform[i]; + } + + return *this; +} bool LLGLTFMaterial::fromJSON(const std::string& json, std::string& warn_msg, std::string& error_msg) { +#if 1 tinygltf::TinyGLTF gltf; - + tinygltf::Model model_in; if (gltf.LoadASCIIFromString(&model_in, &error_msg, &warn_msg, json.c_str(), json.length(), "")) @@ -45,12 +77,13 @@ bool LLGLTFMaterial::fromJSON(const std::string& json, std::string& warn_msg, st return true; } - +#endif return false; } std::string LLGLTFMaterial::asJSON(bool prettyprint) const { +#if 1 tinygltf::TinyGLTF gltf; tinygltf::Model model_out; @@ -61,6 +94,9 @@ std::string LLGLTFMaterial::asJSON(bool prettyprint) const gltf.WriteGltfSceneToStream(&model_out, str, prettyprint, false); return str.str(); +#else + return ""; +#endif } void LLGLTFMaterial::setFromModel(const tinygltf::Model& model, S32 mat_index) @@ -130,7 +166,7 @@ void LLGLTFMaterial::setFromModel(const tinygltf::Model& model, S32 mat_index) void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const { - if (model.materials.size() < mat_index+1) + if (model.materials.size() < mat_index + 1) { model.materials.resize(mat_index + 1); } @@ -143,7 +179,7 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const U32 idx = model.images.size(); model.images.resize(idx + 1); model.textures.resize(idx + 1); - + material_out.pbrMetallicRoughness.baseColorTexture.index = idx; model.textures[idx].source = idx; model.images[idx].uri = mBaseColorId.asString(); @@ -160,7 +196,7 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const model.textures[idx].source = idx; model.images[idx].uri = mNormalId.asString(); } - + // set metallic-roughness texture if (mMetallicRoughnessId.notNull()) { @@ -187,7 +223,7 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const material_out.alphaMode = getAlphaMode(); material_out.alphaCutoff = mAlphaCutoff; - + mBaseColor.write(material_out.pbrMetallicRoughness.baseColorFactor); mEmissiveColor.write(material_out.emissiveFactor); @@ -195,11 +231,130 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const material_out.pbrMetallicRoughness.roughnessFactor = mRoughnessFactor; material_out.doubleSided = mDoubleSided; + + model.asset.version = "2.0"; +} + + +void LLGLTFMaterial::setBaseColorId(const LLUUID& id) +{ + mBaseColorId = id; +} + +void LLGLTFMaterial::setNormalId(const LLUUID& id) +{ + mNormalId = id; +} + +void LLGLTFMaterial::setMetallicRoughnessId(const LLUUID& id) +{ + mMetallicRoughnessId = id; +} + +void LLGLTFMaterial::setEmissiveId(const LLUUID& id) +{ + mEmissiveId = id; +} + +void LLGLTFMaterial::setBaseColorFactor(const LLColor3& baseColor, F32 transparency) +{ + mBaseColor.set(baseColor, transparency); + mBaseColor.clamp(); +} + +void LLGLTFMaterial::setAlphaCutoff(F32 cutoff) +{ + mAlphaCutoff = llclamp(cutoff, 0.f, 1.f); +} + +void LLGLTFMaterial::setEmissiveColorFactor(const LLColor3& emissiveColor) +{ + mEmissiveColor = emissiveColor; + mEmissiveColor.clamp(); +} + +void LLGLTFMaterial::setMetallicFactor(F32 metallic) +{ + mMetallicFactor = llclamp(metallic, 0.f, 1.f); +} + +void LLGLTFMaterial::setRoughnessFactor(F32 roughness) +{ + mRoughnessFactor = llclamp(roughness, 0.f, 1.f); +} + +void LLGLTFMaterial::setAlphaMode(S32 mode) +{ + mAlphaMode = (AlphaMode)llclamp(mode, (S32)ALPHA_MODE_OPAQUE, (S32)ALPHA_MODE_MASK); +} + +void LLGLTFMaterial::setDoubleSided(bool double_sided) +{ + // sure, no clamping will ever be needed for a bool, but include the + // setter for consistency with the clamping API + mDoubleSided = double_sided; +} + +// Default value accessors + +LLUUID LLGLTFMaterial::getDefaultBaseColorId() +{ + return LLUUID::null; +} + +LLUUID LLGLTFMaterial::getDefaultNormalId() +{ + return LLUUID::null; +} + +LLUUID LLGLTFMaterial::getDefaultEmissiveId() +{ + return LLUUID::null; +} + +LLUUID LLGLTFMaterial::getDefaultMetallicRoughnessId() +{ + return LLUUID::null; +} + +F32 LLGLTFMaterial::getDefaultAlphaCutoff() +{ + return 0.f; +} + +S32 LLGLTFMaterial::getDefaultAlphaMode() +{ + return (S32)ALPHA_MODE_OPAQUE; +} + +F32 LLGLTFMaterial::getDefaultMetallicFactor() +{ + return 0.f; +} + +F32 LLGLTFMaterial::getDefaultRoughnessFactor() +{ + return 0.f; +} + +LLColor4 LLGLTFMaterial::getDefaultBaseColor() +{ + return LLColor4::white; +} + +LLColor3 LLGLTFMaterial::getDefaultEmissiveColor() +{ + return LLColor3::black; +} + +bool LLGLTFMaterial::getDefaultDoubleSided() +{ + return false; } -void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model & model, S32 mat_index, LLGLTFMaterial const * base_material) const +void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model& model, S32 mat_index, LLGLTFMaterial const* base_material) const { - if (model.materials.size() < mat_index+1) + if (model.materials.size() < mat_index + 1) { model.materials.resize(mat_index + 1); } @@ -256,37 +411,37 @@ void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model & model, S32 mat_inde model.images[idx].uri = mEmissiveId.asString(); } - if(mAlphaMode != base_material->mAlphaMode) + if (mAlphaMode != base_material->mAlphaMode) { material_out.alphaMode = getAlphaMode(); } - if(mAlphaCutoff != base_material->mAlphaCutoff) + if (mAlphaCutoff != base_material->mAlphaCutoff) { material_out.alphaCutoff = mAlphaCutoff; } - if(mBaseColor != base_material->mBaseColor) + if (mBaseColor != base_material->mBaseColor) { mBaseColor.write(material_out.pbrMetallicRoughness.baseColorFactor); } - if(mEmissiveColor != base_material->mEmissiveColor) + if (mEmissiveColor != base_material->mEmissiveColor) { mEmissiveColor.write(material_out.emissiveFactor); } - if(mMetallicFactor != base_material->mMetallicFactor) + if (mMetallicFactor != base_material->mMetallicFactor) { material_out.pbrMetallicRoughness.metallicFactor = mMetallicFactor; } - if(mRoughnessFactor != base_material->mRoughnessFactor) + if (mRoughnessFactor != base_material->mRoughnessFactor) { material_out.pbrMetallicRoughness.roughnessFactor = mRoughnessFactor; } - if(mDoubleSided != base_material->mDoubleSided) + if (mDoubleSided != base_material->mDoubleSided) { material_out.doubleSided = mDoubleSided; } -- cgit v1.2.3 From 8741c05cc10d3f39f272bb4739e7313309539d07 Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Wed, 19 Oct 2022 17:23:54 -0500 Subject: SL-18105 Hook up TE override material to render pipe by way of render material. --- indra/llprimitive/llgltfmaterial.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 19081cd76f..b9ef2de61a 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -446,3 +446,28 @@ void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model& model, S32 mat_index material_out.doubleSided = mDoubleSided; } } + +void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) +{ + if (override_mat.mBaseColorId != getDefaultBaseColorId()) + { + mBaseColorId = override_mat.mBaseColorId; + } + + if (override_mat.mNormalId != getDefaultNormalId()) + { + mNormalId = override_mat.mNormalId; + } + + if (override_mat.mMetallicRoughnessId != getDefaultMetallicRoughnessId()) + { + mMetallicRoughnessId = override_mat.mMetallicRoughnessId; + } + + if (override_mat.mEmissiveId != getDefaultEmissiveId()) + { + mEmissiveId = override_mat.mEmissiveId; + } + + //TODO -- implement non texture parameters +} -- cgit v1.2.3 From 7231e0b3dd5bff9c87349d52b6927cec354527cd Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Thu, 20 Oct 2022 10:20:18 -0700 Subject: SL-18411: Copy over LLGLTFMaterial changes (most notably various getters/setters and texture transform stub) --- indra/llprimitive/llgltfmaterial.cpp | 43 +++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index b9ef2de61a..8f08a8b2cf 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -37,7 +37,7 @@ LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) { - //have to do a manual operator= because of LLRefCount + //have to do a manual operator= because of LLRefCount mBaseColorId = rhs.mBaseColorId; mNormalId = rhs.mNormalId; mMetallicRoughnessId = rhs.mMetallicRoughnessId; @@ -53,10 +53,7 @@ LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) mDoubleSided = rhs.mDoubleSided; mAlphaMode = rhs.mAlphaMode; - for (S32 i = 0; i < 3; ++i) - { - mTextureTransform[i] = rhs.mTextureTransform[i]; - } + mTextureTransform = rhs.mTextureTransform; return *this; } @@ -285,16 +282,31 @@ void LLGLTFMaterial::setRoughnessFactor(F32 roughness) void LLGLTFMaterial::setAlphaMode(S32 mode) { - mAlphaMode = (AlphaMode)llclamp(mode, (S32)ALPHA_MODE_OPAQUE, (S32)ALPHA_MODE_MASK); + mAlphaMode = (AlphaMode) llclamp(mode, (S32) ALPHA_MODE_OPAQUE, (S32) ALPHA_MODE_MASK); } void LLGLTFMaterial::setDoubleSided(bool double_sided) { - // sure, no clamping will ever be needed for a bool, but include the + // sure, no clamping will ever be needed for a bool, but include the // setter for consistency with the clamping API mDoubleSided = double_sided; } +void LLGLTFMaterial::setTextureOffset(TextureInfo texture_info, const LLVector2& offset) +{ + mTextureTransform[texture_info].mOffset = offset; +} + +void LLGLTFMaterial::setTextureScale(TextureInfo texture_info, const LLVector2& scale) +{ + mTextureTransform[texture_info].mScale = scale; +} + +void LLGLTFMaterial::setTextureRotation(TextureInfo texture_info, float rotation) +{ + mTextureTransform[texture_info].mRotation = rotation; +} + // Default value accessors LLUUID LLGLTFMaterial::getDefaultBaseColorId() @@ -324,7 +336,7 @@ F32 LLGLTFMaterial::getDefaultAlphaCutoff() S32 LLGLTFMaterial::getDefaultAlphaMode() { - return (S32)ALPHA_MODE_OPAQUE; + return (S32) ALPHA_MODE_OPAQUE; } F32 LLGLTFMaterial::getDefaultMetallicFactor() @@ -352,6 +364,21 @@ bool LLGLTFMaterial::getDefaultDoubleSided() return false; } +LLVector2 LLGLTFMaterial::getDefaultTextureOffset() +{ + return LLVector2(0.f, 0.f); +} + +LLVector2 LLGLTFMaterial::getDefaultTextureScale() +{ + return LLVector2(1.f, 1.f); +} + +F32 LLGLTFMaterial::getDefaultTextureRotation() +{ + return 0.f; +} + void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model& model, S32 mat_index, LLGLTFMaterial const* base_material) const { if (model.materials.size() < mat_index + 1) -- cgit v1.2.3 From fd751f4e99557f4d1ef9cbdc4ab7b7a765b563d1 Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Thu, 20 Oct 2022 17:45:03 -0500 Subject: SL-18105 Add remaining parameters to applyOverride --- indra/llprimitive/llgltfmaterial.cpp | 59 +++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 8f08a8b2cf..7b6612b90a 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -476,6 +476,8 @@ void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model& model, S32 mat_index void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) { + // TODO: potentially reimplement this with a more general purpose JSON merge + if (override_mat.mBaseColorId != getDefaultBaseColorId()) { mBaseColorId = override_mat.mBaseColorId; @@ -496,5 +498,60 @@ void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) mEmissiveId = override_mat.mEmissiveId; } - //TODO -- implement non texture parameters + if (override_mat.mBaseColor != getDefaultBaseColor()) + { + mBaseColor = override_mat.mBaseColor; + } + + if (override_mat.mEmissiveColor != getDefaultEmissiveColor()) + { + mEmissiveColor = override_mat.mEmissiveColor; + } + + if (override_mat.mMetallicFactor != getDefaultMetallicFactor()) + { + mMetallicFactor = override_mat.mMetallicFactor; + } + + if (override_mat.mRoughnessFactor != getDefaultRoughnessFactor()) + { + mRoughnessFactor = override_mat.mRoughnessFactor; + } + + if (override_mat.mAlphaMode != getDefaultAlphaMode()) + { + mAlphaMode = override_mat.mAlphaMode; + } + if (override_mat.mAlphaCutoff != getDefaultAlphaCutoff()) + { + mAlphaCutoff = override_mat.mAlphaCutoff; + } + + if (override_mat.mDoubleSided != getDefaultDoubleSided()) + { + mDoubleSided = override_mat.mDoubleSided; + } + + for (int i = 0; i < GLTF_TEXTURE_INFO_COUNT; ++i) + { + if (override_mat.mTextureTransform[i].mOffset != getDefaultTextureOffset()) + { + mTextureTransform[i].mOffset = override_mat.mTextureTransform[i].mOffset; + } + + if (override_mat.mTextureTransform[i].mScale != getDefaultTextureScale()) + { + mTextureTransform[i].mScale = override_mat.mTextureTransform[i].mScale; + } + + if (override_mat.mTextureTransform[i].mScale != getDefaultTextureScale()) + { + mTextureTransform[i].mScale = override_mat.mTextureTransform[i].mScale; + } + + if (override_mat.mTextureTransform[i].mRotation != getDefaultTextureRotation()) + { + mTextureTransform[i].mRotation = override_mat.mTextureTransform[i].mRotation; + } + } } -- cgit v1.2.3 From 5364da4d8eae518b0a53fdbbf675c6bff1797fed Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Thu, 27 Oct 2022 12:22:00 -0700 Subject: SL-18411: GLTF material transform serialization, plus fix improper indexing not matching GLTF spec --- indra/llprimitive/llgltfmaterial.cpp | 266 +++++++++++++++++++---------------- 1 file changed, 144 insertions(+), 122 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 7b6612b90a..6164234d6c 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -30,6 +30,11 @@ #include "tinygltf/tiny_gltf.h" +const char* GLTF_FILE_EXTENSION_TRANSFORM = "KHR_texture_transform"; +const char* GLTF_FILE_EXTENSION_TRANSFORM_SCALE = "scale"; +const char* GLTF_FILE_EXTENSION_TRANSFORM_OFFSET = "offset"; +const char* GLTF_FILE_EXTENSION_TRANSFORM_ROTATION = "rotation"; + LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) { *this = rhs; @@ -82,6 +87,7 @@ std::string LLGLTFMaterial::asJSON(bool prettyprint) const { #if 1 tinygltf::TinyGLTF gltf; + tinygltf::Model model_out; std::ostringstream str; @@ -105,118 +111,131 @@ void LLGLTFMaterial::setFromModel(const tinygltf::Model& model, S32 mat_index) const tinygltf::Material& material_in = model.materials[mat_index]; - // get base color texture - S32 tex_index = material_in.pbrMetallicRoughness.baseColorTexture.index; - if (tex_index >= 0) - { - mBaseColorId.set(model.images[tex_index].uri); - } - else + // Apply base color texture + setFromTexture(model, material_in.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR, mBaseColorId); + // Apply normal map + setFromTexture(model, material_in.normalTexture, GLTF_TEXTURE_INFO_NORMAL, mNormalId); + // Apply metallic-roughness texture + setFromTexture(model, material_in.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS, mMetallicRoughnessId); + // Apply emissive texture + setFromTexture(model, material_in.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE, mEmissiveId); + + setAlphaMode(material_in.alphaMode); + mAlphaCutoff = llclamp((F32)material_in.alphaCutoff, 0.f, 1.f); + + mBaseColor.set(material_in.pbrMetallicRoughness.baseColorFactor); + mEmissiveColor.set(material_in.emissiveFactor); + + mMetallicFactor = llclamp((F32)material_in.pbrMetallicRoughness.metallicFactor, 0.f, 1.f); + mRoughnessFactor = llclamp((F32)material_in.pbrMetallicRoughness.roughnessFactor, 0.f, 1.f); + + mDoubleSided = material_in.doubleSided; +} + +LLVector2 vec2_from_json(const tinygltf::Value::Object& object, const char* key, const LLVector2& default_value) +{ + const auto it = object.find(key); + if (it == object.end()) { - mBaseColorId.setNull(); + return default_value; } - - // get normal map - tex_index = material_in.normalTexture.index; - if (tex_index >= 0) + const tinygltf::Value& vec2_json = std::get<1>(*it); + if (!vec2_json.IsArray() || vec2_json.ArrayLen() < LENGTHOFVECTOR2) { - mNormalId.set(model.images[tex_index].uri); + return default_value; } - else + LLVector2 value; + for (U32 i = 0; i < LENGTHOFVECTOR2; ++i) { - mNormalId.setNull(); + const tinygltf::Value& real_json = vec2_json.Get(i); + if (!real_json.IsReal()) + { + return default_value; + } + value.mV[i] = (F32)real_json.Get(); } + return value; +} - // get metallic-roughness texture - tex_index = material_in.pbrMetallicRoughness.metallicRoughnessTexture.index; - if (tex_index >= 0) +F32 float_from_json(const tinygltf::Value::Object& object, const char* key, const F32 default_value) +{ + const auto it = object.find(key); + if (it == object.end()) { - mMetallicRoughnessId.set(model.images[tex_index].uri); + return default_value; } - else + const tinygltf::Value& real_json = std::get<1>(*it); + if (!real_json.IsReal()) { - mMetallicRoughnessId.setNull(); + return default_value; } + return (F32)real_json.GetNumberAsDouble(); +} - // get emissive texture - tex_index = material_in.emissiveTexture.index; - if (tex_index >= 0) +template +std::string gltf_get_texture_image(const tinygltf::Model& model, const T& texture_info) +{ + const S32 texture_idx = texture_info.index; + if (texture_idx < 0 || texture_idx >= model.textures.size()) { - mEmissiveId.set(model.images[tex_index].uri); + return ""; } - else + const tinygltf::Texture& texture = model.textures[texture_idx]; + + // Ignore texture.sampler for now + + const S32 image_idx = texture.source; + if (image_idx < 0 || image_idx >= model.images.size()) { - mEmissiveId.setNull(); + return ""; } + const tinygltf::Image& image = model.images[image_idx]; - setAlphaMode(material_in.alphaMode); - mAlphaCutoff = llclamp((F32)material_in.alphaCutoff, 0.f, 1.f); - - mBaseColor.set(material_in.pbrMetallicRoughness.baseColorFactor); - mEmissiveColor.set(material_in.emissiveFactor); + return image.uri; +} - mMetallicFactor = llclamp((F32)material_in.pbrMetallicRoughness.metallicFactor, 0.f, 1.f); - mRoughnessFactor = llclamp((F32)material_in.pbrMetallicRoughness.roughnessFactor, 0.f, 1.f); +// *NOTE: Use template here as workaround for the different similar texture info classes +template +void LLGLTFMaterial::setFromTexture(const tinygltf::Model& model, const T& texture_info, TextureInfo texture_info_id, LLUUID& texture_id_out) +{ + const std::string uri = gltf_get_texture_image(model, texture_info); + texture_id_out.set(uri); - mDoubleSided = material_in.doubleSided; + const tinygltf::Value::Object& extensions_object = texture_info.extensions; + const auto transform_it = extensions_object.find(GLTF_FILE_EXTENSION_TRANSFORM); + if (transform_it != extensions_object.end()) + { + const tinygltf::Value& transform_json = std::get<1>(*transform_it); + if (transform_json.IsObject()) + { + const tinygltf::Value::Object& transform_object = transform_json.Get(); + TextureTransform& transform = mTextureTransform[texture_info_id]; + transform.mOffset = vec2_from_json(transform_object, GLTF_FILE_EXTENSION_TRANSFORM_OFFSET, getDefaultTextureOffset()); + transform.mScale = vec2_from_json(transform_object, GLTF_FILE_EXTENSION_TRANSFORM_SCALE, getDefaultTextureScale()); + transform.mRotation = float_from_json(transform_object, GLTF_FILE_EXTENSION_TRANSFORM_ROTATION, getDefaultTextureRotation()); + } + } } void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const { - if (model.materials.size() < mat_index + 1) + if (model.materials.size() < mat_index+1) { model.materials.resize(mat_index + 1); } tinygltf::Material& material_out = model.materials[mat_index]; - // set base color texture - if (mBaseColorId.notNull()) - { - U32 idx = model.images.size(); - model.images.resize(idx + 1); - model.textures.resize(idx + 1); - - material_out.pbrMetallicRoughness.baseColorTexture.index = idx; - model.textures[idx].source = idx; - model.images[idx].uri = mBaseColorId.asString(); - } + constexpr bool is_override = false; + // set base color texture + writeToTexture(model, material_out.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR, mBaseColorId, is_override, LLUUID()); // set normal texture - if (mNormalId.notNull()) - { - U32 idx = model.images.size(); - model.images.resize(idx + 1); - model.textures.resize(idx + 1); - - material_out.normalTexture.index = idx; - model.textures[idx].source = idx; - model.images[idx].uri = mNormalId.asString(); - } - + writeToTexture(model, material_out.normalTexture, GLTF_TEXTURE_INFO_NORMAL, mNormalId, is_override, LLUUID()); // set metallic-roughness texture - if (mMetallicRoughnessId.notNull()) - { - U32 idx = model.images.size(); - model.images.resize(idx + 1); - model.textures.resize(idx + 1); - - material_out.pbrMetallicRoughness.metallicRoughnessTexture.index = idx; - model.textures[idx].source = idx; - model.images[idx].uri = mMetallicRoughnessId.asString(); - } - + writeToTexture(model, material_out.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS, mMetallicRoughnessId, is_override, LLUUID()); // set emissive texture - if (mEmissiveId.notNull()) - { - U32 idx = model.images.size(); - model.images.resize(idx + 1); - model.textures.resize(idx + 1); - - material_out.emissiveTexture.index = idx; - model.textures[idx].source = idx; - model.images[idx].uri = mEmissiveId.asString(); - } + writeToTexture(model, material_out.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE, mEmissiveId, is_override, LLUUID()); material_out.alphaMode = getAlphaMode(); material_out.alphaCutoff = mAlphaCutoff; @@ -232,6 +251,46 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const model.asset.version = "2.0"; } +template +void gltf_allocate_texture_image(tinygltf::Model& model, T& texture_info, const std::string& uri) +{ + const S32 image_idx = model.images.size(); + model.images.emplace_back(); + model.images[image_idx].uri = uri; + + // The texture, not to be confused with the texture info + const S32 texture_idx = model.textures.size(); + model.textures.emplace_back(); + tinygltf::Texture& texture = model.textures[texture_idx]; + texture.source = image_idx; + + texture_info.index = texture_idx; +} + +template +void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, TextureInfo texture_info_id, const LLUUID& texture_id, bool is_override, const LLUUID& base_texture_id) const +{ + if (texture_id.isNull() || (is_override && texture_id == base_texture_id)) + { + return; + } + + gltf_allocate_texture_image(model, texture_info, texture_id.asString()); + + tinygltf::Value::Object transform_map; + const TextureTransform& transform = mTextureTransform[texture_info_id]; + transform_map[GLTF_FILE_EXTENSION_TRANSFORM_OFFSET] = tinygltf::Value(tinygltf::Value::Array({ + tinygltf::Value(transform.mOffset.mV[VX]), + tinygltf::Value(transform.mOffset.mV[VY]) + })); + transform_map[GLTF_FILE_EXTENSION_TRANSFORM_SCALE] = tinygltf::Value(tinygltf::Value::Array({ + tinygltf::Value(transform.mScale.mV[VX]), + tinygltf::Value(transform.mScale.mV[VY]) + })); + transform_map[GLTF_FILE_EXTENSION_TRANSFORM_ROTATION] = tinygltf::Value(transform.mRotation); + texture_info.extensions[GLTF_FILE_EXTENSION_TRANSFORM] = tinygltf::Value(transform_map); +} + void LLGLTFMaterial::setBaseColorId(const LLUUID& id) { @@ -390,53 +449,16 @@ void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model& model, S32 mat_index // TODO - fix handling of resetting to null/default values - // set base color texture - if (mBaseColorId.notNull() && mBaseColorId != base_material->mBaseColorId) - { - U32 idx = model.images.size(); - model.images.resize(idx + 1); - model.textures.resize(idx + 1); - - material_out.pbrMetallicRoughness.baseColorTexture.index = idx; - model.textures[idx].source = idx; - model.images[idx].uri = mBaseColorId.asString(); - } + constexpr bool is_override = true; + // set base color texture + writeToTexture(model, material_out.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR, mBaseColorId, is_override, base_material->mBaseColorId); // set normal texture - if (mNormalId.notNull() && mNormalId != base_material->mNormalId) - { - U32 idx = model.images.size(); - model.images.resize(idx + 1); - model.textures.resize(idx + 1); - - material_out.normalTexture.index = idx; - model.textures[idx].source = idx; - model.images[idx].uri = mNormalId.asString(); - } - + writeToTexture(model, material_out.normalTexture, GLTF_TEXTURE_INFO_NORMAL, mNormalId, is_override, base_material->mNormalId); // set metallic-roughness texture - if (mMetallicRoughnessId.notNull() && mMetallicRoughnessId != base_material->mMetallicRoughnessId) - { - U32 idx = model.images.size(); - model.images.resize(idx + 1); - model.textures.resize(idx + 1); - - material_out.pbrMetallicRoughness.metallicRoughnessTexture.index = idx; - model.textures[idx].source = idx; - model.images[idx].uri = mMetallicRoughnessId.asString(); - } - + writeToTexture(model, material_out.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS, mMetallicRoughnessId, is_override, base_material->mMetallicRoughnessId); // set emissive texture - if (mEmissiveId.notNull() && mEmissiveId != base_material->mEmissiveId) - { - U32 idx = model.images.size(); - model.images.resize(idx + 1); - model.textures.resize(idx + 1); - - material_out.emissiveTexture.index = idx; - model.textures[idx].source = idx; - model.images[idx].uri = mEmissiveId.asString(); - } + writeToTexture(model, material_out.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE, mEmissiveId, is_override, base_material->mEmissiveId); if (mAlphaMode != base_material->mAlphaMode) { -- cgit v1.2.3 From b1f529082ba0d785bafe1aa49298eb2f9eca1d3f Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Mon, 31 Oct 2022 20:42:47 +0200 Subject: SL-18446 Fix emissive tint --- indra/llprimitive/llgltfmaterial.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 6164234d6c..9743bad7a7 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -241,6 +241,7 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const material_out.alphaCutoff = mAlphaCutoff; mBaseColor.write(material_out.pbrMetallicRoughness.baseColorFactor); + material_out.emissiveFactor.resize(3); // 0 size by default mEmissiveColor.write(material_out.emissiveFactor); material_out.pbrMetallicRoughness.metallicFactor = mMetallicFactor; @@ -477,6 +478,7 @@ void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model& model, S32 mat_index if (mEmissiveColor != base_material->mEmissiveColor) { + material_out.emissiveFactor.resize(3); // 0 size by default mEmissiveColor.write(material_out.emissiveFactor); } -- cgit v1.2.3 From 75e743be2f60916e6bf78da24814551bd6415116 Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Mon, 31 Oct 2022 15:58:20 -0500 Subject: SL-18442 Port of Caladbolg's fix for emissive overrides not taking. Remove unused function. --- indra/llprimitive/llgltfmaterial.cpp | 67 ++++-------------------------------- 1 file changed, 7 insertions(+), 60 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 9743bad7a7..f771337c92 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -241,8 +241,14 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const material_out.alphaCutoff = mAlphaCutoff; mBaseColor.write(material_out.pbrMetallicRoughness.baseColorFactor); + material_out.emissiveFactor.resize(3); // 0 size by default - mEmissiveColor.write(material_out.emissiveFactor); + + if (mEmissiveColor != LLGLTFMaterial::getDefaultEmissiveColor()) + { + material_out.emissiveFactor.resize(3); + mEmissiveColor.write(material_out.emissiveFactor); + } material_out.pbrMetallicRoughness.metallicFactor = mMetallicFactor; material_out.pbrMetallicRoughness.roughnessFactor = mRoughnessFactor; @@ -439,65 +445,6 @@ F32 LLGLTFMaterial::getDefaultTextureRotation() return 0.f; } -void LLGLTFMaterial::writeOverridesToModel(tinygltf::Model& model, S32 mat_index, LLGLTFMaterial const* base_material) const -{ - if (model.materials.size() < mat_index + 1) - { - model.materials.resize(mat_index + 1); - } - - tinygltf::Material& material_out = model.materials[mat_index]; - - // TODO - fix handling of resetting to null/default values - - constexpr bool is_override = true; - - // set base color texture - writeToTexture(model, material_out.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR, mBaseColorId, is_override, base_material->mBaseColorId); - // set normal texture - writeToTexture(model, material_out.normalTexture, GLTF_TEXTURE_INFO_NORMAL, mNormalId, is_override, base_material->mNormalId); - // set metallic-roughness texture - writeToTexture(model, material_out.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS, mMetallicRoughnessId, is_override, base_material->mMetallicRoughnessId); - // set emissive texture - writeToTexture(model, material_out.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE, mEmissiveId, is_override, base_material->mEmissiveId); - - if (mAlphaMode != base_material->mAlphaMode) - { - material_out.alphaMode = getAlphaMode(); - } - - if (mAlphaCutoff != base_material->mAlphaCutoff) - { - material_out.alphaCutoff = mAlphaCutoff; - } - - if (mBaseColor != base_material->mBaseColor) - { - mBaseColor.write(material_out.pbrMetallicRoughness.baseColorFactor); - } - - if (mEmissiveColor != base_material->mEmissiveColor) - { - material_out.emissiveFactor.resize(3); // 0 size by default - mEmissiveColor.write(material_out.emissiveFactor); - } - - if (mMetallicFactor != base_material->mMetallicFactor) - { - material_out.pbrMetallicRoughness.metallicFactor = mMetallicFactor; - } - - if (mRoughnessFactor != base_material->mRoughnessFactor) - { - material_out.pbrMetallicRoughness.roughnessFactor = mRoughnessFactor; - } - - if (mDoubleSided != base_material->mDoubleSided) - { - material_out.doubleSided = mDoubleSided; - } -} - void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) { // TODO: potentially reimplement this with a more general purpose JSON merge -- cgit v1.2.3 From 9f21fba6d9a28cd1b324a115a0a2f86613a134e7 Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Tue, 1 Nov 2022 08:31:01 -0500 Subject: SL-18513 Put profile markers around GLTF code. --- indra/llprimitive/llgltfmaterial.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index f771337c92..cd629dbbd8 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -42,6 +42,7 @@ LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) { + LL_PROFILE_ZONE_SCOPED; //have to do a manual operator= because of LLRefCount mBaseColorId = rhs.mBaseColorId; mNormalId = rhs.mNormalId; @@ -65,7 +66,7 @@ LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) bool LLGLTFMaterial::fromJSON(const std::string& json, std::string& warn_msg, std::string& error_msg) { -#if 1 + LL_PROFILE_ZONE_SCOPED; tinygltf::TinyGLTF gltf; tinygltf::Model model_in; @@ -74,18 +75,14 @@ bool LLGLTFMaterial::fromJSON(const std::string& json, std::string& warn_msg, st { setFromModel(model_in, 0); - //DEBUG generate json and print - LL_INFOS() << asJSON(true) << LL_ENDL; - return true; } -#endif return false; } std::string LLGLTFMaterial::asJSON(bool prettyprint) const { -#if 1 + LL_PROFILE_ZONE_SCOPED; tinygltf::TinyGLTF gltf; tinygltf::Model model_out; @@ -97,13 +94,11 @@ std::string LLGLTFMaterial::asJSON(bool prettyprint) const gltf.WriteGltfSceneToStream(&model_out, str, prettyprint, false); return str.str(); -#else - return ""; -#endif } void LLGLTFMaterial::setFromModel(const tinygltf::Model& model, S32 mat_index) { + LL_PROFILE_ZONE_SCOPED; if (model.materials.size() <= mat_index) { return; @@ -198,6 +193,7 @@ std::string gltf_get_texture_image(const tinygltf::Model& model, const T& textur template void LLGLTFMaterial::setFromTexture(const tinygltf::Model& model, const T& texture_info, TextureInfo texture_info_id, LLUUID& texture_id_out) { + LL_PROFILE_ZONE_SCOPED; const std::string uri = gltf_get_texture_image(model, texture_info); texture_id_out.set(uri); @@ -219,6 +215,7 @@ void LLGLTFMaterial::setFromTexture(const tinygltf::Model& model, const T& textu void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const { + LL_PROFILE_ZONE_SCOPED; if (model.materials.size() < mat_index+1) { model.materials.resize(mat_index + 1); @@ -277,6 +274,7 @@ void gltf_allocate_texture_image(tinygltf::Model& model, T& texture_info, const template void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, TextureInfo texture_info_id, const LLUUID& texture_id, bool is_override, const LLUUID& base_texture_id) const { + LL_PROFILE_ZONE_SCOPED; if (texture_id.isNull() || (is_override && texture_id == base_texture_id)) { return; @@ -447,6 +445,7 @@ F32 LLGLTFMaterial::getDefaultTextureRotation() void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) { + LL_PROFILE_ZONE_SCOPED; // TODO: potentially reimplement this with a more general purpose JSON merge if (override_mat.mBaseColorId != getDefaultBaseColorId()) -- cgit v1.2.3 From a4ad75e93c20f140d9503c119201128b0f9e4d0e Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Tue, 1 Nov 2022 15:17:22 -0500 Subject: SL-18520 WIP - Use off-by-epsilon and special UUID identifier hacks to allow overriding to default values. --- indra/llprimitive/llgltfmaterial.cpp | 176 ++++++++++++++++++++++++++--------- 1 file changed, 130 insertions(+), 46 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index cd629dbbd8..4b1f89f99f 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -28,6 +28,7 @@ #include "llgltfmaterial.h" +// NOTE -- this should be the one and only place tiny_gltf.h is included #include "tinygltf/tiny_gltf.h" const char* GLTF_FILE_EXTENSION_TRANSFORM = "KHR_texture_transform"; @@ -35,6 +36,9 @@ const char* GLTF_FILE_EXTENSION_TRANSFORM_SCALE = "scale"; const char* GLTF_FILE_EXTENSION_TRANSFORM_OFFSET = "offset"; const char* GLTF_FILE_EXTENSION_TRANSFORM_ROTATION = "rotation"; +// special UUID that indicates a null UUID in override data +static const LLUUID GLTF_OVERRIDE_NULL_UUID = LLUUID("ffffffff-ffff-ffff-ffff-ffffffffffff"); + LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) { *this = rhs; @@ -296,64 +300,144 @@ void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, Tex texture_info.extensions[GLTF_FILE_EXTENSION_TRANSFORM] = tinygltf::Value(transform_map); } +// static +void LLGLTFMaterial::hackOverrideUUID(LLUUID& id) +{ + if (id == LLUUID::null) + { + id = GLTF_OVERRIDE_NULL_UUID; + } +} -void LLGLTFMaterial::setBaseColorId(const LLUUID& id) +void LLGLTFMaterial::setBaseColorId(const LLUUID& id, bool for_override) { mBaseColorId = id; + if (for_override) + { + hackOverrideUUID(mBaseColorId); + } } -void LLGLTFMaterial::setNormalId(const LLUUID& id) +void LLGLTFMaterial::setNormalId(const LLUUID& id, bool for_override) { mNormalId = id; + if (for_override) + { + hackOverrideUUID(mNormalId); + } } -void LLGLTFMaterial::setMetallicRoughnessId(const LLUUID& id) +void LLGLTFMaterial::setMetallicRoughnessId(const LLUUID& id, bool for_override) { mMetallicRoughnessId = id; + if (for_override) + { + hackOverrideUUID(mMetallicRoughnessId); + } } -void LLGLTFMaterial::setEmissiveId(const LLUUID& id) +void LLGLTFMaterial::setEmissiveId(const LLUUID& id, bool for_override) { mEmissiveId = id; + if (for_override) + { + hackOverrideUUID(mEmissiveId); + } } -void LLGLTFMaterial::setBaseColorFactor(const LLColor3& baseColor, F32 transparency) +void LLGLTFMaterial::setBaseColorFactor(const LLColor4& baseColor, bool for_override) { - mBaseColor.set(baseColor, transparency); + mBaseColor.set(baseColor); mBaseColor.clamp(); + + if (for_override) + { // hack -- nudge off of default value + if (mBaseColor == getDefaultBaseColor()) + { + mBaseColor.mV[3] -= FLT_EPSILON; + } + } } -void LLGLTFMaterial::setAlphaCutoff(F32 cutoff) +void LLGLTFMaterial::setAlphaCutoff(F32 cutoff, bool for_override) { mAlphaCutoff = llclamp(cutoff, 0.f, 1.f); + if (for_override) + { // hack -- nudge off of default value + if (mAlphaCutoff == getDefaultAlphaCutoff()) + { + mAlphaCutoff -= FLT_EPSILON; + } + } } -void LLGLTFMaterial::setEmissiveColorFactor(const LLColor3& emissiveColor) +void LLGLTFMaterial::setEmissiveColorFactor(const LLColor3& emissiveColor, bool for_override) { mEmissiveColor = emissiveColor; mEmissiveColor.clamp(); + + if (for_override) + { // hack -- nudge off of default value + if (mEmissiveColor == getDefaultEmissiveColor()) + { + mEmissiveColor.mV[0] += FLT_EPSILON; + } + } +} + +void LLGLTFMaterial::setMetallicFactor(F32 metallic, bool for_override) +{ + mMetallicFactor = llclamp(metallic, 0.f, for_override ? 1.f - FLT_EPSILON : 1.f); } -void LLGLTFMaterial::setMetallicFactor(F32 metallic) +void LLGLTFMaterial::setRoughnessFactor(F32 roughness, bool for_override) { - mMetallicFactor = llclamp(metallic, 0.f, 1.f); + mRoughnessFactor = llclamp(roughness, 0.f, for_override ? 1.f - FLT_EPSILON : 1.f); } -void LLGLTFMaterial::setRoughnessFactor(F32 roughness) +void LLGLTFMaterial::setAlphaMode(const std::string& mode, bool for_override) { - mRoughnessFactor = llclamp(roughness, 0.f, 1.f); + S32 m = getDefaultAlphaMode(); + if (mode == "MASK") + { + m = ALPHA_MODE_MASK; + } + else if (mode == "BLEND") + { + m = ALPHA_MODE_BLEND; + } + + setAlphaMode(m, for_override); +} + +const char* LLGLTFMaterial::getAlphaMode() const +{ + switch (mAlphaMode) + { + case ALPHA_MODE_MASK: return "MASK"; + case ALPHA_MODE_BLEND: return "BLEND"; + default: return "OPAQUE"; + } } -void LLGLTFMaterial::setAlphaMode(S32 mode) +void LLGLTFMaterial::setAlphaMode(S32 mode, bool for_override) { mAlphaMode = (AlphaMode) llclamp(mode, (S32) ALPHA_MODE_OPAQUE, (S32) ALPHA_MODE_MASK); + if (for_override) + { + // TODO: what do? + } } -void LLGLTFMaterial::setDoubleSided(bool double_sided) +void LLGLTFMaterial::setDoubleSided(bool double_sided, bool for_override) { // sure, no clamping will ever be needed for a bool, but include the // setter for consistency with the clamping API mDoubleSided = double_sided; + if (for_override) + { + // TODO: what do? + } } void LLGLTFMaterial::setTextureOffset(TextureInfo texture_info, const LLVector2& offset) @@ -371,102 +455,102 @@ void LLGLTFMaterial::setTextureRotation(TextureInfo texture_info, float rotation mTextureTransform[texture_info].mRotation = rotation; } -// Default value accessors +// Default value accessors (NOTE: these MUST match the GLTF specification) + +// Make a static default material for accessors +const LLGLTFMaterial LLGLTFMaterial::sDefault; LLUUID LLGLTFMaterial::getDefaultBaseColorId() { - return LLUUID::null; + return sDefault.mBaseColorId; } LLUUID LLGLTFMaterial::getDefaultNormalId() { - return LLUUID::null; + return sDefault.mNormalId; } LLUUID LLGLTFMaterial::getDefaultEmissiveId() { - return LLUUID::null; + return sDefault.mEmissiveId; } LLUUID LLGLTFMaterial::getDefaultMetallicRoughnessId() { - return LLUUID::null; + return sDefault.mMetallicRoughnessId; } F32 LLGLTFMaterial::getDefaultAlphaCutoff() { - return 0.f; + return sDefault.mAlphaCutoff; } S32 LLGLTFMaterial::getDefaultAlphaMode() { - return (S32) ALPHA_MODE_OPAQUE; + return (S32) sDefault.mAlphaMode; } F32 LLGLTFMaterial::getDefaultMetallicFactor() { - return 0.f; + return sDefault.mMetallicFactor; } F32 LLGLTFMaterial::getDefaultRoughnessFactor() { - return 0.f; + return sDefault.mRoughnessFactor; } LLColor4 LLGLTFMaterial::getDefaultBaseColor() { - return LLColor4::white; + return sDefault.mBaseColor; } LLColor3 LLGLTFMaterial::getDefaultEmissiveColor() { - return LLColor3::black; + return sDefault.mEmissiveColor; } bool LLGLTFMaterial::getDefaultDoubleSided() { - return false; + return sDefault.mDoubleSided; } LLVector2 LLGLTFMaterial::getDefaultTextureOffset() { - return LLVector2(0.f, 0.f); + return sDefault.mTextureTransform[0].mOffset; } LLVector2 LLGLTFMaterial::getDefaultTextureScale() { - return LLVector2(1.f, 1.f); + return sDefault.mTextureTransform[0].mScale; } F32 LLGLTFMaterial::getDefaultTextureRotation() { - return 0.f; + return sDefault.mTextureTransform[0].mRotation; } -void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) +// static +void LLGLTFMaterial::applyOverrideUUID(LLUUID& dst_id, const LLUUID& override_id) { - LL_PROFILE_ZONE_SCOPED; - // TODO: potentially reimplement this with a more general purpose JSON merge - - if (override_mat.mBaseColorId != getDefaultBaseColorId()) + if (override_id != GLTF_OVERRIDE_NULL_UUID) { - mBaseColorId = override_mat.mBaseColorId; + dst_id = override_id; } - - if (override_mat.mNormalId != getDefaultNormalId()) + else { - mNormalId = override_mat.mNormalId; + dst_id = LLUUID::null; } +} - if (override_mat.mMetallicRoughnessId != getDefaultMetallicRoughnessId()) - { - mMetallicRoughnessId = override_mat.mMetallicRoughnessId; - } +void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) +{ + LL_PROFILE_ZONE_SCOPED; - if (override_mat.mEmissiveId != getDefaultEmissiveId()) - { - mEmissiveId = override_mat.mEmissiveId; - } + applyOverrideUUID(mBaseColorId, override_mat.mBaseColorId); + applyOverrideUUID(mNormalId, override_mat.mNormalId); + applyOverrideUUID(mMetallicRoughnessId, override_mat.mMetallicRoughnessId); + applyOverrideUUID(mEmissiveId, override_mat.mEmissiveId); if (override_mat.mBaseColor != getDefaultBaseColor()) { -- cgit v1.2.3 From c3f94ab9a1da2c0c5304ff624b54fad6a43506ae Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Wed, 2 Nov 2022 12:14:56 -0500 Subject: SL-18520 Use GLTF material.extras to pass flags for enabling overriding alpha mode and double sided to default --- indra/llprimitive/llgltfmaterial.cpp | 73 ++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 25 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 4b1f89f99f..172cbdb7ce 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -65,6 +65,9 @@ LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) mTextureTransform = rhs.mTextureTransform; + mOverrideDoubleSided = rhs.mOverrideDoubleSided; + mOverrideAlphaMode = rhs.mOverrideDoubleSided; + return *this; } @@ -129,6 +132,22 @@ void LLGLTFMaterial::setFromModel(const tinygltf::Model& model, S32 mat_index) mRoughnessFactor = llclamp((F32)material_in.pbrMetallicRoughness.roughnessFactor, 0.f, 1.f); mDoubleSided = material_in.doubleSided; + + if (material_in.extras.IsObject()) + { + tinygltf::Value::Object extras = material_in.extras.Get(); + auto& alpha_mode = extras.find("override_alpha_mode"); + if (alpha_mode != extras.end()) + { + mOverrideAlphaMode = alpha_mode->second.Get(); + } + + auto& double_sided = extras.find("override_double_sided"); + if (double_sided != extras.end()) + { + mOverrideDoubleSided = double_sided->second.Get(); + } + } } LLVector2 vec2_from_json(const tinygltf::Value::Object& object, const char* key, const LLVector2& default_value) @@ -256,6 +275,27 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const material_out.doubleSided = mDoubleSided; + + // generate "extras" string + tinygltf::Value::Object extras; + bool write_extras = false; + if (mOverrideAlphaMode && mAlphaMode == getDefaultAlphaMode()) + { + extras["override_alpha_mode"] = tinygltf::Value(mOverrideAlphaMode); + write_extras = true; + } + + if (mOverrideDoubleSided && mDoubleSided == getDefaultDoubleSided()) + { + extras["override_double_sided"] = tinygltf::Value(mOverrideDoubleSided); + write_extras = true; + } + + if (write_extras) + { + material_out.extras = tinygltf::Value(extras); + } + model.asset.version = "2.0"; } @@ -425,7 +465,7 @@ void LLGLTFMaterial::setAlphaMode(S32 mode, bool for_override) mAlphaMode = (AlphaMode) llclamp(mode, (S32) ALPHA_MODE_OPAQUE, (S32) ALPHA_MODE_MASK); if (for_override) { - // TODO: what do? + mOverrideAlphaMode = true; } } @@ -436,7 +476,7 @@ void LLGLTFMaterial::setDoubleSided(bool double_sided, bool for_override) mDoubleSided = double_sided; if (for_override) { - // TODO: what do? + mOverrideDoubleSided = true; } } @@ -460,26 +500,6 @@ void LLGLTFMaterial::setTextureRotation(TextureInfo texture_info, float rotation // Make a static default material for accessors const LLGLTFMaterial LLGLTFMaterial::sDefault; -LLUUID LLGLTFMaterial::getDefaultBaseColorId() -{ - return sDefault.mBaseColorId; -} - -LLUUID LLGLTFMaterial::getDefaultNormalId() -{ - return sDefault.mNormalId; -} - -LLUUID LLGLTFMaterial::getDefaultEmissiveId() -{ - return sDefault.mEmissiveId; -} - -LLUUID LLGLTFMaterial::getDefaultMetallicRoughnessId() -{ - return sDefault.mMetallicRoughnessId; -} - F32 LLGLTFMaterial::getDefaultAlphaCutoff() { return sDefault.mAlphaCutoff; @@ -535,7 +555,10 @@ void LLGLTFMaterial::applyOverrideUUID(LLUUID& dst_id, const LLUUID& override_id { if (override_id != GLTF_OVERRIDE_NULL_UUID) { - dst_id = override_id; + if (override_id != LLUUID::null) + { + dst_id = override_id; + } } else { @@ -572,7 +595,7 @@ void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) mRoughnessFactor = override_mat.mRoughnessFactor; } - if (override_mat.mAlphaMode != getDefaultAlphaMode()) + if (override_mat.mAlphaMode != getDefaultAlphaMode() || override_mat.mOverrideAlphaMode) { mAlphaMode = override_mat.mAlphaMode; } @@ -581,7 +604,7 @@ void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) mAlphaCutoff = override_mat.mAlphaCutoff; } - if (override_mat.mDoubleSided != getDefaultDoubleSided()) + if (override_mat.mDoubleSided != getDefaultDoubleSided() || override_mat.mOverrideDoubleSided) { mDoubleSided = override_mat.mDoubleSided; } -- cgit v1.2.3 From 9e7b725c15ea0bfea5246eb81dd7a100b7ac5b6b Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Mon, 31 Oct 2022 09:42:27 -0700 Subject: SL-18485: Render GLTF materials with extension KHR_texture_transform with approprate texture transforms --- indra/llprimitive/llgltfmaterial.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 172cbdb7ce..649d4dbe8f 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -39,6 +39,28 @@ const char* GLTF_FILE_EXTENSION_TRANSFORM_ROTATION = "rotation"; // special UUID that indicates a null UUID in override data static const LLUUID GLTF_OVERRIDE_NULL_UUID = LLUUID("ffffffff-ffff-ffff-ffff-ffffffffffff"); +// https://github.com/KhronosGroup/glTF/tree/main/extensions/3.0/Khronos/KHR_texture_transform +LLMatrix3 LLGLTFMaterial::TextureTransform::asMatrix() +{ + LLMatrix3 scale; + scale.mMatrix[0][0] = mScale[0]; + scale.mMatrix[1][1] = mScale[1]; + + LLMatrix3 rotation; + const F32 cos_r = cos(mRotation); + const F32 sin_r = sin(mRotation); + rotation.mMatrix[0][0] = cos_r; + rotation.mMatrix[0][1] = sin_r; + rotation.mMatrix[1][0] = -sin_r; + rotation.mMatrix[1][1] = cos_r; + + LLMatrix3 offset; + offset.mMatrix[2][0] = mOffset[0]; + offset.mMatrix[2][1] = mOffset[1]; + + return offset * rotation * scale; +} + LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) { *this = rhs; -- cgit v1.2.3 From 3e7e146be689233b21e31c091298dcfa2cb1adfc Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Wed, 2 Nov 2022 14:58:24 -0500 Subject: SL-18520 Fix for mac build. --- indra/llprimitive/llgltfmaterial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 172cbdb7ce..c76f547570 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -136,13 +136,13 @@ void LLGLTFMaterial::setFromModel(const tinygltf::Model& model, S32 mat_index) if (material_in.extras.IsObject()) { tinygltf::Value::Object extras = material_in.extras.Get(); - auto& alpha_mode = extras.find("override_alpha_mode"); + const auto& alpha_mode = extras.find("override_alpha_mode"); if (alpha_mode != extras.end()) { mOverrideAlphaMode = alpha_mode->second.Get(); } - auto& double_sided = extras.find("override_double_sided"); + const auto& double_sided = extras.find("override_double_sided"); if (double_sided != extras.end()) { mOverrideDoubleSided = double_sided->second.Get(); -- cgit v1.2.3 From cba87c62cc3c31d48c680bf92aa7ae2b9555ba69 Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Tue, 8 Nov 2022 10:53:24 -0800 Subject: SL-18523: When editing an object's material override, use the object's material override as a base, rather than its render material --- indra/llprimitive/llgltfmaterial.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 6d23cb8039..ee1931a27b 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -61,6 +61,29 @@ LLMatrix3 LLGLTFMaterial::TextureTransform::asMatrix() return offset * rotation * scale; } +LLGLTFMaterial::LLGLTFMaterial(bool for_override) +: LLGLTFMaterial() +{ + if (for_override) + { + setBaseColorId(mBaseColorId, for_override); + setNormalId(mNormalId, for_override); + setMetallicRoughnessId(mMetallicRoughnessId, for_override); + setEmissiveId(mEmissiveId, for_override); + + setBaseColorFactor(mBaseColor, for_override); + setAlphaCutoff(mAlphaCutoff, for_override); + setEmissiveColorFactor(mEmissiveColor, for_override); + setMetallicFactor(mMetallicFactor, for_override); + setRoughnessFactor(mRoughnessFactor, for_override); + setAlphaMode(mAlphaMode, for_override); + setDoubleSided(mDoubleSided, for_override); + + // *NOTE: Texture offsets only exist in overrides, so there is no need + // to hack in the override value here. + } +} + LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) { *this = rhs; @@ -588,6 +611,9 @@ void LLGLTFMaterial::applyOverrideUUID(LLUUID& dst_id, const LLUUID& override_id } } +// Make a static default material override for editing materials +const LLGLTFMaterial LLGLTFMaterial::sOverrideDefault{true}; + void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) { LL_PROFILE_ZONE_SCOPED; -- cgit v1.2.3 From 4aaa48419594c993c6c1b0bd2f5585c6492b6a31 Mon Sep 17 00:00:00 2001 From: Sabrina Shanman Date: Wed, 9 Nov 2022 00:16:41 +0000 Subject: Revert "SL-18523: When editing an object's material override, use the object's material override as a base, rather than its render material (pull request #1190)" --- indra/llprimitive/llgltfmaterial.cpp | 26 -------------------------- 1 file changed, 26 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index ee1931a27b..6d23cb8039 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -61,29 +61,6 @@ LLMatrix3 LLGLTFMaterial::TextureTransform::asMatrix() return offset * rotation * scale; } -LLGLTFMaterial::LLGLTFMaterial(bool for_override) -: LLGLTFMaterial() -{ - if (for_override) - { - setBaseColorId(mBaseColorId, for_override); - setNormalId(mNormalId, for_override); - setMetallicRoughnessId(mMetallicRoughnessId, for_override); - setEmissiveId(mEmissiveId, for_override); - - setBaseColorFactor(mBaseColor, for_override); - setAlphaCutoff(mAlphaCutoff, for_override); - setEmissiveColorFactor(mEmissiveColor, for_override); - setMetallicFactor(mMetallicFactor, for_override); - setRoughnessFactor(mRoughnessFactor, for_override); - setAlphaMode(mAlphaMode, for_override); - setDoubleSided(mDoubleSided, for_override); - - // *NOTE: Texture offsets only exist in overrides, so there is no need - // to hack in the override value here. - } -} - LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) { *this = rhs; @@ -611,9 +588,6 @@ void LLGLTFMaterial::applyOverrideUUID(LLUUID& dst_id, const LLUUID& override_id } } -// Make a static default material override for editing materials -const LLGLTFMaterial LLGLTFMaterial::sOverrideDefault{true}; - void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) { LL_PROFILE_ZONE_SCOPED; -- cgit v1.2.3 From fe2a07a80f4f0ae913078cca11a5242c24e550ed Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Fri, 11 Nov 2022 14:34:57 -0600 Subject: SL-18616 Fix for dropping mOverrideAlphaMode on copy --- indra/llprimitive/llgltfmaterial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 6d23cb8039..6fbcf50482 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -88,7 +88,7 @@ LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) mTextureTransform = rhs.mTextureTransform; mOverrideDoubleSided = rhs.mOverrideDoubleSided; - mOverrideAlphaMode = rhs.mOverrideDoubleSided; + mOverrideAlphaMode = rhs.mOverrideAlphaMode; return *this; } -- cgit v1.2.3 From e4dd9c1e648e38c15a1fa792ffeb5754ae936709 Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Mon, 14 Nov 2022 14:40:24 -0800 Subject: SL-18634: Fix GLTF material texture transform not serializing when texture ID is null --- indra/llprimitive/llgltfmaterial.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 6fbcf50482..e8e4b62934 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -61,6 +61,11 @@ LLMatrix3 LLGLTFMaterial::TextureTransform::asMatrix() return offset * rotation * scale; } +bool LLGLTFMaterial::TextureTransform::operator==(const TextureTransform& other) const +{ + return mOffset == other.mOffset && mScale == other.mScale && mRotation == other.mRotation; +} + LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) { *this = rhs; @@ -268,16 +273,14 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const tinygltf::Material& material_out = model.materials[mat_index]; - constexpr bool is_override = false; - // set base color texture - writeToTexture(model, material_out.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR, mBaseColorId, is_override, LLUUID()); + writeToTexture(model, material_out.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR, mBaseColorId); // set normal texture - writeToTexture(model, material_out.normalTexture, GLTF_TEXTURE_INFO_NORMAL, mNormalId, is_override, LLUUID()); + writeToTexture(model, material_out.normalTexture, GLTF_TEXTURE_INFO_NORMAL, mNormalId); // set metallic-roughness texture - writeToTexture(model, material_out.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS, mMetallicRoughnessId, is_override, LLUUID()); + writeToTexture(model, material_out.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS, mMetallicRoughnessId); // set emissive texture - writeToTexture(model, material_out.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE, mEmissiveId, is_override, LLUUID()); + writeToTexture(model, material_out.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE, mEmissiveId); material_out.alphaMode = getAlphaMode(); material_out.alphaCutoff = mAlphaCutoff; @@ -338,10 +341,11 @@ void gltf_allocate_texture_image(tinygltf::Model& model, T& texture_info, const } template -void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, TextureInfo texture_info_id, const LLUUID& texture_id, bool is_override, const LLUUID& base_texture_id) const +void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, TextureInfo texture_info_id, const LLUUID& texture_id) const { LL_PROFILE_ZONE_SCOPED; - if (texture_id.isNull() || (is_override && texture_id == base_texture_id)) + const TextureTransform& transform = mTextureTransform[texture_info_id]; + if (texture_id.isNull() && transform == sDefault.mTextureTransform[0]) { return; } @@ -349,7 +353,6 @@ void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, Tex gltf_allocate_texture_image(model, texture_info, texture_id.asString()); tinygltf::Value::Object transform_map; - const TextureTransform& transform = mTextureTransform[texture_info_id]; transform_map[GLTF_FILE_EXTENSION_TRANSFORM_OFFSET] = tinygltf::Value(tinygltf::Value::Array({ tinygltf::Value(transform.mOffset.mV[VX]), tinygltf::Value(transform.mOffset.mV[VY]) -- cgit v1.2.3 From 366efa4384e6effa6640c9f125756c58c07fcabe Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Wed, 4 Jan 2023 18:00:06 +0200 Subject: SL-18741 Add gltf to bulk uploads on mac And cleaned up dupplicate mScale code --- indra/llprimitive/llgltfmaterial.cpp | 5 ----- 1 file changed, 5 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index e8e4b62934..c98fd7d1ee 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -646,11 +646,6 @@ void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) mTextureTransform[i].mScale = override_mat.mTextureTransform[i].mScale; } - if (override_mat.mTextureTransform[i].mScale != getDefaultTextureScale()) - { - mTextureTransform[i].mScale = override_mat.mTextureTransform[i].mScale; - } - if (override_mat.mTextureTransform[i].mRotation != getDefaultTextureRotation()) { mTextureTransform[i].mRotation = override_mat.mTextureTransform[i].mRotation; -- cgit v1.2.3 From 4fa77b6f728b6be3d2e7fc74dda97b835476f797 Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Mon, 12 Dec 2022 14:12:03 -0800 Subject: SL-18820: Update LLGLTFMaterial: Add setBaseMaterial() and equality comparison --- indra/llprimitive/llgltfmaterial.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index c98fd7d1ee..5442af33d0 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -98,6 +98,29 @@ LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) return *this; } +bool LLGLTFMaterial::operator==(const LLGLTFMaterial& rhs) const +{ + return mBaseColorId == rhs.mBaseColorId && + mNormalId == rhs.mNormalId && + mMetallicRoughnessId == rhs.mMetallicRoughnessId && + mEmissiveId == rhs.mEmissiveId && + + mBaseColor == rhs.mBaseColor && + mEmissiveColor == rhs.mEmissiveColor && + + mMetallicFactor == rhs.mMetallicFactor && + mRoughnessFactor == rhs.mRoughnessFactor && + mAlphaCutoff == rhs.mAlphaCutoff && + + mDoubleSided == rhs.mDoubleSided && + mAlphaMode == rhs.mAlphaMode && + + mTextureTransform == rhs.mTextureTransform && + + mOverrideDoubleSided == rhs.mOverrideDoubleSided && + mOverrideAlphaMode == rhs.mOverrideAlphaMode; +} + bool LLGLTFMaterial::fromJSON(const std::string& json, std::string& warn_msg, std::string& error_msg) { LL_PROFILE_ZONE_SCOPED; @@ -365,6 +388,16 @@ void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, Tex texture_info.extensions[GLTF_FILE_EXTENSION_TRANSFORM] = tinygltf::Value(transform_map); } +bool LLGLTFMaterial::setBaseMaterial() +{ + const LLGLTFMaterial old_override = *this; + *this = sDefault; + // Preserve the texture transforms + mTextureTransform = old_override.mTextureTransform; + return *this != old_override; +} + + // static void LLGLTFMaterial::hackOverrideUUID(LLUUID& id) { -- cgit v1.2.3 From 693925ef23ef41e3927a9654a7f423d0e24ce19a Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Tue, 13 Dec 2022 10:41:21 -0800 Subject: SL-18820: Fix applying material clearing transform overrides. Loosen some asserts to allow non-default transform overrides. --- indra/llprimitive/llgltfmaterial.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 5442af33d0..a8dad89292 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -392,11 +392,23 @@ bool LLGLTFMaterial::setBaseMaterial() { const LLGLTFMaterial old_override = *this; *this = sDefault; - // Preserve the texture transforms - mTextureTransform = old_override.mTextureTransform; + setBaseMaterial(old_override); return *this != old_override; } +bool LLGLTFMaterial::isClearedForBaseMaterial() +{ + LLGLTFMaterial cleared_override = sDefault; + cleared_override.setBaseMaterial(*this); + return *this == cleared_override; +} + +// For material overrides only. Copies transforms from the old override. +void LLGLTFMaterial::setBaseMaterial(const LLGLTFMaterial& old_override_mat) +{ + mTextureTransform = old_override_mat.mTextureTransform; +} + // static void LLGLTFMaterial::hackOverrideUUID(LLUUID& id) -- 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/llprimitive/llgltfmaterial.cpp | 151 ++++++++++++++++++----------------- 1 file changed, 78 insertions(+), 73 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index a8dad89292..2e920aa44e 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -31,6 +31,10 @@ // NOTE -- this should be the one and only place tiny_gltf.h is included #include "tinygltf/tiny_gltf.h" +const char* LLGLTFMaterial::ASSET_VERSION = "1.1"; +const char* LLGLTFMaterial::ASSET_TYPE = "GLTF 2.0"; +const std::array LLGLTFMaterial::ACCEPTED_ASSET_VERSIONS = { "1.0", "1.1" }; + const char* GLTF_FILE_EXTENSION_TRANSFORM = "KHR_texture_transform"; const char* GLTF_FILE_EXTENSION_TRANSFORM_SCALE = "scale"; const char* GLTF_FILE_EXTENSION_TRANSFORM_OFFSET = "offset"; @@ -73,16 +77,14 @@ LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) { - LL_PROFILE_ZONE_SCOPED; - //have to do a manual operator= because of LLRefCount - mBaseColorId = rhs.mBaseColorId; - mNormalId = rhs.mNormalId; - mMetallicRoughnessId = rhs.mMetallicRoughnessId; - mEmissiveId = rhs.mEmissiveId; + //have to do a manual operator= because of LLRefCount + mTextureId = rhs.mTextureId; + + mTextureTransform = rhs.mTextureTransform; mBaseColor = rhs.mBaseColor; mEmissiveColor = rhs.mEmissiveColor; - + mMetallicFactor = rhs.mMetallicFactor; mRoughnessFactor = rhs.mRoughnessFactor; mAlphaCutoff = rhs.mAlphaCutoff; @@ -90,8 +92,6 @@ LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) mDoubleSided = rhs.mDoubleSided; mAlphaMode = rhs.mAlphaMode; - mTextureTransform = rhs.mTextureTransform; - mOverrideDoubleSided = rhs.mOverrideDoubleSided; mOverrideAlphaMode = rhs.mOverrideAlphaMode; @@ -100,10 +100,9 @@ LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) bool LLGLTFMaterial::operator==(const LLGLTFMaterial& rhs) const { - return mBaseColorId == rhs.mBaseColorId && - mNormalId == rhs.mNormalId && - mMetallicRoughnessId == rhs.mMetallicRoughnessId && - mEmissiveId == rhs.mEmissiveId && + return mTextureId == rhs.mTextureId && + + mTextureTransform == rhs.mTextureTransform && mBaseColor == rhs.mBaseColor && mEmissiveColor == rhs.mEmissiveColor && @@ -115,8 +114,6 @@ bool LLGLTFMaterial::operator==(const LLGLTFMaterial& rhs) const mDoubleSided == rhs.mDoubleSided && mAlphaMode == rhs.mAlphaMode && - mTextureTransform == rhs.mTextureTransform && - mOverrideDoubleSided == rhs.mOverrideDoubleSided && mOverrideAlphaMode == rhs.mOverrideAlphaMode; } @@ -148,6 +145,8 @@ std::string LLGLTFMaterial::asJSON(bool prettyprint) const writeToModel(model_out, 0); + // To ensure consistency in asset upload, this should be the only reference + // to WriteGltfSceneToStream in the viewer. gltf.WriteGltfSceneToStream(&model_out, str, prettyprint, false); return str.str(); @@ -164,13 +163,13 @@ void LLGLTFMaterial::setFromModel(const tinygltf::Model& model, S32 mat_index) const tinygltf::Material& material_in = model.materials[mat_index]; // Apply base color texture - setFromTexture(model, material_in.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR, mBaseColorId); + setFromTexture(model, material_in.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR); // Apply normal map - setFromTexture(model, material_in.normalTexture, GLTF_TEXTURE_INFO_NORMAL, mNormalId); + setFromTexture(model, material_in.normalTexture, GLTF_TEXTURE_INFO_NORMAL); // Apply metallic-roughness texture - setFromTexture(model, material_in.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS, mMetallicRoughnessId); + setFromTexture(model, material_in.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS); // Apply emissive texture - setFromTexture(model, material_in.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE, mEmissiveId); + setFromTexture(model, material_in.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE); setAlphaMode(material_in.alphaMode); mAlphaCutoff = llclamp((F32)material_in.alphaCutoff, 0.f, 1.f); @@ -264,11 +263,11 @@ std::string gltf_get_texture_image(const tinygltf::Model& model, const T& textur // *NOTE: Use template here as workaround for the different similar texture info classes template -void LLGLTFMaterial::setFromTexture(const tinygltf::Model& model, const T& texture_info, TextureInfo texture_info_id, LLUUID& texture_id_out) +void LLGLTFMaterial::setFromTexture(const tinygltf::Model& model, const T& texture_info, TextureInfo texture_info_id) { LL_PROFILE_ZONE_SCOPED; const std::string uri = gltf_get_texture_image(model, texture_info); - texture_id_out.set(uri); + mTextureId[texture_info_id].set(uri); const tinygltf::Value::Object& extensions_object = texture_info.extensions; const auto transform_it = extensions_object.find(GLTF_FILE_EXTENSION_TRANSFORM); @@ -297,21 +296,24 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const tinygltf::Material& material_out = model.materials[mat_index]; // set base color texture - writeToTexture(model, material_out.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR, mBaseColorId); + writeToTexture(model, material_out.pbrMetallicRoughness.baseColorTexture, GLTF_TEXTURE_INFO_BASE_COLOR); // set normal texture - writeToTexture(model, material_out.normalTexture, GLTF_TEXTURE_INFO_NORMAL, mNormalId); + writeToTexture(model, material_out.normalTexture, GLTF_TEXTURE_INFO_NORMAL); // set metallic-roughness texture - writeToTexture(model, material_out.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS, mMetallicRoughnessId); + writeToTexture(model, material_out.pbrMetallicRoughness.metallicRoughnessTexture, GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS); // set emissive texture - writeToTexture(model, material_out.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE, mEmissiveId); + writeToTexture(model, material_out.emissiveTexture, GLTF_TEXTURE_INFO_EMISSIVE); + // set occlusion texture + // *NOTE: This is required for ORM materials for GLTF compliance. + // See: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#_material_occlusiontexture + writeToTexture(model, material_out.occlusionTexture, GLTF_TEXTURE_INFO_OCCLUSION); + material_out.alphaMode = getAlphaMode(); material_out.alphaCutoff = mAlphaCutoff; - + mBaseColor.write(material_out.pbrMetallicRoughness.baseColorFactor); - material_out.emissiveFactor.resize(3); // 0 size by default - if (mEmissiveColor != LLGLTFMaterial::getDefaultEmissiveColor()) { material_out.emissiveFactor.resize(3); @@ -323,7 +325,6 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const material_out.doubleSided = mDoubleSided; - // generate "extras" string tinygltf::Value::Object extras; bool write_extras = false; @@ -364,28 +365,43 @@ void gltf_allocate_texture_image(tinygltf::Model& model, T& texture_info, const } template -void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, TextureInfo texture_info_id, const LLUUID& texture_id) const +void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, TextureInfo texture_info_id, bool force_write) const { LL_PROFILE_ZONE_SCOPED; + const LLUUID& texture_id = mTextureId[texture_info_id]; const TextureTransform& transform = mTextureTransform[texture_info_id]; - if (texture_id.isNull() && transform == sDefault.mTextureTransform[0]) + const bool is_blank_transform = transform == sDefault.mTextureTransform[0]; + // Check if this material matches all the fallback values, and if so, then + // skip including it to reduce material size + if (!force_write && texture_id.isNull() && is_blank_transform) { return; } + // tinygltf will discard this texture info if there is no valid texture, + // causing potential loss of information for overrides, so ensure one is + // defined. -Cosmic,2023-01-30 gltf_allocate_texture_image(model, texture_info, texture_id.asString()); - tinygltf::Value::Object transform_map; - transform_map[GLTF_FILE_EXTENSION_TRANSFORM_OFFSET] = tinygltf::Value(tinygltf::Value::Array({ - tinygltf::Value(transform.mOffset.mV[VX]), - tinygltf::Value(transform.mOffset.mV[VY]) - })); - transform_map[GLTF_FILE_EXTENSION_TRANSFORM_SCALE] = tinygltf::Value(tinygltf::Value::Array({ - tinygltf::Value(transform.mScale.mV[VX]), - tinygltf::Value(transform.mScale.mV[VY]) - })); - transform_map[GLTF_FILE_EXTENSION_TRANSFORM_ROTATION] = tinygltf::Value(transform.mRotation); - texture_info.extensions[GLTF_FILE_EXTENSION_TRANSFORM] = tinygltf::Value(transform_map); + if (!is_blank_transform) + { + tinygltf::Value::Object transform_map; + transform_map[GLTF_FILE_EXTENSION_TRANSFORM_OFFSET] = tinygltf::Value(tinygltf::Value::Array({ + tinygltf::Value(transform.mOffset.mV[VX]), + tinygltf::Value(transform.mOffset.mV[VY]) + })); + transform_map[GLTF_FILE_EXTENSION_TRANSFORM_SCALE] = tinygltf::Value(tinygltf::Value::Array({ + tinygltf::Value(transform.mScale.mV[VX]), + tinygltf::Value(transform.mScale.mV[VY]) + })); + transform_map[GLTF_FILE_EXTENSION_TRANSFORM_ROTATION] = tinygltf::Value(transform.mRotation); + texture_info.extensions[GLTF_FILE_EXTENSION_TRANSFORM] = tinygltf::Value(transform_map); + } +} + +void LLGLTFMaterial::sanitizeAssetMaterial() +{ + mTextureTransform = sDefault.mTextureTransform; } bool LLGLTFMaterial::setBaseMaterial() @@ -419,40 +435,33 @@ void LLGLTFMaterial::hackOverrideUUID(LLUUID& id) } } -void LLGLTFMaterial::setBaseColorId(const LLUUID& id, bool for_override) +void LLGLTFMaterial::setTextureId(TextureInfo texture_info, const LLUUID& id, bool for_override) { - mBaseColorId = id; + mTextureId[texture_info] = id; if (for_override) { - hackOverrideUUID(mBaseColorId); + hackOverrideUUID(mTextureId[texture_info]); } } +void LLGLTFMaterial::setBaseColorId(const LLUUID& id, bool for_override) +{ + setTextureId(GLTF_TEXTURE_INFO_BASE_COLOR, id, for_override); +} + void LLGLTFMaterial::setNormalId(const LLUUID& id, bool for_override) { - mNormalId = id; - if (for_override) - { - hackOverrideUUID(mNormalId); - } + setTextureId(GLTF_TEXTURE_INFO_NORMAL, id, for_override); } -void LLGLTFMaterial::setMetallicRoughnessId(const LLUUID& id, bool for_override) +void LLGLTFMaterial::setOcclusionRoughnessMetallicId(const LLUUID& id, bool for_override) { - mMetallicRoughnessId = id; - if (for_override) - { - hackOverrideUUID(mMetallicRoughnessId); - } + setTextureId(GLTF_TEXTURE_INFO_METALLIC_ROUGHNESS, id, for_override); } void LLGLTFMaterial::setEmissiveId(const LLUUID& id, bool for_override) { - mEmissiveId = id; - if (for_override) - { - hackOverrideUUID(mEmissiveId); - } + setTextureId(GLTF_TEXTURE_INFO_EMISSIVE, id, for_override); } void LLGLTFMaterial::setBaseColorFactor(const LLColor4& baseColor, bool for_override) @@ -533,10 +542,7 @@ const char* LLGLTFMaterial::getAlphaMode() const void LLGLTFMaterial::setAlphaMode(S32 mode, bool for_override) { mAlphaMode = (AlphaMode) llclamp(mode, (S32) ALPHA_MODE_OPAQUE, (S32) ALPHA_MODE_MASK); - if (for_override) - { - mOverrideAlphaMode = true; - } + mOverrideAlphaMode = for_override && mAlphaMode == getDefaultAlphaMode(); } void LLGLTFMaterial::setDoubleSided(bool double_sided, bool for_override) @@ -544,10 +550,7 @@ void LLGLTFMaterial::setDoubleSided(bool double_sided, bool for_override) // sure, no clamping will ever be needed for a bool, but include the // setter for consistency with the clamping API mDoubleSided = double_sided; - if (for_override) - { - mOverrideDoubleSided = true; - } + mOverrideDoubleSided = for_override && mDoubleSided == getDefaultDoubleSided(); } void LLGLTFMaterial::setTextureOffset(TextureInfo texture_info, const LLVector2& offset) @@ -640,10 +643,12 @@ void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) { LL_PROFILE_ZONE_SCOPED; - applyOverrideUUID(mBaseColorId, override_mat.mBaseColorId); - applyOverrideUUID(mNormalId, override_mat.mNormalId); - applyOverrideUUID(mMetallicRoughnessId, override_mat.mMetallicRoughnessId); - applyOverrideUUID(mEmissiveId, override_mat.mEmissiveId); + for (int i = 0; i < GLTF_TEXTURE_INFO_COUNT; ++i) + { + LLUUID& texture_id = mTextureId[i]; + const LLUUID& override_texture_id = override_mat.mTextureId[i]; + applyOverrideUUID(texture_id, override_texture_id); + } if (override_mat.mBaseColor != getDefaultBaseColor()) { -- cgit v1.2.3 From a56385345f3fb261b8c2c375960f1328c296ebf2 Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Thu, 9 Feb 2023 16:13:57 -0800 Subject: SL-19080: Address clang-provided errors --- indra/llprimitive/llgltfmaterial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 2e920aa44e..291e2c2bf5 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -33,7 +33,7 @@ const char* LLGLTFMaterial::ASSET_VERSION = "1.1"; const char* LLGLTFMaterial::ASSET_TYPE = "GLTF 2.0"; -const std::array LLGLTFMaterial::ACCEPTED_ASSET_VERSIONS = { "1.0", "1.1" }; +const std::array LLGLTFMaterial::ACCEPTED_ASSET_VERSIONS = { "1.0", "1.1" }; const char* GLTF_FILE_EXTENSION_TRANSFORM = "KHR_texture_transform"; const char* GLTF_FILE_EXTENSION_TRANSFORM_SCALE = "scale"; -- cgit v1.2.3 From df440f3f33a277f927035d051bc98341227bbea5 Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Tue, 14 Feb 2023 15:33:52 -0800 Subject: SL-19121: Address review comments from SL-19080 phase 2 --- indra/llprimitive/llgltfmaterial.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 291e2c2bf5..4d7b10982a 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -31,14 +31,14 @@ // NOTE -- this should be the one and only place tiny_gltf.h is included #include "tinygltf/tiny_gltf.h" -const char* LLGLTFMaterial::ASSET_VERSION = "1.1"; -const char* LLGLTFMaterial::ASSET_TYPE = "GLTF 2.0"; +const char* const LLGLTFMaterial::ASSET_VERSION = "1.1"; +const char* const LLGLTFMaterial::ASSET_TYPE = "GLTF 2.0"; const std::array LLGLTFMaterial::ACCEPTED_ASSET_VERSIONS = { "1.0", "1.1" }; -const char* GLTF_FILE_EXTENSION_TRANSFORM = "KHR_texture_transform"; -const char* GLTF_FILE_EXTENSION_TRANSFORM_SCALE = "scale"; -const char* GLTF_FILE_EXTENSION_TRANSFORM_OFFSET = "offset"; -const char* GLTF_FILE_EXTENSION_TRANSFORM_ROTATION = "rotation"; +const char* const GLTF_FILE_EXTENSION_TRANSFORM = "KHR_texture_transform"; +const char* const GLTF_FILE_EXTENSION_TRANSFORM_SCALE = "scale"; +const char* const GLTF_FILE_EXTENSION_TRANSFORM_OFFSET = "offset"; +const char* const GLTF_FILE_EXTENSION_TRANSFORM_ROTATION = "rotation"; // special UUID that indicates a null UUID in override data static const LLUUID GLTF_OVERRIDE_NULL_UUID = LLUUID("ffffffff-ffff-ffff-ffff-ffffffffffff"); -- cgit v1.2.3 From 6494eed242b1cf64160e379c6d40df333deae23a Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Thu, 23 Feb 2023 10:58:39 -0800 Subject: SL-19228: Fix GLTF texture transform rotation and add UV debug (PBR only). See textureUtilV.glsl for UV coordinate comments --- indra/llprimitive/llgltfmaterial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 4d7b10982a..d0ecf611ff 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -54,8 +54,8 @@ LLMatrix3 LLGLTFMaterial::TextureTransform::asMatrix() const F32 cos_r = cos(mRotation); const F32 sin_r = sin(mRotation); rotation.mMatrix[0][0] = cos_r; - rotation.mMatrix[0][1] = sin_r; - rotation.mMatrix[1][0] = -sin_r; + rotation.mMatrix[0][1] = -sin_r; + rotation.mMatrix[1][0] = sin_r; rotation.mMatrix[1][1] = cos_r; LLMatrix3 offset; -- cgit v1.2.3 From b27c41578b4bb54ee87fbb8d05f29d9d9a9e2768 Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Mon, 27 Feb 2023 15:57:45 -0800 Subject: SL-19279: LLGLSLShader::bindXXX is not free. Pack the uniforms --- indra/llprimitive/llgltfmaterial.cpp | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index d0ecf611ff..62b693919c 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -43,26 +43,16 @@ const char* const GLTF_FILE_EXTENSION_TRANSFORM_ROTATION = "rotation"; // special UUID that indicates a null UUID in override data static const LLUUID GLTF_OVERRIDE_NULL_UUID = LLUUID("ffffffff-ffff-ffff-ffff-ffffffffffff"); -// https://github.com/KhronosGroup/glTF/tree/main/extensions/3.0/Khronos/KHR_texture_transform -LLMatrix3 LLGLTFMaterial::TextureTransform::asMatrix() -{ - LLMatrix3 scale; - scale.mMatrix[0][0] = mScale[0]; - scale.mMatrix[1][1] = mScale[1]; - - LLMatrix3 rotation; - const F32 cos_r = cos(mRotation); - const F32 sin_r = sin(mRotation); - rotation.mMatrix[0][0] = cos_r; - rotation.mMatrix[0][1] = -sin_r; - rotation.mMatrix[1][0] = sin_r; - rotation.mMatrix[1][1] = cos_r; - - LLMatrix3 offset; - offset.mMatrix[2][0] = mOffset[0]; - offset.mMatrix[2][1] = mOffset[1]; - - return offset * rotation * scale; +void LLGLTFMaterial::TextureTransform::getPacked(F32 (&packed)[8]) +{ + packed[0] = mScale.mV[VX]; + packed[1] = mScale.mV[VY]; + packed[2] = mRotation; + // packed[3] = unused + packed[4] = mOffset.mV[VX]; + packed[5] = mOffset.mV[VY]; + // packed[6] = unused + // packed[7] = unused } bool LLGLTFMaterial::TextureTransform::operator==(const TextureTransform& other) const -- cgit v1.2.3 From e23b3972a00370aff25d582ce33dc0db6d795213 Mon Sep 17 00:00:00 2001 From: Dave Parks Date: Wed, 22 Mar 2023 18:25:47 -0500 Subject: DRTVWR-559 Fix for bad hashing of materials breaking render batches and who knows what else. --- indra/llprimitive/llgltfmaterial.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 62b693919c..f3aa5b0648 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -692,3 +692,13 @@ void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) } } } + +LLUUID LLGLTFMaterial::getHash() const +{ + LL_PROFILE_ZONE_SCOPED_CATEGORY_TEXTURE; + // HACK - hash the bytes of this object but don't include the ref count + LLUUID hash; + HBXXH128::digest(hash, (unsigned char*)this + sizeof(LLRefCount), sizeof(*this) - sizeof(LLRefCount)); + return hash; +} + -- cgit v1.2.3 From 005a5fa207d158995217cb9ecf5c214c8b2354cc Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Fri, 21 Apr 2023 13:26:24 -0700 Subject: SL-19606: Fix missing GLTF texture transforms in PBR alpha mask/alpha blend shadows --- indra/llprimitive/llgltfmaterial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index f3aa5b0648..c16803d39d 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -43,7 +43,7 @@ const char* const GLTF_FILE_EXTENSION_TRANSFORM_ROTATION = "rotation"; // special UUID that indicates a null UUID in override data static const LLUUID GLTF_OVERRIDE_NULL_UUID = LLUUID("ffffffff-ffff-ffff-ffff-ffffffffffff"); -void LLGLTFMaterial::TextureTransform::getPacked(F32 (&packed)[8]) +void LLGLTFMaterial::TextureTransform::getPacked(F32 (&packed)[8]) const { packed[0] = mScale.mV[VX]; packed[1] = mScale.mV[VY]; -- cgit v1.2.3 From 455bbcf742691b709353aa3c3e35a76d0ff38ee4 Mon Sep 17 00:00:00 2001 From: RunitaiLinden Date: Tue, 29 Aug 2023 16:42:55 -0500 Subject: SL-20229 Add GenericStreamingMessage and use it to receive GLTF material overrides --- indra/llprimitive/llgltfmaterial.cpp | 172 +++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index c16803d39d..8475e7231a 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -27,6 +27,7 @@ #include "linden_common.h" #include "llgltfmaterial.h" +#include "llsdserialize.h" // NOTE -- this should be the one and only place tiny_gltf.h is included #include "tinygltf/tiny_gltf.h" @@ -693,6 +694,177 @@ void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) } } +void LLGLTFMaterial::getOverrideLLSD(const LLGLTFMaterial& override_mat, LLSD& data) +{ + LL_PROFILE_ZONE_SCOPED; + llassert(data.isUndefined()); + + // make every effort to shave bytes here + + for (int i = 0; i < GLTF_TEXTURE_INFO_COUNT; ++i) + { + LLUUID& texture_id = mTextureId[i]; + const LLUUID& override_texture_id = override_mat.mTextureId[i]; + if (override_texture_id.notNull() && override_texture_id != texture_id) + { + data["tex"][i] = LLSD::UUID(override_texture_id); + } + + } + + if (override_mat.mBaseColor != getDefaultBaseColor()) + { + data["bc"] = override_mat.mBaseColor.getValue(); + } + + if (override_mat.mEmissiveColor != getDefaultEmissiveColor()) + { + data["ec"] = override_mat.mEmissiveColor.getValue(); + } + + if (override_mat.mMetallicFactor != getDefaultMetallicFactor()) + { + data["mf"] = override_mat.mMetallicFactor; + } + + if (override_mat.mRoughnessFactor != getDefaultRoughnessFactor()) + { + data["rf"] = override_mat.mRoughnessFactor; + } + + if (override_mat.mAlphaMode != getDefaultAlphaMode() || override_mat.mOverrideAlphaMode) + { + data["am"] = override_mat.mAlphaMode; + } + + if (override_mat.mAlphaCutoff != getDefaultAlphaCutoff()) + { + data["ac"] = override_mat.mAlphaCutoff; + } + + if (override_mat.mDoubleSided != getDefaultDoubleSided() || override_mat.mOverrideDoubleSided) + { + data["ds"] = override_mat.mDoubleSided; + } + + for (int i = 0; i < GLTF_TEXTURE_INFO_COUNT; ++i) + { + if (override_mat.mTextureTransform[i].mOffset != getDefaultTextureOffset()) + { + data["ti"][i]["o"] = override_mat.mTextureTransform[i].mOffset.getValue(); + } + + if (override_mat.mTextureTransform[i].mScale != getDefaultTextureScale()) + { + data["ti"][i]["s"] = override_mat.mTextureTransform[i].mScale.getValue(); + } + + if (override_mat.mTextureTransform[i].mRotation != getDefaultTextureRotation()) + { + data["ti"][i]["r"] = override_mat.mTextureTransform[i].mRotation; + } + } + +#if 0 + { + std::ostringstream ostr; + LLSDSerialize::serialize(data, ostr, LLSDSerialize::LLSD_NOTATION); + std::string param_str(ostr.str()); + LL_INFOS() << param_str << LL_ENDL; + LL_INFOS() << "Notation size: " << param_str.size() << LL_ENDL; + } + + { + std::ostringstream ostr; + LLSDSerialize::serialize(data, ostr, LLSDSerialize::LLSD_BINARY); + std::string param_str(ostr.str()); + LL_INFOS() << "Binary size: " << param_str.size() << LL_ENDL; + } +#endif +} + + +void LLGLTFMaterial::applyOverrideLLSD(const LLSD& data) +{ + const LLSD& tex = data["tex"]; + + if (tex.isArray()) + { + for (int i = 0; i < tex.size(); ++i) + { + mTextureId[i] = tex[i].asUUID(); + } + } + + const LLSD& bc = data["bc"]; + if (bc.isDefined()) + { + mBaseColor.setValue(bc); + } + + const LLSD& ec = data["ec"]; + if (ec.isDefined()) + { + mEmissiveColor.setValue(ec); + } + + const LLSD& mf = data["mf"]; + if (mf.isReal()) + { + mMetallicFactor = mf.asReal(); + } + + const LLSD& rf = data["rf"]; + if (rf.isReal()) + { + mRoughnessFactor = rf.asReal(); + } + + const LLSD& am = data["am"]; + if (am.isInteger()) + { + mAlphaMode = (AlphaMode) am.asInteger(); + } + + const LLSD& ac = data["ac"]; + if (ac.isReal()) + { + mAlphaCutoff = ac.asReal(); + } + + const LLSD& ds = data["ds"]; + if (data.isBoolean()) + { + mDoubleSided = ds.asBoolean(); + mOverrideDoubleSided = true; + } + + const LLSD& ti = data["ti"]; + if (ti.isArray()) + { + for (int i = 0; i < GLTF_TEXTURE_INFO_COUNT; ++i) + { + const LLSD& o = ti[i]["o"]; + if (o.isDefined()) + { + mTextureTransform[i].mOffset.setValue(o); + } + + const LLSD& s = ti[i]["s"]; + if (s.isDefined()) + { + mTextureTransform[i].mScale.setValue(s); + } + + const LLSD& r = ti[i]["r"]; + if (r.isReal()) + { + mTextureTransform[i].mRotation = r.asReal(); + } + } + } +} + LLUUID LLGLTFMaterial::getHash() const { LL_PROFILE_ZONE_SCOPED_CATEGORY_TEXTURE; -- cgit v1.2.3 From 813acc39feec14b0c78fd9f704b358331ff87896 Mon Sep 17 00:00:00 2001 From: RunitaiLinden Date: Fri, 22 Sep 2023 14:23:07 -0500 Subject: SL-20325 Fix for double sided not working. --- indra/llprimitive/llgltfmaterial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 8475e7231a..19b7413934 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -833,7 +833,7 @@ void LLGLTFMaterial::applyOverrideLLSD(const LLSD& data) } const LLSD& ds = data["ds"]; - if (data.isBoolean()) + if (ds.isBoolean()) { mDoubleSided = ds.asBoolean(); mOverrideDoubleSided = true; -- cgit v1.2.3 From d22ea319a51707bdcc0a8cb946143208d8c3f553 Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Mon, 9 Oct 2023 16:05:58 -0700 Subject: SL-20225: LLGLTFMaterial code sync --- indra/llprimitive/llgltfmaterial.cpp | 161 ++++++----------------------------- 1 file changed, 26 insertions(+), 135 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 19b7413934..f42c11ee21 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -24,25 +24,28 @@ * $/LicenseInfo$ */ + #include "linden_common.h" #include "llgltfmaterial.h" + #include "llsdserialize.h" // NOTE -- this should be the one and only place tiny_gltf.h is included #include "tinygltf/tiny_gltf.h" +#include "llgltfmaterial_templates.h" const char* const LLGLTFMaterial::ASSET_VERSION = "1.1"; const char* const LLGLTFMaterial::ASSET_TYPE = "GLTF 2.0"; const std::array LLGLTFMaterial::ACCEPTED_ASSET_VERSIONS = { "1.0", "1.1" }; -const char* const GLTF_FILE_EXTENSION_TRANSFORM = "KHR_texture_transform"; -const char* const GLTF_FILE_EXTENSION_TRANSFORM_SCALE = "scale"; -const char* const GLTF_FILE_EXTENSION_TRANSFORM_OFFSET = "offset"; -const char* const GLTF_FILE_EXTENSION_TRANSFORM_ROTATION = "rotation"; +const char* const LLGLTFMaterial::GLTF_FILE_EXTENSION_TRANSFORM = "KHR_texture_transform"; +const char* const LLGLTFMaterial::GLTF_FILE_EXTENSION_TRANSFORM_SCALE = "scale"; +const char* const LLGLTFMaterial::GLTF_FILE_EXTENSION_TRANSFORM_OFFSET = "offset"; +const char* const LLGLTFMaterial::GLTF_FILE_EXTENSION_TRANSFORM_ROTATION = "rotation"; // special UUID that indicates a null UUID in override data -static const LLUUID GLTF_OVERRIDE_NULL_UUID = LLUUID("ffffffff-ffff-ffff-ffff-ffffffffffff"); +const LLUUID LLGLTFMaterial::GLTF_OVERRIDE_NULL_UUID = LLUUID("ffffffff-ffff-ffff-ffff-ffffffffffff"); void LLGLTFMaterial::TextureTransform::getPacked(F32 (&packed)[8]) const { @@ -68,14 +71,14 @@ LLGLTFMaterial::LLGLTFMaterial(const LLGLTFMaterial& rhs) LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) { - //have to do a manual operator= because of LLRefCount + //have to do a manual operator= because of LLRefCount mTextureId = rhs.mTextureId; mTextureTransform = rhs.mTextureTransform; mBaseColor = rhs.mBaseColor; mEmissiveColor = rhs.mEmissiveColor; - + mMetallicFactor = rhs.mMetallicFactor; mRoughnessFactor = rhs.mRoughnessFactor; mAlphaCutoff = rhs.mAlphaCutoff; @@ -97,7 +100,7 @@ bool LLGLTFMaterial::operator==(const LLGLTFMaterial& rhs) const mBaseColor == rhs.mBaseColor && mEmissiveColor == rhs.mEmissiveColor && - + mMetallicFactor == rhs.mMetallicFactor && mRoughnessFactor == rhs.mRoughnessFactor && mAlphaCutoff == rhs.mAlphaCutoff && @@ -122,6 +125,7 @@ bool LLGLTFMaterial::fromJSON(const std::string& json, std::string& warn_msg, st return true; } + return false; } @@ -190,7 +194,8 @@ void LLGLTFMaterial::setFromModel(const tinygltf::Model& model, S32 mat_index) } } -LLVector2 vec2_from_json(const tinygltf::Value::Object& object, const char* key, const LLVector2& default_value) +// static +LLVector2 LLGLTFMaterial::vec2FromJson(const tinygltf::Value::Object& object, const char* key, const LLVector2& default_value) { const auto it = object.find(key); if (it == object.end()) @@ -215,7 +220,8 @@ LLVector2 vec2_from_json(const tinygltf::Value::Object& object, const char* key, return value; } -F32 float_from_json(const tinygltf::Value::Object& object, const char* key, const F32 default_value) +// static +F32 LLGLTFMaterial::floatFromJson(const tinygltf::Value::Object& object, const char* key, const F32 default_value) { const auto it = object.find(key); if (it == object.end()) @@ -230,52 +236,6 @@ F32 float_from_json(const tinygltf::Value::Object& object, const char* key, cons return (F32)real_json.GetNumberAsDouble(); } -template -std::string gltf_get_texture_image(const tinygltf::Model& model, const T& texture_info) -{ - const S32 texture_idx = texture_info.index; - if (texture_idx < 0 || texture_idx >= model.textures.size()) - { - return ""; - } - const tinygltf::Texture& texture = model.textures[texture_idx]; - - // Ignore texture.sampler for now - - const S32 image_idx = texture.source; - if (image_idx < 0 || image_idx >= model.images.size()) - { - return ""; - } - const tinygltf::Image& image = model.images[image_idx]; - - return image.uri; -} - -// *NOTE: Use template here as workaround for the different similar texture info classes -template -void LLGLTFMaterial::setFromTexture(const tinygltf::Model& model, const T& texture_info, TextureInfo texture_info_id) -{ - LL_PROFILE_ZONE_SCOPED; - const std::string uri = gltf_get_texture_image(model, texture_info); - mTextureId[texture_info_id].set(uri); - - const tinygltf::Value::Object& extensions_object = texture_info.extensions; - const auto transform_it = extensions_object.find(GLTF_FILE_EXTENSION_TRANSFORM); - if (transform_it != extensions_object.end()) - { - const tinygltf::Value& transform_json = std::get<1>(*transform_it); - if (transform_json.IsObject()) - { - const tinygltf::Value::Object& transform_object = transform_json.Get(); - TextureTransform& transform = mTextureTransform[texture_info_id]; - transform.mOffset = vec2_from_json(transform_object, GLTF_FILE_EXTENSION_TRANSFORM_OFFSET, getDefaultTextureOffset()); - transform.mScale = vec2_from_json(transform_object, GLTF_FILE_EXTENSION_TRANSFORM_SCALE, getDefaultTextureScale()); - transform.mRotation = float_from_json(transform_object, GLTF_FILE_EXTENSION_TRANSFORM_ROTATION, getDefaultTextureRotation()); - } - } -} - void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const { LL_PROFILE_ZONE_SCOPED; @@ -302,7 +262,7 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const material_out.alphaMode = getAlphaMode(); material_out.alphaCutoff = mAlphaCutoff; - + mBaseColor.write(material_out.pbrMetallicRoughness.baseColorFactor); if (mEmissiveColor != LLGLTFMaterial::getDefaultEmissiveColor()) @@ -320,7 +280,7 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const tinygltf::Value::Object extras; bool write_extras = false; if (mOverrideAlphaMode && mAlphaMode == getDefaultAlphaMode()) - { + { extras["override_alpha_mode"] = tinygltf::Value(mOverrideAlphaMode); write_extras = true; } @@ -339,57 +299,6 @@ void LLGLTFMaterial::writeToModel(tinygltf::Model& model, S32 mat_index) const model.asset.version = "2.0"; } -template -void gltf_allocate_texture_image(tinygltf::Model& model, T& texture_info, const std::string& uri) -{ - const S32 image_idx = model.images.size(); - model.images.emplace_back(); - model.images[image_idx].uri = uri; - - // The texture, not to be confused with the texture info - const S32 texture_idx = model.textures.size(); - model.textures.emplace_back(); - tinygltf::Texture& texture = model.textures[texture_idx]; - texture.source = image_idx; - - texture_info.index = texture_idx; -} - -template -void LLGLTFMaterial::writeToTexture(tinygltf::Model& model, T& texture_info, TextureInfo texture_info_id, bool force_write) const -{ - LL_PROFILE_ZONE_SCOPED; - const LLUUID& texture_id = mTextureId[texture_info_id]; - const TextureTransform& transform = mTextureTransform[texture_info_id]; - const bool is_blank_transform = transform == sDefault.mTextureTransform[0]; - // Check if this material matches all the fallback values, and if so, then - // skip including it to reduce material size - if (!force_write && texture_id.isNull() && is_blank_transform) - { - return; - } - - // tinygltf will discard this texture info if there is no valid texture, - // causing potential loss of information for overrides, so ensure one is - // defined. -Cosmic,2023-01-30 - gltf_allocate_texture_image(model, texture_info, texture_id.asString()); - - if (!is_blank_transform) - { - tinygltf::Value::Object transform_map; - transform_map[GLTF_FILE_EXTENSION_TRANSFORM_OFFSET] = tinygltf::Value(tinygltf::Value::Array({ - tinygltf::Value(transform.mOffset.mV[VX]), - tinygltf::Value(transform.mOffset.mV[VY]) - })); - transform_map[GLTF_FILE_EXTENSION_TRANSFORM_SCALE] = tinygltf::Value(tinygltf::Value::Array({ - tinygltf::Value(transform.mScale.mV[VX]), - tinygltf::Value(transform.mScale.mV[VY]) - })); - transform_map[GLTF_FILE_EXTENSION_TRANSFORM_ROTATION] = tinygltf::Value(transform.mRotation); - texture_info.extensions[GLTF_FILE_EXTENSION_TRANSFORM] = tinygltf::Value(transform_map); - } -} - void LLGLTFMaterial::sanitizeAssetMaterial() { mTextureTransform = sDefault.mTextureTransform; @@ -403,19 +312,19 @@ bool LLGLTFMaterial::setBaseMaterial() return *this != old_override; } -bool LLGLTFMaterial::isClearedForBaseMaterial() -{ - LLGLTFMaterial cleared_override = sDefault; - cleared_override.setBaseMaterial(*this); - return *this == cleared_override; -} - // For material overrides only. Copies transforms from the old override. void LLGLTFMaterial::setBaseMaterial(const LLGLTFMaterial& old_override_mat) { mTextureTransform = old_override_mat.mTextureTransform; } +bool LLGLTFMaterial::isClearedForBaseMaterial() const +{ + LLGLTFMaterial cleared_override = sDefault; + cleared_override.setBaseMaterial(*this); + return *this == cleared_override; +} + // static void LLGLTFMaterial::hackOverrideUUID(LLUUID& id) @@ -516,7 +425,7 @@ void LLGLTFMaterial::setAlphaMode(const std::string& mode, bool for_override) { m = ALPHA_MODE_BLEND; } - + setAlphaMode(m, for_override); } @@ -709,7 +618,6 @@ void LLGLTFMaterial::getOverrideLLSD(const LLGLTFMaterial& override_mat, LLSD& d { data["tex"][i] = LLSD::UUID(override_texture_id); } - } if (override_mat.mBaseColor != getDefaultBaseColor()) @@ -764,23 +672,6 @@ void LLGLTFMaterial::getOverrideLLSD(const LLGLTFMaterial& override_mat, LLSD& d data["ti"][i]["r"] = override_mat.mTextureTransform[i].mRotation; } } - -#if 0 - { - std::ostringstream ostr; - LLSDSerialize::serialize(data, ostr, LLSDSerialize::LLSD_NOTATION); - std::string param_str(ostr.str()); - LL_INFOS() << param_str << LL_ENDL; - LL_INFOS() << "Notation size: " << param_str.size() << LL_ENDL; - } - - { - std::ostringstream ostr; - LLSDSerialize::serialize(data, ostr, LLSDSerialize::LLSD_BINARY); - std::string param_str(ostr.str()); - LL_INFOS() << "Binary size: " << param_str.size() << LL_ENDL; - } -#endif } -- cgit v1.2.3 From 596a63051ebabfec51e48be02bbec33ab962d915 Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Fri, 27 Oct 2023 23:41:13 +0300 Subject: SL-20523 Local textures not updating on PBR Materials #2 --- indra/llprimitive/llgltfmaterial.cpp | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index f42c11ee21..6afd83904f 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -89,6 +89,11 @@ LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) mOverrideDoubleSided = rhs.mOverrideDoubleSided; mOverrideAlphaMode = rhs.mOverrideAlphaMode; + mLocalTextureIds = rhs.mLocalTextureIds; + mLocalTextureTrackingIds = rhs.mLocalTextureTrackingIds; + + updateTextureTracking(); + return *this; } @@ -765,3 +770,44 @@ LLUUID LLGLTFMaterial::getHash() const return hash; } +void LLGLTFMaterial::addLocalTextureTracking(const LLUUID& tracking_id, const LLUUID& tex_id) +{ + mLocalTextureTrackingIds.insert(tracking_id); + mLocalTextureIds.insert(tex_id); +} + +void LLGLTFMaterial::removeLocalTextureTracking(const LLUUID& tracking_id, const LLUUID& tex_id) +{ + mLocalTextureTrackingIds.erase(tracking_id); + mLocalTextureIds.erase(tex_id); +} + +bool LLGLTFMaterial::replaceLocalTexture(const LLUUID& old_id, const LLUUID& new_id) +{ + bool res = false; + + for (int i = 0; i < GLTF_TEXTURE_INFO_COUNT; ++i) + { + if (mTextureId[i] == old_id) + { + mTextureId[i] = new_id; + res = true; + } + } + + mLocalTextureIds.erase(old_id); + if (res) + { + mLocalTextureIds.insert(new_id); + } + + return res; +} + +void LLGLTFMaterial::updateTextureTracking() +{ + if (mLocalTextureTrackingIds.size() > 0) + { + LL_WARNS() << "copied a material with local textures, but tracking not implemented" << LL_ENDL; + } +} -- cgit v1.2.3 From 3a5b678eba5d86acccb1a1f233f862d292258fac Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Mon, 30 Oct 2023 23:56:33 +0200 Subject: SL-20523 Local textures not updating on PBR Materials #3 --- indra/llprimitive/llgltfmaterial.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 6afd83904f..e590b14656 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -770,6 +770,15 @@ LLUUID LLGLTFMaterial::getHash() const return hash; } +void LLGLTFMaterial::addTextureEntry(LLTextureEntry* te) +{ + mTextureEntires.insert(te); +} +void LLGLTFMaterial::removeTextureEntry(LLTextureEntry* te) +{ + mTextureEntires.erase(te); +} + void LLGLTFMaterial::addLocalTextureTracking(const LLUUID& tracking_id, const LLUUID& tex_id) { mLocalTextureTrackingIds.insert(tracking_id); -- cgit v1.2.3 From 52c60ab3fdb8617471eccd9df52cc126e0243e76 Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Thu, 2 Nov 2023 00:33:39 +0200 Subject: SL-20523 Local textures not updating on PBR Materials #4 --- indra/llprimitive/llgltfmaterial.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index e590b14656..3945be3254 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -89,8 +89,7 @@ LLGLTFMaterial& LLGLTFMaterial::operator=(const LLGLTFMaterial& rhs) mOverrideDoubleSided = rhs.mOverrideDoubleSided; mOverrideAlphaMode = rhs.mOverrideAlphaMode; - mLocalTextureIds = rhs.mLocalTextureIds; - mLocalTextureTrackingIds = rhs.mLocalTextureTrackingIds; + mTrackingIdToLocalTexture = rhs.mTrackingIdToLocalTexture; updateTextureTracking(); @@ -606,6 +605,8 @@ void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) mTextureTransform[i].mRotation = override_mat.mTextureTransform[i].mRotation; } } + + mTrackingIdToLocalTexture.insert(override_mat.mTrackingIdToLocalTexture.begin(), override_mat.mTrackingIdToLocalTexture.begin()); } void LLGLTFMaterial::getOverrideLLSD(const LLGLTFMaterial& override_mat, LLSD& data) @@ -781,17 +782,15 @@ void LLGLTFMaterial::removeTextureEntry(LLTextureEntry* te) void LLGLTFMaterial::addLocalTextureTracking(const LLUUID& tracking_id, const LLUUID& tex_id) { - mLocalTextureTrackingIds.insert(tracking_id); - mLocalTextureIds.insert(tex_id); + mTrackingIdToLocalTexture[tracking_id] = tex_id; } -void LLGLTFMaterial::removeLocalTextureTracking(const LLUUID& tracking_id, const LLUUID& tex_id) +void LLGLTFMaterial::removeLocalTextureTracking(const LLUUID& tracking_id) { - mLocalTextureTrackingIds.erase(tracking_id); - mLocalTextureIds.erase(tex_id); + mTrackingIdToLocalTexture.erase(tracking_id); } -bool LLGLTFMaterial::replaceLocalTexture(const LLUUID& old_id, const LLUUID& new_id) +bool LLGLTFMaterial::replaceLocalTexture(const LLUUID& tracking_id, const LLUUID& old_id, const LLUUID& new_id) { bool res = false; @@ -804,10 +803,13 @@ bool LLGLTFMaterial::replaceLocalTexture(const LLUUID& old_id, const LLUUID& new } } - mLocalTextureIds.erase(old_id); if (res) { - mLocalTextureIds.insert(new_id); + mTrackingIdToLocalTexture[tracking_id] = new_id; + } + else + { + mTrackingIdToLocalTexture.erase(tracking_id); } return res; @@ -815,8 +817,5 @@ bool LLGLTFMaterial::replaceLocalTexture(const LLUUID& old_id, const LLUUID& new void LLGLTFMaterial::updateTextureTracking() { - if (mLocalTextureTrackingIds.size() > 0) - { - LL_WARNS() << "copied a material with local textures, but tracking not implemented" << LL_ENDL; - } + // setTEGLTFMaterialOverride is responsible for tracking } -- cgit v1.2.3 From 0d8893822d8975194313e940914afc8945754a21 Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Thu, 2 Nov 2023 23:49:55 +0200 Subject: SL-20523 Local textures not updating on PBR Materials #5 --- indra/llprimitive/llgltfmaterial.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 3945be3254..390110b0ec 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -607,6 +607,8 @@ void LLGLTFMaterial::applyOverride(const LLGLTFMaterial& override_mat) } mTrackingIdToLocalTexture.insert(override_mat.mTrackingIdToLocalTexture.begin(), override_mat.mTrackingIdToLocalTexture.begin()); + + updateTextureTracking(); } void LLGLTFMaterial::getOverrideLLSD(const LLGLTFMaterial& override_mat, LLSD& data) @@ -771,15 +773,6 @@ LLUUID LLGLTFMaterial::getHash() const return hash; } -void LLGLTFMaterial::addTextureEntry(LLTextureEntry* te) -{ - mTextureEntires.insert(te); -} -void LLGLTFMaterial::removeTextureEntry(LLTextureEntry* te) -{ - mTextureEntires.erase(te); -} - void LLGLTFMaterial::addLocalTextureTracking(const LLUUID& tracking_id, const LLUUID& tex_id) { mTrackingIdToLocalTexture[tracking_id] = tex_id; @@ -818,4 +811,5 @@ bool LLGLTFMaterial::replaceLocalTexture(const LLUUID& tracking_id, const LLUUID void LLGLTFMaterial::updateTextureTracking() { // setTEGLTFMaterialOverride is responsible for tracking + // for material overrides editor will set it } -- cgit v1.2.3 From cc089d88ad5ab9088a5036e1d6f301d87cb3b127 Mon Sep 17 00:00:00 2001 From: Andrey Kleshchev Date: Fri, 3 Nov 2023 21:07:46 +0200 Subject: SL-20523 Ensure override gets updated before render material --- indra/llprimitive/llgltfmaterial.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index 390110b0ec..d2c911a189 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -794,6 +794,10 @@ bool LLGLTFMaterial::replaceLocalTexture(const LLUUID& tracking_id, const LLUUID mTextureId[i] = new_id; res = true; } + else if (mTextureId[i] == new_id) + { + res = true; + } } if (res) -- cgit v1.2.3 From 843866d193a0fb5ea882408c8862335ab9c5539b Mon Sep 17 00:00:00 2001 From: RunitaiLinden Date: Mon, 13 Nov 2023 13:12:48 -0600 Subject: Drtvwr 596 11/8/2023 (#501) * SL-20570 Fix for lossy (and square) normal maps when importing GLTF materials. * SL-20582 Fix for overriding to alpha mode blend not working. Incidental decruft of dead code (thanks, Rye!) --- indra/llprimitive/llgltfmaterial.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'indra/llprimitive/llgltfmaterial.cpp') diff --git a/indra/llprimitive/llgltfmaterial.cpp b/indra/llprimitive/llgltfmaterial.cpp index d2c911a189..ae165f7fa4 100644 --- a/indra/llprimitive/llgltfmaterial.cpp +++ b/indra/llprimitive/llgltfmaterial.cpp @@ -723,6 +723,7 @@ void LLGLTFMaterial::applyOverrideLLSD(const LLSD& data) if (am.isInteger()) { mAlphaMode = (AlphaMode) am.asInteger(); + mOverrideAlphaMode = true; } const LLSD& ac = data["ac"]; -- cgit v1.2.3