Open Scene Graph粒子系统

Open Scene Graph 提供了强大的粒子系统功能,允许开发者创建各种视觉效果,如火焰、烟雾、爆炸、魔法效果等。

核心组件

1. osgParticle::ParticleSystem

粒子系统的基础类,管理所有粒子的渲染和更新。

osg::ref_ptr<osgParticle::ParticleSystem> ps = new osgParticle::ParticleSystem;

2. osgParticle::Particle

定义单个粒子的属性,包括:

  • 位置、速度、加速度

  • 生命周期

  • 大小、颜色

  • 质量、形状

3. osgParticle::Emitter

粒子发射器,控制粒子的生成:

  • PointPlacer - 从点发射

  • SectorPlacer - 从扇形区域发射

  • RadialShooter - 径向发射

  • RandomRateCounter - 随机发射率

osg::ref_ptr<osgParticle::ModularEmitter> emitter = new osgParticle::ModularEmitter;
emitter->setParticleSystem(ps.get());

4. osgParticle::Program

粒子行为控制器:

  • AccelOperator - 加速度操作

  • FluidFrictionOperator - 流体摩擦

  • ForceOperator - 力场

osg::ref_ptr<osgParticle::ModularProgram> program = new osgParticle::ModularProgram;
program->setParticleSystem(ps.get());

创建基本粒子系统示例

#include <osgParticle/ParticleSystem>
#include <osgParticle/ModularEmitter>
#include <osgParticle/ModularProgram>
#include <osgParticle/RandomRateCounter>
#include <osgParticle/RadialShooter>
#include <osgParticle/ParticleSystemUpdater>
#include <osg/Geode>

osg::Node* createFireEffect()
{
    // 创建粒子系统
    osgParticle::ParticleSystem* ps = new osgParticle::ParticleSystem;
    
    // 设置粒子系统属性
    ps->setDefaultAttributes("Images/smoke.rgb", false, false);
    ps->setDefaultParticleTemplate(createParticleTemplate());
    
    // 创建发射器
    osgParticle::ModularEmitter* emitter = new osgParticle::ModularEmitter;
    emitter->setParticleSystem(ps);
    
    // 设置发射计数器
    osgParticle::RandomRateCounter* rrc = new osgParticle::RandomRateCounter;
    rrc->setRateRange(50, 100);
    emitter->setCounter(rrc);
    
    // 设置发射角度和速度
    osgParticle::RadialShooter* shooter = new osgParticle::RadialShooter;
    shooter->setInitialSpeedRange(0.5f, 1.5f);
    shooter->setThetaRange(0.0f, osg::PI_4);
    emitter->setShooter(shooter);
    
    // 创建粒子程序
    osgParticle::ModularProgram* program = new osgParticle::ModularProgram;
    program->setParticleSystem(ps);
    
    // 添加重力
    osgParticle::AccelOperator* gravity = new osgParticle::AccelOperator;
    gravity->setToGravity();
    program->addOperator(gravity);
    
    // 创建更新器
    osgParticle::ParticleSystemUpdater* updater = new osgParticle::ParticleSystemUpdater;
    updater->addParticleSystem(ps);
    
    // 创建场景图
    osg::Group* root = new osg::Group;
    root->addChild(emitter);
    root->addChild(program);
    root->addChild(updater);
    
    // 将粒子系统添加到Geode
    osg::Geode* geode = new osg::Geode;
    geode->addDrawable(ps);
    root->addChild(geode);
    
    return root;
}

osgParticle::Particle createParticleTemplate()
{
    osgParticle::Particle p;
    p.setLifeTime(3.0f);
    p.setSizeRange(osgParticle::rangef(0.1f, 1.0f));
    p.setAlphaRange(osgParticle::rangef(1.0f, 0.0f));
    p.setColorRange(osgParticle::rangev4(
        osg::Vec4(1.0f, 0.5f, 0.0f, 1.0f),
        osg::Vec4(0.5f, 0.5f, 0.5f, 0.0f)));
    return p;
}

高级特性

  1. 粒子碰撞检测 - 使用 osgParticle::CollisionOperator

  2. 风场效果 - 使用 osgParticle::ForceOperator

  3. 粒子纹理动画 - 通过多纹理帧实现

  4. GPU加速粒子 - 使用着色器实现大规模粒子效果

性能优化建议

  1. 合理设置粒子数量和生命周期

  2. 使用LOD(Level of Detail)控制粒子细节

  3. 对于静态粒子效果,考虑使用公告板(Billboard)

  4. 使用粒子池减少内存分配开销