Debian Codegraph @

editor/scene/2d/particles_2d_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* particles_2d_editor_plugin.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "particles_2d_editor_plugin.h"
32
33 #include "core/io/image_loader.h"
34 #include "editor/editor_node.h"
35 #include "editor/editor_string_names.h"
36 #include "editor/editor_undo_redo_manager.h"
37 #include "editor/gui/editor_file_dialog.h"
38 #include "scene/2d/cpu_particles_2d.h"
39 #include "scene/2d/gpu_particles_2d.h"
40 #include "scene/gui/box_container.h"
41 #include "scene/gui/check_box.h"
42 #include "scene/gui/margin_container.h"
43 #include "scene/gui/option_button.h"
44 #include "scene/gui/popup_menu.h"
45 #include "scene/gui/spin_box.h"
46 #include "scene/resources/image_texture.h"
47 #include "scene/resources/particle_process_material.h"
48
49 void GPUParticles2DEditorPlugin::_menu_callback(int p_idx) {
50 if (p_idx == MENU_GENERATE_VISIBILITY_RECT) {
51 if (need_show_lifetime_dialog(generate_seconds)) {
52 generate_visibility_rect->popup_centered();
53 } else {
54 _generate_visibility_rect();
55 }
56 } else {
57 Particles2DEditorPlugin::_menu_callback(p_idx);
58 }
59 }
60
61 void GPUParticles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
62 Particles2DEditorPlugin::_add_menu_options(p_menu);
63 p_menu->add_item(TTR("Generate Visibility Rect"), MENU_GENERATE_VISIBILITY_RECT);
64 }
65
66 void Particles2DEditorPlugin::_browse_mask_texture_pressed() {
67 browsing_texture_type = TEXTURE_TYPE_MASK;
68 file_dialog->popup_file_dialog();
69 }
70
71 void Particles2DEditorPlugin::_browse_direction_texture_pressed() {
72 browsing_texture_type = TEXTURE_TYPE_DIRECTION;
73 file_dialog->popup_centered();
74 }
75
76 void Particles2DEditorPlugin::_file_selected(const String &p_file) {
77 switch (browsing_texture_type) {
78 case TEXTURE_TYPE_MASK: {
79 mask_img_path_line_edit->set_text(p_file);
80 break;
81 }
82 case TEXTURE_TYPE_DIRECTION: {
83 direction_img_path_line_edit->set_text(p_file);
84 break;
85 }
86 }
87
88 _validate_textures();
89 }
90
91 void Particles2DEditorPlugin::_process_emission_masks(PackedVector2Array &r_valid_positions, PackedVector2Array &r_valid_normals, PackedByteArray &r_valid_colors, Vector2i &r_image_size) {
92 Ref<Image> mask_img;
93 mask_img.instantiate();
94 Error err = ImageLoader::load_image(mask_img_path_line_edit->get_text(), mask_img);
95 ERR_FAIL_COND_MSG(err != OK, vformat("Error loading image '%s'.", mask_img_path_line_edit->get_text()));
96
97 if (mask_img->is_compressed()) {
98 mask_img->decompress();
99 }
100 mask_img->convert(Image::FORMAT_RGBA8);
101 ERR_FAIL_COND(mask_img->get_format() != Image::FORMAT_RGBA8);
102 Size2i mask_img_size = mask_img->get_size();
103 ERR_FAIL_COND(mask_img_size.width == 0 || mask_img_size.height == 0);
104
105 r_image_size = mask_img_size;
106
107 r_valid_positions.resize(mask_img_size.width * mask_img_size.height);
108
109 MaskMode emission_mode = static_cast<MaskMode>(emission_mask_mode->get_selected());
110 DirectionMode direction_mode = static_cast<DirectionMode>(emission_direction_mode->get_selected());
111
112 if (direction_mode != DIRECTION_MODE_NONE) {
113 r_valid_normals.resize(mask_img_size.width * mask_img_size.height);
114 }
115
116 bool capture_colors = emission_mask_colors->is_pressed();
117
118 if (capture_colors) {
119 r_valid_colors.resize(mask_img_size.width * mask_img_size.height * 4);
120 }
121
122 int valid_point_count = 0;
123
124 {
125 Vector<uint8_t> mask_img_data = mask_img->get_data();
126 const uint8_t *mask_img_ptr = mask_img_data.ptr();
127
128 for (int mask_img_x = 0; mask_img_x < mask_img_size.width; mask_img_x++) {
129 for (int mask_img_y = 0; mask_img_y < mask_img_size.height; mask_img_y++) {
130 uint8_t mask_alpha = mask_img_ptr[(mask_img_y * mask_img_size.width + mask_img_x) * 4 + 3];
131
132 if (mask_alpha <= 128) {
133 continue;
134 }
135
136 if (emission_mode == MASK_MODE_SOLID) {
137 r_valid_positions.write[valid_point_count++] = Point2(mask_img_x, mask_img_y);
138 } else {
139 bool pixel_is_on_border = false;
140 for (int x = mask_img_x - 1; x <= mask_img_x + 1; x++) {
141 for (int y = mask_img_y - 1; y <= mask_img_y + 1; y++) {
142 if (x < 0 || y < 0 || x >= mask_img_size.width || y >= mask_img_size.height || mask_img_ptr[(y * mask_img_size.width + x) * 4 + 3] <= 128) {
143 pixel_is_on_border = true;
144 break;
145 }
146 }
147
148 if (pixel_is_on_border) {
149 break;
150 }
151 }
152
153 if (!pixel_is_on_border) {
154 continue;
155 }
156
157 r_valid_positions.write[valid_point_count] = Point2(mask_img_x, mask_img_y);
158
159 if (direction_mode == DIRECTION_MODE_GENERATE) {
160 Vector2 normal;
161 for (int x = mask_img_x - 2; x <= mask_img_x + 2; x++) {
162 for (int y = mask_img_y - 2; y <= mask_img_y + 2; y++) {
163 if (x == mask_img_x && y == mask_img_y) {
164 continue;
165 }
166
167 if (x < 0 || y < 0 || x >= mask_img_size.width || y >= mask_img_size.height || mask_img_ptr[(y * mask_img_size.width + x) * 4 + 3] <= 128) {
168 normal += Vector2(x - mask_img_x, y - mask_img_y).normalized();
169 }
170 }
171 }
172
173 normal.normalize();
174 r_valid_normals.write[valid_point_count] = normal;
175 }
176
177 valid_point_count++;
178 }
179 }
180 }
181
182 if (capture_colors) {
183 for (int i = 0; i < valid_point_count; ++i) {
184 const Point2i point = r_valid_positions.get(i);
185 r_valid_colors.write[i * 4 + 0] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 0];
186 r_valid_colors.write[i * 4 + 1] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 1];
187 r_valid_colors.write[i * 4 + 2] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 2];
188 r_valid_colors.write[i * 4 + 3] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 3];
189 }
190 }
191 }
192
193 if (direction_mode == DIRECTION_MODE_TEXTURE) {
194 Ref<Image> normal_img;
195 normal_img.instantiate();
196 err = ImageLoader::load_image(direction_img_path_line_edit->get_text(), normal_img);
197 ERR_FAIL_COND_MSG(err != OK, vformat("Error loading image '%s'.", direction_img_path_line_edit->get_text()));
198
199 if (normal_img->is_compressed()) {
200 normal_img->decompress();
201 }
202 normal_img->convert(Image::FORMAT_RGB8);
203 ERR_FAIL_COND(normal_img->get_format() != Image::FORMAT_RGB8);
204 Size2i normal_img_size = normal_img->get_size();
205 ERR_FAIL_COND(normal_img_size.width == 0 || normal_img_size.height == 0);
206 ERR_FAIL_COND_MSG(normal_img_size != mask_img_size, "Mask and Normal texture must have the same size.");
207
208 Vector<uint8_t> normal_img_data = normal_img->get_data();
209 const uint8_t *normal_img_ptr = normal_img_data.ptr();
210
211 for (int i = 0; i < valid_point_count; ++i) {
212 const Point2i point = r_valid_positions.get(i);
213 const uint8_t normal_r = normal_img_ptr[(point.y * normal_img_size.width + point.x) * 3 + 0];
214 const uint8_t normal_g = normal_img_ptr[(point.y * normal_img_size.width + point.x) * 3 + 1];
215
216 Vector2 normal;
217 normal.x = static_cast<float>(normal_r) / 255.0f - 0.5f;
218 normal.y = static_cast<float>(normal_g) / 255.0f - 0.5f;
219
220 normal.normalize();
221
222 r_valid_normals.write[i] = normal;
223 }
224 }
225
226 r_valid_positions.resize(valid_point_count);
227 if (!r_valid_normals.is_empty()) {
228 r_valid_normals.resize(valid_point_count);
229 }
230 }
231
232 Particles2DEditorPlugin::Particles2DEditorPlugin() {
233 file_dialog = memnew(EditorFileDialog);
234
235 List<String> ext;
236 ImageLoader::get_recognized_extensions(&ext);
237 for (const String &E : ext) {
238 file_dialog->add_filter("*." + E, E.to_upper());
239 }
240
241 file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
242 file_dialog->connect("file_selected", callable_mp(this, &Particles2DEditorPlugin::_file_selected));
243
244 emission_mask_dialog = memnew(ConfirmationDialog);
245 emission_mask_dialog->set_title(TTRC("Load Emission Mask"));
246 emission_mask_dialog->add_child(file_dialog);
247 emission_mask_dialog->get_ok_button()->set_disabled(true);
248
249 VBoxContainer *emvb = memnew(VBoxContainer);
250 emission_mask_dialog->add_child(emvb);
251
252 HBoxContainer *mask_img_hbox = memnew(HBoxContainer);
253
254 mask_img_path_line_edit = memnew(LineEdit);
255 mask_img_hbox->add_child(mask_img_path_line_edit);
256 mask_img_path_line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
257 mask_img_path_line_edit->set_editable(false);
258 mask_img_path_line_edit->connect(SceneStringName(text_changed), callable_mp(this, &Particles2DEditorPlugin::_validate_textures).unbind(1));
259
260 mask_browse_button = memnew(Button);
261 mask_img_hbox->add_child(mask_browse_button);
262 mask_browse_button->connect(SceneStringName(pressed), callable_mp(this, &Particles2DEditorPlugin::_browse_mask_texture_pressed));
263 emvb->add_margin_child(TTRC("Mask Texture"), mask_img_hbox);
264
265 emission_mask_mode = memnew(OptionButton);
266 emission_mask_mode->add_item(TTRC("Solid Pixels"), MASK_MODE_SOLID);
267 emission_mask_mode->add_item(TTRC("Border Pixels"), MASK_MODE_BORDER);
268 emission_mask_mode->connect(SceneStringName(item_selected), callable_mp(this, &Particles2DEditorPlugin::_emission_mask_mode_item_changed));
269 emvb->add_margin_child(TTRC("Mask Mode"), emission_mask_mode);
270
271 emission_direction_mode = memnew(OptionButton);
272 emission_direction_mode->add_item(TTRC("None"), DIRECTION_MODE_NONE);
273 emission_direction_mode->add_item(TTRC("Generate"), DIRECTION_MODE_GENERATE);
274 emission_direction_mode->add_item(TTRC("Texture"), DIRECTION_MODE_TEXTURE);
275 emission_direction_mode->connect(SceneStringName(item_selected), callable_mp(this, &Particles2DEditorPlugin::_validate_textures).unbind(1));
276 emission_direction_mode->set_item_disabled(DIRECTION_MODE_GENERATE, true);
277 emvb->add_margin_child(TTRC("Direction Mode"), emission_direction_mode);
278
279 direction_img_label = memnew(Label);
280 direction_img_label->set_text(TTRC("Direction Texture"));
281 direction_img_label->set_theme_type_variation("HeaderSmall");
282 emvb->add_child(direction_img_label);
283 direction_img_label->hide();
284
285 direction_img_hbox = memnew(HBoxContainer);
286 direction_img_path_line_edit = memnew(LineEdit);
287 direction_img_hbox->add_child(direction_img_path_line_edit);
288 direction_img_path_line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
289 direction_img_path_line_edit->set_editable(false);
290 direction_img_path_line_edit->connect(SceneStringName(text_changed), callable_mp(this, &Particles2DEditorPlugin::_validate_textures).unbind(1));
291
292 direction_browse_button = memnew(Button);
293 direction_img_hbox->add_child(direction_browse_button);
294 direction_browse_button->connect(SceneStringName(pressed), callable_mp(this, &Particles2DEditorPlugin::_browse_direction_texture_pressed));
295 emvb->add_child(direction_img_hbox);
296 direction_img_hbox->hide();
297
298 VBoxContainer *optionsvb = memnew(VBoxContainer);
299 emvb->add_margin_child(TTRC("Options"), optionsvb);
300
301 emission_mask_centered = memnew(CheckBox(TTRC("Centered")));
302 emission_mask_centered->set_pressed(true);
303 optionsvb->add_child(emission_mask_centered);
304 emission_mask_colors = memnew(CheckBox(TTRC("Copy Color from Mask Texture")));
305 optionsvb->add_child(emission_mask_colors);
306
307 error_message = memnew(Label);
308 error_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
309 error_message->set_h_size_flags(Control::SIZE_EXPAND_FILL);
310 error_message->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("error_color"), EditorStringName(Editor)));
311 emvb->add_child(error_message);
312
313 EditorNode::get_singleton()->get_gui_base()->add_child(emission_mask_dialog);
314
315 emission_mask_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Particles2DEditorPlugin::_generate_emission_mask));
316 emission_mask_dialog->connect(SceneStringName(theme_changed), callable_mp(this, &Particles2DEditorPlugin::_theme_changed));
317 }
318
319 void Particles2DEditorPlugin::_set_show_gizmos(Node *p_node, bool p_show) {
320 GPUParticles2D *gpu_particles = Object::cast_to<GPUParticles2D>(p_node);
321 if (gpu_particles) {
322 gpu_particles->set_show_gizmos(p_show);
323 }
324 CPUParticles2D *cpu_particles = Object::cast_to<CPUParticles2D>(p_node);
325 if (cpu_particles) {
326 cpu_particles->set_show_gizmos(p_show);
327 }
328
329 // The `selection_changed` signal is deferred. A node could be deleted before the signal is emitted.
330 if (p_show) {
331 p_node->connect(SceneStringName(tree_exiting), callable_mp(this, &Particles2DEditorPlugin::_node_removed).bind(p_node));
332 } else {
333 p_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Particles2DEditorPlugin::_node_removed));
334 }
335 }
336
337 void Particles2DEditorPlugin::_selection_changed() {
338 const List<Node *> &current_selection = EditorNode::get_singleton()->get_editor_selection()->get_top_selected_node_list();
339 if (selected_particles.is_empty() && current_selection.is_empty()) {
340 return;
341 }
342
343 // Turn gizmos on for nodes that are newly selected.
344 HashSet<ObjectID> nodes_in_current_selection;
345 for (Node *node : current_selection) {
346 ObjectID nid = node->get_instance_id();
347 nodes_in_current_selection.insert(nid);
348 if (!selected_particles.has(nid)) {
349 _set_show_gizmos(node, true);
350 selected_particles.insert(nid);
351 }
352 }
353
354 mask_img_path_line_edit->set_text("");
355 emission_mask_mode->select(MASK_MODE_SOLID);
356 emission_direction_mode->select(DIRECTION_MODE_NONE);
357 emission_mask_centered->set_pressed(true);
358 emission_mask_colors->set_pressed(false);
359 direction_img_path_line_edit->set_text("");
360
361 // Turn gizmos off for nodes that are no longer selected.
362 LocalVector<ObjectID> to_erase;
363 for (const ObjectID &nid : selected_particles) {
364 if (!nodes_in_current_selection.has(nid)) {
365 Node *node = ObjectDB::get_instance<Node>(nid);
366 if (node) {
367 _set_show_gizmos(node, false);
368 }
369 to_erase.push_back(nid);
370 }
371 }
372
373 for (const ObjectID &nid : to_erase) {
374 selected_particles.erase(nid);
375 }
376 }
377
378 void Particles2DEditorPlugin::_node_removed(Node *p_node) {
379 if (p_node && selected_particles.erase(p_node->get_instance_id())) {
380 _set_show_gizmos(p_node, false);
381 }
382 }
383
384 void GPUParticles2DEditorPlugin::_generate_visibility_rect() {
385 GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
386
387 double time = generate_seconds->get_value();
388
389 float running = 0.0;
390
391 EditorProgress ep("gen_vrect", TTR("Generating Visibility Rect (Waiting for Particle Simulation)"), int(time));
392
393 bool was_emitting = particles->is_emitting();
394 if (!was_emitting) {
395 particles->set_emitting(true);
396 OS::get_singleton()->delay_usec(1000);
397 }
398
399 Rect2 rect;
400 while (running < time) {
401 uint64_t ticks = OS::get_singleton()->get_ticks_usec();
402 ep.step(TTR("Generating..."), int(running), true);
403 OS::get_singleton()->delay_usec(1000);
404
405 Rect2 capture = particles->capture_rect();
406 if (rect == Rect2()) {
407 rect = capture;
408 } else {
409 rect = rect.merge(capture);
410 }
411
412 running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
413 }
414
415 if (!was_emitting) {
416 particles->set_emitting(false);
417 }
418
419 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
420 undo_redo->create_action(TTR("Generate Visibility Rect"));
421 undo_redo->add_do_method(particles, "set_visibility_rect", rect);
422 undo_redo->add_undo_method(particles, "set_visibility_rect", particles->get_visibility_rect());
423 undo_redo->commit_action();
424 }
425
426 void Particles2DEditorPlugin::_notification(int p_what) {
427 switch (p_what) {
428 case NOTIFICATION_ENTER_TREE: {
429 EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", callable_mp(this, &Particles2DEditorPlugin::_selection_changed));
430 } break;
431 }
432 }
433
434 void Particles2DEditorPlugin::_theme_changed() {
435 mask_browse_button->set_button_icon(mask_browse_button->get_editor_theme_icon(SNAME("FileBrowse")));
436 direction_browse_button->set_button_icon(direction_browse_button->get_editor_theme_icon(SNAME("FileBrowse")));
437 }
438
439 void Particles2DEditorPlugin::_menu_callback(int p_idx) {
440 if (p_idx == MENU_LOAD_EMISSION_MASK) {
441 GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
442 if (particles && particles->get_process_material().is_null()) {
443 EditorNode::get_singleton()->show_warning(TTR("Loading emission mask requires ParticleProcessMaterial."));
444 return;
445 }
446
447 emission_mask_dialog->popup_centered();
448 } else {
449 ParticlesEditorPlugin::_menu_callback(p_idx);
450 }
451 }
452
453 void Particles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
454 p_menu->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
455 }
456
457 void Particles2DEditorPlugin::_validate_textures() {
458 DirectionMode direction_mode = static_cast<DirectionMode>(emission_direction_mode->get_selected());
459 direction_img_label->set_visible(direction_mode == DIRECTION_MODE_TEXTURE);
460 direction_img_hbox->set_visible(direction_mode == DIRECTION_MODE_TEXTURE);
461
462 error_message->hide();
463 emission_mask_dialog->get_ok_button()->set_disabled(true);
464
465 if (mask_img_path_line_edit->get_text().is_empty()) {
466 emission_mask_dialog->reset_size();
467 return;
468 }
469
470 Ref<Image> mask_img;
471 mask_img.instantiate();
472 Error err = ImageLoader::load_image(mask_img_path_line_edit->get_text(), mask_img);
473 if (err != OK) {
474 error_message->show();
475 error_message->set_text(TTRC("Failed to load mask texture."));
476 emission_mask_dialog->reset_size();
477 return;
478 }
479
480 if (mask_img->is_compressed()) {
481 mask_img->decompress();
482 }
483 mask_img->convert(Image::FORMAT_RGBA8);
484
485 if (mask_img->get_format() != Image::FORMAT_RGBA8) {
486 error_message->show();
487 error_message->set_text(TTRC("Failed to convert mask texture to RGBA8."));
488 emission_mask_dialog->reset_size();
489 return;
490 }
491
492 Size2i mask_img_size = mask_img->get_size();
493 if (mask_img_size.width == 0 || mask_img_size.height == 0) {
494 error_message->show();
495 error_message->set_text(TTRC("Mask texture has an invalid size."));
496 emission_mask_dialog->reset_size();
497 return;
498 }
499
500 if (direction_mode == DIRECTION_MODE_TEXTURE) {
501 if (direction_img_path_line_edit->get_text().is_empty()) {
502 return;
503 }
504
505 Ref<Image> direction_img;
506 direction_img.instantiate();
507 err = ImageLoader::load_image(direction_img_path_line_edit->get_text(), direction_img);
508
509 if (err != OK) {
510 error_message->show();
511 error_message->set_text(TTRC("Failed to load direction texture."));
512 emission_mask_dialog->reset_size();
513 return;
514 }
515
516 if (direction_img->is_compressed()) {
517 direction_img->decompress();
518 }
519 direction_img->convert(Image::FORMAT_RGBA8);
520
521 if (direction_img->get_format() != Image::FORMAT_RGBA8) {
522 error_message->show();
523 error_message->set_text(TTRC("Failed to convert direction texture to RGBA8."));
524 emission_mask_dialog->reset_size();
525 return;
526 }
527
528 Size2i direction_img_size = direction_img->get_size();
529
530 if (direction_img_size.width == 0 || direction_img_size.height == 0 || direction_img_size != mask_img_size) {
531 error_message->show();
532 error_message->set_text(TTRC("Direction texture has an invalid size. It must have the same size as the mask texture."));
533 emission_mask_dialog->reset_size();
534 return;
535 }
536 }
537
538 emission_mask_dialog->get_ok_button()->set_disabled(false);
539 emission_mask_dialog->reset_size();
540 }
541
542 void Particles2DEditorPlugin::_emission_mask_mode_item_changed(int p_idx) const {
543 emission_direction_mode->set_item_disabled(DIRECTION_MODE_GENERATE, p_idx == static_cast<int>(MASK_MODE_SOLID));
544
545 if (emission_direction_mode->get_selected() == DIRECTION_MODE_GENERATE) {
546 emission_direction_mode->select(DIRECTION_MODE_NONE);
547 }
548 }
549
550 Node *GPUParticles2DEditorPlugin::_convert_particles() {
551 GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
552
553 CPUParticles2D *cpu_particles = memnew(CPUParticles2D);
554 cpu_particles->convert_from_particles(particles);
555 cpu_particles->set_name(particles->get_name());
556 cpu_particles->set_transform(particles->get_transform());
557 cpu_particles->set_visible(particles->is_visible());
558 cpu_particles->set_process_mode(particles->get_process_mode());
559 cpu_particles->set_z_index(particles->get_z_index());
560 return cpu_particles;
561 }
562
563 void GPUParticles2DEditorPlugin::_generate_emission_mask() {
564 GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
565 Ref<ParticleProcessMaterial> pm = particles->get_process_material();
566 ERR_FAIL_COND(pm.is_null());
567
568 PackedVector2Array emission_positions;
569 PackedVector2Array emission_normals;
570 PackedByteArray emission_colors;
571 Vector2i texture_size;
572 _process_emission_masks(emission_positions, emission_normals, emission_colors, texture_size);
573
574 ERR_FAIL_COND_MSG(emission_positions.is_empty(), "No pixels with transparency > 128 in image...");
575
576 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
577 undo_redo->create_action(TTR("Load Emission Mask"));
578 ParticleProcessMaterial *pmptr = pm.ptr();
579
580 Vector<uint8_t> mask_texture_data;
581
582 int valid_positions_count = emission_positions.size();
583 int w = 2048;
584 int h = (valid_positions_count / 2048) + 1;
585
586 mask_texture_data.resize_initialized(w * h * 2 * sizeof(float));
587
588 {
589 Vector2 offset;
590 if (emission_mask_centered->is_pressed()) {
591 offset = Vector2(-texture_size.width * 0.5, -texture_size.height * 0.5);
592 }
593
594 uint8_t *tw = mask_texture_data.ptrw();
595 float *twf = reinterpret_cast<float *>(tw);
596 for (int i = 0; i < valid_positions_count; i++) {
597 twf[i * 2 + 0] = emission_positions[i].x + offset.x;
598 twf[i * 2 + 1] = emission_positions[i].y + offset.y;
599 }
600 }
601
602 Ref<Image> img;
603 img.instantiate();
604 img->set_data(w, h, false, Image::FORMAT_RGF, mask_texture_data);
605 undo_redo->add_do_property(pmptr, "emission_point_texture", ImageTexture::create_from_image(img));
606 undo_redo->add_undo_property(pmptr, "emission_point_texture", pm->get_emission_point_texture());
607 undo_redo->add_do_property(pmptr, "emission_point_count", valid_positions_count);
608 undo_redo->add_undo_property(pmptr, "emission_point_count", pm->get_emission_point_count());
609
610 if (emission_mask_colors->is_pressed()) {
611 Vector<uint8_t> color_texture_data;
612 color_texture_data.resize_initialized(w * h * 4);
613
614 {
615 uint8_t *tw = color_texture_data.ptrw();
616 for (int i = 0; i < valid_positions_count * 4; i++) {
617 tw[i] = emission_colors[i];
618 }
619 }
620
621 img.instantiate();
622 img->set_data(w, h, false, Image::FORMAT_RGBA8, color_texture_data);
623 undo_redo->add_do_property(pmptr, "emission_color_texture", ImageTexture::create_from_image(img));
624 undo_redo->add_undo_property(pmptr, "emission_color_texture", pm->get_emission_color_texture());
625 }
626
627 if (emission_normals.size()) {
628 undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
629 undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());
630 pm->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
631
632 Vector<uint8_t> normal_texture_data;
633 normal_texture_data.resize_initialized(w * h * 2 * sizeof(float));
634
635 {
636 uint8_t *tw = normal_texture_data.ptrw();
637 float *twf = reinterpret_cast<float *>(tw);
638 for (int i = 0; i < valid_positions_count; i++) {
639 twf[i * 2 + 0] = emission_normals[i].x;
640 twf[i * 2 + 1] = emission_normals[i].y;
641 }
642 }
643
644 img.instantiate();
645 img->set_data(w, h, false, Image::FORMAT_RGF, normal_texture_data);
646 undo_redo->add_do_property(pmptr, "emission_normal_texture", ImageTexture::create_from_image(img));
647 undo_redo->add_undo_property(pmptr, "emission_normal_texture", pm->get_emission_normal_texture());
648 } else {
649 undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
650 undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());
651 }
652 undo_redo->commit_action();
653 }
654
655 GPUParticles2DEditorPlugin::GPUParticles2DEditorPlugin() {
656 handled_type = TTRC("GPUParticles2D");
657 conversion_option_name = TTR("Convert to CPUParticles2D");
658
659 generate_visibility_rect = memnew(ConfirmationDialog);
660 generate_visibility_rect->set_title(TTR("Generate Visibility Rect"));
661
662 VBoxContainer *genvb = memnew(VBoxContainer);
663 generate_visibility_rect->add_child(genvb);
664
665 generate_seconds = memnew(SpinBox);
666 generate_seconds->set_min(0.1);
667 generate_seconds->set_max(25);
668 generate_seconds->set_value(2);
669 genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
670
671 EditorNode::get_singleton()->get_gui_base()->add_child(generate_visibility_rect);
672
673 generate_visibility_rect->connect(SceneStringName(confirmed), callable_mp(this, &GPUParticles2DEditorPlugin::_generate_visibility_rect));
674 }
675
676 Node *CPUParticles2DEditorPlugin::_convert_particles() {
677 CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);
678
679 GPUParticles2D *gpu_particles = memnew(GPUParticles2D);
680 gpu_particles->convert_from_particles(particles);
681 gpu_particles->set_name(particles->get_name());
682 gpu_particles->set_transform(particles->get_transform());
683 gpu_particles->set_visible(particles->is_visible());
684 gpu_particles->set_process_mode(particles->get_process_mode());
685 return gpu_particles;
686 }
687
688 void CPUParticles2DEditorPlugin::_generate_emission_mask() {
689 CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);
690
691 PackedVector2Array valid_positions;
692 PackedVector2Array valid_normals;
693 PackedByteArray valid_colors;
694 Vector2i image_size;
695 _process_emission_masks(valid_positions, valid_normals, valid_colors, image_size);
696
697 ERR_FAIL_COND_MSG(valid_positions.is_empty(), "No pixels with transparency > 128 in image...");
698
699 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
700 undo_redo->create_action(TTR("Load Emission Mask"));
701
702 int valid_point_count = valid_positions.size();
703 if (emission_mask_colors->is_pressed()) {
704 PackedColorArray pca;
705 pca.resize(valid_point_count);
706 Color *pcaw = pca.ptrw();
707 for (int i = 0; i < valid_point_count; i += 1) {
708 Color color;
709 color.r = valid_colors[i * 4 + 0] / 255.0f;
710 color.g = valid_colors[i * 4 + 1] / 255.0f;
711 color.b = valid_colors[i * 4 + 2] / 255.0f;
712 color.a = valid_colors[i * 4 + 3] / 255.0f;
713 pcaw[i] = color;
714 }
715 undo_redo->add_do_property(particles, "emission_colors", pca);
716 undo_redo->add_undo_property(particles, "emission_colors", particles->get_emission_colors());
717 }
718
719 if (!valid_normals.is_empty()) {
720 undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_DIRECTED_POINTS);
721 undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
722 PackedVector2Array norms;
723 norms.resize(valid_normals.size());
724 Vector2 *normsw = norms.ptrw();
725 for (int i = 0; i < valid_normals.size(); i += 1) {
726 normsw[i] = valid_normals[i];
727 }
728 undo_redo->add_do_property(particles, "emission_normals", norms);
729 undo_redo->add_undo_property(particles, "emission_normals", particles->get_emission_normals());
730 } else {
731 undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_POINTS);
732 undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
733 }
734
735 {
736 Vector2 offset;
737 if (emission_mask_centered->is_pressed()) {
738 offset = Vector2(-image_size.width * 0.5, -image_size.height * 0.5);
739 }
740
741 PackedVector2Array points;
742 points.resize(valid_positions.size());
743 Vector2 *pointsw = points.ptrw();
744 for (int i = 0; i < valid_positions.size(); i += 1) {
745 pointsw[i] = valid_positions[i] + offset;
746 }
747 undo_redo->add_do_property(particles, "emission_points", points);
748 undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_points());
749 }
750 undo_redo->commit_action();
751 }
752
753 CPUParticles2DEditorPlugin::CPUParticles2DEditorPlugin() {
754 handled_type = TTRC("CPUParticles2D");
755 conversion_option_name = TTR("Convert to GPUParticles2D");
756 }