Skip to content

Commit d3bc054

Browse files
Google DeepMindcopybara-github
authored andcommitted
Add basic support for geom rgba.
Adds primvar:displayColor/primvar:displayOpacity to the geom prim if its rgba attribute differs from the default (0.5, 0.5, 0.5, 1.0). This sets the color and opacity directly on the prim with no need for an external material, making it consistent with how rgba is used in mujoco. PiperOrigin-RevId: 751013128 Change-Id: I059943ec5b266e62c3696d9a0ac4f2033db9e07f
1 parent 8df3614 commit d3bc054

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/experimental/usd/plugins/mjcf/mujoco_to_usd.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,29 @@ class ModelWriter {
897897
}
898898
}
899899

900+
// If geom rgba is not the default (0.5, 0.5, 0.5, 1), then set the
901+
// displayColor attribute.
902+
// No effort is made to properly handle the interaction between geom rgba
903+
// and the material if both are specified.
904+
if (geom->rgba[0] != 0.5f || geom->rgba[1] != 0.5f ||
905+
geom->rgba[2] != 0.5f || geom->rgba[3] != 1.0f) {
906+
// Set the displayColor attribute.
907+
pxr::SdfPath display_color_attr = CreateAttributeSpec(
908+
data_, geom_path, pxr::UsdGeomTokens->primvarsDisplayColor,
909+
pxr::SdfValueTypeNames->Color3fArray);
910+
SetAttributeDefault(data_, display_color_attr,
911+
pxr::VtArray<pxr::GfVec3f>{
912+
{geom->rgba[0], geom->rgba[1], geom->rgba[2]}});
913+
// Set the displayOpacity attribute, only if the opacity is not 1.
914+
if (geom->rgba[3] != 1.0f) {
915+
pxr::SdfPath display_opacity_attr = CreateAttributeSpec(
916+
data_, geom_path, pxr::UsdGeomTokens->primvarsDisplayOpacity,
917+
pxr::SdfValueTypeNames->FloatArray);
918+
SetAttributeDefault(data_, display_opacity_attr,
919+
pxr::VtArray<float>{geom->rgba[3]});
920+
}
921+
}
922+
900923
if (body_id == kWorldIndex) {
901924
SetPrimKind(data_, geom_path, pxr::KindTokens->component);
902925
}

test/experimental/usd/plugins/mjcf/fixture.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
.Get(&prim_purpose); \
6161
EXPECT_EQ(prim_purpose, purpose); \
6262
}
63+
64+
#define EXPECT_ATTRIBUTE_HAS_VALUE(stage, path) \
65+
EXPECT_TRUE((stage)->GetAttributeAtPath(SdfPath(path)).HasValue());
66+
67+
#define EXPECT_ATTRIBUTE_HAS_NO_VALUE(stage, path) \
68+
EXPECT_FALSE((stage)->GetAttributeAtPath(SdfPath(path)).HasValue());
69+
6370
namespace mujoco {
6471

6572
pxr::SdfLayerRefPtr LoadLayer(

test/experimental/usd/plugins/mjcf/mjcf_file_format_test.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,53 @@ TEST_F(MjcfSdfFileFormatPluginTest, TestMaterials) {
140140
pxr::SdfAssetPath("textures/cube.png"));
141141
}
142142

143+
TEST_F(MjcfSdfFileFormatPluginTest, TestGeomRgba) {
144+
static constexpr char kXml[] = R"(
145+
<mujoco model="test">
146+
<worldbody>
147+
<geom type="sphere" name="sphere_red" size="1" rgba="1 0 0 1"/>
148+
<geom type="sphere" name="sphere_default" size="1"/>
149+
<geom type="sphere" name="sphere_also_default" size="1" rgba="0.5 0.5 0.5 1"/>
150+
<geom type="sphere" name="sphere_almost_default" size="1" rgba="0.5 0.5 0.5 0.9"/>
151+
</worldbody>
152+
</mujoco>
153+
)";
154+
155+
pxr::SdfLayerRefPtr layer = LoadLayer(kXml);
156+
auto stage = pxr::UsdStage::Open(layer);
157+
158+
EXPECT_PRIM_VALID(stage, "/test/sphere_red");
159+
ExpectAttributeEqual(stage, "/test/sphere_red.primvars:displayColor",
160+
pxr::VtArray<pxr::GfVec3f>{{1, 0, 0}});
161+
EXPECT_ATTRIBUTE_HAS_NO_VALUE(stage,
162+
"/test/sphere_red.primvars:displayOpacity");
163+
164+
// There's no mechanism in Mujoco to specify whether an attribute was set
165+
// explicitly or not. We do the same as Mujoco does, which is to compare with
166+
// the default value.
167+
// Which explains why not setting rgba is the same as setting it to the
168+
// default value of (0.5, 0.5, 0.5, 1).
169+
EXPECT_PRIM_VALID(stage, "/test/sphere_default");
170+
EXPECT_ATTRIBUTE_HAS_NO_VALUE(stage,
171+
"/test/sphere_default.primvars:displayColor");
172+
EXPECT_ATTRIBUTE_HAS_NO_VALUE(stage,
173+
"/test/sphere_default.primvars:displayOpacity");
174+
175+
EXPECT_PRIM_VALID(stage, "/test/sphere_also_default");
176+
EXPECT_ATTRIBUTE_HAS_NO_VALUE(
177+
stage, "/test/sphere_also_default.primvars:displayColor");
178+
EXPECT_ATTRIBUTE_HAS_NO_VALUE(
179+
stage, "/test/sphere_also_default.primvars:displayOpacity");
180+
181+
EXPECT_PRIM_VALID(stage, "/test/sphere_almost_default");
182+
ExpectAttributeEqual(stage,
183+
"/test/sphere_almost_default.primvars:displayColor",
184+
pxr::VtArray<pxr::GfVec3f>{{0.5, 0.5, 0.5}});
185+
ExpectAttributeEqual(stage,
186+
"/test/sphere_almost_default.primvars:displayOpacity",
187+
pxr::VtArray<float>{0.9});
188+
}
189+
143190
TEST_F(MjcfSdfFileFormatPluginTest, TestFaceVaryingMeshSourcesSimpleMjcfMesh) {
144191
static constexpr char kXml[] = R"(
145192
<mujoco model="mesh test">

0 commit comments

Comments
 (0)