Skip to content

Commit d52ac7a

Browse files
committed
Custom ("visiblity") AABB support for CPUParticles
- Improves performance by reducing time spent on AABB generation. - Also adds an option to generate the AABB manually in the CPUParticles3D dropdown.
1 parent e74e80c commit d52ac7a

6 files changed

Lines changed: 149 additions & 0 deletions

File tree

doc/classes/CPUParticles3D.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@
309309
<member name="tangential_accel_min" type="float" setter="set_param_min" getter="get_param_min" default="0.0">
310310
Minimum tangent acceleration.
311311
</member>
312+
<member name="visibility_aabb" type="AABB" setter="set_visibility_aabb" getter="get_visibility_aabb" default="AABB(-4, -4, -4, 8, 8, 8)">
313+
The [AABB] that determines the node's region which needs to be visible on screen for the particle system to be active.
314+
Grow the box if particles suddenly appear/disappear when the node enters/exits the screen. The [AABB] can be grown via code or with the [b]Particles → Generate AABB[/b] editor tool.
315+
</member>
312316
</members>
313317
<signals>
314318
<signal name="finished">

editor/plugins/cpu_particles_3d_editor_plugin.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,60 @@ void CPUParticles3DEditor::_menu_option(int p_option) {
7777
ur->commit_action(false);
7878

7979
} break;
80+
case MENU_OPTION_GENERATE_AABB: {
81+
// Add one second to the default generation lifetime, since the progress is updated every second.
82+
generate_seconds->set_value(MAX(1.0, trunc(node->get_lifetime()) + 1.0));
83+
84+
if (generate_seconds->get_value() >= 11.0 + CMP_EPSILON) {
85+
// Only pop up the time dialog if the particle's lifetime is long enough to warrant shortening it.
86+
generate_aabb->popup_centered();
87+
} else {
88+
// Generate the visibility AABB immediately.
89+
_generate_aabb();
90+
}
91+
} break;
92+
}
93+
}
94+
95+
void CPUParticles3DEditor::_generate_aabb() {
96+
double time = generate_seconds->get_value();
97+
98+
double running = 0.0;
99+
100+
EditorProgress ep("gen_aabb", TTR("Generating Visibility AABB (Waiting for Particle Simulation)"), int(time));
101+
102+
bool was_emitting = node->is_emitting();
103+
if (!was_emitting) {
104+
node->set_emitting(true);
105+
OS::get_singleton()->delay_usec(1000);
106+
}
107+
108+
AABB rect;
109+
110+
while (running < time) {
111+
uint64_t ticks = OS::get_singleton()->get_ticks_usec();
112+
ep.step("Generating...", int(running), true);
113+
OS::get_singleton()->delay_usec(1000);
114+
115+
AABB capture = node->capture_aabb();
116+
if (rect == AABB()) {
117+
rect = capture;
118+
} else {
119+
rect.merge_with(capture);
120+
}
121+
122+
running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
123+
}
124+
125+
if (!was_emitting) {
126+
node->set_emitting(false);
80127
}
128+
129+
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
130+
ur->create_action(TTR("Generate Visibility AABB"));
131+
ur->add_do_method(node, "set_visibility_aabb", rect);
132+
ur->add_undo_method(node, "set_visibility_aabb", node->get_visibility_aabb());
133+
ur->commit_action();
81134
}
82135

83136
void CPUParticles3DEditor::edit(CPUParticles3D *p_particles) {
@@ -117,9 +170,24 @@ CPUParticles3DEditor::CPUParticles3DEditor() {
117170

118171
options->set_text(TTR("CPUParticles3D"));
119172
options->get_popup()->add_item(TTR("Restart"), MENU_OPTION_RESTART);
173+
options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
120174
options->get_popup()->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
121175
options->get_popup()->add_item(TTR("Convert to GPUParticles3D"), MENU_OPTION_CONVERT_TO_GPU_PARTICLES);
122176
options->get_popup()->connect("id_pressed", callable_mp(this, &CPUParticles3DEditor::_menu_option));
177+
178+
generate_aabb = memnew(ConfirmationDialog);
179+
generate_aabb->set_title(TTR("Generate Visibility AABB"));
180+
VBoxContainer *genvb = memnew(VBoxContainer);
181+
generate_aabb->add_child(genvb);
182+
generate_seconds = memnew(SpinBox);
183+
genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
184+
generate_seconds->set_min(0.1);
185+
generate_seconds->set_max(25);
186+
generate_seconds->set_value(2);
187+
188+
add_child(generate_aabb);
189+
190+
generate_aabb->connect("confirmed", callable_mp(this, &CPUParticles3DEditor::_generate_aabb));
123191
}
124192

125193
void CPUParticles3DEditorPlugin::edit(Object *p_object) {

editor/plugins/cpu_particles_3d_editor_plugin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ class CPUParticles3DEditor : public GPUParticles3DEditorBase {
3838
GDCLASS(CPUParticles3DEditor, GPUParticles3DEditorBase);
3939

4040
enum Menu {
41+
MENU_OPTION_GENERATE_AABB,
4142
MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE,
4243
MENU_OPTION_CLEAR_EMISSION_VOLUME,
4344
MENU_OPTION_RESTART,
4445
MENU_OPTION_CONVERT_TO_GPU_PARTICLES,
4546
};
4647

48+
ConfirmationDialog *generate_aabb = nullptr;
49+
SpinBox *generate_seconds = nullptr;
4750
CPUParticles3D *node = nullptr;
4851

52+
void _generate_aabb();
53+
4954
void _menu_option(int);
5055

5156
friend class CPUParticles3DEditorPlugin;

editor/plugins/gizmos/cpu_particles_3d_gizmo_plugin.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@
3131
#include "cpu_particles_3d_gizmo_plugin.h"
3232

3333
#include "editor/editor_node.h"
34+
#include "editor/editor_settings.h"
3435
#include "editor/editor_string_names.h"
3536
#include "editor/plugins/node_3d_editor_plugin.h"
3637
#include "scene/3d/cpu_particles_3d.h"
3738

3839
CPUParticles3DGizmoPlugin::CPUParticles3DGizmoPlugin() {
40+
Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/particles", Color(0.8, 0.7, 0.4));
41+
create_material("particles_material", gizmo_color);
42+
gizmo_color.a = MAX((gizmo_color.a - 0.2) * 0.02, 0.0);
43+
create_material("particles_solid_material", gizmo_color);
3944
create_icon_material("particles_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoCPUParticles3D"), EditorStringName(EditorIcons)));
4045
}
4146

@@ -56,6 +61,48 @@ bool CPUParticles3DGizmoPlugin::is_selectable_when_hidden() const {
5661
}
5762

5863
void CPUParticles3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
64+
CPUParticles3D *particles = Object::cast_to<CPUParticles3D>(p_gizmo->get_node_3d());
65+
66+
p_gizmo->clear();
67+
68+
Vector<Vector3> lines;
69+
AABB aabb = particles->get_visibility_aabb();
70+
71+
for (int i = 0; i < 12; i++) {
72+
Vector3 a, b;
73+
aabb.get_edge(i, a, b);
74+
lines.push_back(a);
75+
lines.push_back(b);
76+
}
77+
78+
Vector<Vector3> handles;
79+
80+
for (int i = 0; i < 3; i++) {
81+
Vector3 ax;
82+
ax[i] = aabb.position[i] + aabb.size[i];
83+
ax[(i + 1) % 3] = aabb.position[(i + 1) % 3] + aabb.size[(i + 1) % 3] * 0.5;
84+
ax[(i + 2) % 3] = aabb.position[(i + 2) % 3] + aabb.size[(i + 2) % 3] * 0.5;
85+
handles.push_back(ax);
86+
}
87+
88+
Vector3 center = aabb.get_center();
89+
for (int i = 0; i < 3; i++) {
90+
Vector3 ax;
91+
ax[i] = 1.0;
92+
handles.push_back(center + ax);
93+
lines.push_back(center);
94+
lines.push_back(center + ax);
95+
}
96+
97+
Ref<Material> material = get_material("particles_material", p_gizmo);
98+
99+
p_gizmo->add_lines(lines, material);
100+
101+
if (p_gizmo->is_selected()) {
102+
Ref<Material> solid_material = get_material("particles_solid_material", p_gizmo);
103+
p_gizmo->add_solid_box(solid_material, aabb.get_size(), aabb.get_center());
104+
}
105+
59106
Ref<Material> icon = get_material("particles_icon", p_gizmo);
60107
p_gizmo->add_unscaled_billboard(icon, 0.05);
61108
}

scene/3d/cpu_particles_3d.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ void CPUParticles3D::set_randomness_ratio(real_t p_ratio) {
101101
randomness_ratio = p_ratio;
102102
}
103103

104+
void CPUParticles3D::set_visibility_aabb(const AABB &p_aabb) {
105+
RS::get_singleton()->multimesh_set_custom_aabb(multimesh, p_aabb);
106+
visibility_aabb = p_aabb;
107+
update_gizmos();
108+
}
109+
104110
void CPUParticles3D::set_lifetime_randomness(double p_random) {
105111
lifetime_randomness = p_random;
106112
}
@@ -141,6 +147,10 @@ real_t CPUParticles3D::get_randomness_ratio() const {
141147
return randomness_ratio;
142148
}
143149

150+
AABB CPUParticles3D::get_visibility_aabb() const {
151+
return visibility_aabb;
152+
}
153+
144154
double CPUParticles3D::get_lifetime_randomness() const {
145155
return lifetime_randomness;
146156
}
@@ -520,6 +530,11 @@ bool CPUParticles3D::get_split_scale() {
520530
return split_scale;
521531
}
522532

533+
AABB CPUParticles3D::capture_aabb() const {
534+
RS::get_singleton()->multimesh_set_custom_aabb(multimesh, AABB());
535+
return RS::get_singleton()->multimesh_get_aabb(multimesh);
536+
}
537+
523538
void CPUParticles3D::_validate_property(PropertyInfo &p_property) const {
524539
if (p_property.name == "emission_sphere_radius" && (emission_shape != EMISSION_SHAPE_SPHERE && emission_shape != EMISSION_SHAPE_SPHERE_SURFACE)) {
525540
p_property.usage = PROPERTY_USAGE_NONE;
@@ -1341,6 +1356,7 @@ void CPUParticles3D::convert_from_particles(Node *p_particles) {
13411356
set_pre_process_time(gpu_particles->get_pre_process_time());
13421357
set_explosiveness_ratio(gpu_particles->get_explosiveness_ratio());
13431358
set_randomness_ratio(gpu_particles->get_randomness_ratio());
1359+
set_visibility_aabb(gpu_particles->get_visibility_aabb());
13441360
set_use_local_coordinates(gpu_particles->get_use_local_coordinates());
13451361
set_fixed_fps(gpu_particles->get_fixed_fps());
13461362
set_fractional_delta(gpu_particles->get_fractional_delta());
@@ -1420,6 +1436,7 @@ void CPUParticles3D::_bind_methods() {
14201436
ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles3D::set_pre_process_time);
14211437
ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles3D::set_explosiveness_ratio);
14221438
ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles3D::set_randomness_ratio);
1439+
ClassDB::bind_method(D_METHOD("set_visibility_aabb", "aabb"), &CPUParticles3D::set_visibility_aabb);
14231440
ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles3D::set_lifetime_randomness);
14241441
ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles3D::set_use_local_coordinates);
14251442
ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles3D::set_fixed_fps);
@@ -1433,6 +1450,7 @@ void CPUParticles3D::_bind_methods() {
14331450
ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles3D::get_pre_process_time);
14341451
ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles3D::get_explosiveness_ratio);
14351452
ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles3D::get_randomness_ratio);
1453+
ClassDB::bind_method(D_METHOD("get_visibility_aabb"), &CPUParticles3D::get_visibility_aabb);
14361454
ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles3D::get_lifetime_randomness);
14371455
ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles3D::get_use_local_coordinates);
14381456
ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles3D::get_fixed_fps);
@@ -1461,6 +1479,7 @@ void CPUParticles3D::_bind_methods() {
14611479
ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
14621480
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
14631481
ADD_GROUP("Drawing", "");
1482+
ADD_PROPERTY(PropertyInfo(Variant::AABB, "visibility_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_visibility_aabb", "get_visibility_aabb");
14641483
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
14651484
ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,View Depth"), "set_draw_order", "get_draw_order");
14661485
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
@@ -1665,6 +1684,7 @@ CPUParticles3D::CPUParticles3D() {
16651684

16661685
set_emitting(true);
16671686
set_amount(8);
1687+
set_visibility_aabb(AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8)));
16681688

16691689
set_param_min(PARAM_INITIAL_LINEAR_VELOCITY, 0);
16701690
set_param_min(PARAM_ANGULAR_VELOCITY, 0);

scene/3d/cpu_particles_3d.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class CPUParticles3D : public GeometryInstance3D {
138138
real_t randomness_ratio = 0.0;
139139
double lifetime_randomness = 0.0;
140140
double speed_scale = 1.0;
141+
AABB visibility_aabb;
141142
bool local_coords = false;
142143
int fixed_fps = 0;
143144
bool fractional_delta = true;
@@ -210,6 +211,7 @@ class CPUParticles3D : public GeometryInstance3D {
210211
void set_pre_process_time(double p_time);
211212
void set_explosiveness_ratio(real_t p_ratio);
212213
void set_randomness_ratio(real_t p_ratio);
214+
void set_visibility_aabb(const AABB &p_aabb);
213215
void set_lifetime_randomness(double p_random);
214216
void set_use_local_coordinates(bool p_enable);
215217
void set_speed_scale(double p_scale);
@@ -221,6 +223,7 @@ class CPUParticles3D : public GeometryInstance3D {
221223
double get_pre_process_time() const;
222224
real_t get_explosiveness_ratio() const;
223225
real_t get_randomness_ratio() const;
226+
AABB get_visibility_aabb() const;
224227
double get_lifetime_randomness() const;
225228
bool get_use_local_coordinates() const;
226229
double get_speed_scale() const;
@@ -308,6 +311,8 @@ class CPUParticles3D : public GeometryInstance3D {
308311

309312
void convert_from_particles(Node *p_particles);
310313

314+
AABB capture_aabb() const;
315+
311316
CPUParticles3D();
312317
~CPUParticles3D();
313318
};

0 commit comments

Comments
 (0)