目标:使用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节点之下试试看。