particle_systems_2d.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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, and selecting **Particles > Convert to
  28. CPUParticles2D** in the toolbar at the top of the 3D editor viewport.
  29. .. image:: img/particles_convert.png
  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. Fract Delta
  141. ~~~~~~~~~~~
  142. This can be used to turn Fract Delta on or off.
  143. Drawing parameters
  144. ------------------
  145. Visibility Rect
  146. ~~~~~~~~~~~~~~~
  147. 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.
  148. The rectangle's ``W`` and ``H`` properties respectively control its Width and its Height.
  149. The ``X`` and ``Y`` properties control the position of the upper-left
  150. corner of the rectangle, relative to the particle emitter.
  151. 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.
  152. 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.
  153. Local Coords
  154. ~~~~~~~~~~~~
  155. By default this option is on, and it means that the space that particles
  156. are emitted to is relative to the node. If the node is moved, all
  157. particles are moved with it:
  158. .. image:: img/paranim20.gif
  159. If disabled, particles will emit to global space, meaning that if the
  160. node is moved, already emitted particles are not affected:
  161. .. image:: img/paranim21.gif
  162. Draw Order
  163. ~~~~~~~~~~
  164. This controls the order in which individual particles are drawn. ``Index``
  165. means particles are drawn according to their emission order (default).
  166. ``Lifetime`` means they are drawn in order of remaining lifetime.
  167. ParticleProcessMaterial settings
  168. --------------------------------
  169. Direction
  170. ~~~~~~~~~
  171. This is the base direction at which particles emit. The default is
  172. ``Vector3(1, 0, 0)`` which makes particles emit to the right. However,
  173. with the default gravity settings, particles will go straight down.
  174. .. image:: img/direction1.png
  175. For this property to be noticeable, you need an *initial velocity* greater
  176. than 0. Here, we set the initial velocity to 40. You'll notice that
  177. particles emit toward the right, then go down because of gravity.
  178. .. image:: img/direction2.png
  179. Spread
  180. ~~~~~~
  181. This parameter is the angle in degrees which will be randomly added in
  182. either direction to the base ``Direction``. A spread of ``180`` will emit
  183. in all directions (+/- 180). For spread to do anything the "Initial Velocity"
  184. parameter must be greater than 0.
  185. .. image:: img/paranim3.gif
  186. Flatness
  187. ~~~~~~~~
  188. This property is only useful for 3D particles.
  189. Gravity
  190. ~~~~~~~
  191. The gravity applied to every particle.
  192. .. image:: img/paranim7.gif
  193. Initial Velocity
  194. ~~~~~~~~~~~~~~~~
  195. Initial velocity is the speed at which particles will be emitted (in
  196. pixels/sec). Speed might later be modified by gravity or other
  197. accelerations (as described further below).
  198. .. image:: img/paranim4.gif
  199. Angular Velocity
  200. ~~~~~~~~~~~~~~~~
  201. Angular velocity is the initial angular velocity applied to particles.
  202. Spin Velocity
  203. ~~~~~~~~~~~~~
  204. Spin velocity is the speed at which particles turn around their center
  205. (in degrees/sec).
  206. .. image:: img/paranim5.gif
  207. Orbit Velocity
  208. ~~~~~~~~~~~~~~
  209. Orbit velocity is used to make particles turn around their center.
  210. .. image:: img/paranim6.gif
  211. Linear Acceleration
  212. ~~~~~~~~~~~~~~~~~~~
  213. The linear acceleration applied to each particle.
  214. Radial Acceleration
  215. ~~~~~~~~~~~~~~~~~~~
  216. If this acceleration is positive, particles are accelerated away from
  217. the center. If negative, they are absorbed towards it.
  218. .. image:: img/paranim8.gif
  219. Tangential Acceleration
  220. ~~~~~~~~~~~~~~~~~~~~~~~
  221. This acceleration will use the tangent vector to the center. Combining
  222. with radial acceleration can do nice effects.
  223. .. image:: img/paranim9.gif
  224. Damping
  225. ~~~~~~~
  226. Damping applies friction to the particles, forcing them to stop. It is
  227. especially useful for sparks or explosions, which usually begin with a
  228. high linear velocity and then stop as they fade.
  229. .. image:: img/paranim10.gif
  230. Angle
  231. ~~~~~
  232. Determines the initial angle of the particle (in degrees). This parameter
  233. is mostly useful randomized.
  234. .. image:: img/paranim11.gif
  235. Scale
  236. ~~~~~
  237. Determines the initial scale of the particles.
  238. .. image:: img/paranim12.gif
  239. Color
  240. ~~~~~
  241. Used to change the color of the particles being emitted.
  242. Hue Variation
  243. ~~~~~~~~~~~~~
  244. The ``Variation`` value sets the initial hue variation applied to each
  245. particle. The ``Variation Random`` value controls the hue variation
  246. randomness ratio.
  247. .. _doc_particle_systems_2d_animation:
  248. Animation
  249. ~~~~~~~~~
  250. .. note::
  251. Particle flipbook animation is only effective if the CanvasItemMaterial used
  252. on the GPUParticles2D or CPUParticles2D node has been
  253. :ref:`configured accordingly <doc_particle_systems_2d_using_flipbook>`.
  254. To set up the particle flipbook for linear playback, set the **Speed Min** and **Speed Max** values to 1:
  255. .. figure:: img/particles_flipbook_configure_animation_speed.webp
  256. :align: center
  257. :alt: Setting up particle animation for playback during the particle's lifetime
  258. Setting up particle animation for playback during the particle's lifetime
  259. By default, looping is disabled. If the particle is done playing before its
  260. lifetime ends, the particle will keep using the flipbook's last frame (which may
  261. be fully transparent depending on how the flipbook texture is designed). If
  262. looping is enabled, the animation will loop back to the first frame and resume
  263. playing.
  264. Depending on how many images your sprite sheet contains and for how long your
  265. particle is alive, the animation might not look smooth. The relationship between
  266. particle lifetime, animation speed, and number of images in the sprite sheet is
  267. this:
  268. .. note::
  269. At an animation speed of ``1.0``, the animation will reach the last image
  270. in the sequence just as the particle's lifetime ends.
  271. .. math::
  272. Animation\ FPS = \frac{Number\ of\ images}{Lifetime}
  273. If you wish the particle flipbook to be used as a source of random particle
  274. textures for every particle, keep the speed values at 0 and set **Offset Max**
  275. to 1 instead:
  276. .. figure:: img/particles_flipbook_configure_animation_offset.webp
  277. :align: center
  278. :alt: Setting up particle animation for random offset on emission
  279. Setting up particle animation for random offset on emission
  280. Note that the GPUParticles2D node's **Fixed FPS** also affects animation
  281. playback. For smooth animation playback, it's recommended to set it to 0 so that
  282. the particle is simulated on every rendered frame. If this is not an option for
  283. your use case, set **Fixed FPS** to be equal to the effective framerate used by
  284. the flipbook animation (see above for the formula).
  285. Emission Shapes
  286. ---------------
  287. ParticleProcessMaterials allow you to set an Emission Mask, which dictates
  288. the area and direction in which particles are emitted.
  289. These can be generated from textures in your project.
  290. Ensure that a ParticleProcessMaterial is set, and the GPUParticles2D node is selected.
  291. A "Particles" menu should appear in the Toolbar:
  292. .. image:: img/emission_shapes1.png
  293. Open it and select "Load Emission Mask":
  294. .. image:: img/emission_shapes2.png
  295. Then select which texture you want to use as your mask:
  296. .. image:: img/emission_shapes3.png
  297. A dialog box with several settings will appear.
  298. Emission Mask
  299. ~~~~~~~~~~~~~
  300. Three types of emission masks can be generated from a texture:
  301. - Solid Pixels: Particles will spawn from any area of the texture,
  302. excluding transparent areas.
  303. .. image:: img/emission_mask_solid.gif
  304. - Border Pixels: Particles will spawn from the outer edges of the texture.
  305. .. image:: img/emission_mask_border.gif
  306. - Directed Border Pixels: Similar to Border Pixels, but adds extra
  307. information to the mask to give particles the ability to emit away
  308. from the borders. Note that an ``Initial Velocity`` will need to
  309. be set in order to utilize this.
  310. .. image:: img/emission_mask_directed_border.gif
  311. Emission Colors
  312. ~~~~~~~~~~~~~~~
  313. ``Capture from Pixel`` will cause the particles to inherit the color of the mask at their spawn points.
  314. Once you click "OK", the mask will be generated and set to the ParticleProcessMaterial, under the ``Emission Shape`` section:
  315. .. image:: img/emission_shapes4.png
  316. All of the values within this section have been automatically generated by the
  317. "Load Emission Mask" menu, so they should generally be left alone.
  318. .. note:: An image should not be added to ``Point Texture`` or ``Color Texture`` directly.
  319. The "Load Emission Mask" menu should always be used instead.