Add support for separate occlusion, roughness and metallic textures

PiperOrigin-RevId: 775632047
Change-Id: I40ba0b3d4ecb0c0a03534b8d93f85d279febbc9c
This commit is contained in:
Google DeepMind
2025-06-25 04:36:18 -07:00
committed by Copybara-Service
parent 81d16b7ccc
commit da831f5d3e
6 changed files with 134 additions and 38 deletions
@@ -757,21 +757,29 @@ class ModelWriter {
AddUVTextureShader(material_path, pxr::TfToken("uvmap"));
const mjStringVec &textures = *(material->textures);
// Set the values of metallic and roughness. These can come from an ORM
// texture or as a value defined in mjsMaterial_. Occlusion is only present
// in the ORM texture.
// Set the values of metallic, roughness and occlusion. These can come from
// an ORM packed texture, as individual textures, or as a values defined in
// mjsMaterial_ (with the exception of occlusion).
pxr::SdfPath metallic_attr = CreateAttributeSpec(
data_, preview_surface_shader_path, kTokens->inputsMetallic,
pxr::SdfValueTypeNames->Float);
pxr::SdfPath roughness_attr = CreateAttributeSpec(
data_, preview_surface_shader_path, kTokens->inputsRoughness,
pxr::SdfValueTypeNames->Float);
// Find the ORM (occlusion, roughness, metallic) packed-channel texture if
// specified.
// Find the occlusion, roughness, and metallic textures.
if (mjTEXROLE_ORM < textures.size()) {
std::string orm_texture_name = textures[mjTEXROLE_ORM];
mjsTexture *orm_texture = mjs_asTexture(
mjs_findElement(spec_, mjOBJ_TEXTURE, orm_texture_name.c_str()));
std::string occlusion_texture_name = textures[mjTEXROLE_OCCLUSION];
mjsTexture *occlusion_texture = mjs_asTexture(mjs_findElement(
spec_, mjOBJ_TEXTURE, occlusion_texture_name.c_str()));
std::string roughness_texture_name = textures[mjTEXROLE_ROUGHNESS];
mjsTexture *roughness_texture = mjs_asTexture(mjs_findElement(
spec_, mjOBJ_TEXTURE, roughness_texture_name.c_str()));
std::string metallic_texture_name = textures[mjTEXROLE_METALLIC];
mjsTexture *metallic_texture = mjs_asTexture(
mjs_findElement(spec_, mjOBJ_TEXTURE, metallic_texture_name.c_str()));
if (orm_texture) {
// Create the ORM shader and connect its output to the preview
// surface ORM attrs.
@@ -788,8 +796,43 @@ class ModelWriter {
AddAttributeConnection(data_, metallic_attr, orm_output_attrs[2]);
}
} else {
SetAttributeDefault(data_, metallic_attr, material->metallic);
SetAttributeDefault(data_, roughness_attr, material->roughness);
if (metallic_texture) {
const std::vector<pxr::SdfPath> metallic_output_attrs =
AddTextureShader(material_path, metallic_texture->file->c_str(),
pxr::TfToken("metallic"), uvmap_st_output_attr,
{kTokens->outputsRgb});
if (metallic_output_attrs.size() == 1) {
AddAttributeConnection(data_, metallic_attr,
metallic_output_attrs[0]);
}
} else {
SetAttributeDefault(data_, metallic_attr, material->metallic);
}
if (roughness_texture) {
const std::vector<pxr::SdfPath> roughness_output_attrs =
AddTextureShader(material_path, roughness_texture->file->c_str(),
pxr::TfToken("roughness"), uvmap_st_output_attr,
{kTokens->outputsRgb});
if (roughness_output_attrs.size() == 1) {
AddAttributeConnection(data_, roughness_attr,
roughness_output_attrs[0]);
}
} else {
SetAttributeDefault(data_, roughness_attr, material->roughness);
}
if (occlusion_texture) {
pxr::SdfPath occlusion_attr = CreateAttributeSpec(
data_, preview_surface_shader_path, kTokens->inputsOcclusion,
pxr::SdfValueTypeNames->Float);
const std::vector<pxr::SdfPath> occlusion_output_attrs =
AddTextureShader(material_path, occlusion_texture->file->c_str(),
pxr::TfToken("occlusion"), uvmap_st_output_attr,
{kTokens->outputsRgb});
if (occlusion_output_attrs.size() == 1) {
AddAttributeConnection(data_, occlusion_attr,
occlusion_output_attrs[0]);
}
}
}
}
// Find the normal texture if specified.
@@ -166,56 +166,101 @@ TEST_F(MjcfSdfFileFormatPluginTest, TestMaterials) {
stage, "/mesh_test/Materials/material_texture/diffuse.inputs:file",
pxr::SdfAssetPath("textures/cube.png"));
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_metallic");
EXPECT_PRIM_VALID(stage,
"/mesh_test/Materials/material_metallic/PreviewSurface");
ExpectAttributeEqual(
stage,
"/mesh_test/Materials/material_metallic/PreviewSurface.inputs:metallic",
0.6f);
}
TEST_F(MjcfSdfFileFormatPluginTest, TestMaterialLayers) {
const std::string xml_path = GetTestDataFilePath(kMaterialsPath);
auto stage = pxr::UsdStage::Open(xml_path);
EXPECT_THAT(stage, testing::NotNull());
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_layered");
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_layered/uvmap");
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_layered/diffuse");
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_layered/normal");
ExpectAttributeHasConnection(
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:normal",
"/mesh_test/Materials/material_layered/normal.outputs:rgb");
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:normal",
"/mesh_test/Materials/material_layered/normal.outputs:rgb");
ExpectAttributeEqual(
stage, "/mesh_test/Materials/material_layered/normal.inputs:file",
pxr::SdfAssetPath("textures/normal.png"));
stage, "/mesh_test/Materials/material_layered/normal.inputs:file",
pxr::SdfAssetPath("textures/normal.png"));
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_layered/orm_packed");
ExpectAttributeHasConnection(
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:occlusion",
"/mesh_test/Materials/material_layered/orm_packed.outputs:r");
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:occlusion",
"/mesh_test/Materials/material_layered/orm_packed.outputs:r");
ExpectAttributeHasConnection(
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:roughness",
"/mesh_test/Materials/material_layered/orm_packed.outputs:g");
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:roughness",
"/mesh_test/Materials/material_layered/orm_packed.outputs:g");
ExpectAttributeHasConnection(
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:metallic",
"/mesh_test/Materials/material_layered/orm_packed.outputs:b");
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:metallic",
"/mesh_test/Materials/material_layered/orm_packed.outputs:b");
ExpectAttributeEqual(
stage, "/mesh_test/Materials/material_layered/orm_packed.inputs:file",
pxr::SdfAssetPath("textures/orm.png"));
stage, "/mesh_test/Materials/material_layered/orm_packed.inputs:file",
pxr::SdfAssetPath("textures/orm.png"));
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_layered/emissive");
ExpectAttributeHasConnection(
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:emissiveColor",
"/mesh_test/Materials/material_layered/emissive.outputs:rgb");
stage,
"/mesh_test/Materials/material_layered/"
"PreviewSurface.inputs:emissiveColor",
"/mesh_test/Materials/material_layered/emissive.outputs:rgb");
ExpectAttributeEqual(
stage, "/mesh_test/Materials/material_layered/emissive.inputs:file",
pxr::SdfAssetPath("textures/emissive.png"));
stage, "/mesh_test/Materials/material_layered/emissive.inputs:file",
pxr::SdfAssetPath("textures/emissive.png"));
}
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_metallic");
TEST_F(MjcfSdfFileFormatPluginTest, TestMaterialPBRSeparate) {
const std::string xml_path = GetTestDataFilePath(kMaterialsPath);
auto stage = pxr::UsdStage::Open(xml_path);
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_pbr_separate");
EXPECT_PRIM_VALID(stage, "/mesh_test/Materials/material_pbr_separate/uvmap");
EXPECT_PRIM_VALID(stage,
"/mesh_test/Materials/material_metallic/PreviewSurface");
ExpectAttributeEqual(stage,
"/mesh_test/Materials/material_metallic/PreviewSurface.inputs:metallic",
0.6f);
"/mesh_test/Materials/material_pbr_separate/occlusion");
ExpectAttributeHasConnection(
stage,
"/mesh_test/Materials/material_pbr_separate/"
"PreviewSurface.inputs:occlusion",
"/mesh_test/Materials/material_pbr_separate/occlusion.outputs:rgb");
ExpectAttributeEqual(
stage, "/mesh_test/Materials/material_pbr_separate/occlusion.inputs:file",
pxr::SdfAssetPath("textures/occlusion.png"));
EXPECT_PRIM_VALID(stage,
"/mesh_test/Materials/material_pbr_separate/roughness");
ExpectAttributeHasConnection(
stage,
"/mesh_test/Materials/material_pbr_separate/"
"PreviewSurface.inputs:roughness",
"/mesh_test/Materials/material_pbr_separate/roughness.outputs:rgb");
ExpectAttributeEqual(
stage, "/mesh_test/Materials/material_pbr_separate/roughness.inputs:file",
pxr::SdfAssetPath("textures/roughness.png"));
EXPECT_PRIM_VALID(stage,
"/mesh_test/Materials/material_pbr_separate/metallic");
ExpectAttributeHasConnection(
stage,
"/mesh_test/Materials/material_pbr_separate/"
"PreviewSurface.inputs:metallic",
"/mesh_test/Materials/material_pbr_separate/metallic.outputs:rgb");
ExpectAttributeEqual(
stage, "/mesh_test/Materials/material_pbr_separate/metallic.inputs:file",
pxr::SdfAssetPath("textures/metallic.png"));
}
TEST_F(MjcfSdfFileFormatPluginTest, TestGeomRgba) {
@@ -4,6 +4,9 @@
<texture name="normal_tex" file="textures/normal.png" type="2d" />
<texture name="orm_tex" file="textures/orm.png" type="2d" />
<texture name="emissive_tex" file="textures/emissive.png" type="2d" />
<texture name="occlusion_tex" file="textures/occlusion.png" type="2d" />
<texture name="roughness_tex" file="textures/roughness.png" type="2d" />
<texture name="metallic_tex" file="textures/metallic.png" type="2d" />
<material name="material_red" rgba="0.8 0 0 1" />
<material name="material_texture" texture="diffuse_tex" />
<material name="material_metallic" rgba="0.8 0 0 1" metallic="0.6"/>
@@ -13,6 +16,11 @@
<layer texture="orm_tex" role="orm"/>
<layer texture="emissive_tex" role="emissive"/>
</material>
<material name="material_pbr_separate">
<layer texture="occlusion_tex" role="occlusion"/>
<layer texture="roughness_tex" role="roughness"/>
<layer texture="metallic_tex" role="metallic"/>
</material>
2d texture="diffuse" normal="normal" />
<mesh name="tetrahedron" vertex="0 0 0 1 0 0 0 1 0 0 0 1"/>
</asset>
Binary file not shown.

After

Width:  |  Height:  |  Size: 480 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 480 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 480 KiB