柔性粒子系统-更新器(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/flexible-particle-system-updaters-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 4 分钟阅读 - 1611 个词 阅读量 0柔性粒子系统-更新器(译文)
原文地址:https://www.codeproject.com/Articles/783575/Flexible-Particle-System-Updaters
原文作者:Bartlomiej Filipek
译文由本站 robot-v1.0 翻译
前言
Flexible Particle System - Updaters
柔性粒子系统-更新器 在上一个(In the previous) 粒子柱(particle post) ,介绍了粒子生成系统.但是在创建新粒子之后,我们需要一种方法来更新其参数.这次,我们来看看(, the particle generation system was introduced. But after a new particle is created, we need to have a way to update its parameters. This time, we will take a look at)更新者(updaters)-实际上是那些使事物动起来和生活的类.(- those are the classes that, actually, make things moving and living.)
该系列(The Series)
- 介绍(Introduction)
- 粒子容器1-问题(Particle Container 1 - problems)
- 粒子容器2-实施(Particle Container 2 - implementation)
- 发电机和发射器(Generators & Emitters)
- 更新者(Updaters)(这个帖子)((this post))
- 渲染器(Renderer)
- 工具优化(Tools Optimizations)
- SIMD优化(SIMD Optimizations)
- 渲染器优化(Renderer Optimizations)
介绍(Introduction)
更新者也关注(Updaters also follow)建议零售价(SRP)原理.它们仅用于更新粒子的参数,并最终确定粒子是否处于活动状态.我们还可以走得更远,创造出"杀手级"-可以杀死粒子,但可能过于夸张了.(principle. They are used only to update particle’s parameters and finally decide if the particle is alive or not. We could also go further and create ‘killers’ - that would kill particles, but probably it would be too exaggerated design.) 要点位于此处:(The gist is located here:) fenbf/BasicParticleUpdaters(fenbf / BasicParticleUpdaters)
更新器界面(The Updater Interface)
class ParticleUpdater
{
public:
ParticleUpdater() { }
virtual ~ParticleUpdater() { }
virtual void update(double dt, ParticleData *p) = 0;
};
Updater获取增量时间和所有粒子数据.它遍历活动的粒子并执行某些操作.该课程相当"广泛",并提供了很多可能性.甚至有人可能指出它提供了太多选择.但是目前,我不认为我们应该限制这种行为.(Updater gets delta time and all the particle data. It iterates through alive particles and does some things. The class is quite ‘broad’ and gives a lot of possibilities. Someone might even point out that it gives too many options. But at this time, I do not think we should restrict this behaviour.)
理想情况下,更新程序应仅关注一组参数.例如,(Ideally, an updater should focus only on one set of params. For instance,) EulerUpdater
要么(or) ColorUpdater
.(.)
粒子更新程序实施(Particle Updaters Implementation)
让我们来看看(Let’s have a look at) EulerUpdater
.(.)
这是一个例子(Here is an example of) BoxPosGen
:(:)
class EulerUpdater : public ParticleUpdater
{
public:
glm::vec4 m_globalAcceleration{ 0.0f };
public:
virtual void update(double dt, ParticleData *p) override;
};
void EulerUpdater::update(double dt, ParticleData *p)
{
const glm::vec4 globalA{ dt * m_globalAcceleration.x,
dt * m_globalAcceleration.y,
dt * m_globalAcceleration.z,
0.0 };
const float localDT = (float)dt;
const unsigned int endId = p->m_countAlive;
for (size_t i = 0; i < endId; ++i)
p->m_acc[i] += globalA;
for (size_t i = 0; i < endId; ++i)
p->m_vel[i] += localDT * p->m_acc[i];
for (size_t i = 0; i < endId; ++i)
p->m_pos[i] += localDT * p->m_vel[i];
}
很简单!与生成器一样,我们可以混合使用不同的更新器来创建所需的效果.在我的旧粒子系统中,我通常会有一个巨大的"更新程序"(尽管整个系统完全不同).然后,当我想要稍微修改一下效果时,我需要一次又一次地复制和粘贴通用代码.这绝对不是最好的模式!您可能会将此视为(Pretty simple! As with generators, we can mix different updaters to create the desired effect. In my old particle system, I would usually have one huge ‘updater’ (although the whole system was totally different). Then, when I wanted to have a slightly modified effect, I needed to copy and paste common code again and again. This was definitely not a best pattern! You might treat this like an)反模式(antipattern):)(:)) 其他(Other)更新者(updaters):(:)
FloorUpdater
-可以将颗粒弹离地板.(- can bounce particle off the floor.)AttractorUpdater
-引力系统中的吸引子.(- attractors in a gravity system.)BasicColorUpdater
-根据时间以及最小和最大颜色生成当前的粒子颜色.(- generate current particle color based on time and min and max color.)PosColorUpdater
-当前颜色来自位置.(- current color comes from position.)VelColorUpdater
-当前的颜色来自速度.(- current color comes from velocity.)BasicTimeUpdater
-测量粒子的寿命.如果时间结束,它将杀死粒子.(- measures the time of life of a particle. It kills a particle if its time is over.)
更新程序组成示例(Example Updater Composition)
对于"地板效果",我使用以下代码:(For ‘floor effect’, I use the following code:)
auto timeUpdater = std::make_shared<particles::updaters::BasicTimeUpdater>();
m_system->addUpdater(timeUpdater);
auto colorUpdater = std::make_shared<particles::updaters::BasicColorUpdater>();
m_system->addUpdater(colorUpdater);
m_eulerUpdater = std::make_shared<particles::updaters::EulerUpdater>();
m_eulerUpdater->m_globalAcceleration = glm::vec4{ 0.0, -15.0, 0.0, 0.0 };
m_system->addUpdater(m_eulerUpdater);
m_floorUpdater = std::make_shared<particles::updaters::FloorUpdater>();
m_system->addUpdater(m_floorUpdater);
您可以在这里看到它-从39秒开始:(You can see it here in action - from 39 sec:)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VisualC++ C++ GCC animation performance 新闻 翻译