Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

粒子着色器

Particle shaders are a special type of shader that runs before the object is drawn. They are used for calculating material properties such as color, position, and rotation. They are drawn with any regular material for CanvasItem or Spatial, depending on whether they are 2D or 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 the CanvasItem of Spatial shader. They contain two processor functions: start() and process().

Unlike other shader types, particle shaders keep the data that was output the previous frame. Therefore, particle shaders can be used for complex effects that take place over multiple frames.

备注

Particle shaders are only available with GPU-based particle nodes (GPUParticles2D and GPUParticles3D).

CPU-based particle nodes (CPUParticles2D and CPUParticles3D) are rendered on the GPU (which means they can use custom CanvasItem or Spatial shaders), but their motion is simulated on the CPU.

渲染模式

渲染模式

描述

keep_data

不要在重启时清除以前的数据.

disable_force

Disable attractor force.

disable_velocity

忽略 VELOCITY 值.

内置

标记为 "in" 的值是只读的. 标记为 "out" 的值是可以选择写入的, 不一定包含合理的值. 标记为 "inout" 的值提供一个合理的默认值, 并且可以选择写入. 采样器不是写入的对象, 它们没有被标记.

全局内置

全局内置的功能随处可见, 包括自定义功能.

内置

描述

in float TIME

全球时间, 以秒为单位.

in float PI

A PI constant (3.141592). A ration of circle's circumference to its diameter and amount of radians in half turn.

in float TAU

A TAU constant (6.283185). An equivalent of PI * 2 and amount of radians in full turn.

in float E

A E constant (2.718281). Euler's number and a base of the natural logarithm.

Start and Process built-ins

函数

描述

in float LIFETIME

粒子寿命。

in float DELTA

处理时间差。

in uint NUMBER

发射开始后的唯一编号。

in uint INDEX

粒子索引(在所有粒子范围内)。

in mat4 EMISSION_TRANSFORM

发射器变换(用于非本地坐标系)。

in uint RANDOM_SEED

随机种子,是随机的基础。

inout bool ACTIVE

true when the particle is active, can be set false.

inout vec4 COLOR

粒子的颜色, 可以写入Mesh的顶点函数中, 也可以访问.

inout vec3 VELOCITY

粒子速度, 可以修改.

inout mat4 TRANSFORM

粒子变换。

inout vec4 CUSTOM

自定义粒子数据。网格的着色器可以使用 INSTANCE_CUSTOM 访问。

inout float MASS

Particle mass, intended to be used with attractors. Equals 1.0 by default.

备注

In order to use the COLOR variable in a StandardMaterial3D, set vertex_color_use_as_albedo to true. In a ShaderMaterial, access it with the COLOR variable.

Start built-ins

内置

描述

in bool RESTART_POSITION

in bool RESTART_ROT_SCALE

in bool RESTART_VELOCITY

in bool RESTART_COLOR

in bool RESTART_CUSTOM

in bool RESTART_VELOCITY

Process built-ins

内置

描述

in bool RESTART

true if the current process frame is first for the particle.

in uint FLAG_EMIT_POSITION

A flag for using on the last argument of emit_subparticle function to assign a position to a new particle's transform.

in uint FLAG_EMIT_ROT_SCALE

A flag for using on the last argument of emit_subparticle function to assign the rotation and scale to a new particle's transform.

in uint FLAG_EMIT_VELOCITY

A flag for using on the last argument of emit_subparticle function to assign a velocity to a new particle.

in uint FLAG_EMIT_COLOR

A flag for using on the last argument of emit_subparticle function to assign a color to a new particle.

in uint FLAG_EMIT_CUSTOM

A flag for using on the last argument of emit_subparticle function to assign a custom data vector to a new particle.

in bool COLLIDED

true when the particle has collided with a particle collider.

in vec3 COLLISION_NORMAL

A normal of the last collision. If there is no collision detected it is equal to vec3(0.0).

in float COLLISION_DEPTH

A length of normal of the last collision. If there is no collision detected it is equal to 0.0.

in vec3 ATTRACTOR_FORCE

A combined force of the attractors at the moment on that particle.

in vec4 USERDATAX

Vector that enables the integration of supplementary user-defined data into the particle process shader. USERDATAX are six built-ins identified by number, X can be numbers between 1 and 6.

Process functions

函数

描述

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

Forces to emit a particle from a sub-emitter.