Skip to content

Commit

Permalink
pass-based tinting
Browse files Browse the repository at this point in the history
Tint the mesh by re-rendering on top of the original one with the marker color.
Also consider the alpha value of the marker color for the mesh.
  • Loading branch information
rhaschke committed Sep 9, 2019
1 parent 4c66ddb commit 400652d
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions src/rviz/default_plugin/markers/mesh_resource_marker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ void MeshResourceMarker::onNewMessage(const MarkerConstPtr& old_message, const M
if (material->getName() != "BaseWhiteNoLighting")
{
Ogre::MaterialPtr new_material = material->clone(id + material->getName());
// add a new pass to every custom material to perform the color tinting
Ogre::Pass* pass = new_material->getTechnique(0)->createPass();
pass->setAmbient( 0.0f, 0.0f, 0.0f );
pass->setDiffuse( 0.0f, 0.0f, 0.0f, 0.0f );
pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
pass->setDepthWriteEnabled(false);
pass->setLightingEnabled(true);
materials_.insert(new_material);
}
}
Expand Down Expand Up @@ -196,24 +203,32 @@ void MeshResourceMarker::onNewMessage(const MarkerConstPtr& old_message, const M
{
bool depth_write = a >= 0.9998;
Ogre::SceneBlendType blending = depth_write ? Ogre::SBT_REPLACE : Ogre::SBT_TRANSPARENT_ALPHA;

// if the mesh_use_embedded_materials is true and color is non-zero
// then the color will be used to tint the embedded materials
if( new_message->mesh_use_embedded_materials && r == 0 && g == 0 && b == 0 && a == 0 )
{
blending = Ogre::SBT_REPLACE;
depth_write = true;
r = 1; g = 1; b = 1; a = 1;
}
bool tinting = new_message->mesh_use_embedded_materials;

for (const Ogre::MaterialPtr& material : materials_)
{
Ogre::Technique* technique = material->getTechnique(0);
technique->setAmbient( r*0.5, g*0.5, b*0.5 );
technique->setDiffuse( r, g, b, a );
technique->setSceneBlending( blending );
technique->setDepthWriteEnabled( depth_write );
technique->setLightingEnabled( true );
Ogre::Pass* pass0 = technique->getPass(0);
Ogre::Pass* passT = technique->getPass(technique->getNumPasses()-1);
if (tinting)
{
// modify material's original color to use given alpha value
Ogre::ColourValue color = pass0->getDiffuse();
color.a = a;
pass0->setDiffuse(color);
// tint by re-rendering with marker color
passT->setAmbient( r*0.5f, g*0.5f, b*0.5f );
passT->setDiffuse( r, g, b, std::min(a, 0.5f) );
}
else
{
pass0->setAmbient( r*0.5f, g*0.5f, b*0.5f );
pass0->setDiffuse( r, g, b, a );
}

pass0->setSceneBlending( blending );
pass0->setDepthWriteEnabled( depth_write );
pass0->setLightingEnabled( true );
}
}

Expand Down

0 comments on commit 400652d

Please sign in to comment.