Skip to content

Commit a5cb37d

Browse files
committed
Prevent material destruction if setting the same material to a submesh (#1168)
Signed-off-by: Ian Chen <[email protected]> (cherry picked from commit 546f5dc)
1 parent 48bb874 commit a5cb37d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

include/gz/rendering/base/BaseMesh.hh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,13 @@ namespace gz
308308
subMesh->SetMaterial(_material, false);
309309
}
310310

311+
// If the same material is being set, return early and don't try
312+
// to destroy the material. We still need call SetMaterial on the
313+
// submeshes in case the user changed some material properties
314+
// before setting it back.
315+
if (!_unique && _material == this->material)
316+
return;
317+
311318
if (this->material && this->ownsMaterial)
312319
this->Scene()->DestroyMaterial(this->material);
313320

@@ -441,6 +448,13 @@ namespace gz
441448

442449
this->SetMaterialImpl(_material);
443450

451+
// If the same material is being set, return early and don't try
452+
// to destroy the material. We still need to call SetMaterialImpl
453+
// above in case the user changed some material properties
454+
// before setting it back.
455+
if (!_unique && _material == origMaterial)
456+
return;
457+
444458
if (origMaterial && origUnique)
445459
this->Scene()->DestroyMaterial(origMaterial);
446460

test/common_test/Mesh_TEST.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ TEST_F(MeshTest, MeshSubMesh)
8181
submesh->SetMaterial(matClone, true);
8282
EXPECT_NE(matClone, submesh->Material());
8383

84+
// Setting the same material but do not clone
85+
MaterialPtr newMatClone = submesh->Material();
86+
submesh->SetMaterial(newMatClone, false);
87+
EXPECT_EQ(newMatClone, submesh->Material());
88+
8489
// Clean up
8590
engine->DestroyScene(scene);
8691
}
@@ -309,6 +314,11 @@ TEST_F(MeshTest, MeshClone)
309314
false);
310315
}
311316

317+
// Setting the same material but do not clone
318+
MaterialPtr newMatClone = clonedMesh->Material();
319+
clonedMesh->SetMaterial(newMatClone, false);
320+
EXPECT_EQ(newMatClone, clonedMesh->Material());
321+
312322
// Clean up
313323
engine->DestroyScene(scene);
314324
}

0 commit comments

Comments
 (0)