#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geode>
#include<osgAnimation/easeMotion>
#include<osg/BlendFunc>
#include<osg/ShapeDrawable>
class FaderCallBack : public osg::NodeCallback
{
public:
FaderCallBack()
{
_motion = new osgAnimation::InOutCubicMotion;
}
virtual void operator()(osg::Node* node, osg::NodeVisitor*nv)
{
osg::Geode* geode = dynamic_cast<osg::Geode*>(node);
if (nullptr == geode)
{
traverse(node, nv);
return;
}
osg::ShapeDrawable* shape = dynamic_cast<osg::ShapeDrawable*>(geode->getDrawable(0));
if (shape)
{
_motion->update(0.05); // 值越小,渐入速度越慢,人眼越有感觉;值越大,渐入速度越快,人眼察觉越困难
float alpha = _motion->getValue();
if (alpha > 1.0)
{
alpha = 2.0 - alpha;
}
shape->setColor(osg::Vec4(1.0, 1.0, 1.0, alpha));
}
traverse(node, nv);
}
protected:
osg::ref_ptr< osgAnimation::InOutCubicMotion> _motion;
};
int main()
{
osg::ref_ptr<osg::ShapeDrawable>shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 0.0, 0.0), 1.0));
shape->setColor(osg::Vec4(1.0, 1.0, 1.0, 1.0));
shape->setSupportsDisplayList(false);
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(shape.get());
geode->setUpdateCallback(new FaderCallBack);
geode->getOrCreateStateSet()->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
osgViewer::Viewer viewer;
viewer.setSceneData(geode.get());
return viewer.run();
}
9.4.2.物体的淡出淡入
于 2021-03-28 12:21:12 首次发布