Skip to content

Commit

Permalink
Fix transparency of RobotLinks (ros-visualization#1751)
Browse files Browse the repository at this point in the history
If a RobotLink has multiple visuals with different alpha values,
only the last one was actually used (stored in material_alpha_).
Instead, store the original material and use alpha from there.
  • Loading branch information
rhaschke authored and Ryan Sinnet committed Jul 20, 2022
1 parent 771b81b commit 278eeea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
23 changes: 10 additions & 13 deletions src/rviz/robot/robot_link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ RobotLink::RobotLink( Robot* robot,
, collision_node_( NULL )
, trail_( NULL )
, axes_( NULL )
, material_alpha_( 1.0 )
, robot_alpha_(1.0)
, only_render_depth_(false)
, using_color_( false )
Expand Down Expand Up @@ -385,7 +384,8 @@ void RobotLink::updateAlpha()
else
{
Ogre::ColourValue color = material->getTechnique(0)->getPass(0)->getDiffuse();
color.a = robot_alpha_ * material_alpha_ * link_alpha;
const float material_alpha = original->getTechnique(0)->getPass(0)->getDiffuse().a;
color.a = robot_alpha_ * material_alpha * link_alpha;
material->setDiffuse( color );

if ( color.a < 0.9998 )
Expand Down Expand Up @@ -473,8 +473,6 @@ Ogre::MaterialPtr RobotLink::getMaterialForLink( const urdf::LinkConstSharedPtr&
const urdf::Color& col = visual->material->color;
mat->getTechnique(0)->setAmbient(col.r * 0.5, col.g * 0.5, col.b * 0.5);
mat->getTechnique(0)->setDiffuse(col.r, col.g, col.b, col.a);

material_alpha_ = col.a;
}
else
{
Expand Down Expand Up @@ -650,20 +648,19 @@ void RobotLink::createEntityForGeometryElement(const urdf::LinkConstSharedPtr& l

if (material_name == "BaseWhite" || material_name == "BaseWhiteNoLighting")
{
sub->setMaterialName(default_material_name_);
original = default_material_;
}
else
{
// Need to clone here due to how selection works. Once selection id is done per object and not per material,
// this can go away
std::stringstream ss;
ss << material_name << count++ << "Robot";
std::string cloned_name = ss.str();
sub->getMaterial()->clone(cloned_name);
sub->setMaterialName(cloned_name);
original = sub->getMaterial();
}

materials_[sub] = sub->getMaterial();
// create a new material copy for each instance of a RobotLink to allow modification per link
active = Ogre::MaterialPtr(new Ogre::Material(
nullptr, material_name, 0, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME));
*active = *original;
sub->setMaterial(active);
materials_[sub] = std::make_pair(active, original);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/rviz/robot/robot_link.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ private Q_SLOTS:

Axes* axes_;

float material_alpha_; ///< If material is not a texture, this saves the alpha value set in the URDF, otherwise is 1.0.
float robot_alpha_; ///< Alpha value from top-level robot alpha Property (set via setRobotAlpha()).

bool only_render_depth_;
Expand Down

0 comments on commit 278eeea

Please sign in to comment.