particle_systems_2d.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. :article_outdated: True
  2. .. _doc_particle_systems_2d:
  3. 2D particle systems
  4. ===================
  5. Intro
  6. -----
  7. Particle systems are used to simulate complex physical effects,
  8. such as sparks, fire, magic particles, smoke, mist, etc.
  9. The idea is that a "particle" is emitted at a fixed interval and with a
  10. fixed lifetime. During its lifetime, every particle will have the same
  11. base behavior. What makes each particle different from the rest and provides a more
  12. organic look is the "randomness" associated with each parameter. In
  13. essence, creating a particle system means setting base physics
  14. parameters and then adding randomness to them.
  15. Particle nodes
  16. ~~~~~~~~~~~~~~
  17. Godot provides two different nodes for 2D particles, :ref:`class_GPUParticles2D`
  18. and :ref:`class_CPUParticles2D`. GPUParticles2D is more advanced and uses the
  19. GPU to process particle effects. CPUParticles2D is a CPU-driven option with
  20. near-feature parity with GPUParticles2D, but lower performance when using large
  21. amounts of particles. On the other hand, CPUParticles2D may perform better on
  22. low-end systems or in GPU-bottlenecked situations.
  23. While GPUParticles2D is configured via a :ref:`class_ParticleProcessMaterial`
  24. (and optionally with a custom shader), the matching options are provided via
  25. node properties in CPUParticles2D (with the exception of the trail settings).
  26. You can convert a GPUParticles2D node into a CPUParticles2D node by clicking on
  27. the node in the inspector, selecting the 2D viewport, and selecting
  28. **GPUParticles2D > Convert to CPUParticles2D** in the viewport toolbar.
  29. .. image:: img/particles_convert.webp
  30. The rest of this tutorial is going to use the GPUParticles2D node. First, add a GPUParticles2D
  31. node to your scene. After creating that node you will notice that only a white dot was created,
  32. and that there is a warning icon next to your GPUParticles2D node in the scene dock. This
  33. is because the node needs a ParticleProcessMaterial to function.
  34. ParticleProcessMaterial
  35. ~~~~~~~~~~~~~~~~~~~~~~~
  36. To add a process material to your particles node, go to ``Process Material`` in
  37. your inspector panel. Click on the box next to ``Material``, and from the dropdown
  38. menu select ``New ParticleProcessMaterial``.
  39. .. image:: img/particles_material.png
  40. Your GPUParticles2D node should now be emitting
  41. white points downward.
  42. .. image:: img/particles1.png
  43. Texture
  44. ~~~~~~~
  45. A particle system can use a single texture or an animation *flipbook*. A
  46. flipbook is a texture that contains several frames of animation that can be
  47. played back, or chosen at random during emission. This is equivalent to a
  48. spritesheet for particles.
  49. The texture is set via the **Texture** property:
  50. .. image:: img/particles2.png
  51. .. _doc_particle_systems_2d_using_flipbook:
  52. Using an animation flipbook
  53. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  54. Particle flipbooks are suited to reproduce complex effects such as smoke, fire,
  55. explosions. They can also be used to introduce random texture variation, by
  56. making every particle use a different texture. You can find existing particle
  57. flipbook images online, or pre-render them using external tools such as `Blender
  58. <https://www.blender.org/>`__ or `EmberGen <https://jangafx.com/software/embergen/>`__.
  59. .. figure:: img/particles_flipbook_result.webp
  60. :align: center
  61. :alt: Example of a particle system that uses a flipbook texture
  62. Example of a particle system that uses a flipbook texture
  63. Using an animation flipbook requires additional configuration compared to a
  64. single texture. For demonstration purposes, we'll use this texture with 5
  65. columns and 7 rows (right-click and choose **Save as…**):
  66. .. figure:: img/particles_flipbook_example.webp
  67. :align: center
  68. :width: 240
  69. :alt: Particle flipbook texture example
  70. Credit: `JoesAlotofthings <https://opengameart.org/content/alot-of-particles-indispersal-special-effect-alotofparticles30>`__
  71. (CC BY 4.0)
  72. To use an animation flipbook, you must create a new CanvasItemMaterial in the
  73. Material section of the GPUParticles2D (or CPUParticles2D) node:
  74. .. figure:: img/particles_flipbook_create_canvasitemmaterial.webp
  75. :align: center
  76. :alt: Creating a CanvasItemMaterial at the bottom of the particles node inspector
  77. Creating a CanvasItemMaterial at the bottom of the particles node inspector
  78. In this CanvasItemMaterial, enable **Particle Animation** and set **H Frames** and **V Frames**
  79. to the number of columns and rows present in your flipbook texture:
  80. .. figure:: img/particles_flipbook_configure_canvasitemmaterial.webp
  81. :align: center
  82. :alt: Configuring the CanvasItemMaterial for the example flipbook texture
  83. Configuring the CanvasItemMaterial for the example flipbook texture
  84. Once this is done, the :ref:`Animation section <doc_particle_systems_2d_animation>`
  85. in ParticleProcessMaterial (for GPUParticles2D) or in the CPUParticles2D inspector
  86. will be effective.
  87. .. tip::
  88. If your flipbook texture has a black background instead of a transparent
  89. background, you will also need to set the blend mode to **Add** instead of
  90. **Mix** for correct display. Alternatively, you can modify the texture to
  91. have a transparent background in an image editor. In `GIMP <https://gimp.org>`__,
  92. this can be done using the **Color > Color to Alpha** menu.
  93. Time parameters
  94. ---------------
  95. Lifetime
  96. ~~~~~~~~
  97. The time in seconds that every particle will stay alive. When lifetime
  98. ends, a new particle is created to replace it.
  99. Lifetime: 0.5
  100. .. image:: img/paranim14.gif
  101. Lifetime: 4.0
  102. .. image:: img/paranim15.gif
  103. One Shot
  104. ~~~~~~~~
  105. When enabled, a GPUParticles2D node will emit all of its particles once
  106. and then never again.
  107. Preprocess
  108. ~~~~~~~~~~
  109. Particle systems begin with zero particles emitted, then start emitting.
  110. This can be an inconvenience when loading a scene and systems like
  111. a torch, mist, etc. begin emitting the moment you enter. Preprocess is
  112. used to let the system process a given number of seconds before it is
  113. actually drawn the first time.
  114. Speed Scale
  115. ~~~~~~~~~~~
  116. The speed scale has a default value of ``1`` and is used to adjust the
  117. speed of a particle system. Lowering the value will make the particles
  118. slower while increasing the value will make the particles much faster.
  119. Explosiveness
  120. ~~~~~~~~~~~~~
  121. If lifetime is ``1`` and there are 10 particles, it means a particle
  122. will be emitted every 0.1 seconds. The explosiveness parameter changes
  123. this, and forces particles to be emitted all together. Ranges are:
  124. - 0: Emit particles at regular intervals (default value).
  125. - 1: Emit all particles simultaneously.
  126. Values in the middle are also allowed. This feature is useful for
  127. creating explosions or sudden bursts of particles:
  128. .. image:: img/paranim18.gif
  129. Randomness
  130. ~~~~~~~~~~
  131. All physics parameters can be randomized. Random values range from ``0`` to
  132. ``1``. The formula to randomize a parameter is:
  133. ::
  134. initial_value = param_value + param_value * randomness
  135. Fixed FPS
  136. ~~~~~~~~~
  137. This setting can be used to set the particle system to render at a fixed
  138. FPS. For instance, changing the value to ``2`` will make the particles render
  139. at 2 frames per second. Note this does not slow down the particle system itself.
  140. .. note::
  141. Godot 4.3 does not currently support physics interpolation for 2D particles.
  142. As a workaround, disable physics interpolation for the particles node by setting
  143. **Node > Physics Interpolation > Mode** at the bottom of the inspector.
  144. Fract Delta
  145. ~~~~~~~~~~~
  146. This can be used to turn Fract Delta on or off.
  147. Drawing parameters
  148. ------------------
  149. Visibility Rect
  150. ~~~~~~~~~~~~~~~
  151. The visibility rectangle controls the visibility of the particles on screen. If this rectangle is outside of the viewport, the engine will not render the particles on screen.
  152. The rectangle's ``W`` and ``H`` properties respectively control its Width and its Height.
  153. The ``X`` and ``Y`` properties control the position of the upper-left
  154. corner of the rectangle, relative to the particle emitter.
  155. You can have Godot generate a Visibility Rect automatically using the toolbar above the 2d view. To do so, select the GPUParticles2D node and Click ``Particles > Generate Visibility Rect``. Godot will simulate the Particles2D node emitting particles for a few seconds and set the rectangle to fit the surface the particles take.
  156. You can control the emit duration with the ``Generation Time (sec)`` option. The maximum value is 25 seconds. If you need more time for your particles to move around, you can temporarily change the ``preprocess`` duration on the Particles2D node.
  157. Local Coords
  158. ~~~~~~~~~~~~
  159. By default this option is on, and it means that the space that particles
  160. are emitted to is relative to the node. If the node is moved, all
  161. particles are moved with it:
  162. .. image:: img/paranim20.gif
  163. If disabled, particles will emit to global space, meaning that if the
  164. node is moved, already emitted particles are not affected:
  165. .. image:: img/paranim21.gif
  166. Draw Order
  167. ~~~~~~~~~~
  168. This controls the order in which individual particles are drawn. ``Index``
  169. means particles are drawn according to their emission order (default).
  170. ``Lifetime`` means they are drawn in order of remaining lifetime.
  171. ParticleProcessMaterial settings
  172. --------------------------------
  173. Direction
  174. ~~~~~~~~~
  175. This is the base direction at which particles emit. The default is
  176. ``Vector3(1, 0, 0)`` which makes particles emit to the right. However,
  177. with the default gravity settings, particles will go straight down.
  178. .. image:: img/direction1.png
  179. For this property to be noticeable, you need an *initial velocity* greater
  180. than 0. Here, we set the initial velocity to 40. You'll notice that
  181. particles emit toward the right, then go down because of gravity.
  182. .. image:: img/direction2.png
  183. Spread
  184. ~~~~~~
  185. This parameter is the angle in degrees which will be randomly added in
  186. either direction to the base ``Direction``. A spread of ``180`` will emit
  187. in all directions (+/- 180). For spread to do anything the "Initial Velocity"
  188. parameter must be greater than 0.
  189. .. image:: img/paranim3.gif
  190. Flatness
  191. ~~~~~~~~
  192. This property is only useful for 3D particles.
  193. Gravity
  194. ~~~~~~~
  195. The gravity applied to every particle.
  196. .. image:: img/paranim7.gif
  197. Initial Velocity
  198. ~~~~~~~~~~~~~~~~
  199. Initial velocity is the speed at which particles will be emitted (in
  200. pixels/sec). Speed might later be modified by gravity or other
  201. accelerations (as described further below).
  202. .. image:: img/paranim4.gif
  203. Angular Velocity
  204. ~~~~~~~~~~~~~~~~
  205. Angular velocity is the initial angular velocity applied to particles.
  206. Spin Velocity
  207. ~~~~~~~~~~~~~
  208. Spin velocity is the speed at which particles turn around their center
  209. (in degrees/sec).
  210. .. image:: img/paranim5.gif
  211. Orbit Velocity
  212. ~~~~~~~~~~~~~~
  213. Orbit velocity is used to make particles turn around their center.
  214. .. image:: img/paranim6.gif
  215. Linear Acceleration
  216. ~~~~~~~~~~~~~~~~~~~
  217. The linear acceleration applied to each particle.
  218. Radial Acceleration
  219. ~~~~~~~~~~~~~~~~~~~
  220. If this acceleration is positive, particles are accelerated away from
  221. the center. If negative, they are absorbed towards it.
  222. .. image:: img/paranim8.gif
  223. Tangential Acceleration
  224. ~~~~~~~~~~~~~~~~~~~~~~~
  225. This acceleration will use the tangent vector to the center. Combining
  226. with radial acceleration can do nice effects.
  227. .. image:: img/paranim9.gif
  228. Damping
  229. ~~~~~~~
  230. Damping applies friction to the particles, forcing them to stop. It is
  231. especially useful for sparks or explosions, which usually begin with a
  232. high linear velocity and then stop as they fade.
  233. .. image:: img/paranim10.gif
  234. Angle
  235. ~~~~~
  236. Determines the initial angle of the particle (in degrees). This parameter
  237. is mostly useful randomized.
  238. .. image:: img/paranim11.gif
  239. Scale
  240. ~~~~~
  241. Determines the initial scale of the particles.
  242. .. image:: img/paranim12.gif
  243. Color
  244. ~~~~~
  245. Used to change the color of the particles being emitted.
  246. Hue Variation
  247. ~~~~~~~~~~~~~
  248. The ``Variation`` value sets the initial hue variation applied to each
  249. particle. The ``Variation Random`` value controls the hue variation
  250. randomness ratio.
  251. .. _doc_particle_systems_2d_animation:
  252. Animation
  253. ~~~~~~~~~
  254. .. note::
  255. Particle flipbook animation is only effective if the CanvasItemMaterial used
  256. on the GPUParticles2D or CPUParticles2D node has been
  257. :ref:`configured accordingly <doc_particle_systems_2d_using_flipbook>`.
  258. To set up the particle flipbook for linear playback, set the **Speed Min** and **Speed Max** values to 1:
  259. .. figure:: img/particles_flipbook_configure_animation_speed.webp
  260. :align: center
  261. :alt: Setting up particle animation for playback during the particle's lifetime
  262. Setting up particle animation for playback during the particle's lifetime
  263. By default, looping is disabled. If the particle is done playing before its
  264. lifetime ends, the particle will keep using the flipbook's last frame (which may
  265. be fully transparent depending on how the flipbook texture is designed). If
  266. looping is enabled, the animation will loop back to the first frame and resume
  267. playing.
  268. Depending on how many images your sprite sheet contains and for how long your
  269. particle is alive, the animation might not look smooth. The relationship between
  270. particle lifetime, animation speed, and number of images in the sprite sheet is
  271. this:
  272. .. note::
  273. At an animation speed of ``1.0``, the animation will reach the last image
  274. in the sequence just as the particle's lifetime ends.
  275. .. math::
  276. Animation\ FPS = \frac{Number\ of\ images}{Lifetime}
  277. If you wish the particle flipbook to be used as a source of random particle
  278. textures for every particle, keep the speed values at 0 and set **Offset Max**
  279. to 1 instead:
  280. .. figure:: img/particles_flipbook_configure_animation_offset.webp
  281. :align: center
  282. :alt: Setting up particle animation for random offset on emission
  283. Setting up particle animation for random offset on emission
  284. Note that the GPUParticles2D node's **Fixed FPS** also affects animation
  285. playback. For smooth animation playback, it's recommended to set it to 0 so that
  286. the particle is simulated on every rendered frame. If this is not an option for
  287. your use case, set **Fixed FPS** to be equal to the effective framerate used by
  288. the flipbook animation (see above for the formula).
  289. Emission Shapes
  290. ---------------
  291. ParticleProcessMaterials allow you to set an Emission Mask, which dictates
  292. the area and direction in which particles are emitted.
  293. These can be generated from textures in your project.
  294. Ensure that a ParticleProcessMaterial is set, and the GPUParticles2D node is selected.
  295. A "Particles" menu should appear in the Toolbar:
  296. .. image:: img/emission_shapes1.png
  297. Open it and select "Load Emission Mask":
  298. .. image:: img/emission_shapes2.png
  299. Then select which texture you want to use as your mask:
  300. .. image:: img/emission_shapes3.png
  301. A dialog box with several settings will appear.
  302. Emission Mask
  303. ~~~~~~~~~~~~~
  304. Three types of emission masks can be generated from a texture:
  305. - Solid Pixels: Particles will spawn from any area of the texture,
  306. excluding transparent areas.
  307. .. image:: img/emission_mask_solid.gif
  308. - Border Pixels: Particles will spawn from the outer edges of the texture.
  309. .. image:: img/emission_mask_border.gif
  310. - Directed Border Pixels: Similar to Border Pixels, but adds extra
  311. information to the mask to give particles the ability to emit away
  312. from the borders. Note that an ``Initial Velocity`` will need to
  313. be set in order to utilize this.
  314. .. image:: img/emission_mask_directed_border.gif
  315. Emission Colors
  316. ~~~~~~~~~~~~~~~
  317. ``Capture from Pixel`` will cause the particles to inherit the color of the mask at their spawn points.
  318. Once you click "OK", the mask will be generated and set to the ParticleProcessMaterial, under the ``Emission Shape`` section:
  319. .. image:: img/emission_shapes4.png
  320. All of the values within this section have been automatically generated by the
  321. "Load Emission Mask" menu, so they should generally be left alone.
  322. .. note:: An image should not be added to ``Point Texture`` or ``Color Texture`` directly.
  323. The "Load Emission Mask" menu should always be used instead.