Work in progress
The content of this page was not yet updated for Godot
4.2
and may be outdated. If you know how to improve this page or you can confirm
that it's up to date, feel free to open a pull request.
2D 粒子系统¶
简介¶
粒子系统用于模拟复杂的物理效果,例如火花、火焰、魔法粒子、烟雾、薄雾等。
这个想法是以固定的间隔发射具有固定的寿命的 "粒子". 在其生命周期中, 每个粒子都具有相同的基本行为. 让每个粒子变得不同并提供整体更加 "有机" 外观的是与各个参数相关的 "随机性". 实质上, 创建粒子系统意味着设置基本物理参数, 然后为它们添加随机性.
粒子节点¶
Godot 为 2D 粒子提供了两个不同的节点: GPUParticles2D 和 CPUParticles2D 。 GPUParticles2D 更先进,使用 GPU 来处理粒子效果。 CPUParticles2D 是 CPU 驱动的选项,其功能与 GPUParticles2D 几乎相同,但在使用大量粒子时性能较低。 另一方面,CPUParticles2D 在低端系统或 GPU 瓶颈情况下可能表现更好。
虽然 GPUParticles2D 是通过 ParticleProcessMaterial(还可以使用自定义着色器)进行配置的,不过匹配的选项是通过 CPUParticles2D 中的节点属性提供的(除了轨迹设置)。
You can convert a GPUParticles2D node into a CPUParticles2D node by clicking on the node in the inspector, selecting the 2D viewport, and selecting GPUParticles2D > Convert to CPUParticles2D in the viewport toolbar.

本教程的其余部分将使用 GPUParticles2D 节点。 首先,将 GPUParticles2D 节点添加到场景中。 创建该节点后,你会注意到仅创建了一个白点,并且场景停靠栏中的 GPUParticles2D 节点旁边有一个警告图标。 这是因为节点需要 ParticleProcessMaterial 才能发挥作用。
ParticleProcessMaterial¶
要将处理材质添加到粒子节点,请转到检查器面板中的 处理材质
。单击 材质
旁边的框,然后从下拉菜单中选择 新建 ParticleProcessMaterial
。

你的 GPUParticles2D 节点现在应该可以向下发射白点了。

Texture (纹理)¶
A particle system can use a single texture or an animation flipbook. A flipbook is a texture that contains several frames of animation that can be played back, or chosen at random during emission. This is equivalent to a spritesheet for particles.
The texture is set via the Texture property:

Using an animation flipbook¶
Particle flipbooks are suited to reproduce complex effects such as smoke, fire, explosions. They can also be used to introduce random texture variation, by making every particle use a different texture. You can find existing particle flipbook images online, or pre-render them using external tools such as Blender or EmberGen.

Example of a particle system that uses a flipbook texture¶
Using an animation flipbook requires additional configuration compared to a single texture. For demonstration purposes, we'll use this texture with 5 columns and 7 rows (right-click and choose Save as…):

Credit: JoesAlotofthings (CC BY 4.0)¶
To use an animation flipbook, you must create a new CanvasItemMaterial in the Material section of the GPUParticles2D (or CPUParticles2D) node:

Creating a CanvasItemMaterial at the bottom of the particles node inspector¶
In this CanvasItemMaterial, enable Particle Animation and set H Frames and V Frames to the number of columns and rows present in your flipbook texture:

Configuring the CanvasItemMaterial for the example flipbook texture¶
Once this is done, the Animation section in ParticleProcessMaterial (for GPUParticles2D) or in the CPUParticles2D inspector will be effective.
小技巧
If your flipbook texture has a black background instead of a transparent background, you will also need to set the blend mode to Add instead of Mix for correct display. Alternatively, you can modify the texture to have a transparent background in an image editor. In GIMP, this can be done using the Color > Color to Alpha menu.
Time (时间)参数¶
Lifetime(寿命)¶
每个粒子存活的时间(以秒为单位). 生命周期结束时, 会创建一个新粒子来替换它.
寿命:0.5

寿命:4.0

One Shot (一次性)¶
启用后,GPUParticles2D 节点将一次发射其所有粒子,之后将不再发射。
Preprocess(预处理)¶
粒子系统从没有粒子被发射开始, 然后开始发射. 当加载场景如火炬, 雾等系统时可能会带来不便, 因为它会在进入场景的那一刻开始发射. 预处理用于让系统在第一次实际绘制之前处理给定的秒数.
Speed Scale(速度比例)¶
速度比例具有默认值 1
, 用于调整粒子系统的速度. 降低值会使粒子变慢, 而增加值会使粒子更快.
Explosiveness(爆炸性)¶
如果有10个寿命为 1
的粒子, 则意味着粒子将每0.1秒发射一次. 爆炸性参数改变了这一点, 并迫使粒子一起发射. 范围是:
0: 定期发射粒子(默认值).
1: 同时发射所有粒子.
中间的值也是允许的. 此功能对于创建爆炸或突然爆发的粒子非常有用:

Randomness(随机性)¶
所有物理参数都可以随机化. 随机值范围从 0
到 1
. 随机化参数的公式为:
initial_value = param_value + param_value * randomness
Fixed FPS(固定帧率)¶
此设置可用于将粒子系统设置为以固定的帧率渲染. 例如, 将值更改为 2
将使粒子以每秒2帧的速度渲染. 请注意, 这不会减慢粒子系统本身的速度.
Fract Delta¶
这可用于打开或关闭Fract Delta.
Drawing parameters (绘图参数)¶
Visibility Rect(可见性区域)¶
可见性矩形控制粒子在屏幕上的可见性. 如果此矩形位于视口之外, 则引擎将不会在屏幕上渲染粒子.
矩形的 W
和 H
属性分别控制其宽度和高度. X
和 Y
属性控制矩形左上角相对于粒子发射器的位置.
可以使用 2d 视图上方的工具栏让 Godot 自动生成可见性矩形。 为此,请选择 GPUParticles2D 节点并单击 粒子 > 生成可见性矩形
。 Godot 将模拟 Particles2D 节点发射粒子几秒钟,并设置矩形以适合粒子所占据的表面。
你可以使用 Generation Time (sec)
选项控制发射持续时间. 最大值为25秒. 如果你需要更多时间让粒子移动, 你可以暂时更改Particles2D节点上的 preprocess
时间.
Local Coords(局部坐标)¶
默认情况下, 此选项处于启用状态, 这意味着粒子发射的空间是相对于节点来算的. 如果移动节点, 则所有粒子会随之移动:

如果禁用, 粒子将发射到全局空间, 这意味着如果移动节点, 则已发射的粒子不会受到影响:

Draw Order(绘图顺序)¶
这可以控制绘制单个粒子的顺序. Index
表示粒子根据它们的发射顺序被绘制(默认). Lifetime
表示它们按照剩余寿命的顺序被绘制.
ParticleProcessMaterial 设置¶
方向¶
这是粒子发射的基础方向. 默认值是 Vector3(1,0,0)
, 它使粒子向右发射. 然而, 在默认的重力设置下, 粒子会直线下降.

为了让这个属性作用更明显, 你需要一个大于0的 初始速度(initial velocity) . 这里, 我们把初始速度设为40. 你会注意到粒子向右发射, 然后受重力作用下降.

Spread(铺开角度)¶
此参数是以度为单位的角度, 它会被随机加减到基础 Direction
上. 180
的铺开角度将向所有方向发射(+/- 180).

Flatness(平直度)¶
这个属性只对3D粒子有用.
Gravity(重力)¶
应用于每个粒子上的重力.

Initial Velocity(初始速度)¶
初始速度是粒子发射的速度(单位为像素/秒)。以后可以通过重力或其他加速度来修改速度(后述)。

Angular Velocity(角速度)¶
角速度是应用于粒子的初始角速度.
Spin Velocity(旋转速度)¶
旋转速度是粒子围绕其中心转动的速度(以度/秒为单位).

Orbit Velocity(环绕速度)¶
环绕速度速度用于使粒子绕它们的中心转动.

Linear Acceleration(线性加速度)¶
应用于每个粒子的线性加速度.
Radial Acceleration(径向加速度)¶
如果此加速度为正, 则粒子会向远离发射中心加速. 如果是负的, 他们会被加速吸进去.

Tangential Acceleration(切向加速度)¶
该加速度会使用从粒子到中心点的切向量, 结合径向加速度可以做出很酷炫的效果.

Damping(阻尼)¶
阻尼选项会对颗粒施加摩擦力, 迫使它们停止. 它特别适用于火花或爆炸, 火花或爆炸通常以高线速度开始, 然后在他们隐去时停下来.

Angle(角度)¶
确定粒子的初始角度(以度为单位). 该参数通常在随机化后会有用.

Scale(大小)¶
确定粒子的初始大小.

Color¶
用于改变发射出来的粒子颜色.
Hue Variation¶
Variation
值设置的是应用于每个粒子的初始色调变化. Variation Rand
值控制色调变化的随机性比率.
动画¶
备注
Particle flipbook animation is only effective if the CanvasItemMaterial used on the GPUParticles2D or CPUParticles2D node has been configured accordingly.
To set up the particle flipbook for linear playback, set the Speed Min and Speed Max values to 1:

Setting up particle animation for playback during the particle's lifetime¶
By default, looping is disabled. If the particle is done playing before its lifetime ends, the particle will keep using the flipbook's last frame (which may be fully transparent depending on how the flipbook texture is designed). If looping is enabled, the animation will loop back to the first frame and resume playing.
Depending on how many images your sprite sheet contains and for how long your particle is alive, the animation might not look smooth. The relationship between particle lifetime, animation speed, and number of images in the sprite sheet is this:
备注
At an animation speed of 1.0
, the animation will reach the last image
in the sequence just as the particle's lifetime ends.
If you wish the particle flipbook to be used as a source of random particle textures for every particle, keep the speed values at 0 and set Offset Max to 1 instead:

Setting up particle animation for random offset on emission¶
Note that the GPUParticles2D node's Fixed FPS also affects animation playback. For smooth animation playback, it's recommended to set it to 0 so that the particle is simulated on every rendered frame. If this is not an option for your use case, set Fixed FPS to be equal to the effective framerate used by the flipbook animation (see above for the formula).
发射形状¶
ParticleProcessMaterials 允许你设置发射蒙版,它决定发射粒子的区域和方向。 这些可以从项目中的纹理生成。
在设置了 ParticleProcessMaterial,并且选择了 GPUParticles2D 节点。 工具栏中就会出现“粒子”菜单:

打开它, 选择"加载 Emission Mask(发射遮挡)":

然后选择你想要用作遮挡的纹理:

会出现一个具有多个设置的对话框.
发射遮罩¶
纹理可以生成三种类型的发射遮挡:
Solid Pixels(实心像素): 粒子将从纹理的任何区域产生, 透明区域除外.

Border Pixels(边界像素): 粒子将从纹理的外边缘产生.

Directed Border Pixels(定向边界像素): 类似于边界像素, 在遮罩中添加额外的信息, 使粒子能够从边缘发射出去. 请注意, 想要使用它, 需要设置一个
初始速度
.

发射色彩¶
Capture from Pixel
会使粒子在其产生点处继承遮挡材质的颜色.
在单击“确定”后,将生成遮罩并将其设置为 Emission Shape
下的 ParticleProcessMaterial:

这个部分所有提到的值, "加载Emission Mask(发射遮挡)"菜单都会自动生成, 所以通常他们放着不管就可以了.
备注
不应该直接添加图像到 Point Texture
或 Color Texture
中. 应该始终使用"加载Emisson Mask(发射遮挡)"菜单.