OSG對外部導入的模型進行透明化處理

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));//第四個參數用於調節透明度
}

直接調用這個函數就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章