Skip to content

Commit cc84959

Browse files
committed
fix: preserve texture transforms when switching materials
1 parent 7e7d45e commit cc84959

File tree

6 files changed

+98
-23
lines changed

6 files changed

+98
-23
lines changed

indra/llprimitive/llgltfmaterial.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,3 +923,34 @@ void LLGLTFMaterial::updateTextureTracking()
923923
// setTEGLTFMaterialOverride is responsible for tracking
924924
// for material overrides editor will set it
925925
}
926+
927+
void LLGLTFMaterial::convertTextureTransformToPBR(
928+
F32 tex_scale_s,
929+
F32 tex_scale_t,
930+
F32 tex_offset_s,
931+
F32 tex_offset_t,
932+
F32 tex_rotation,
933+
LLVector2& pbr_scale,
934+
LLVector2& pbr_offset,
935+
F32& pbr_rotation)
936+
{
937+
pbr_scale.set(tex_scale_s, tex_scale_t);
938+
pbr_rotation = -(tex_rotation) / 2.f;
939+
const F32 adjusted_offset_s = tex_offset_s;
940+
const F32 adjusted_offset_t = -tex_offset_t;
941+
F32 center_adjust_s = 0.5f * (1.0f - tex_scale_s);
942+
F32 center_adjust_t = 0.5f * (1.0f - tex_scale_t);
943+
944+
if (pbr_rotation != 0.0f)
945+
{
946+
const F32 c = cosf(pbr_rotation);
947+
const F32 s = sinf(pbr_rotation);
948+
const F32 tmp_s = center_adjust_s * c - center_adjust_t * s;
949+
const F32 tmp_t = center_adjust_s * s + center_adjust_t * c;
950+
center_adjust_s = tmp_s;
951+
center_adjust_t = tmp_t;
952+
}
953+
954+
pbr_offset.set(adjusted_offset_s + center_adjust_s,
955+
adjusted_offset_t + center_adjust_t);
956+
}

indra/llprimitive/llgltfmaterial.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ class LLGLTFMaterial : public LLRefCount
214214
bool hasLocalTextures() { return !mTrackingIdToLocalTexture.empty(); }
215215
virtual bool replaceLocalTexture(const LLUUID& tracking_id, const LLUUID &old_id, const LLUUID& new_id);
216216
virtual void updateTextureTracking();
217+
218+
// Convert legacy TE transform values to PBR transform values.
219+
static void convertTextureTransformToPBR(F32 tex_scale_s, F32 tex_scale_t,
220+
F32 tex_offset_s, F32 tex_offset_t,
221+
F32 tex_rotation,
222+
LLVector2& pbr_scale,
223+
LLVector2& pbr_offset,
224+
F32& pbr_rotation);
217225
protected:
218226
static LLVector2 vec2FromJson(const std::map<std::string, tinygltf::Value>& object, const char* key, const LLVector2& default_value);
219227
static F32 floatFromJson(const std::map<std::string, tinygltf::Value>& object, const char* key, const F32 default_value);

indra/newview/llgltfmateriallist.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,24 +371,19 @@ void LLGLTFMaterialList::queueApply(const LLViewerObject* obj, S32 side, const L
371371
{
372372
if (asset_id.isNull() || material_override == nullptr)
373373
{
374-
queueApply(obj, side, asset_id);
375-
}
376-
else
377-
{
378-
LLGLTFMaterial* material = new LLGLTFMaterial(*material_override);
379-
sApplyQueue.push_back({ obj->getID(), side, asset_id, material });
374+
return;
380375
}
381376

382-
if (sUpdates.size() >= MAX_TASK_UPDATES)
377+
// If face already has this asset id, avoid re-applying the same id; just modify override
378+
if (obj && obj->getRenderMaterialID(side) == asset_id)
383379
{
384-
LLCoros::instance().launch("modifyMaterialCoro",
385-
std::bind(&LLGLTFMaterialList::modifyMaterialCoro,
386-
gAgent.getRegionCapability("ModifyMaterialParams"),
387-
sUpdates,
388-
std::shared_ptr<CallbackHolder>(nullptr)));
389-
390-
sUpdates = LLSD::emptyArray();
380+
queueModify(obj, side, material_override);
381+
return;
391382
}
383+
384+
// Queue an apply with the provided override (make a copy for the queue)
385+
LLGLTFMaterial* override_copy = new LLGLTFMaterial(*material_override);
386+
sApplyQueue.push_back({ obj->getID(), side, asset_id, override_copy });
392387
}
393388

394389
void LLGLTFMaterialList::queueUpdate(const LLSD& data)

indra/newview/llpanelface.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,6 +3225,18 @@ void LLPanelFace::onCommitPbr()
32253225
{
32263226
id = mPBRTextureCtrl->getImageAssetID();
32273227
}
3228+
3229+
// This avoids calling queueApply with the OLD id, which may drop overrides server-side.
3230+
bool identical_pbr = false;
3231+
bool has_pbr_material = false;
3232+
bool has_faces_without_pbr = false;
3233+
LLUUID current_pbr_id;
3234+
LLSelectedTE::getPbrMaterialId(current_pbr_id, identical_pbr, has_pbr_material, has_faces_without_pbr);
3235+
if (identical_pbr && current_pbr_id == id)
3236+
{
3237+
return; // nothing changed
3238+
}
3239+
32283240
if (!LLSelectMgr::getInstance()->selectionSetGLTFMaterial(id))
32293241
{
32303242
// If failed to set material, refresh mPBRTextureCtrl's value

indra/newview/llselectmgr.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,14 @@ bool LLSelectMgr::selectionSetGLTFMaterial(const LLUUID& mat_id)
19971997
}
19981998
}
19991999

2000+
// If this face already has the target material ID, do nothing.
2001+
// This prevents re-sending the same ID on OK, which can cause the server
2002+
// to drop overrides when queueApply is invoked with the OLD id.
2003+
if (objectp->getRenderMaterialID(te) == asset_id)
2004+
{
2005+
return true;
2006+
}
2007+
20002008
// Preserve existing texture transforms when switching to PBR material
20012009
LLTextureEntry* tep = objectp->getTE(te);
20022010
bool should_preserve_transforms = false;
@@ -2047,9 +2055,16 @@ bool LLSelectMgr::selectionSetGLTFMaterial(const LLUUID& mat_id)
20472055
preserved_override = new LLGLTFMaterial();
20482056
for (U32 i = 0; i < LLGLTFMaterial::GLTF_TEXTURE_INFO_COUNT; ++i)
20492057
{
2050-
preserved_override->mTextureTransform[i].mScale.set(existing_scale_s, existing_scale_t);
2051-
preserved_override->mTextureTransform[i].mOffset.set(existing_offset_s, existing_offset_t);
2052-
preserved_override->mTextureTransform[i].mRotation = existing_rotation;
2058+
LLVector2 pbr_scale, pbr_offset;
2059+
F32 pbr_rotation;
2060+
LLGLTFMaterial::convertTextureTransformToPBR(
2061+
existing_scale_s, existing_scale_t,
2062+
existing_offset_s, existing_offset_t,
2063+
existing_rotation,
2064+
pbr_scale, pbr_offset, pbr_rotation);
2065+
preserved_override->mTextureTransform[i].mScale = pbr_scale;
2066+
preserved_override->mTextureTransform[i].mOffset = pbr_offset;
2067+
preserved_override->mTextureTransform[i].mRotation = pbr_rotation;
20532068
}
20542069
should_preserve_transforms = true;
20552070
}

indra/newview/lltooldraganddrop.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,9 +1365,16 @@ void LLToolDragAndDrop::dropMaterialOneFace(LLViewerObject* hit_obj,
13651365
// Transfer texture entry transforms to all PBR texture channels
13661366
for (U32 i = 0; i < LLGLTFMaterial::GLTF_TEXTURE_INFO_COUNT; ++i)
13671367
{
1368-
preserved_override->mTextureTransform[i].mScale.set(existing_scale_s, existing_scale_t);
1369-
preserved_override->mTextureTransform[i].mOffset.set(existing_offset_s, existing_offset_t);
1370-
preserved_override->mTextureTransform[i].mRotation = existing_rotation;
1368+
LLVector2 pbr_scale, pbr_offset;
1369+
F32 pbr_rotation;
1370+
LLGLTFMaterial::convertTextureTransformToPBR(
1371+
existing_scale_s, existing_scale_t,
1372+
existing_offset_s, existing_offset_t,
1373+
existing_rotation,
1374+
pbr_scale, pbr_offset, pbr_rotation);
1375+
preserved_override->mTextureTransform[i].mScale = pbr_scale;
1376+
preserved_override->mTextureTransform[i].mOffset = pbr_offset;
1377+
preserved_override->mTextureTransform[i].mRotation = pbr_rotation;
13711378
}
13721379
}
13731380
}
@@ -1475,9 +1482,16 @@ void LLToolDragAndDrop::dropMaterialAllFaces(LLViewerObject* hit_obj,
14751482
preserved_override = new LLGLTFMaterial();
14761483
for (U32 i = 0; i < LLGLTFMaterial::GLTF_TEXTURE_INFO_COUNT; ++i)
14771484
{
1478-
preserved_override->mTextureTransform[i].mScale.set(existing_scale_s, existing_scale_t);
1479-
preserved_override->mTextureTransform[i].mOffset.set(existing_offset_s, existing_offset_t);
1480-
preserved_override->mTextureTransform[i].mRotation = existing_rotation;
1485+
LLVector2 pbr_scale, pbr_offset;
1486+
F32 pbr_rotation;
1487+
LLGLTFMaterial::convertTextureTransformToPBR(
1488+
existing_scale_s, existing_scale_t,
1489+
existing_offset_s, existing_offset_t,
1490+
existing_rotation,
1491+
pbr_scale, pbr_offset, pbr_rotation);
1492+
preserved_override->mTextureTransform[i].mScale = pbr_scale;
1493+
preserved_override->mTextureTransform[i].mOffset = pbr_offset;
1494+
preserved_override->mTextureTransform[i].mRotation = pbr_rotation;
14811495
}
14821496
should_preserve = true;
14831497
}

0 commit comments

Comments
 (0)