controlling_thousands_of_fish.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. :article_outdated: True
  2. .. _doc_controlling_thousands_of_fish:
  3. Controlling thousands of fish with Particles
  4. ============================================
  5. The problem with :ref:`MeshInstance3D <class_MeshInstance3D>` is that it is expensive to
  6. update their transform array. It is great for placing many static objects around the
  7. scene. But it is still difficult to move the objects around the scene.
  8. To make each instance move in an interesting way, we will use a
  9. :ref:`GPUParticles3D <class_GPUParticles3D>` node. Particles take advantage of GPU acceleration
  10. by computing and setting the per-instance information in a :ref:`Shader <class_Shader>`.
  11. First create a Particles node. Then, under "Draw Passes" set the Particle's "Draw Pass 1" to your
  12. :ref:`Mesh <class_Mesh>`. Then under "Process Material" create a new
  13. :ref:`ShaderMaterial <class_ShaderMaterial>`.
  14. Set the ``shader_type`` to ``particles``.
  15. .. code-block:: glsl
  16. shader_type particles
  17. Then add the following two functions:
  18. .. code-block:: glsl
  19. float rand_from_seed(in uint seed) {
  20. int k;
  21. int s = int(seed);
  22. if (s == 0)
  23. s = 305420679;
  24. k = s / 127773;
  25. s = 16807 * (s - k * 127773) - 2836 * k;
  26. if (s < 0)
  27. s += 2147483647;
  28. seed = uint(s);
  29. return float(seed % uint(65536)) / 65535.0;
  30. }
  31. uint hash(uint x) {
  32. x = ((x >> uint(16)) ^ x) * uint(73244475);
  33. x = ((x >> uint(16)) ^ x) * uint(73244475);
  34. x = (x >> uint(16)) ^ x;
  35. return x;
  36. }
  37. These functions come from the default :ref:`ParticleProcessMaterial <class_ParticleProcessMaterial>`.
  38. They are used to generate a random number from each particle's ``RANDOM_SEED``.
  39. A unique thing about particle shaders is that some built-in variables are saved across frames.
  40. ``TRANSFORM``, ``COLOR``, and ``CUSTOM`` can all be accessed in the shader of the mesh, and
  41. also in the particle shader the next time it is run.
  42. Next, setup your ``start()`` function. Particles shaders contain a ``start()`` function and a
  43. ``process()`` function.
  44. The code in the ``start()`` function only runs when the particle system starts.
  45. The code in the ``process()`` function will always run.
  46. We need to generate 4 random numbers: 3 to create a random position and one for the random
  47. offset of the swim cycle.
  48. First, generate 4 seeds inside the ``start()`` function using the ``hash()`` function provided above:
  49. .. code-block:: glsl
  50. uint alt_seed1 = hash(NUMBER + uint(1) + RANDOM_SEED);
  51. uint alt_seed2 = hash(NUMBER + uint(27) + RANDOM_SEED);
  52. uint alt_seed3 = hash(NUMBER + uint(43) + RANDOM_SEED);
  53. uint alt_seed4 = hash(NUMBER + uint(111) + RANDOM_SEED);
  54. Then, use those seeds to generate random numbers using ``rand_from_seed``:
  55. .. code-block:: glsl
  56. CUSTOM.x = rand_from_seed(alt_seed1);
  57. vec3 position = vec3(rand_from_seed(alt_seed2) * 2.0 - 1.0,
  58. rand_from_seed(alt_seed3) * 2.0 - 1.0,
  59. rand_from_seed(alt_seed4) * 2.0 - 1.0);
  60. Finally, assign ``position`` to ``TRANSFORM[3].xyz``, which is the part of the transform that holds
  61. the position information.
  62. .. code-block:: glsl
  63. TRANSFORM[3].xyz = position * 20.0;
  64. Remember, all this code so far goes inside the ``start()`` function.
  65. The vertex shader for your mesh can stay the exact same as it was in the previous tutorial.
  66. Now you can move each fish individually each frame, either by adding to the ``TRANSFORM`` directly
  67. or by writing to ``VELOCITY``.
  68. Let's transform the fish by setting their ``VELOCITY`` in the ``start()`` function.
  69. .. code-block:: glsl
  70. VELOCITY.z = 10.0;
  71. This is the most basic way to set ``VELOCITY`` every particle (or fish) will have the same velocity.
  72. Just by setting ``VELOCITY`` you can make the fish swim however you want. For example, try the code
  73. below.
  74. .. code-block:: glsl
  75. VELOCITY.z = cos(TIME + CUSTOM.x * 6.28) * 4.0 + 6.0;
  76. This will give each fish a unique speed between ``2`` and ``10``.
  77. You can also let each fish change its velocity over time if you set the velocity in the ``process()``
  78. function.
  79. If you used ``CUSTOM.y`` in the last tutorial, you can also set the speed of the swim animation based
  80. on the ``VELOCITY``. Just use ``CUSTOM.y``.
  81. .. code-block:: glsl
  82. CUSTOM.y = VELOCITY.z * 0.1;
  83. This code gives you the following behavior:
  84. .. image:: img/scene.gif
  85. Using a ParticleProcessMaterial you can make the fish behavior as simple or complex as you like. In this
  86. tutorial we only set Velocity, but in your own Shaders you can also set ``COLOR``, rotation, scale
  87. (through ``TRANSFORM``). Please refer to the :ref:`Particles Shader Reference <doc_particle_shader>`
  88. for more information on particle shaders.