OpenSceneGraph icon indicating copy to clipboard operation
OpenSceneGraph copied to clipboard

override material of a loaded obj and reset it back

Open XDinEuro opened this issue 2 years ago • 1 comments

Hello, I am using osg 3.6.5.

I intended to load a mesh and highlight it (by changing its color). I managed to do it with code below;

// load from file
osg::ref_ptr<osg::Node> node;
node = osgDB::readNodeFile(<fileName>, new osgDB::Options("noRotation"));

// override the color
 osg::ref_ptr<osg::StateSet> geo_ss = (geode->getOrCreateStateSet());

  osg::ref_ptr<osg::Material> mat = new osg::Material;
  mat->setDiffuse(osg::Material::FRONT,
                  osg::Vec4(rgba[0], rgba[1], rgba[2], 1.));

  mat->setTransparency(osg::Material::FRONT, rgba[3]);
  geo_ss->setAttributeAndModes(mat, osg::StateAttribute::OVERRIDE);
  geo_ss->setMode(GL_BLEND, osg::StateAttribute::ON);
  geo_ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

Now I want to reset it back, which means cache the material information, and reload it if I need. Could u give some hints ? @

XDinEuro avatar Apr 15 '23 15:04 XDinEuro

One thing you can do with scene graphs like the OSG is take a subgraph and share it between two parents that set different state, then have a Switch about each branch so you can select between then. You can also use NodeMasks to toggle node and traversal of their children on/off instead of a Switch.

IFor instance you have a hiieracchy:

osg::Switch osg::Group->StateSet1->,material1 ->sharedSubgraph

osg::Group->StateSet->material2
                 ->sharedSubgraph

The other approach would be like the one you are following already but cache the old StateSet/Material as UserData on the Node you change. i.e.

node->setUserData(material);

robertosfield avatar Apr 15 '23 16:04 robertosfield