particles_2d_editor_plugin.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. #include "particles_2d_editor_plugin.h"
  31. #include "core/io/image_loader.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/gui/editor_file_dialog.h"
  36. #include "scene/2d/cpu_particles_2d.h"
  37. #include "scene/2d/gpu_particles_2d.h"
  38. #include "scene/gui/box_container.h"
  39. #include "scene/gui/check_box.h"
  40. #include "scene/gui/margin_container.h"
  41. #include "scene/gui/option_button.h"
  42. #include "scene/gui/popup_menu.h"
  43. #include "scene/gui/spin_box.h"
  44. #include "scene/resources/image_texture.h"
  45. #include "scene/resources/particle_process_material.h"
  46. void GPUParticles2DEditorPlugin::_menu_callback(int p_idx) {
  47. if (p_idx == MENU_GENERATE_VISIBILITY_RECT) {
  48. if (need_show_lifetime_dialog(generate_seconds)) {
  49. generate_visibility_rect->popup_centered();
  50. } else {
  51. _generate_visibility_rect();
  52. }
  53. } else {
  54. Particles2DEditorPlugin::_menu_callback(p_idx);
  55. }
  56. }
  57. void GPUParticles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
  58. Particles2DEditorPlugin::_add_menu_options(p_menu);
  59. p_menu->add_item(TTR("Generate Visibility Rect"), MENU_GENERATE_VISIBILITY_RECT);
  60. }
  61. void Particles2DEditorPlugin::_browse_mask_texture_pressed() {
  62. browsing_texture_type = TEXTURE_TYPE_MASK;
  63. file_dialog->popup_file_dialog();
  64. }
  65. void Particles2DEditorPlugin::_browse_direction_texture_pressed() {
  66. browsing_texture_type = TEXTURE_TYPE_DIRECTION;
  67. file_dialog->popup_centered();
  68. }
  69. void Particles2DEditorPlugin::_file_selected(const String &p_file) {
  70. switch (browsing_texture_type) {
  71. case TEXTURE_TYPE_MASK: {
  72. mask_img_path_line_edit->set_text(p_file);
  73. break;
  74. }
  75. case TEXTURE_TYPE_DIRECTION: {
  76. direction_img_path_line_edit->set_text(p_file);
  77. break;
  78. }
  79. }
  80. _validate_textures();
  81. }
  82. void Particles2DEditorPlugin::_process_emission_masks(PackedVector2Array &r_valid_positions, PackedVector2Array &r_valid_normals, PackedByteArray &r_valid_colors, Vector2i &r_image_size) {
  83. Ref<Image> mask_img;
  84. mask_img.instantiate();
  85. Error err = ImageLoader::load_image(mask_img_path_line_edit->get_text(), mask_img);
  86. ERR_FAIL_COND_MSG(err != OK, vformat("Error loading image '%s'.", mask_img_path_line_edit->get_text()));
  87. if (mask_img->is_compressed()) {
  88. mask_img->decompress();
  89. }
  90. mask_img->convert(Image::FORMAT_RGBA8);
  91. ERR_FAIL_COND(mask_img->get_format() != Image::FORMAT_RGBA8);
  92. Size2i mask_img_size = mask_img->get_size();
  93. ERR_FAIL_COND(mask_img_size.width == 0 || mask_img_size.height == 0);
  94. r_image_size = mask_img_size;
  95. r_valid_positions.resize(mask_img_size.width * mask_img_size.height);
  96. MaskMode emission_mode = static_cast<MaskMode>(emission_mask_mode->get_selected());
  97. DirectionMode direction_mode = static_cast<DirectionMode>(emission_direction_mode->get_selected());
  98. if (direction_mode != DIRECTION_MODE_NONE) {
  99. r_valid_normals.resize(mask_img_size.width * mask_img_size.height);
  100. }
  101. bool capture_colors = emission_mask_colors->is_pressed();
  102. if (capture_colors) {
  103. r_valid_colors.resize(mask_img_size.width * mask_img_size.height * 4);
  104. }
  105. int valid_point_count = 0;
  106. {
  107. Vector<uint8_t> mask_img_data = mask_img->get_data();
  108. const uint8_t *mask_img_ptr = mask_img_data.ptr();
  109. for (int mask_img_x = 0; mask_img_x < mask_img_size.width; mask_img_x++) {
  110. for (int mask_img_y = 0; mask_img_y < mask_img_size.height; mask_img_y++) {
  111. uint8_t mask_alpha = mask_img_ptr[(mask_img_y * mask_img_size.width + mask_img_x) * 4 + 3];
  112. if (mask_alpha <= 128) {
  113. continue;
  114. }
  115. if (emission_mode == MASK_MODE_SOLID) {
  116. r_valid_positions.write[valid_point_count++] = Point2(mask_img_x, mask_img_y);
  117. } else {
  118. bool pixel_is_on_border = false;
  119. for (int x = mask_img_x - 1; x <= mask_img_x + 1; x++) {
  120. for (int y = mask_img_y - 1; y <= mask_img_y + 1; y++) {
  121. 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) {
  122. pixel_is_on_border = true;
  123. break;
  124. }
  125. }
  126. if (pixel_is_on_border) {
  127. break;
  128. }
  129. }
  130. if (!pixel_is_on_border) {
  131. continue;
  132. }
  133. r_valid_positions.write[valid_point_count] = Point2(mask_img_x, mask_img_y);
  134. if (direction_mode == DIRECTION_MODE_GENERATE) {
  135. Vector2 normal;
  136. for (int x = mask_img_x - 2; x <= mask_img_x + 2; x++) {
  137. for (int y = mask_img_y - 2; y <= mask_img_y + 2; y++) {
  138. if (x == mask_img_x && y == mask_img_y) {
  139. continue;
  140. }
  141. 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) {
  142. normal += Vector2(x - mask_img_x, y - mask_img_y).normalized();
  143. }
  144. }
  145. }
  146. normal.normalize();
  147. r_valid_normals.write[valid_point_count] = normal;
  148. }
  149. valid_point_count++;
  150. }
  151. }
  152. }
  153. if (capture_colors) {
  154. for (int i = 0; i < valid_point_count; ++i) {
  155. const Point2i point = r_valid_positions.get(i);
  156. r_valid_colors.write[i * 4 + 0] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 0];
  157. r_valid_colors.write[i * 4 + 1] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 1];
  158. r_valid_colors.write[i * 4 + 2] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 2];
  159. r_valid_colors.write[i * 4 + 3] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 3];
  160. }
  161. }
  162. }
  163. if (direction_mode == DIRECTION_MODE_TEXTURE) {
  164. Ref<Image> normal_img;
  165. normal_img.instantiate();
  166. err = ImageLoader::load_image(direction_img_path_line_edit->get_text(), normal_img);
  167. ERR_FAIL_COND_MSG(err != OK, vformat("Error loading image '%s'.", direction_img_path_line_edit->get_text()));
  168. if (normal_img->is_compressed()) {
  169. normal_img->decompress();
  170. }
  171. normal_img->convert(Image::FORMAT_RGB8);
  172. ERR_FAIL_COND(normal_img->get_format() != Image::FORMAT_RGB8);
  173. Size2i normal_img_size = normal_img->get_size();
  174. ERR_FAIL_COND(normal_img_size.width == 0 || normal_img_size.height == 0);
  175. ERR_FAIL_COND_MSG(normal_img_size != mask_img_size, "Mask and Normal texture must have the same size.");
  176. Vector<uint8_t> normal_img_data = normal_img->get_data();
  177. const uint8_t *normal_img_ptr = normal_img_data.ptr();
  178. for (int i = 0; i < valid_point_count; ++i) {
  179. const Point2i point = r_valid_positions.get(i);
  180. const uint8_t normal_r = normal_img_ptr[(point.y * normal_img_size.width + point.x) * 3 + 0];
  181. const uint8_t normal_g = normal_img_ptr[(point.y * normal_img_size.width + point.x) * 3 + 1];
  182. Vector2 normal;
  183. normal.x = static_cast<float>(normal_r) / 255.0f - 0.5f;
  184. normal.y = static_cast<float>(normal_g) / 255.0f - 0.5f;
  185. normal.normalize();
  186. r_valid_normals.write[i] = normal;
  187. }
  188. }
  189. r_valid_positions.resize(valid_point_count);
  190. if (!r_valid_normals.is_empty()) {
  191. r_valid_normals.resize(valid_point_count);
  192. }
  193. }
  194. Particles2DEditorPlugin::Particles2DEditorPlugin() {
  195. file_dialog = memnew(EditorFileDialog);
  196. List<String> ext;
  197. ImageLoader::get_recognized_extensions(&ext);
  198. for (const String &E : ext) {
  199. file_dialog->add_filter("*." + E, E.to_upper());
  200. }
  201. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  202. file_dialog->connect("file_selected", callable_mp(this, &Particles2DEditorPlugin::_file_selected));
  203. emission_mask_dialog = memnew(ConfirmationDialog);
  204. emission_mask_dialog->set_title(TTRC("Load Emission Mask"));
  205. emission_mask_dialog->add_child(file_dialog);
  206. emission_mask_dialog->get_ok_button()->set_disabled(true);
  207. VBoxContainer *emvb = memnew(VBoxContainer);
  208. emission_mask_dialog->add_child(emvb);
  209. HBoxContainer *mask_img_hbox = memnew(HBoxContainer);
  210. mask_img_path_line_edit = memnew(LineEdit);
  211. mask_img_hbox->add_child(mask_img_path_line_edit);
  212. mask_img_path_line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  213. mask_img_path_line_edit->set_editable(false);
  214. mask_img_path_line_edit->connect(SceneStringName(text_changed), callable_mp(this, &Particles2DEditorPlugin::_validate_textures).unbind(1));
  215. mask_browse_button = memnew(Button);
  216. mask_img_hbox->add_child(mask_browse_button);
  217. mask_browse_button->connect(SceneStringName(pressed), callable_mp(this, &Particles2DEditorPlugin::_browse_mask_texture_pressed));
  218. emvb->add_margin_child(TTRC("Mask Texture"), mask_img_hbox);
  219. emission_mask_mode = memnew(OptionButton);
  220. emission_mask_mode->add_item(TTRC("Solid Pixels"), MASK_MODE_SOLID);
  221. emission_mask_mode->add_item(TTRC("Border Pixels"), MASK_MODE_BORDER);
  222. emission_mask_mode->connect(SceneStringName(item_selected), callable_mp(this, &Particles2DEditorPlugin::_emission_mask_mode_item_changed));
  223. emvb->add_margin_child(TTRC("Mask Mode"), emission_mask_mode);
  224. emission_direction_mode = memnew(OptionButton);
  225. emission_direction_mode->add_item(TTRC("None"), DIRECTION_MODE_NONE);
  226. emission_direction_mode->add_item(TTRC("Generate"), DIRECTION_MODE_GENERATE);
  227. emission_direction_mode->add_item(TTRC("Texture"), DIRECTION_MODE_TEXTURE);
  228. emission_direction_mode->connect(SceneStringName(item_selected), callable_mp(this, &Particles2DEditorPlugin::_validate_textures).unbind(1));
  229. emission_direction_mode->set_item_disabled(DIRECTION_MODE_GENERATE, true);
  230. emvb->add_margin_child(TTRC("Direction Mode"), emission_direction_mode);
  231. direction_img_label = memnew(Label);
  232. direction_img_label->set_text(TTRC("Direction Texture"));
  233. direction_img_label->set_theme_type_variation("HeaderSmall");
  234. emvb->add_child(direction_img_label);
  235. direction_img_label->hide();
  236. direction_img_hbox = memnew(HBoxContainer);
  237. direction_img_path_line_edit = memnew(LineEdit);
  238. direction_img_hbox->add_child(direction_img_path_line_edit);
  239. direction_img_path_line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  240. direction_img_path_line_edit->set_editable(false);
  241. direction_img_path_line_edit->connect(SceneStringName(text_changed), callable_mp(this, &Particles2DEditorPlugin::_validate_textures).unbind(1));
  242. direction_browse_button = memnew(Button);
  243. direction_img_hbox->add_child(direction_browse_button);
  244. direction_browse_button->connect(SceneStringName(pressed), callable_mp(this, &Particles2DEditorPlugin::_browse_direction_texture_pressed));
  245. emvb->add_child(direction_img_hbox);
  246. direction_img_hbox->hide();
  247. VBoxContainer *optionsvb = memnew(VBoxContainer);
  248. emvb->add_margin_child(TTRC("Options"), optionsvb);
  249. emission_mask_centered = memnew(CheckBox(TTRC("Centered")));
  250. emission_mask_centered->set_pressed(true);
  251. optionsvb->add_child(emission_mask_centered);
  252. emission_mask_colors = memnew(CheckBox(TTRC("Copy Color from Mask Texture")));
  253. optionsvb->add_child(emission_mask_colors);
  254. error_message = memnew(Label);
  255. error_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  256. error_message->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  257. error_message->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("error_color"), EditorStringName(Editor)));
  258. emvb->add_child(error_message);
  259. EditorNode::get_singleton()->get_gui_base()->add_child(emission_mask_dialog);
  260. emission_mask_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Particles2DEditorPlugin::_generate_emission_mask));
  261. emission_mask_dialog->connect(SceneStringName(theme_changed), callable_mp(this, &Particles2DEditorPlugin::_theme_changed));
  262. }
  263. void Particles2DEditorPlugin::_set_show_gizmos(Node *p_node, bool p_show) {
  264. GPUParticles2D *gpu_particles = Object::cast_to<GPUParticles2D>(p_node);
  265. if (gpu_particles) {
  266. gpu_particles->set_show_gizmos(p_show);
  267. }
  268. CPUParticles2D *cpu_particles = Object::cast_to<CPUParticles2D>(p_node);
  269. if (cpu_particles) {
  270. cpu_particles->set_show_gizmos(p_show);
  271. }
  272. // The `selection_changed` signal is deferred. A node could be deleted before the signal is emitted.
  273. if (p_show) {
  274. p_node->connect(SceneStringName(tree_exiting), callable_mp(this, &Particles2DEditorPlugin::_node_removed).bind(p_node));
  275. } else {
  276. p_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Particles2DEditorPlugin::_node_removed));
  277. }
  278. }
  279. void Particles2DEditorPlugin::_selection_changed() {
  280. const List<Node *> &current_selection = EditorNode::get_singleton()->get_editor_selection()->get_top_selected_node_list();
  281. if (selected_particles.is_empty() && current_selection.is_empty()) {
  282. return;
  283. }
  284. // Turn gizmos on for nodes that are newly selected.
  285. HashSet<ObjectID> nodes_in_current_selection;
  286. for (Node *node : current_selection) {
  287. ObjectID nid = node->get_instance_id();
  288. nodes_in_current_selection.insert(nid);
  289. if (!selected_particles.has(nid)) {
  290. _set_show_gizmos(node, true);
  291. selected_particles.insert(nid);
  292. }
  293. }
  294. mask_img_path_line_edit->set_text("");
  295. emission_mask_mode->select(MASK_MODE_SOLID);
  296. emission_direction_mode->select(DIRECTION_MODE_NONE);
  297. emission_mask_centered->set_pressed(true);
  298. emission_mask_colors->set_pressed(false);
  299. direction_img_path_line_edit->set_text("");
  300. // Turn gizmos off for nodes that are no longer selected.
  301. LocalVector<ObjectID> to_erase;
  302. for (const ObjectID &nid : selected_particles) {
  303. if (!nodes_in_current_selection.has(nid)) {
  304. Node *node = ObjectDB::get_instance<Node>(nid);
  305. if (node) {
  306. _set_show_gizmos(node, false);
  307. }
  308. to_erase.push_back(nid);
  309. }
  310. }
  311. for (const ObjectID &nid : to_erase) {
  312. selected_particles.erase(nid);
  313. }
  314. }
  315. void Particles2DEditorPlugin::_node_removed(Node *p_node) {
  316. if (p_node && selected_particles.erase(p_node->get_instance_id())) {
  317. _set_show_gizmos(p_node, false);
  318. }
  319. }
  320. void GPUParticles2DEditorPlugin::_generate_visibility_rect() {
  321. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  322. double time = generate_seconds->get_value();
  323. float running = 0.0;
  324. EditorProgress ep("gen_vrect", TTR("Generating Visibility Rect (Waiting for Particle Simulation)"), int(time));
  325. bool was_emitting = particles->is_emitting();
  326. if (!was_emitting) {
  327. particles->set_emitting(true);
  328. OS::get_singleton()->delay_usec(1000);
  329. }
  330. Rect2 rect;
  331. while (running < time) {
  332. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  333. ep.step(TTR("Generating..."), int(running), true);
  334. OS::get_singleton()->delay_usec(1000);
  335. Rect2 capture = particles->capture_rect();
  336. if (rect == Rect2()) {
  337. rect = capture;
  338. } else {
  339. rect = rect.merge(capture);
  340. }
  341. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  342. }
  343. if (!was_emitting) {
  344. particles->set_emitting(false);
  345. }
  346. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  347. undo_redo->create_action(TTR("Generate Visibility Rect"));
  348. undo_redo->add_do_method(particles, "set_visibility_rect", rect);
  349. undo_redo->add_undo_method(particles, "set_visibility_rect", particles->get_visibility_rect());
  350. undo_redo->commit_action();
  351. }
  352. void Particles2DEditorPlugin::_notification(int p_what) {
  353. switch (p_what) {
  354. case NOTIFICATION_ENTER_TREE: {
  355. EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", callable_mp(this, &Particles2DEditorPlugin::_selection_changed));
  356. } break;
  357. }
  358. }
  359. void Particles2DEditorPlugin::_theme_changed() {
  360. mask_browse_button->set_button_icon(mask_browse_button->get_editor_theme_icon(SNAME("FileBrowse")));
  361. direction_browse_button->set_button_icon(direction_browse_button->get_editor_theme_icon(SNAME("FileBrowse")));
  362. }
  363. void Particles2DEditorPlugin::_menu_callback(int p_idx) {
  364. if (p_idx == MENU_LOAD_EMISSION_MASK) {
  365. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  366. if (particles && particles->get_process_material().is_null()) {
  367. EditorNode::get_singleton()->show_warning(TTR("Loading emission mask requires ParticleProcessMaterial."));
  368. return;
  369. }
  370. emission_mask_dialog->popup_centered();
  371. } else {
  372. ParticlesEditorPlugin::_menu_callback(p_idx);
  373. }
  374. }
  375. void Particles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
  376. p_menu->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
  377. }
  378. void Particles2DEditorPlugin::_validate_textures() {
  379. DirectionMode direction_mode = static_cast<DirectionMode>(emission_direction_mode->get_selected());
  380. direction_img_label->set_visible(direction_mode == DIRECTION_MODE_TEXTURE);
  381. direction_img_hbox->set_visible(direction_mode == DIRECTION_MODE_TEXTURE);
  382. error_message->hide();
  383. emission_mask_dialog->get_ok_button()->set_disabled(true);
  384. if (mask_img_path_line_edit->get_text().is_empty()) {
  385. emission_mask_dialog->reset_size();
  386. return;
  387. }
  388. Ref<Image> mask_img;
  389. mask_img.instantiate();
  390. Error err = ImageLoader::load_image(mask_img_path_line_edit->get_text(), mask_img);
  391. if (err != OK) {
  392. error_message->show();
  393. error_message->set_text(TTRC("Failed to load mask texture."));
  394. emission_mask_dialog->reset_size();
  395. return;
  396. }
  397. if (mask_img->is_compressed()) {
  398. mask_img->decompress();
  399. }
  400. mask_img->convert(Image::FORMAT_RGBA8);
  401. if (mask_img->get_format() != Image::FORMAT_RGBA8) {
  402. error_message->show();
  403. error_message->set_text(TTRC("Failed to convert mask texture to RGBA8."));
  404. emission_mask_dialog->reset_size();
  405. return;
  406. }
  407. Size2i mask_img_size = mask_img->get_size();
  408. if (mask_img_size.width == 0 || mask_img_size.height == 0) {
  409. error_message->show();
  410. error_message->set_text(TTRC("Mask texture has an invalid size."));
  411. emission_mask_dialog->reset_size();
  412. return;
  413. }
  414. if (direction_mode == DIRECTION_MODE_TEXTURE) {
  415. if (direction_img_path_line_edit->get_text().is_empty()) {
  416. return;
  417. }
  418. Ref<Image> direction_img;
  419. direction_img.instantiate();
  420. err = ImageLoader::load_image(direction_img_path_line_edit->get_text(), direction_img);
  421. if (err != OK) {
  422. error_message->show();
  423. error_message->set_text(TTRC("Failed to load direction texture."));
  424. emission_mask_dialog->reset_size();
  425. return;
  426. }
  427. if (direction_img->is_compressed()) {
  428. direction_img->decompress();
  429. }
  430. direction_img->convert(Image::FORMAT_RGBA8);
  431. if (direction_img->get_format() != Image::FORMAT_RGBA8) {
  432. error_message->show();
  433. error_message->set_text(TTRC("Failed to convert direction texture to RGBA8."));
  434. emission_mask_dialog->reset_size();
  435. return;
  436. }
  437. Size2i direction_img_size = direction_img->get_size();
  438. if (direction_img_size.width == 0 || direction_img_size.height == 0 || direction_img_size != mask_img_size) {
  439. error_message->show();
  440. error_message->set_text(TTRC("Direction texture has an invalid size. It must have the same size as the mask texture."));
  441. emission_mask_dialog->reset_size();
  442. return;
  443. }
  444. }
  445. emission_mask_dialog->get_ok_button()->set_disabled(false);
  446. emission_mask_dialog->reset_size();
  447. }
  448. void Particles2DEditorPlugin::_emission_mask_mode_item_changed(int p_idx) const {
  449. emission_direction_mode->set_item_disabled(DIRECTION_MODE_GENERATE, p_idx == static_cast<int>(MASK_MODE_SOLID));
  450. if (emission_direction_mode->get_selected() == DIRECTION_MODE_GENERATE) {
  451. emission_direction_mode->select(DIRECTION_MODE_NONE);
  452. }
  453. }
  454. Node *GPUParticles2DEditorPlugin::_convert_particles() {
  455. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  456. CPUParticles2D *cpu_particles = memnew(CPUParticles2D);
  457. cpu_particles->convert_from_particles(particles);
  458. cpu_particles->set_name(particles->get_name());
  459. cpu_particles->set_transform(particles->get_transform());
  460. cpu_particles->set_visible(particles->is_visible());
  461. cpu_particles->set_process_mode(particles->get_process_mode());
  462. cpu_particles->set_z_index(particles->get_z_index());
  463. return cpu_particles;
  464. }
  465. void GPUParticles2DEditorPlugin::_generate_emission_mask() {
  466. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  467. Ref<ParticleProcessMaterial> pm = particles->get_process_material();
  468. ERR_FAIL_COND(pm.is_null());
  469. PackedVector2Array emission_positions;
  470. PackedVector2Array emission_normals;
  471. PackedByteArray emission_colors;
  472. Vector2i texture_size;
  473. _process_emission_masks(emission_positions, emission_normals, emission_colors, texture_size);
  474. ERR_FAIL_COND_MSG(emission_positions.is_empty(), "No pixels with transparency > 128 in image...");
  475. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  476. undo_redo->create_action(TTR("Load Emission Mask"));
  477. ParticleProcessMaterial *pmptr = pm.ptr();
  478. Vector<uint8_t> mask_texture_data;
  479. int valid_positions_count = emission_positions.size();
  480. int w = 2048;
  481. int h = (valid_positions_count / 2048) + 1;
  482. mask_texture_data.resize_initialized(w * h * 2 * sizeof(float));
  483. {
  484. Vector2 offset;
  485. if (emission_mask_centered->is_pressed()) {
  486. offset = Vector2(-texture_size.width * 0.5, -texture_size.height * 0.5);
  487. }
  488. uint8_t *tw = mask_texture_data.ptrw();
  489. float *twf = reinterpret_cast<float *>(tw);
  490. for (int i = 0; i < valid_positions_count; i++) {
  491. twf[i * 2 + 0] = emission_positions[i].x + offset.x;
  492. twf[i * 2 + 1] = emission_positions[i].y + offset.y;
  493. }
  494. }
  495. Ref<Image> img;
  496. img.instantiate();
  497. img->set_data(w, h, false, Image::FORMAT_RGF, mask_texture_data);
  498. undo_redo->add_do_property(pmptr, "emission_point_texture", ImageTexture::create_from_image(img));
  499. undo_redo->add_undo_property(pmptr, "emission_point_texture", pm->get_emission_point_texture());
  500. undo_redo->add_do_property(pmptr, "emission_point_count", valid_positions_count);
  501. undo_redo->add_undo_property(pmptr, "emission_point_count", pm->get_emission_point_count());
  502. if (emission_mask_colors->is_pressed()) {
  503. Vector<uint8_t> color_texture_data;
  504. color_texture_data.resize_initialized(w * h * 4);
  505. {
  506. uint8_t *tw = color_texture_data.ptrw();
  507. for (int i = 0; i < valid_positions_count * 4; i++) {
  508. tw[i] = emission_colors[i];
  509. }
  510. }
  511. img.instantiate();
  512. img->set_data(w, h, false, Image::FORMAT_RGBA8, color_texture_data);
  513. undo_redo->add_do_property(pmptr, "emission_color_texture", ImageTexture::create_from_image(img));
  514. undo_redo->add_undo_property(pmptr, "emission_color_texture", pm->get_emission_color_texture());
  515. }
  516. if (emission_normals.size()) {
  517. undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  518. undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());
  519. pm->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  520. Vector<uint8_t> normal_texture_data;
  521. normal_texture_data.resize_initialized(w * h * 2 * sizeof(float));
  522. {
  523. uint8_t *tw = normal_texture_data.ptrw();
  524. float *twf = reinterpret_cast<float *>(tw);
  525. for (int i = 0; i < valid_positions_count; i++) {
  526. twf[i * 2 + 0] = emission_normals[i].x;
  527. twf[i * 2 + 1] = emission_normals[i].y;
  528. }
  529. }
  530. img.instantiate();
  531. img->set_data(w, h, false, Image::FORMAT_RGF, normal_texture_data);
  532. undo_redo->add_do_property(pmptr, "emission_normal_texture", ImageTexture::create_from_image(img));
  533. undo_redo->add_undo_property(pmptr, "emission_normal_texture", pm->get_emission_normal_texture());
  534. } else {
  535. undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
  536. undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());
  537. }
  538. undo_redo->commit_action();
  539. }
  540. GPUParticles2DEditorPlugin::GPUParticles2DEditorPlugin() {
  541. handled_type = TTRC("GPUParticles2D");
  542. conversion_option_name = TTR("Convert to CPUParticles2D");
  543. generate_visibility_rect = memnew(ConfirmationDialog);
  544. generate_visibility_rect->set_title(TTR("Generate Visibility Rect"));
  545. VBoxContainer *genvb = memnew(VBoxContainer);
  546. generate_visibility_rect->add_child(genvb);
  547. generate_seconds = memnew(SpinBox);
  548. generate_seconds->set_min(0.1);
  549. generate_seconds->set_max(25);
  550. generate_seconds->set_value(2);
  551. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  552. EditorNode::get_singleton()->get_gui_base()->add_child(generate_visibility_rect);
  553. generate_visibility_rect->connect(SceneStringName(confirmed), callable_mp(this, &GPUParticles2DEditorPlugin::_generate_visibility_rect));
  554. }
  555. Node *CPUParticles2DEditorPlugin::_convert_particles() {
  556. CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);
  557. GPUParticles2D *gpu_particles = memnew(GPUParticles2D);
  558. gpu_particles->convert_from_particles(particles);
  559. gpu_particles->set_name(particles->get_name());
  560. gpu_particles->set_transform(particles->get_transform());
  561. gpu_particles->set_visible(particles->is_visible());
  562. gpu_particles->set_process_mode(particles->get_process_mode());
  563. return gpu_particles;
  564. }
  565. void CPUParticles2DEditorPlugin::_generate_emission_mask() {
  566. CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);
  567. PackedVector2Array valid_positions;
  568. PackedVector2Array valid_normals;
  569. PackedByteArray valid_colors;
  570. Vector2i image_size;
  571. _process_emission_masks(valid_positions, valid_normals, valid_colors, image_size);
  572. ERR_FAIL_COND_MSG(valid_positions.is_empty(), "No pixels with transparency > 128 in image...");
  573. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  574. undo_redo->create_action(TTR("Load Emission Mask"));
  575. int valid_point_count = valid_positions.size();
  576. if (emission_mask_colors->is_pressed()) {
  577. PackedColorArray pca;
  578. pca.resize(valid_point_count);
  579. Color *pcaw = pca.ptrw();
  580. for (int i = 0; i < valid_point_count; i += 1) {
  581. Color color;
  582. color.r = valid_colors[i * 4 + 0] / 255.0f;
  583. color.g = valid_colors[i * 4 + 1] / 255.0f;
  584. color.b = valid_colors[i * 4 + 2] / 255.0f;
  585. color.a = valid_colors[i * 4 + 3] / 255.0f;
  586. pcaw[i] = color;
  587. }
  588. undo_redo->add_do_property(particles, "emission_colors", pca);
  589. undo_redo->add_undo_property(particles, "emission_colors", particles->get_emission_colors());
  590. }
  591. if (!valid_normals.is_empty()) {
  592. undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_DIRECTED_POINTS);
  593. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
  594. PackedVector2Array norms;
  595. norms.resize(valid_normals.size());
  596. Vector2 *normsw = norms.ptrw();
  597. for (int i = 0; i < valid_normals.size(); i += 1) {
  598. normsw[i] = valid_normals[i];
  599. }
  600. undo_redo->add_do_property(particles, "emission_normals", norms);
  601. undo_redo->add_undo_property(particles, "emission_normals", particles->get_emission_normals());
  602. } else {
  603. undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_POINTS);
  604. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
  605. }
  606. {
  607. Vector2 offset;
  608. if (emission_mask_centered->is_pressed()) {
  609. offset = Vector2(-image_size.width * 0.5, -image_size.height * 0.5);
  610. }
  611. PackedVector2Array points;
  612. points.resize(valid_positions.size());
  613. Vector2 *pointsw = points.ptrw();
  614. for (int i = 0; i < valid_positions.size(); i += 1) {
  615. pointsw[i] = valid_positions[i] + offset;
  616. }
  617. undo_redo->add_do_property(particles, "emission_points", points);
  618. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_points());
  619. }
  620. undo_redo->commit_action();
  621. }
  622. CPUParticles2DEditorPlugin::CPUParticles2DEditorPlugin() {
  623. handled_type = TTRC("CPUParticles2D");
  624. conversion_option_name = TTR("Convert to GPUParticles2D");
  625. }