mirror of
https://github.com/stan220/godot-docs.git
synced 2026-09-08 18:29:11 +00:00
Update particle shader format in Controlling thousands of fish (#6604)
This commit is contained in:
@@ -55,30 +55,16 @@ A unique thing about particle shaders is that some built-in variables are saved
|
||||
``TRANSFORM``, ``COLOR``, and ``CUSTOM`` can all be accessed in the shader of the mesh, and
|
||||
also in the particle shader the next time it is run.
|
||||
|
||||
Next, setup your ``vertex`` function. Particles shaders only contain a vertex function
|
||||
and no others.
|
||||
Next, setup your ``start()`` function. Particles shaders contain a ``start()`` function and a
|
||||
``process()`` function.
|
||||
|
||||
First we will distinguish between code that needs to be run only when the particle system starts
|
||||
and code that should always run. We want to give each fish a random position and a random animation
|
||||
offset when the system is first run. To do so, we wrap that code in an ``if`` statement that checks the
|
||||
built-in variable ``RESTART`` which becomes ``true`` for one frame when the particle system is restarted.
|
||||
The code in the ``start()`` function only runs when the particle system starts.
|
||||
The code in the ``process()`` function will always run.
|
||||
|
||||
From a high level, this looks like:
|
||||
|
||||
.. code-block:: glsl
|
||||
|
||||
void vertex() {
|
||||
if (RESTART) {
|
||||
//Initialization code goes here
|
||||
} else {
|
||||
//per-frame code goes here
|
||||
}
|
||||
}
|
||||
|
||||
Next, we need to generate 4 random numbers: 3 to create a random position and one for the random
|
||||
We need to generate 4 random numbers: 3 to create a random position and one for the random
|
||||
offset of the swim cycle.
|
||||
|
||||
First, generate 4 seeds inside the ``RESTART`` block using the ``hash`` function provided above:
|
||||
First, generate 4 seeds inside the ``start()`` function using the ``hash()`` function provided above:
|
||||
|
||||
.. code-block:: glsl
|
||||
|
||||
@@ -103,14 +89,14 @@ the position information.
|
||||
|
||||
TRANSFORM[3].xyz = position * 20.0;
|
||||
|
||||
Remember, all this code so far goes inside the ``RESTART`` block.
|
||||
Remember, all this code so far goes inside the ``start()`` function.
|
||||
|
||||
The vertex shader for your mesh can stay the exact same as it was in the previous tutorial.
|
||||
|
||||
Now you can move each fish individually each frame, either by adding to the ``TRANSFORM`` directly
|
||||
or by writing to ``VELOCITY``.
|
||||
|
||||
Let's transform the fish by setting their ``VELOCITY``.
|
||||
Let's transform the fish by setting their ``VELOCITY`` in the ``start()`` function.
|
||||
|
||||
.. code-block:: glsl
|
||||
|
||||
@@ -127,6 +113,9 @@ below.
|
||||
|
||||
This will give each fish a unique speed between ``2`` and ``10``.
|
||||
|
||||
You can also let each fish change its velocity over time if you set the velocity in the ``process()``
|
||||
function.
|
||||
|
||||
If you used ``CUSTOM.y`` in the last tutorial, you can also set the speed of the swim animation based
|
||||
on the ``VELOCITY``. Just use ``CUSTOM.y``.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user