Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

粒子著色器

粒子著色器是一種特殊型別的頂點著色器, 在物體被繪製之前運作. 它們用於計算材質屬性, 如顏色, 位置和旋轉. 它們與CanvasItem或Spatial的任何常規材質一起繪製, 這取決於它們是2D還是3D.

Particle shaders are unique because they are not used to draw the object itself; they are used to calculate particle properties, which are then used by a CanvasItem or Spatial shader. They contain two processor functions: start() and process().

與其他著色器型別不同,粒子著色器保留前一影格輸出的資料。因此,粒子著色器可用於在多個影格上發生的複雜效果。

備註

粒子著色器僅在 GLES3 後端中可用。如果您需要在 GLES2 中使用粒子系統,請使用 CPUParticles

基於 CPU 的粒子節點(class_CPUParticles2D 和 class_CPUParticles3D)在 GPU 上「渲染」(這表示它們可以使用自訂 CanvasItem 或空間著色器),但它們的運動是在 CPU 上「模擬」 。

渲染模式

渲染模式

說明

keep_data

不要在重啟時清除以前的資料.

disable_force

禁用吸引力。

disable_velocity

忽略 VELOCITY 值.

collision_use_scale

Scale the particle's size for collisions.

內建

標記為 "in" 的值是唯讀的. 標記為 "out" 的值是可以選擇寫入的, 不一定包含合理的值. 標記為 "inout" 的值提供一個合理的預設值, 並且可以選擇寫入. 取樣器不是寫入的物件, 它們沒有被標記.

定數

全域內建的功能隨處可見, 包括自訂功能.

內建

說明

in float TIME

全球時間, 以秒為單位.

in float PI

一個“PI”常數(“3.141592”)。圓的周長與其直徑的比率以及半圈的弧度數。

in float TAU

一個“TAU”常數(“6.283185”)。相當於“PI * 2”和完整的弧度數。

in float E

一個“E”常數(“2.718281”)。歐拉數和自然對數的底數。

頂點內建

These properties can be accessed from both the start() and process() functions.

函式

說明

in float LIFETIME

粒子壽命。

in float DELTA

處理時間差。

in uint NUMBER

發射開始後的唯一編號。

in uint INDEX

粒子索引(在所有粒子範圍內)。

in mat4 EMISSION_TRANSFORM

發射器變換(用於非本地坐標系)。

in uint RANDOM_SEED

隨機種子,是隨機的基礎。

inout bool ACTIVE

當粒子被啟動時為 true , 可以設定為 false .

inout vec4 COLOR

粒子的顏色, 可以寫入Mesh的頂點函式中, 也可以存取.

inout vec3 VELOCITY

粒子速度, 可以修改.

inout mat4 TRANSFORM

粒子變換。

inout vec4 CUSTOM

自訂粒子資料。網格的著色器可以使用 INSTANCE_CUSTOM 存取。

inout float MASS

粒子質量,旨在與吸引子一起使用。預設等於``1.0``。

in vec4 USERDATAX

可將補充的使用者定義資料整合到粒子過程著色器中的向量。 USERDATAX 是由數字標識的六個內建函式,X 可以是 1 到 6 之間的數字。

in uint FLAG_EMIT_POSITION

用於在「emit_subarticle」函式的最後一個參數上使用的旗標,用於為新粒子的變換分配位置。

in uint FLAG_EMIT_ROT_SCALE

用於在“emit_subarticle”函式的最後一個參數上使用的旗標,將旋轉和縮放分配給新粒子的變換。

in uint FLAG_EMIT_VELOCITY

用於使用“emit_subarticle”函式的最後一個參數來為新粒子分配速度的旗標。

in uint FLAG_EMIT_COLOR

用於在“emit_subarticle”函式的最後一個參數上使用的旗標,為新粒子分配顏色。

in uint FLAG_EMIT_CUSTOM

用於在「emit_subarticle」函式的最後一個參數上使用的旗標,以將自訂資料向量指派給新粒子。

in vec3 EMITTER_VELOCITY

Velocity of the Particles node.

in float INTERPOLATE_TO_END

Value of interp_to_end property of Particles node.

in uint AMOUNT_RATIO

Value of amount_ratio property of Particles node.

備註

為了在SpatialMaterial中使用 COLOR 變數, 將 use_vertex_as_albedo 設定為 true . 在ShaderMaterial中, 用 COLOR 變數存取它.

定數

內建

說明

in bool RESTART_POSITION

true if particle is restarted, or emitted without a custom position (i.e. this particle was created by emit_subparticle() without the FLAG_EMIT_POSITION flag).

in bool RESTART_ROT_SCALE

true if particle is restarted, or emitted without a custom rotation or scale (i.e. this particle was created by emit_subparticle() without the FLAG_EMIT_ROT_SCALE flag).

in bool RESTART_VELOCITY

true if particle is restarted, or emitted without a custom velocity (i.e. this particle was created by emit_subparticle() without the FLAG_EMIT_VELOCITY flag).

in bool RESTART_COLOR

true if particle is restarted, or emitted without a custom color (i.e. this particle was created by emit_subparticle() without the FLAG_EMIT_COLOR flag).

in bool RESTART_CUSTOM

true if particle is restarted, or emitted without a custom property (i.e. this particle was created by emit_subparticle() without the FLAG_EMIT_CUSTOM flag).

定數

內建

說明

in bool RESTART

true , 如果目前面是正面.

in bool COLLIDED

true 當粒子必須重新啟動時(生命週期迴圈).

in vec3 COLLISION_NORMAL

上次碰撞的法線。如果沒有偵測到碰撞,則等於「vec3(0.0)」。

in float COLLISION_DEPTH

最後一次碰撞的法線長度。如果沒有偵測到碰撞,則等於「0.0」。

in vec3 ATTRACTOR_FORCE

此時該粒子上吸引子的合力。

其他函式

emit_subparticle is currently the only custom function supported by particles shaders. It allows users to add a new particle with specified parameters from a sub-emitter. The newly created particle will only use the properties that match the flags parameter. For example, the following code will emit a particle with a specified position, velocity, and color, but unspecified rotation, scale, and custom value:

mat4 custom_transform = mat4(1.0);
custom_transform[3].xyz = vec3(10.5, 0.0, 4.0);
emit_subparticle(custom_transform, vec3(1.0, 0.5, 1.0), vec4(1.0, 0.0, 0.0, 1.0), vec4(1.0), FLAG_EMIT_POSITION | FLAG_EMIT_VELOCITY | FLAG_EMIT_COLOR);

函式

說明

bool emit_subparticle (mat4 xform, vec3 velocity, vec4 color, vec4 custom, uint flags)

Emits a particle from a sub-emitter.