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.

粒子

簡介

Godot提供了一種簡單(但對於大多數用途而言足夠靈活)的粒子系統. 粒子系統用於類比複雜的物理效應, 如火花, 火焰, 魔法粒子, 煙霧, 霧氣, 等.

這個想法是以固定的間隔發射具有固定的壽命的 "粒子". 在其生命週期中, 每個粒子都具有相同的基本行為. 讓每個粒子變得不同並提供整體更加 "有機" 外觀的是與各個參數相關的 "隨機性". 實質上, 建立粒子系統意味著設定基本物理參數, 然後為它們新增隨機性.

粒子

Godot 為 2D 粒子提供了兩個不同的節點:class_Particles2DCPUParticles2D。Particles2D 更為先進,使用 GPU 處理粒子效果,但這將其限制於相對高端的圖形 API 中,在我們的情況下僅限於 GLES3 渲染器。對於使用 GLES2 後端的專案,CPUParticles2D 是一個 CPU 驅動的選項。其功能與 Particles2D 接近,但性能較低。Particles2D 是通過 :ref:`class_ParticlesMaterial`(以及可選的自訂 Shader)來配置的,但相應的選項在 CPUParticles2D 中是通過節點屬性提供的(軌跡設定除外)。您可以通過按一下場景中的節點,然後在工具列的“粒子”功能表中選擇“轉換為 CPU 粒子”,將 Particles2D 節點轉換為 CPUParticles2D 節點。

雖然 GPUParticles2D 是通過 :ref:`class_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.

../../_images/particles_convert.webp

這個教學後續將使用 Particles2D 節點。首先,新增一個 Particles2D 節點到場景中。在建立該節點後,您會注意到只建立了一個白點,並且在場景面板的 Particles2D 節點旁邊有一個警告圖示。這是因為節點需要 ParticlesMaterial 才能工作。

ParticleProcessMaterial

要將運作材質新增到粒子節點,請轉到屬性面板面板中的 Process Material。按一下 Material 旁邊的框,然後從下拉式功能表中選擇 新建 ParticlesMaterial

../../_images/particles_material.png

您的 Particles2D 節點現在應該可以向下發射白點了。

../../_images/particles1.png

紋理貼圖

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:

../../_images/particles2.png

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

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…):

Particle flipbook texture example

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

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

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 (時間)參數

生命期

每個粒子存活的時間(以秒為單位). 生命週期結束時, 會建立一個新粒子來替換它.

壽命:0.5

../../_images/paranim14.gif

壽命:4.0

../../_images/paranim15.gif

一次性

啟用後,Particles2D節點將一次性發出所有粒子, 然後再也不發射.

預處理

粒子系統從沒有粒子被發射開始, 然後開始發射. 當載入場景如火炬, 霧等系統時可能會帶來不便, 因為它會在進入場景的那一刻開始發射. 預處理用於讓系統在第一次實際繪製之前處理給定的秒數.

速度縮放

速度比例具有預設值 1 , 用於調整粒子系統的速度. 降低值會使粒子變慢, 而增加值會使粒子更快.

爆炸性

如果有10個壽命為 1 的粒子, 則意味著粒子將每0.1秒發射一次. 爆炸性參數改變了這一點, 並迫使粒子一起發射. 範圍是:

  • 0: 定期發射粒子(預設值).

  • 1: 同時發射所有粒子.

中間的值也是允許的. 此功能對於建立爆炸或突然爆發的粒子非常有用:

../../_images/paranim18.gif

隨機性

所有物理參數都可以隨機化. 隨機值範圍從 01 . 隨機化參數的公式為:

initial_value = param_value + param_value * randomness

固定 FPS

此設定可用於將粒子系統設定為以固定的影格率渲染. 例如, 將值更改為 2 將使粒子以每秒2影格的速度渲染. 請注意, 這不會減慢粒子系統本身的速度.

分數差異量

這可用於打開或關閉Fract Delta.

Drawing parameters (繪圖參數)

可見矩形

可見性矩形控制粒子在螢幕上的可見性. 如果此矩形位於視口之外, 則引擎將不會在螢幕上渲染粒子.

矩形的 WH 屬性分別控制其寬度和高度. XY 屬性控制矩形左上角相對於粒子發射器的位置.

可以使用2D視圖上方的工具列自動生成可見性區域. 為此, 請選擇Particles2D節點, 然後按一下 粒子 > 生成視覺化區域 . Godot將模擬Particles2D節點發射粒子幾秒鐘, 並將矩形區域設定為適合粒子的大小.

你可以使用 Generation Time (sec) 選項控制發射持續時間. 最大值為25秒. 如果您需要更多時間讓粒子移動, 您可以暫時更改Particles2D節點上的 preprocess 時間.

本地座標

預設情況下, 此選項處於啟用狀態, 這意味著粒子發射的空間是相對於節點來算的. 如果移動節點, 則所有粒子會隨之移動:

../../_images/paranim20.gif

如果禁用, 粒子將發射到全域空間, 這意味著如果移動節點, 則已發射的粒子不會受到影響:

../../_images/paranim21.gif

繪製順序

這可以控制繪製單個粒子的順序. Index 表示粒子根據它們的發射順序被繪製(預設). Lifetime 表示它們按照剩餘壽命的順序被繪製.

ParticleProcessMaterial 設定

方向

這是粒子發射的基礎方向. 預設值是 Vector3(1,0,0) , 它使粒子向右發射. 然而, 在預設的重力設定下, 粒子會直線下降.

../../_images/direction1.png

為了讓這個屬性作用更明顯, 你需要一個大於0的 初始速度(initial velocity) . 這裡, 我們把初始速度設為40. 你會注意到粒子向右發射, 然後受重力作用下降.

../../_images/direction2.png

發散

此參數是以度為單位的角度, 它會被隨機加減到基礎 Direction 上. 180 的鋪開角度將向所有方向發射(+/- 180).

../../_images/paranim3.gif

扁平度

這個屬性只對3D粒子有用.

重力

應用於每個粒子上的重力.

../../_images/paranim7.gif

初速度

初始速度是粒子發射的速度(單位為像素/秒)。以後可以通過重力或其他加速度來修改速度(後述)。

../../_images/paranim4.gif

角速度

角速度是應用於粒子的初始角速度.

Spin Velocity(旋轉速度)

旋轉速度是粒子圍繞其中心轉動的速度(以度/秒為單位).

../../_images/paranim5.gif

環繞速度

環繞速度速度用於使粒子繞它們的中心轉動.

../../_images/paranim6.gif

Linear Acceleration(線性加速度)

應用於每個粒子的線性加速度.

Radial Acceleration(徑向加速度)

如果此加速度為正, 則粒子會向遠離發射中心加速. 如果是負的, 他們會被加速吸進去.

../../_images/paranim8.gif

Tangential Acceleration(切向加速度)

該加速度會使用從粒子到中心點的切向量, 結合徑向加速度可以做出很酷炫的效果.

../../_images/paranim9.gif

阻尼

阻尼選項會對顆粒施加摩擦力, 迫使它們停止. 它特別適用於火花或爆炸, 火花或爆炸通常以高線速度開始, 然後在他們隱去時停下來.

../../_images/paranim10.gif

角度

確定粒子的初始角度(以度為單位). 該參數通常在隨機化後會有用.

../../_images/paranim11.gif

縮放

確定粒子的初始大小.

../../_images/paranim12.gif

顏色

用於改變發射出來的粒子顏色.

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

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.

\[Animation\ FPS = \frac{Number\ of\ images}{Lifetime}\]

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

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).

碰撞區域的顯示

ParticlesMaterial 可以讓你設定一個發射遮擋(Emission Mask), 它決定了粒子發射的區域和方向. 這些可以用專案中的紋理來生成.

確保設定了ParticlesMaterial, 選中Particles2D節點. 工具列中應顯示 "Particles(粒子)" 功能表:

../../_images/emission_shapes1.png

打開它, 選擇"載入 Emission Mask(發射遮擋)":

../../_images/emission_shapes2.png

然後選擇你想要用作遮擋的紋理:

../../_images/emission_shapes3.png

會出現一個具有多個設定的對話方塊.

發射遮罩

紋理可以生成三種型別的發射遮擋:

  • Solid Pixels(實心像素): 粒子將從紋理的任何區域產生, 透明區域除外.

../../_images/emission_mask_solid.gif
  • Border Pixels(邊界像素): 粒子將從紋理的外邊緣產生.

../../_images/emission_mask_border.gif
  • Directed Border Pixels(定向邊界像素): 類似於邊界像素, 在遮罩中新增額外的資訊, 使粒子能夠從邊緣發射出去. 請注意, 想要使用它, 需要設定一個 初始速度.

../../_images/emission_mask_directed_border.gif

發射色彩

Capture from Pixel 會使粒子在其產生點處繼承遮擋材質的顏色.

一旦你點擊 "確定", 將生成遮擋並設定為粒子材質. 可以在 Emission Shape(發射形狀) 部分看到它:

../../_images/emission_shapes4.png

這個部分所有提到的值, "載入Emission Mask(發射遮擋)"功能表都會自動生成, 所以通常他們放著不管就可以了.

備註

不應該直接新增圖像到 Point TextureColor Texture 中. 應該始終使用"載入Emisson Mask(發射遮擋)"功能表.