Skip to content

Commit

Permalink
Scene3D: port mesh material fixes from ign-gazebo (#191)
Browse files Browse the repository at this point in the history
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
gazebosim/gz-sim@897ef65 as part of
bitbucket PR 315.

Signed-off-by: Steve Peters <scpeters@openrobotics.org>
  • Loading branch information
scpeters authored Mar 13, 2021
1 parent 09fd5f7 commit 071a8e4
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/plugins/scene3d/Scene3D.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<rendering::Mesh>(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
{
Expand Down

0 comments on commit 071a8e4

Please sign in to comment.