From cd68b6e1e272479cc39e4d0ada0cdd6e1da0e931 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Mon, 19 Sep 2022 16:53:01 +0100 Subject: [PATCH] CPUParticles initialize data on set_amount Fills particle data with zeroes on resize using set_amount(), to prevent garbage data including Nans being sent to VisualServer, which can corrupt the spatial partitioning. --- scene/2d/cpu_particles_2d.cpp | 7 +++++++ scene/3d/cpu_particles.cpp | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/scene/2d/cpu_particles_2d.cpp b/scene/2d/cpu_particles_2d.cpp index 39fffdf4d..94c40218c 100644 --- a/scene/2d/cpu_particles_2d.cpp +++ b/scene/2d/cpu_particles_2d.cpp @@ -63,6 +63,13 @@ void CPUParticles2D::set_amount(int p_amount) { } particle_data.resize((8 + 4 + 1) * p_amount); + + // We must fill immediately to prevent garbage data and Nans + // being sent to the visual server with set_as_bulk_array, + // if this is sent before being regularly updated. + particle_data.fill(0); + particle_data_prev.fill(0); + RS::get_singleton()->multimesh_allocate(multimesh, p_amount, RS::MULTIMESH_TRANSFORM_2D, RS::MULTIMESH_COLOR_8BIT, RS::MULTIMESH_CUSTOM_DATA_FLOAT); particle_order.resize(p_amount); diff --git a/scene/3d/cpu_particles.cpp b/scene/3d/cpu_particles.cpp index 1781bc361..732a2f488 100644 --- a/scene/3d/cpu_particles.cpp +++ b/scene/3d/cpu_particles.cpp @@ -88,7 +88,14 @@ void CPUParticles::set_amount(int p_amount) { particle_data.resize((12 + 4 + 1) * p_amount); particle_data_prev.resize(particle_data.size()); - RS::get_singleton()->multimesh_allocate(multimesh, p_amount, RS::MULTIMESH_TRANSFORM_3D, RS::MULTIMESH_COLOR_8BIT, RS::MULTIMESH_CUSTOM_DATA_FLOAT); + + // We must fill immediately to prevent garbage data and Nans + // being sent to the visual server with set_as_bulk_array, + // if this is sent before being regularly updated. + particle_data.fill(0); + particle_data_prev.fill(0); + + RS::get_singleton()->multimesh_allocate(multimesh, p_amount, RS::MULTIMESH_TRANSFORM_2D, RS::MULTIMESH_COLOR_8BIT, RS::MULTIMESH_CUSTOM_DATA_FLOAT); particle_order.resize(p_amount); }