From 071a8e4b30f0828e2b59b602eae9132df27090ee Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Fri, 12 Mar 2021 23:36:42 -0800 Subject: [PATCH] Scene3D: port mesh material fixes from ign-gazebo (#191) As noted in #137, the Scene3D plugin was forked for use in ign-gazebo, with the intention of consolidating back with the ign-gui plugin to reduce duplication. This ports some fixes for mesh material loading to ign-gui made in ignitionrobotics/ign-gazebo@897ef65 as part of bitbucket PR 315. Signed-off-by: Steve Peters --- src/plugins/scene3d/Scene3D.cc | 47 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/plugins/scene3d/Scene3D.cc b/src/plugins/scene3d/Scene3D.cc index 828cf266a..93a9b0180 100644 --- a/src/plugins/scene3d/Scene3D.cc +++ b/src/plugins/scene3d/Scene3D.cc @@ -603,11 +603,7 @@ rendering::VisualPtr SceneManager::LoadVisual(const msgs::Visual &_msg) // Don't set a default material for meshes because they // may have their own // TODO(anyone) support overriding mesh material - else if (_msg.geometry().has_mesh()) - { - material = geom->Material(); - } - else + else if (!_msg.geometry().has_mesh()) { // create default material material = this->scene->Material("ign-grey"); @@ -621,15 +617,40 @@ rendering::VisualPtr SceneManager::LoadVisual(const msgs::Visual &_msg) material->SetMetalness(1.0f); } } + else + { + // meshes created by mesh loader may have their own materials + // update/override their properties based on input sdf element values + auto mesh = std::dynamic_pointer_cast(geom); + for (unsigned int i = 0; i < mesh->SubMeshCount(); ++i) + { + auto submesh = mesh->SubMeshByIndex(i); + auto submeshMat = submesh->Material(); + if (submeshMat) + { + double productAlpha = (1.0-_msg.transparency()) * + (1.0 - submeshMat->Transparency()); + submeshMat->SetTransparency(1 - productAlpha); + submeshMat->SetCastShadows(_msg.cast_shadows()); + } + } + } - material->SetTransparency(_msg.transparency()); - - // TODO(anyone) Get roughness and metalness from message instead - // of giving a default value. - material->SetRoughness(0.3f); - material->SetMetalness(0.3f); - - geom->SetMaterial(material); + if (material) + { + // set transparency + material->SetTransparency(_msg.transparency()); + + // cast shadows + material->SetCastShadows(_msg.cast_shadows()); + + geom->SetMaterial(material); + // todo(anyone) SetMaterial function clones the input material. + // but does not take ownership of it so we need to destroy it here. + // This is not ideal. We should let ign-rendering handle the lifetime + // of this material + this->scene->DestroyMaterial(material); + } } else {