Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix particle not re-randomizing every emission #103068

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions scene/2d/cpu_particles_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,22 @@ void CPUParticles2D::set_emitting(bool p_emitting) {
return;
}

if (p_emitting && !use_fixed_seed) {
set_seed(Math::rand());
}

emitting = p_emitting;
if (emitting) {
active = true;
set_process_internal(true);
_set_emitting();
}
}

void CPUParticles2D::_set_emitting() {
active = true;
set_process_internal(true);
// first update before rendering to avoid one frame delay after emitting starts
if (time == 0) {
_update_internal();
Comment on lines +62 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is carried over from CPUParticles3D. It seems correct to me, so let's keep it

}
}

Expand Down Expand Up @@ -310,7 +322,8 @@ void CPUParticles2D::restart(bool p_keep_seed) {
seed = Math::rand();
}

set_emitting(true);
emitting = true;
_set_emitting();
}

void CPUParticles2D::set_direction(Vector2 p_direction) {
Expand Down
1 change: 1 addition & 0 deletions scene/2d/cpu_particles_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class CPUParticles2D : public Node2D {
void _update_internal();
void _particles_process(double p_delta);
void _update_particle_data_buffer();
void _set_emitting();

Mutex update_mutex;

Expand Down
3 changes: 3 additions & 0 deletions scene/2d/gpu_particles_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
void GPUParticles2D::set_emitting(bool p_emitting) {
// Do not return even if `p_emitting == emitting` because `emitting` is just an approximation.

if (p_emitting && p_emitting != emitting && !use_fixed_seed) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (p_emitting && p_emitting != emitting && !use_fixed_seed) {
if (p_emitting && !emitting && !use_fixed_seed) {

Looks like it could be simplified, right? The same is valid below.

set_seed(Math::rand());
}
if (p_emitting && one_shot) {
if (!active && !emitting) {
// Last cycle ended.
Expand Down
22 changes: 15 additions & 7 deletions scene/3d/cpu_particles_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,22 @@ void CPUParticles3D::set_emitting(bool p_emitting) {
return;
}

if (p_emitting && !use_fixed_seed) {
set_seed(Math::rand());
}

emitting = p_emitting;
if (emitting) {
active = true;
set_process_internal(true);
_set_emitting();
}
}

// first update before rendering to avoid one frame delay after emitting starts
if (time == 0) {
_update_internal();
}
void CPUParticles3D::_set_emitting() {
active = true;
set_process_internal(true);
// first update before rendering to avoid one frame delay after emitting starts
if (time == 0) {
_update_internal();
}
}

Expand Down Expand Up @@ -251,7 +258,8 @@ void CPUParticles3D::restart(bool p_keep_seed) {
seed = Math::rand();
}

set_emitting(true);
emitting = true;
_set_emitting();
}

void CPUParticles3D::set_direction(Vector3 p_direction) {
Expand Down
1 change: 1 addition & 0 deletions scene/3d/cpu_particles_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class CPUParticles3D : public GeometryInstance3D {
void _update_internal();
void _particles_process(double p_delta);
void _update_particle_data_buffer();
void _set_emitting();

Mutex update_mutex;

Expand Down
4 changes: 3 additions & 1 deletion scene/3d/gpu_particles_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ AABB GPUParticles3D::get_aabb() const {

void GPUParticles3D::set_emitting(bool p_emitting) {
// Do not return even if `p_emitting == emitting` because `emitting` is just an approximation.

if (p_emitting && p_emitting != emitting && !use_fixed_seed) {
set_seed(Math::rand());
}
if (p_emitting && one_shot) {
if (!active && !emitting) {
// Last cycle ended.
Expand Down