OSG粒子系統應用:噴泉效果

粒子系統主要分爲兩大類:
- 一是Effective中已有的效果,如雨雪的效果應用;
- 二類是一步步從模版,發射器等創建。

關於第一種的使用可以參見上一篇博客OSG粒子系統應用:雨雪效果。本篇博客來使用第二種方式創建一個噴泉的效果。
廢話補多說,直接上代碼:

osg::ref_ptr<osg::Node> Fountain::createFountain(osg::Group* parent)
{
    // 設置粒子模板的參數,包括生命週期、顏色、透明度、半徑、重量等
    osgParticle::Particle pParticle;
    pParticle.setLifeTime(3);
    pParticle.setColorRange(osgParticle::rangev4(
        osg::Vec4(0.7, 1.0, 1.0, 1.0),
        osg::Vec4(0.8, 1.0, 1.0, 0.0)));
    pParticle.setSizeRange(osgParticle::rangef(0.1f, 0.1f));
    pParticle.setAlphaRange(osgParticle::rangef(1.0f, 0.5f));
    pParticle.setRadius(m_dParticlesSize);
    pParticle.setMass(0.05);

    osg::ref_ptr<osgParticle::ParticleSystem> pParticleSys =
        new osgParticle::ParticleSystem;
    pParticleSys->setDefaultAttributes("Image/smoke.rgb", false, false);
    pParticleSys->setDefaultParticleTemplate(pParticle);

    // 發射器
    osg::ref_ptr<osgParticle::ModularEmitter> pEmitter = new osgParticle::ModularEmitter;
    pEmitter->setParticleSystem(pParticleSys);

    osg::ref_ptr<osgParticle::RandomRateCounter> pRandomCounter = new osgParticle::RandomRateCounter;
    pRandomCounter->setRateRange(osgParticle::rangef(m_dParticlesNum, m_dParticlesNum));
    pEmitter->setCounter(pRandomCounter);

    // 粒子發射槍
    osg::ref_ptr<osgParticle::RadialShooter> pShooter = new osgParticle::RadialShooter;
    pShooter->setInitialSpeedRange(m_dParticlesSpeed, 0);
    pEmitter->setShooter(pShooter);

    // 速度細節控制
    osg::ref_ptr<osgParticle::ModularProgram> pProgram = new osgParticle::ModularProgram;
    pProgram->setParticleSystem(pParticleSys);
    osg::ref_ptr<osgParticle::AccelOperator> pAccelOperator = new osgParticle::AccelOperator;
    pAccelOperator->setToGravity();
    pProgram->addOperator(pAccelOperator);
    osg::ref_ptr<osgParticle::FluidFrictionOperator> pOperator = new osgParticle::FluidFrictionOperator;
    pOperator->setFluidToAir();
    pProgram->addOperator(pOperator);

    osg::ref_ptr<osg::Geode> pGeode = new osg::Geode;
    pGeode->addDrawable(pParticleSys);
    parent->addChild(pEmitter);
    parent->addChild(pProgram);
    parent->addChild(pGeode);

    osg::ref_ptr<osgParticle::ParticleSystemUpdater> pParticleSysUpdate =
        new osgParticle::ParticleSystemUpdater;
    pParticleSysUpdate->addParticleSystem(pParticleSys);
    return pParticleSysUpdate;
}

創建粒子的時候傳入一個osg::Group*參數,創建成功後,返回一個osg::Node*值,這個Node還可以成爲傳入參數的子節點。

PS:此篇博客代碼以及前幾篇的均已放在github,歡迎獲取,歡迎交流!

發佈了55 篇原創文章 · 獲贊 118 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章