Skip to content

Commit

Permalink
Extend AssimpLoader to parse material transmission factor (#577)
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
Co-authored-by: Michael Carroll <[email protected]>
  • Loading branch information
iche033 and mjcarroll authored Mar 4, 2024
1 parent 3571cb4 commit b3ba7ab
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
23 changes: 21 additions & 2 deletions graphics/src/AssimpLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,26 @@ MaterialPtr AssimpLoader::Implementation::CreateMaterial(
}
float opacity = 1.0;
ret = assimpMat->Get(AI_MATKEY_OPACITY, opacity);
mat->SetTransparency(1.0 - opacity);
mat->SetBlendFactors(opacity, 1.0 - opacity);
if (ret == AI_SUCCESS)
{
mat->SetTransparency(1.0 - opacity);
mat->SetBlendFactors(opacity, 1.0 - opacity);
}

#ifndef GZ_ASSIMP_PRE_5_1_0
// basic support for transmission - currently just overrides opacity
// \todo(iche033) The transmission factor can be used with volume
// material extension to simulate effects like refraction
// so consider also extending support for other properties like
// AI_MATKEY_VOLUME_THICKNESS_FACTOR
float transmission = 0.0;
ret = assimpMat->Get(AI_MATKEY_TRANSMISSION_FACTOR, transmission);
if (ret == AI_SUCCESS)
{
mat->SetTransparency(transmission);
}
#endif

// TODO(luca) more than one texture, Gazebo assumes UV index 0
Pbr pbr;
aiString texturePath(_path.c_str());
Expand Down Expand Up @@ -593,6 +611,7 @@ std::string AssimpLoader::Implementation::GenerateTextureName(
"_" + _type;
}

//////////////////////////////////////////////////
SubMesh AssimpLoader::Implementation::CreateSubMesh(
const aiMesh* _assimpMesh, const math::Matrix4d& _transform) const
{
Expand Down
24 changes: 24 additions & 0 deletions graphics/src/AssimpLoader_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,30 @@ TEST_F(AssimpLoader, LoadGlTF2BoxExternalTexture)
delete mesh;
}

/////////////////////////////////////////////////
// Open a gltf mesh with transmission extension
TEST_F(AssimpLoader, LoadGlTF2BoxTransmission)
{
#ifndef GZ_ASSIMP_PRE_5_1_0
common::AssimpLoader loader;
common::Mesh *mesh = loader.Load(
common::testing::TestFile("data", "box_transmission.glb"));

EXPECT_STREQ("unknown", mesh->Name().c_str());

// Make sure we can read the submesh name
EXPECT_STREQ("Cube", mesh->SubMeshByIndex(0).lock()->Name().c_str());

EXPECT_EQ(mesh->MaterialCount(), 1u);

const common::MaterialPtr mat = mesh->MaterialByIndex(0u);
ASSERT_TRUE(mat.get());
// transmission currently modeled as transparency
EXPECT_FLOAT_EQ(0.1, mat->Transparency());
delete mesh;
#endif
}

/////////////////////////////////////////////////
// This test loads a box glb mesh with embedded compressed jpeg texture
TEST_F(AssimpLoader, LoadGlTF2BoxWithJPEGTexture)
Expand Down
10 changes: 8 additions & 2 deletions graphics/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,22 @@ gz_build_tests(
)

# Assimp doesn't offer preprocessor version, use cmake to set a compatibility
# mode for versions below 5.2.0
# mode for versions below 5.2.0 and 5.1.0
if(${GzAssimp_VERSION} STRLESS "5.2.0")
message("Warning, assimp below 5.2.0 detected, setting compatibility mode")
target_compile_definitions(${graphics_target} PRIVATE GZ_ASSIMP_PRE_5_2_0)
if(TARGET UNIT_AssimpLoader_TEST)
target_compile_definitions(UNIT_AssimpLoader_TEST PRIVATE GZ_ASSIMP_PRE_5_2_0)
endif()
if(${GzAssimp_VERSION} STRLESS "5.1.0")
message("Warning, assimp below 5.1.0 detected, setting compatibility mode")
target_compile_definitions(${graphics_target} PRIVATE GZ_ASSIMP_PRE_5_1_0)
if(TARGET UNIT_AssimpLoader_TEST)
target_compile_definitions(UNIT_AssimpLoader_TEST PRIVATE GZ_ASSIMP_PRE_5_1_0)
endif()
endif()
endif()


if(USE_EXTERNAL_TINYXML2)

# If we are using an external copy of tinyxml2, add its imported target
Expand Down
Binary file added test/data/box_transmission.glb
Binary file not shown.

0 comments on commit b3ba7ab

Please sign in to comment.