Particle2D: Rotate particles once on creation?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Irolan

Hello,

I’m trying to create a speed effect with “after-images” of the player. I’d like to use particles for this. The player is a 2D sprite, top-down view, and obviously rotates while moving. So each emitted particle should have the same rotation as the player. However, I can’t seem to be able to do that.
The particles are set to NOT use local coordinates (they’re supposed to be left behind by the player like footsteps, basically).
I tried manipulating the “angle” property of the process material, but that rotates ALL particles currently on the screen.
I tried rotating the Particle2D node itself, but that doesn’t do anything.

Does anyone have an idea how to do this (preferably in Godot Script).

Okay, so I gave up on the particle thing. It kinda felt like every idea I had (from rotating sprites to swapping sprites on the fly), the devs had forseen and actively prevented it. Godot does have a couple of strange decisions in there, this just seems to be one of them.
I decided to create a trail effect using the Line2D node instead. It’s not exactly the look I wanted, but it’s a helluva lot better than creating a trail out of a couple dozen sprites. It might be feasible to do that but I just don’t like the idea.
Here’s hoping that they’ll add some more flexibility to particles in future versions.

Irolan | 2021-09-21 23:11

:bust_in_silhouette: Reply From: skysphr

Convert the process material to a shader material. Modify the following:

uniform float initial_linear_velocity;
uniform float emission_angle; // <<<<<<<<< Add this
uniform float initial_angle;
  1. Inside the vertex function, somewhere below if (RESTART || restart) {, change the assignment of CUSTOM.x from base_angle * degree_to_rad to (base_angle + emission_angle) * degree_to_rad

  2. In the else bracket following the previous if, find where it says CUSTOM.x = base_angle * degree_to_rad; and change it to CUSTOM.x += base_angle * degree_to_rad;

After the changes are done, you can send the shader an angle parameter by calling $Particles2D.process_material.set_shader_param("emission_angle", your_angle_in_degrees)

I hope this helps :slight_smile:

Hm… I have only just started using Godot and, truth be told, I haven’t got the slightest clue what you are talking about. I tried changing the process material from particle material to shader material, but that only resulted in no visible particles at all.
So the first thing I’d need to figure out is how to make anything show up at all.

Also, I couldn’t find any of the code you wrote anywhere. Where would that be?

Irolan | 2021-09-21 20:42

You can convert a particle material to a shader material from the drop down menu:

Image

This will make the particle logic editable. Upon conversion, the bottom panel should open the shader code editor.

skysphr | 2021-09-22 13:55

I found it, and it works! Thank you skysphr!
Incredible how much effort this takes, considering how simple it seems.

Also figured out how make the particles fade over time:
Add another variable uniform float alpha_fade, change line COLOR = hue_rot_mat * color_value; to COLOR = (hue_rot_mat * color_value) - vec4(0, 0, 0, CUSTOM.y * alpha_fade);
I would have NEVER guessed that CUSTOM.x, CUSTOM.y and so on are just random names and do not refer to axis.

Irolan | 2021-09-23 18:53

Yeah particle shaders and shaders in general are not quite the most documented feature around :stuck_out_tongue: Pretty sure you could have done the alpha fade out without editing the shader though: Go to color, add a gradient texture, set the leftmost color to white, set the rightmost color to transparent.

skysphr | 2021-09-24 10:48

Nope. I would’ve done that if it were possible, but the color value is indeed just that. Can’t turn it into a gradient.

Irolan | 2021-09-24 10:57

Sorry, I meant color ramp:

enter image description here

skysphr | 2021-09-24 13:35

Hm… I can’t find it. It may just be me being blind, but the Particle2D’s inspector is so convoluted I have severe trouble finding the settings I need. I see there is a “Scale” above the “Color” in your screenshot. I clicked through everything I could find, I have no idea where that might be. The only “Scale” I find is under “Transform” and there’s no “Color” there.
I know I can do this if I use a ParticlesMaterial, but nowhere in the ShaderMaterial can I find any way to introduce a GradientTexture, aside from when I add a CanvasItemMaterial under “Material”. However, whatever I do there doesn’t seem to do anything.
Well, honestly, I’m done with this. I spent a week’s worth of evenings trying to figure this out, it works now, quite well actually (in no small part thanks to you), so I’m not going to try and touch it again.
I hear the particle system in Godot 4 is going to see some significant improvement over Godot 3, so here’s hoping for that. Because right now I don’t feel like touching these again.

Irolan | 2021-09-24 15:16

Well yeah it only exists there before you convert the material to a shader material. Afterwards, you’re in control of everything that particles do. However, using a simple multiplication in the shader the way you did is probably much more optimized for the GPU.

skysphr | 2021-09-24 16:41