Skip to content

Commit 69d7c0f

Browse files
cosmic-lindenakleshchev
authored andcommitted
#2570: Fix baked texture shared between avatars when in PBR material
1 parent eb9cb1a commit 69d7c0f

File tree

4 files changed

+62
-33
lines changed

4 files changed

+62
-33
lines changed

indra/newview/llfetchedgltfmaterial.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ void LLFetchedGLTFMaterial::updateTextureTracking()
222222
}
223223
}
224224

225+
void LLFetchedGLTFMaterial::clearFetchedTextures()
226+
{
227+
mBaseColorTexture = nullptr;
228+
mNormalTexture = nullptr;
229+
mMetallicRoughnessTexture = nullptr;
230+
mEmissiveTexture = nullptr;
231+
}
232+
225233
void LLFetchedGLTFMaterial::materialBegin()
226234
{
227235
llassert(!mFetching);

indra/newview/llfetchedgltfmaterial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class LLFetchedGLTFMaterial: public LLGLTFMaterial
6767
LLPointer<LLViewerFetchedTexture> mNormalTexture;
6868
LLPointer<LLViewerFetchedTexture> mMetallicRoughnessTexture;
6969
LLPointer<LLViewerFetchedTexture> mEmissiveTexture;
70+
void clearFetchedTextures();
7071

7172
std::set<LLTextureEntry*> mTextureEntires;
7273

indra/newview/llviewerobject.cpp

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5090,11 +5090,10 @@ void LLViewerObject::setNumTEs(const U8 num_tes)
50905090
if (base_material && override_material)
50915091
{
50925092
tep->setGLTFMaterialOverride(new LLGLTFMaterial(*override_material));
5093-
5094-
LLGLTFMaterial* render_material = new LLFetchedGLTFMaterial();
5095-
*render_material = *base_material;
5096-
render_material->applyOverride(*override_material);
5097-
tep->setGLTFRenderMaterial(render_material);
5093+
}
5094+
if (base_material)
5095+
{
5096+
initRenderMaterial(i);
50985097
}
50995098
}
51005099
}
@@ -5270,6 +5269,9 @@ void LLViewerObject::updateTEMaterialTextures(U8 te)
52705269
});
52715270
}
52725271
getTE(te)->setGLTFMaterial(mat);
5272+
initRenderMaterial(te);
5273+
mat = (LLFetchedGLTFMaterial*) getTE(te)->getGLTFRenderMaterial();
5274+
llassert(mat == nullptr || dynamic_cast<LLFetchedGLTFMaterial*>(getTE(te)->getGLTFRenderMaterial()) != nullptr);
52735275
}
52745276
else if (mat_id.isNull() && mat != nullptr)
52755277
{
@@ -5659,6 +5661,42 @@ S32 LLViewerObject::setTEMaterialParams(const U8 te, const LLMaterialPtr pMateri
56595661
return retval;
56605662
}
56615663

5664+
// Set render material if there are overrides or if the base material is has a
5665+
// baked texture. Otherwise, set it to null.
5666+
// If you are setting the material override and not sending an update message,
5667+
// you should probably call this function.
5668+
S32 LLViewerObject::initRenderMaterial(U8 te)
5669+
{
5670+
LL_PROFILE_ZONE_SCOPED;
5671+
5672+
LLTextureEntry* tep = getTE(te);
5673+
if (!tep) { return 0; }
5674+
const LLFetchedGLTFMaterial* base_material = static_cast<LLFetchedGLTFMaterial*>(tep->getGLTFMaterial());
5675+
llassert(base_material);
5676+
if (!base_material) { return 0; }
5677+
const LLGLTFMaterial* override_material = tep->getGLTFMaterialOverride();
5678+
LLFetchedGLTFMaterial* render_material = nullptr;
5679+
bool need_render_material = override_material;
5680+
if (!need_render_material)
5681+
{
5682+
for (const LLUUID& texture_id : base_material->mTextureId)
5683+
{
5684+
if (LLAvatarAppearanceDefines::LLAvatarAppearanceDictionary::isBakedImageId(texture_id))
5685+
{
5686+
need_render_material = true;
5687+
break;
5688+
}
5689+
}
5690+
}
5691+
if (need_render_material)
5692+
{
5693+
render_material = new LLFetchedGLTFMaterial(*base_material);
5694+
if (override_material) { render_material->applyOverride(*override_material); }
5695+
render_material->clearFetchedTextures();
5696+
}
5697+
return tep->setGLTFRenderMaterial(render_material);
5698+
}
5699+
56625700
S32 LLViewerObject::setTEGLTFMaterialOverride(U8 te, LLGLTFMaterial* override_mat)
56635701
{
56645702
LL_PROFILE_ZONE_SCOPED;
@@ -5692,22 +5730,13 @@ S32 LLViewerObject::setTEGLTFMaterialOverride(U8 te, LLGLTFMaterial* override_ma
56925730

56935731
if (retval)
56945732
{
5733+
retval = initRenderMaterial(te) | retval;
56955734
if (override_mat)
56965735
{
5697-
LLFetchedGLTFMaterial* render_mat = new LLFetchedGLTFMaterial(*src_mat);
5698-
render_mat->applyOverride(*override_mat);
5699-
tep->setGLTFRenderMaterial(render_mat);
5700-
retval = TEM_CHANGE_TEXTURE;
5701-
57025736
for (LLGLTFMaterial::local_tex_map_t::value_type &val : override_mat->mTrackingIdToLocalTexture)
57035737
{
57045738
LLLocalBitmapMgr::getInstance()->associateGLTFMaterial(val.first, override_mat);
57055739
}
5706-
5707-
}
5708-
else if (tep->setGLTFRenderMaterial(nullptr))
5709-
{
5710-
retval = TEM_CHANGE_TEXTURE;
57115740
}
57125741
}
57135742

@@ -7577,25 +7606,15 @@ void LLViewerObject::setRenderMaterialID(S32 te_in, const LLUUID& id, bool updat
75777606
// the overrides have not changed due to being only texture
75787607
// transforms. Re-apply the overrides to the render material here,
75797608
// if present.
7580-
const LLGLTFMaterial* override_material = tep->getGLTFMaterialOverride();
7581-
if (override_material)
7609+
// Also, sometimes, the material has baked textures, which requires
7610+
// a copy unique to this object.
7611+
// Currently, we do not deduplicate render materials.
7612+
new_material->onMaterialComplete([obj_id = getID(), te]()
75827613
{
7583-
new_material->onMaterialComplete([obj_id = getID(), te]()
7584-
{
7585-
LLViewerObject* obj = gObjectList.findObject(obj_id);
7586-
if (!obj) { return; }
7587-
LLTextureEntry* tep = obj->getTE(te);
7588-
if (!tep) { return; }
7589-
const LLGLTFMaterial* new_material = tep->getGLTFMaterial();
7590-
if (!new_material) { return; }
7591-
const LLGLTFMaterial* override_material = tep->getGLTFMaterialOverride();
7592-
if (!override_material) { return; }
7593-
LLGLTFMaterial* render_material = new LLFetchedGLTFMaterial();
7594-
*render_material = *new_material;
7595-
render_material->applyOverride(*override_material);
7596-
tep->setGLTFRenderMaterial(render_material);
7597-
});
7598-
}
7614+
LLViewerObject* obj = gObjectList.findObject(obj_id);
7615+
if (!obj) { return; }
7616+
obj->initRenderMaterial(te);
7617+
});
75997618
}
76007619
}
76017620

indra/newview/llviewerobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ class LLViewerObject
392392
/*virtual*/ S32 setTEGlow(const U8 te, const F32 glow);
393393
/*virtual*/ S32 setTEMaterialID(const U8 te, const LLMaterialID& pMaterialID);
394394
/*virtual*/ S32 setTEMaterialParams(const U8 te, const LLMaterialPtr pMaterialParams);
395+
S32 initRenderMaterial(const U8 te);
395396
virtual S32 setTEGLTFMaterialOverride(U8 te, LLGLTFMaterial* mat);
396397

397398
// Used by Materials update functions to properly kick off rebuilds

0 commit comments

Comments
 (0)