Skip to content

Commit f6aa907

Browse files
committed
Merge pull request godotengine#79833 from puchik/multimesh-custom-aabb
Support custom AABBs within MultiMesh resources
2 parents d752046 + d52ac7a commit f6aa907

19 files changed

Lines changed: 261 additions & 8 deletions

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">

doc/classes/MultiMesh.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@
9292
</member>
9393
<member name="color_array" type="PackedColorArray" setter="_set_color_array" getter="_get_color_array" deprecated="Use [method set_instance_color] instead.">
9494
</member>
95+
<member name="custom_aabb" type="AABB" setter="set_custom_aabb" getter="get_custom_aabb" default="AABB(0, 0, 0, 0, 0, 0)">
96+
Custom AABB for this MultiMesh resource. Setting this manually prevents costly runtime AABB recalculations.
97+
</member>
9598
<member name="custom_data_array" type="PackedColorArray" setter="_set_custom_data_array" getter="_get_custom_data_array" deprecated="Use [method set_instance_custom_data] instead.">
99+
See [method set_instance_custom_data].
96100
</member>
97101
<member name="instance_count" type="int" setter="set_instance_count" getter="get_instance_count" default="0">
98102
Number of instances that will get drawn. This clears and (re)sizes the buffers. Setting data format or flags afterwards will have no effect.

doc/classes/RenderingServer.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,13 @@
23322332
[b]Note:[/b] If the buffer is in the engine's internal cache, it will have to be fetched from GPU memory and possibly decompressed. This means [method multimesh_get_buffer] is potentially a slow operation and should be avoided whenever possible.
23332333
</description>
23342334
</method>
2335+
<method name="multimesh_get_custom_aabb" qualifiers="const">
2336+
<return type="AABB" />
2337+
<param index="0" name="multimesh" type="RID" />
2338+
<description>
2339+
Returns the custom AABB defined for this MultiMesh resource.
2340+
</description>
2341+
</method>
23352342
<method name="multimesh_get_instance_count" qualifiers="const">
23362343
<return type="int" />
23372344
<param index="0" name="multimesh" type="RID" />
@@ -2442,6 +2449,14 @@
24422449
[/codeblock]
24432450
</description>
24442451
</method>
2452+
<method name="multimesh_set_custom_aabb">
2453+
<return type="void" />
2454+
<param index="0" name="multimesh" type="RID" />
2455+
<param index="1" name="aabb" type="AABB" />
2456+
<description>
2457+
Sets the custom AABB for this MultiMesh resource.
2458+
</description>
2459+
</method>
24452460
<method name="multimesh_set_mesh">
24462461
<return type="void" />
24472462
<param index="0" name="multimesh" type="RID" />

drivers/gles3/storage/mesh_storage.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,9 @@ void MeshStorage::_multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, b
16071607

16081608
void MeshStorage::_multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances) {
16091609
ERR_FAIL_COND(multimesh->mesh.is_null());
1610+
if (multimesh->custom_aabb != AABB()) {
1611+
return;
1612+
}
16101613
AABB aabb;
16111614
AABB mesh_aabb = mesh_get_aabb(multimesh->mesh);
16121615
for (int i = 0; i < p_instances; i++) {
@@ -1749,9 +1752,25 @@ RID MeshStorage::multimesh_get_mesh(RID p_multimesh) const {
17491752
return multimesh->mesh;
17501753
}
17511754

1755+
void MeshStorage::multimesh_set_custom_aabb(RID p_multimesh, const AABB &p_aabb) {
1756+
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
1757+
ERR_FAIL_NULL(multimesh);
1758+
multimesh->custom_aabb = p_aabb;
1759+
multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
1760+
}
1761+
1762+
AABB MeshStorage::multimesh_get_custom_aabb(RID p_multimesh) const {
1763+
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
1764+
ERR_FAIL_NULL_V(multimesh, AABB());
1765+
return multimesh->custom_aabb;
1766+
}
1767+
17521768
AABB MeshStorage::multimesh_get_aabb(RID p_multimesh) const {
17531769
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
17541770
ERR_FAIL_NULL_V(multimesh, AABB());
1771+
if (multimesh->custom_aabb != AABB()) {
1772+
return multimesh->custom_aabb;
1773+
}
17551774
if (multimesh->aabb_dirty) {
17561775
const_cast<MeshStorage *>(this)->_update_dirty_multimeshes();
17571776
}
@@ -1943,8 +1962,10 @@ void MeshStorage::multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_b
19431962
//if we have a mesh set, we need to re-generate the AABB from the new data
19441963
const float *data = p_buffer.ptr();
19451964

1946-
_multimesh_re_create_aabb(multimesh, data, multimesh->instances);
1947-
multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
1965+
if (multimesh->custom_aabb != AABB()) {
1966+
_multimesh_re_create_aabb(multimesh, data, multimesh->instances);
1967+
multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
1968+
}
19481969
}
19491970
}
19501971

@@ -2091,9 +2112,11 @@ void MeshStorage::_update_dirty_multimeshes() {
20912112
}
20922113

20932114
if (multimesh->aabb_dirty && multimesh->mesh.is_valid()) {
2094-
_multimesh_re_create_aabb(multimesh, data, visible_instances);
20952115
multimesh->aabb_dirty = false;
2096-
multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
2116+
if (multimesh->custom_aabb != AABB()) {
2117+
_multimesh_re_create_aabb(multimesh, data, visible_instances);
2118+
multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
2119+
}
20972120
}
20982121
}
20992122

drivers/gles3/storage/mesh_storage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct MultiMesh {
189189
bool uses_custom_data = false;
190190
int visible_instances = -1;
191191
AABB aabb;
192+
AABB custom_aabb;
192193
bool aabb_dirty = false;
193194
bool buffer_set = false;
194195
uint32_t stride_cache = 0;
@@ -505,6 +506,8 @@ class MeshStorage : public RendererMeshStorage {
505506
virtual void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) override;
506507

507508
virtual RID multimesh_get_mesh(RID p_multimesh) const override;
509+
virtual void multimesh_set_custom_aabb(RID p_multimesh, const AABB &p_aabb) override;
510+
virtual AABB multimesh_get_custom_aabb(RID p_multimesh) const override;
508511
virtual AABB multimesh_get_aabb(RID p_multimesh) const override;
509512

510513
virtual Transform3D multimesh_instance_get_transform(RID p_multimesh, int p_index) const override;

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);

0 commit comments

Comments
 (0)