OSG渲染引擎對於在內部畫的幾何圖形能夠直接使用rgba中的第四個參數對其實現透明化,而從外部導入的模型則需要以下方法:
void setTransparent(osg::Node *node, float trans)//trans用於調節透明度,範圍爲0.0-1.0,
//其中1.0代表不透明。
{
osg::StateSet * stateset = node->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setMode(GL_DEPTH, osg::StateAttribute::OFF);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
osg::BlendColor *bc = new osg::BlendColor(osg::Vec4(1.0, 1.0, 1.0, 1.0));
osg::ref_ptr<osg::BlendFunc>blendFunc = new osg::BlendFunc();
blendFunc->setSource(osg::BlendFunc::CONSTANT_ALPHA);
blendFunc->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
stateset->setAttributeAndModes(bc, osg::StateAttribute::ON);
stateset->setAttributeAndModes(blendFunc, osg::StateAttribute::ON);
bc->setConstantColor(osg::Vec4(1, 1, 1, trans));//第四個參數用於調節透明度
}
直接調用這個函數就可以了。