From b3ba7ab735adaacd8997701d413d6bc8f4cbee2e Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Mon, 4 Mar 2024 07:13:32 -0800 Subject: [PATCH] Extend AssimpLoader to parse material transmission factor (#577) Signed-off-by: Ian Chen Co-authored-by: Michael Carroll --- graphics/src/AssimpLoader.cc | 23 +++++++++++++++++++++-- graphics/src/AssimpLoader_TEST.cc | 24 ++++++++++++++++++++++++ graphics/src/CMakeLists.txt | 10 ++++++++-- test/data/box_transmission.glb | Bin 0 -> 2140 bytes 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 test/data/box_transmission.glb diff --git a/graphics/src/AssimpLoader.cc b/graphics/src/AssimpLoader.cc index 9e49046ed..24b2f2faa 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 40a99f7f4..6be2e5798 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 aa832387e..b153bf67e 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 0000000000000000000000000000000000000000..adc74d3881100ce746497fd1d1aeacf809f2eb54 GIT binary patch literal 2140 zcmb7ETT|0O6g~nfC|(fM*UUc0nxywWh)`juK;yKPaRwPk+il5An=qRcVLF}ZKhvM( zqvve1r43N(!tU8~`_9>Kw`q53y9)sDa~a_862RB0UG6g{2zbZ}eb(k47f$F4R$%+* z!uR|@Yh#MG-Qix73$66g)-IEYOv1`Con3MfxW0$MR>EKdo$;%Xdt?f}2fWD&$83ME za@uu5E?lP*oQA^jg036LEIsCQvFwS?f(ACh3P!XG5T(yNr;9dLl|G$$ev^~KI2mAu z()z5+gL9c0#TR>Z&M0Kuf<$Zjy?TdN-6r)IiXNZroOqbgHF_scvFkJ>T}Hw*M#D_y z4b#k+7V`R&`|Jc6Di#XG`KSqw3Y- z*1=XqKKC1PCdraS)vrT-z`b^eKNKsM(-|4;R!e>X@a98DwCQ7zOyx6pC5noNb0yfk z+4w}KE2pz`kU48-W6AgvnodsAQyq~I5ioJ;T#6o)rtK{PY?sRTH5_h6^Y;UL@~fEp z_L~zJ_w5x1`S02*8_8LF<)0-tqka?)>G8eNN#3zXisT?44dM%ik(35nM!95HLt#mM zsH``6bK_t=8A}j|qxy)~l5W*o)T@e-Ihd=4>@5~UK2>MjTP)@l4%X9f!X5I@r;$A_ zDSn8)xTH9;$0fymj_^sI;RKu%M>?RK)HSpXagwBQM!Lc!8}5}3@CaVQ2Cf_M6rRBo sSjTl8-osmX1FN{M!Yg