Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

override material of a loaded obj and reset it back #1219

Open
XDinEuro opened this issue Apr 15, 2023 · 1 comment
Open

override material of a loaded obj and reset it back #1219

XDinEuro opened this issue Apr 15, 2023 · 1 comment

Comments

@XDinEuro
Copy link

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 ? @

@robertosfield
Copy link
Collaborator

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants