目標:使用OSG的粒子系統完全對天氣中雨雪效果的模擬
雨效果
直接上代碼
osg::Matrixd matrixEffect;
matrixEffect.makeTranslate(pos);
// 設置粒子位置
osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform;
// 對粒子範圍進行了放大
trans->setMatrix(matrixEffect * osg::Matrixd::scale(100, 100, 100));
// 創建雨粒子
osg::ref_ptr<osgParticle::PrecipitationEffect> pe =
new osgParticle::PrecipitationEffect;
pe->rain(1.0);
pe->setUseFarLineSegments(true);
// iLevel參數是一個int值,表示雨的級別,一般1-10就夠用了
pe->setParticleSize(iLevel / 10.0);
// 設置顏色
pe->setParticleColor(osg::Vec4(1, 1, 1, 1));
trans->addChild(pe);
m_pRainGroup->addChild(trans);
雪效果
雪效果的實現和雨幾乎是一樣的,只是對PrecipitationEffect的粒子類型設置不一樣,代碼:
osg::Matrixd mat;
mat.makeTranslate(getPostion(x, y));
// 設置粒子位置
osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform;
trans->setMatrix(mat * osg::Matrixd::scale(10, 10, 10));
osg::ref_ptr<osgParticle::PrecipitationEffect> pe = new osgParticle::PrecipitationEffect;
// 注意,這裏區分雨雪效果
pe->snow(1.0);
pe->setParticleSize(iLevel / 10.0);
// 設置顏色
pe->setParticleColor(osg::Vec4(1, 1, 1, 1));
trans->addChild(pe);
m_pSnowGroup->addChild(trans);
使用上面的方式是爲了加到我的OSGEarth地球中,如果普通的Group不能顯示以上效果的話,可以加入MapNode節點之下試試看。