diff --git a/graphics/src/AssimpLoader.cc b/graphics/src/AssimpLoader.cc index 9e49046e..24b2f2fa 100644 --- a/graphics/src/AssimpLoader.cc +++ b/graphics/src/AssimpLoader.cc @@ -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()); @@ -593,6 +611,7 @@ std::string AssimpLoader::Implementation::GenerateTextureName( "_" + _type; } +////////////////////////////////////////////////// SubMesh AssimpLoader::Implementation::CreateSubMesh( const aiMesh* _assimpMesh, const math::Matrix4d& _transform) const { diff --git a/graphics/src/AssimpLoader_TEST.cc b/graphics/src/AssimpLoader_TEST.cc index 40a99f7f..6be2e579 100644 --- a/graphics/src/AssimpLoader_TEST.cc +++ b/graphics/src/AssimpLoader_TEST.cc @@ -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) diff --git a/graphics/src/CMakeLists.txt b/graphics/src/CMakeLists.txt index aa832387..b153bf67 100644 --- a/graphics/src/CMakeLists.txt +++ b/graphics/src/CMakeLists.txt @@ -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 diff --git a/test/data/box_transmission.glb b/test/data/box_transmission.glb new file mode 100644 index 00000000..adc74d38 Binary files /dev/null and b/test/data/box_transmission.glb differ