From f8f119e4a8b6d3c1f106f5e290d1da187fd9aa17 Mon Sep 17 00:00:00 2001
From: Andrey Kleshchev <andreykproductengine@lindenlab.com>
Date: Thu, 4 May 2023 22:46:24 +0300
Subject: [PATCH] SL-18932 Canceling material selection only reverts override

---
 indra/newview/llgltfmateriallist.cpp | 15 ++++++++++++-
 indra/newview/llgltfmateriallist.h   |  7 ++++++
 indra/newview/llselectmgr.cpp        | 32 ++++++++++------------------
 indra/newview/llselectmgr.h          |  3 +--
 indra/newview/lltooldraganddrop.cpp  |  5 ++++-
 5 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/indra/newview/llgltfmateriallist.cpp b/indra/newview/llgltfmateriallist.cpp
index ed16a1cf7ac..99a052f7190 100644
--- a/indra/newview/llgltfmateriallist.cpp
+++ b/indra/newview/llgltfmateriallist.cpp
@@ -433,6 +433,19 @@ void LLGLTFMaterialList::queueApply(const LLViewerObject* obj, S32 side, const L
     }
 }
 
+void LLGLTFMaterialList::queueApply(const LLViewerObject* obj, S32 side, const LLUUID& asset_id, const LLGLTFMaterial* material_override)
+{
+    if (asset_id.isNull() || material_override == nullptr)
+    {
+        queueApply(obj, side, asset_id);
+    }
+    else
+    {
+        LLGLTFMaterial* material = new LLGLTFMaterial(*material_override);
+        sApplyQueue.push_back({ obj->getID(), side, asset_id, material });
+    }
+}
+
 void LLGLTFMaterialList::queueUpdate(const LLSD& data)
 {
     llassert(is_valid_update(data));
@@ -477,7 +490,7 @@ void LLGLTFMaterialList::flushUpdates(void(*done_callback)(bool))
     }
     sModifyQueue.clear();
 
-    for (auto& e : sApplyQueue)
+    for (ApplyMaterialAssetData& e : sApplyQueue)
     {
         data[i]["object_id"] = e.object_id;
         data[i]["side"] = e.side;
diff --git a/indra/newview/llgltfmateriallist.h b/indra/newview/llgltfmateriallist.h
index 85e60aa17f3..ce8781babac 100644
--- a/indra/newview/llgltfmateriallist.h
+++ b/indra/newview/llgltfmateriallist.h
@@ -70,6 +70,13 @@ class LLGLTFMaterialList
     // NOTE: Implicitly clears most override data if present
     static void queueApply(const LLViewerObject* obj, S32 side, const LLUUID& asset_id);
 
+    // Queue an application of a material asset we want to send to the simulator.  Call "flushUpdates" to flush pending updates.
+    //  object_id - ID of object to apply material asset to
+    //  side - TextureEntry index to apply material to, or -1 for all sides
+    //  asset_id - ID of material asset to apply, or LLUUID::null to disassociate current material asset
+    //  mat - override material, if null, will clear most override data
+    static void queueApply(const LLViewerObject* obj, S32 side, const LLUUID& asset_id, const LLGLTFMaterial* mat);
+
     // flush pending material updates to the simulator
     // Automatically called once per frame, but may be called explicitly
     // for cases that care about the done_callback forwarded to LLCoros::instance().launch
diff --git a/indra/newview/llselectmgr.cpp b/indra/newview/llselectmgr.cpp
index 045972b7a87..71dcc56197f 100644
--- a/indra/newview/llselectmgr.cpp
+++ b/indra/newview/llselectmgr.cpp
@@ -2188,8 +2188,8 @@ void LLSelectMgr::selectionRevertGLTFMaterials()
                 // Enqueue update to server
                 if (asset_id.notNull())
                 {
-                    // Restore overrides
-                    LLGLTFMaterialList::queueModify(objectp, te, nodep->mSavedGLTFOverrideMaterials[te]);
+                    // Restore overrides and base material
+                    LLGLTFMaterialList::queueApply(objectp, te, asset_id, nodep->mSavedGLTFOverrideMaterials[te]);
                 } 
                 else
                 {
@@ -5797,7 +5797,7 @@ void LLSelectMgr::processObjectProperties(LLMessageSystem* msg, void** user_data
                 if (can_copy && can_transfer && node->getObject()->getVolume())
                 {
                     uuid_vec_t material_ids;
-                    gltf_materials_vec_t materials;
+                    gltf_materials_vec_t override_materials;
                     LLVOVolume* vobjp = (LLVOVolume*)node->getObject();
                     for (int i = 0; i < vobjp->getNumTEs(); ++i)
                     {
@@ -5812,18 +5812,16 @@ void LLSelectMgr::processObjectProperties(LLMessageSystem* msg, void** user_data
                         if (old_override)
                         {
                             LLPointer<LLGLTFMaterial> mat = new LLGLTFMaterial(*old_override);
-                            materials.push_back(mat);
+                            override_materials.push_back(mat);
                         }
                         else
                         {
-                            materials.push_back(nullptr);
+                            override_materials.push_back(nullptr);
                         }
                     }
-                    node->saveGLTFMaterialIds(material_ids);
-
                     // processObjectProperties does not include overrides so this
                     // might need to be moved to LLGLTFMaterialOverrideDispatchHandler
-                    node->saveGLTFOverrideMaterials(materials);
+                    node->saveGLTFMaterials(material_ids, override_materials);
                 }
 			}
 
@@ -6576,8 +6574,7 @@ LLSelectNode::LLSelectNode(const LLSelectNode& nodep)
 	}
 	
 	saveTextures(nodep.mSavedTextures);
-    saveGLTFMaterialIds(nodep.mSavedGLTFMaterialIds);
-    saveGLTFOverrideMaterials(nodep.mSavedGLTFOverrideMaterials);
+    saveGLTFMaterials(nodep.mSavedGLTFMaterialIds, nodep.mSavedGLTFOverrideMaterials);
 }
 
 LLSelectNode::~LLSelectNode()
@@ -6711,28 +6708,21 @@ void LLSelectNode::saveTextures(const uuid_vec_t& textures)
 	}
 }
 
-void LLSelectNode::saveGLTFMaterialIds(const uuid_vec_t& materials)
+void LLSelectNode::saveGLTFMaterials(const uuid_vec_t& materials, const gltf_materials_vec_t& override_materials)
 {
     if (mObject.notNull())
     {
         mSavedGLTFMaterialIds.clear();
+        mSavedGLTFOverrideMaterials.clear();
 
         for (uuid_vec_t::const_iterator materials_it = materials.begin();
             materials_it != materials.end(); ++materials_it)
         {
             mSavedGLTFMaterialIds.push_back(*materials_it);
         }
-    }
-}
-
-void LLSelectNode::saveGLTFOverrideMaterials(const gltf_materials_vec_t& materials)
-{
-    if (mObject.notNull())
-    {
-        mSavedGLTFOverrideMaterials.clear();
 
-        for (gltf_materials_vec_t::const_iterator mat_it = materials.begin();
-            mat_it != materials.end(); ++mat_it)
+        for (gltf_materials_vec_t::const_iterator mat_it = override_materials.begin();
+            mat_it != override_materials.end(); ++mat_it)
         {
             mSavedGLTFOverrideMaterials.push_back(*mat_it);
         }
diff --git a/indra/newview/llselectmgr.h b/indra/newview/llselectmgr.h
index 3ee78f9a582..ca9a32f0db7 100644
--- a/indra/newview/llselectmgr.h
+++ b/indra/newview/llselectmgr.h
@@ -197,8 +197,7 @@ class LLSelectNode
     // final gltf material that users see.
     // Ids get applied and restored by tools floater,
     // overrides get applied in live material editor
-    void saveGLTFMaterialIds(const uuid_vec_t& materials);
-    void saveGLTFOverrideMaterials(const gltf_materials_vec_t& materials);
+    void saveGLTFMaterials(const uuid_vec_t& materials, const gltf_materials_vec_t& override_materials);
 
 	BOOL allowOperationOnNode(PermissionBit op, U64 group_proxy_power) const;
 
diff --git a/indra/newview/lltooldraganddrop.cpp b/indra/newview/lltooldraganddrop.cpp
index 1b19ba33a35..6633951db39 100644
--- a/indra/newview/lltooldraganddrop.cpp
+++ b/indra/newview/lltooldraganddrop.cpp
@@ -2150,12 +2150,14 @@ EAcceptance LLToolDragAndDrop::dad3dApplyToObject(
                 if (nodep)
                 {
                     uuid_vec_t material_ids;
+                    gltf_materials_vec_t override_materials;
                     S32 num_faces = obj->getNumTEs();
                     for (S32 face = 0; face < num_faces; face++)
                     {
                         material_ids.push_back(obj->getRenderMaterialID(face));
+                        override_materials.push_back(nullptr);
                     }
-                    nodep->saveGLTFMaterialIds(material_ids);
+                    nodep->saveGLTFMaterials(material_ids, override_materials);
                 }
             }
             else
@@ -2169,6 +2171,7 @@ EAcceptance LLToolDragAndDrop::dad3dApplyToObject(
                     && nodep->mSavedGLTFMaterialIds.size() > face)
                 {
                     nodep->mSavedGLTFMaterialIds[face] = obj->getRenderMaterialID(face);
+                    nodep->mSavedGLTFOverrideMaterials[face] = nullptr;
                 }
             }
         }
-- 
GitLab