particle_shader.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. .. _doc_particle_shader:
  2. Particle shaders
  3. ================
  4. Particle shaders are a special type of vertex shader that runs before the
  5. object is drawn. They are used for calculating material properties such as
  6. color, position, and rotation. They are drawn with any regular material for
  7. CanvasItem or Spatial, depending on whether they are 2D or 3D.
  8. Particle shaders are unique because they are not used to draw the object
  9. itself; they are used to calculate particle properties, which are then used
  10. by the CanvasItem of Spatial shader. They contain only a vertex processor
  11. function that outputs multiple properties (see built-ins below).
  12. Particle shaders use a transform feedback shader, which is a special type of
  13. vertex shader that runs on its own. It takes in data in a buffer like a regular
  14. vertex shader does, but it also outputs to data buffers instead of outputting
  15. to the fragment shader for pixel-processing. Because of this, transform feedback
  16. shaders can build on themselves each run, unlike other shaders that discard the
  17. data they have calculated once they draw to the frame buffer.
  18. .. note:: Particle shaders are only available in the GLES3 backend. If you need
  19. particles in GLES2, use :ref:`CPUParticles <class_CPUParticles>`.
  20. Render modes
  21. ^^^^^^^^^^^^
  22. +-----------------------+----------------------------------------+
  23. | Render mode | Description |
  24. +=======================+========================================+
  25. | **keep_data** | Do not clear previous data on restart. |
  26. +-----------------------+----------------------------------------+
  27. | **disable_force** | Disable attractor force. |
  28. +-----------------------+----------------------------------------+
  29. | **disable_velocity** | Ignore **VELOCITY** value. |
  30. +-----------------------+----------------------------------------+
  31. Built-ins
  32. ^^^^^^^^^
  33. Values marked as "in" are read-only. Values marked as "out" are for optional writing and will
  34. not necessarily contain sensible values. Values marked as "inout" provide a sensible default
  35. value, and can optionally be written to. Samplers are not subjects of writing and they are
  36. not marked.
  37. Global built-ins
  38. ^^^^^^^^^^^^^^^^
  39. Global built-ins are available everywhere, including custom functions.
  40. +-------------------+----------------------------------------------------------------------------------------+
  41. | Built-in | Description |
  42. +===================+========================================================================================+
  43. | in float **TIME** | Global time, in seconds. |
  44. +-------------------+----------------------------------------------------------------------------------------+
  45. | in float **PI** | A ``PI`` constant (``3.141592``). |
  46. | | A ration of circle's circumference to its diameter and amount of radians in half turn. |
  47. +-------------------+----------------------------------------------------------------------------------------+
  48. | in float **TAU** | A ``TAU`` constant (``6.283185``). |
  49. | | An equivalent of ``PI * 2`` and amount of radians in full turn. |
  50. +-------------------+----------------------------------------------------------------------------------------+
  51. | in float **E** | A ``E`` constant (``2.718281``). Euler's number and a base of the natural logarithm. |
  52. +-------------------+----------------------------------------------------------------------------------------+
  53. Start and Process built-ins
  54. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  55. +---------------------------------+--------------------------------------------------------------------------------+
  56. | Function | Description |
  57. +=================================+================================================================================+
  58. | in float **LIFETIME** | Particle lifetime. |
  59. +---------------------------------+--------------------------------------------------------------------------------+
  60. | in float **DELTA** | Delta process time. |
  61. +---------------------------------+--------------------------------------------------------------------------------+
  62. | in uint **NUMBER** | Unique number since emission start. |
  63. +---------------------------------+--------------------------------------------------------------------------------+
  64. | in uint **INDEX** | Particle index (from total particles). |
  65. +---------------------------------+--------------------------------------------------------------------------------+
  66. | in mat4 **EMISSION_TRANSFORM** | Emitter transform (used for non-local systems). |
  67. +---------------------------------+--------------------------------------------------------------------------------+
  68. | in uint **RANDOM_SEED** | Random seed used as base for random. |
  69. +---------------------------------+--------------------------------------------------------------------------------+
  70. | inout bool **ACTIVE** | ``true`` when Particle is active, can be set ``false``. |
  71. +---------------------------------+--------------------------------------------------------------------------------+
  72. | inout vec4 **COLOR** | Particle color, can be written to and accessed in mesh's vertex function. |
  73. +---------------------------------+--------------------------------------------------------------------------------+
  74. | inout vec3 **VELOCITY** | Particle velocity, can be modified. |
  75. +---------------------------------+--------------------------------------------------------------------------------+
  76. | inout mat4 **TRANSFORM** | Particle transform. |
  77. +---------------------------------+--------------------------------------------------------------------------------+
  78. | inout vec4 **CUSTOM** | Custom particle data. Accessible from shader of mesh as **INSTANCE_CUSTOM**. |
  79. +---------------------------------+--------------------------------------------------------------------------------+
  80. | inout float **MASS** | Particle mass, intended to be used with attractors. Equals ``1.0`` by default. |
  81. +---------------------------------+--------------------------------------------------------------------------------+
  82. .. note:: In order to use the ``COLOR`` variable in a StandardMaterial3D, set ``vertex_color_use_as_albedo``
  83. to ``true``. In a ShaderMaterial, access it with the ``COLOR`` variable.
  84. Start built-ins
  85. ^^^^^^^^^^^^^^^
  86. +---------------------------------+-------------+
  87. | Built-in | Description |
  88. +=================================+=============+
  89. | in bool **RESTART_POSITION** | |
  90. +---------------------------------+-------------+
  91. | in bool **RESTART_ROT_SCALE** | |
  92. +---------------------------------+-------------+
  93. | in bool **RESTART_VELOCITY** | |
  94. +---------------------------------+-------------+
  95. | in bool **RESTART_COLOR** | |
  96. +---------------------------------+-------------+
  97. | in bool **RESTART_CUSTOM** | |
  98. +---------------------------------+-------------+
  99. | in bool **RESTART_VELOCITY** | |
  100. +---------------------------------+-------------+
  101. Process built-ins
  102. ^^^^^^^^^^^^^^^^^
  103. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  104. | Built-in | Description |
  105. +====================================+=========================================================================================================================================+
  106. | in bool **RESTART** | ``true`` if the current process frame is first for the particle. |
  107. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  108. | 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. |
  109. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  110. | 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. |
  111. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  112. | 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. |
  113. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  114. | 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. |
  115. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  116. | 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. |
  117. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  118. | in bool **COLLIDED** | ``true`` when the particle has collided with a particle collider. |
  119. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  120. | in vec3 **COLLISION_NORMAL** | A normal of the last collision. If there is no collision detected it is equal to ``vec3(0.0)``. |
  121. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  122. | in float **COLLISION_DEPTH** | A length of normal of the last collision. If there is no collision detected it is equal to ``0.0``. |
  123. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  124. | in vec3 **ATTRACTOR_FORCE** | A combined force of the attractors at the moment on that particle. |
  125. +------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
  126. Process functions
  127. ^^^^^^^^^^^^^^^^^
  128. +--------------------------------------------------------------------------------------------+-----------------------------------------------+
  129. | Function | Description |
  130. +============================================================================================+===============================================+
  131. | bool **emit_subparticle** (mat4 xform, vec3 velocity, vec4 color, vec4 custom, uint flags) | Forces to emit a particle from a sub-emitter. |
  132. +--------------------------------------------------------------------------------------------+-----------------------------------------------+