particle_shader.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. .. _doc_particle_shader:
  2. Particle shaders
  3. ================
  4. Particle shaders are a special type of shader that runs before the object is
  5. drawn. They are used for calculating material properties such as color,
  6. position, and rotation. They are drawn with any regular material for CanvasItem
  7. or Spatial, depending on whether they are 2D or 3D.
  8. Particle shaders are unique because they are not used to draw the object itself;
  9. they are used to calculate particle properties, which are then used by a
  10. :ref:`CanvasItem<doc_canvas_item_shader>` or :ref:`Spatial<doc_spatial_shader>`
  11. shader. They contain two processor functions: ``start()`` and ``process()``.
  12. Unlike other shader types, particle shaders keep the data that was output the
  13. previous frame. Therefore, particle shaders can be used for complex effects that
  14. take place over multiple frames.
  15. .. note::
  16. Particle shaders are only available with GPU-based particle nodes
  17. (:ref:`class_GPUParticles2D` and :ref:`class_GPUParticles3D`).
  18. CPU-based particle nodes (:ref:`class_CPUParticles2D` and
  19. :ref:`class_CPUParticles3D`) are *rendered* on the GPU (which means they can
  20. use custom CanvasItem or Spatial shaders), but their motion is *simulated*
  21. on the CPU.
  22. Render modes
  23. ^^^^^^^^^^^^
  24. +--------------------------+-------------------------------------------+
  25. | Render mode | Description |
  26. +==========================+===========================================+
  27. | **keep_data** | Do not clear previous data on restart. |
  28. +--------------------------+-------------------------------------------+
  29. | **disable_force** | Disable attractor force. |
  30. +--------------------------+-------------------------------------------+
  31. | **disable_velocity** | Ignore ``VELOCITY`` value. |
  32. +--------------------------+-------------------------------------------+
  33. | **collision_use_scale** | Scale the particle's size for collisions. |
  34. +--------------------------+-------------------------------------------+
  35. Built-ins
  36. ^^^^^^^^^
  37. Values marked as ``in`` are read-only. Values marked as ``out`` can optionally be written to and will
  38. not necessarily contain sensible values. Values marked as ``inout`` provide a sensible default
  39. value, and can optionally be written to. Samplers cannot be written to so they are not marked.
  40. Global built-ins
  41. ^^^^^^^^^^^^^^^^
  42. Global built-ins are available everywhere, including custom functions.
  43. +-------------------+------------------------------------------------------------------------------------------+
  44. | Built-in | Description |
  45. +===================+==========================================================================================+
  46. | in float **TIME** | Global time since the engine has started, in seconds. It repeats after every ``3,600`` |
  47. | | seconds (which can be changed with the |
  48. | | :ref:`rollover<class_ProjectSettings_property_rendering/limits/time/time_rollover_secs>` |
  49. | | setting). It's affected by |
  50. | | :ref:`time_scale<class_Engine_property_time_scale>` but not by pausing. If you need a |
  51. | | ``TIME`` variable that is not affected by time scale, add your own |
  52. | | :ref:`global shader uniform<doc_shading_language_global_uniforms>` and update it each |
  53. | | frame. |
  54. +-------------------+------------------------------------------------------------------------------------------+
  55. | in float **PI** | A ``PI`` constant (``3.141592``). |
  56. | | A ratio of a circle's circumference to its diameter and amount of radians in half turn. |
  57. +-------------------+------------------------------------------------------------------------------------------+
  58. | in float **TAU** | A ``TAU`` constant (``6.283185``). |
  59. | | An equivalent of ``PI * 2`` and amount of radians in full turn. |
  60. +-------------------+------------------------------------------------------------------------------------------+
  61. | in float **E** | An ``E`` constant (``2.718281``). Euler's number and a base of the natural logarithm. |
  62. +-------------------+------------------------------------------------------------------------------------------+
  63. Start and Process built-ins
  64. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  65. These properties can be accessed from both the ``start()`` and ``process()`` functions.
  66. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  67. | Function | Description |
  68. +====================================+=========================================================================================================================================+
  69. | in float **LIFETIME** | Particle lifetime. |
  70. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  71. | in float **DELTA** | Delta process time. |
  72. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  73. | in uint **NUMBER** | Unique number since emission start. |
  74. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  75. | in uint **INDEX** | Particle index (from total particles). |
  76. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  77. | in mat4 **EMISSION_TRANSFORM** | Emitter transform (used for non-local systems). |
  78. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  79. | in uint **RANDOM_SEED** | Random seed used as base for random. |
  80. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  81. | inout bool **ACTIVE** | ``true`` when the particle is active, can be set ``false``. |
  82. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  83. | inout vec4 **COLOR** | Particle color, can be written to and accessed in mesh's vertex function. |
  84. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  85. | inout vec3 **VELOCITY** | Particle velocity, can be modified. |
  86. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  87. | inout mat4 **TRANSFORM** | Particle transform. |
  88. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  89. | inout vec4 **CUSTOM** | Custom particle data. Accessible from shader of mesh as ``INSTANCE_CUSTOM``. |
  90. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  91. | inout float **MASS** | Particle mass, intended to be used with attractors. Equals ``1.0`` by default. |
  92. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  93. | in vec4 **USERDATAX** | Vector that enables the integration of supplementary user-defined data into the particle process shader. |
  94. | | ``USERDATAX`` are six built-ins identified by number, ``X`` can be numbers between 1 and 6, for example ``USERDATA3``. |
  95. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  96. | 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. |
  97. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  98. | 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.|
  99. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  100. | 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. |
  101. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  102. | 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. |
  103. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  104. | 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. |
  105. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  106. | in vec3 **EMITTER_VELOCITY** | Velocity of the :ref:`Particles2D<class_GPUParticles2D>` (:ref:`3D<class_GPUParticles3D>`) node. |
  107. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  108. | in float **INTERPOLATE_TO_END** | Value of :ref:`interp_to_end<class_GPUParticles2D_property_interp_to_end>` |
  109. | | (:ref:`3D<class_GPUParticles3D_property_interp_to_end>`) property of Particles node. |
  110. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  111. | in uint **AMOUNT_RATIO** | Value of :ref:`amount_ratio<class_GPUParticles2D_property_amount_ratio>` |
  112. | | (:ref:`3D<class_GPUParticles3D_property_amount_ratio>`) property of Particles node. |
  113. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  114. .. note:: In order to use the ``COLOR`` variable in a StandardMaterial3D, set ``vertex_color_use_as_albedo``
  115. to ``true``. In a ShaderMaterial, access it with the ``COLOR`` variable.
  116. Start built-ins
  117. ^^^^^^^^^^^^^^^
  118. +---------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  119. | Built-in | Description |
  120. +=================================+=======================================================================================================================================================================================+
  121. | 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). |
  122. +---------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  123. | 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). |
  124. +---------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  125. | 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). |
  126. +---------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  127. | 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). |
  128. +---------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  129. | 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). |
  130. +---------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  131. Process built-ins
  132. ^^^^^^^^^^^^^^^^^
  133. +------------------------------------+-------------------------------------------------------------------------------------------------------+
  134. | Built-in | Description |
  135. +====================================+=======================================================================================================+
  136. | in bool **RESTART** | ``true`` if the current process frame is first for the particle. |
  137. +------------------------------------+-------------------------------------------------------------------------------------------------------+
  138. | in bool **COLLIDED** | ``true`` when the particle has collided with a particle collider. |
  139. +------------------------------------+-------------------------------------------------------------------------------------------------------+
  140. | in vec3 **COLLISION_NORMAL** | A normal of the last collision. If there is no collision detected it is equal to ``(0.0, 0.0, 0.0)``. |
  141. +------------------------------------+-------------------------------------------------------------------------------------------------------+
  142. | in float **COLLISION_DEPTH** | A length of normal of the last collision. If there is no collision detected it is equal to ``0.0``. |
  143. +------------------------------------+-------------------------------------------------------------------------------------------------------+
  144. | in vec3 **ATTRACTOR_FORCE** | A combined force of the attractors at the moment on that particle. |
  145. +------------------------------------+-------------------------------------------------------------------------------------------------------+
  146. Process functions
  147. ^^^^^^^^^^^^^^^^^
  148. ``emit_subparticle()`` is currently the only custom function supported by
  149. particles shaders. It allows users to add a new particle with specified
  150. parameters from a sub-emitter. The newly created particle will only use the
  151. properties that match the ``flags`` parameter. For example, the
  152. following code will emit a particle with a specified position, velocity, and
  153. color, but unspecified rotation, scale, and custom value:
  154. .. code-block:: glsl
  155. mat4 custom_transform = mat4(1.0);
  156. custom_transform[3].xyz = vec3(10.5, 0.0, 4.0);
  157. 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);
  158. +--------------------------------------------------------------------------------------------+--------------------------------------+
  159. | Function | Description |
  160. +============================================================================================+======================================+
  161. | bool **emit_subparticle** (mat4 xform, vec3 velocity, vec4 color, vec4 custom, uint flags) | Emits a particle from a sub-emitter. |
  162. +--------------------------------------------------------------------------------------------+--------------------------------------+