gpu_particles_2d_editor_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*************************************************************************/
  2. /* gpu_particles_2d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "gpu_particles_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/io/image_loader.h"
  33. #include "editor/editor_file_dialog.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/scene_tree_dock.h"
  37. #include "scene/2d/cpu_particles_2d.h"
  38. #include "scene/gui/separator.h"
  39. #include "scene/resources/particle_process_material.h"
  40. void GPUParticles2DEditorPlugin::edit(Object *p_object) {
  41. particles = Object::cast_to<GPUParticles2D>(p_object);
  42. }
  43. bool GPUParticles2DEditorPlugin::handles(Object *p_object) const {
  44. return p_object->is_class("GPUParticles2D");
  45. }
  46. void GPUParticles2DEditorPlugin::make_visible(bool p_visible) {
  47. if (p_visible) {
  48. toolbar->show();
  49. } else {
  50. toolbar->hide();
  51. }
  52. }
  53. void GPUParticles2DEditorPlugin::_file_selected(const String &p_file) {
  54. source_emission_file = p_file;
  55. emission_mask->popup_centered();
  56. }
  57. void GPUParticles2DEditorPlugin::_selection_changed() {
  58. List<Node *> selected_nodes = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list();
  59. if (selected_particles.is_empty() && selected_nodes.is_empty()) {
  60. return;
  61. }
  62. for (GPUParticles2D *SP : selected_particles) {
  63. SP->set_show_visibility_rect(false);
  64. }
  65. selected_particles.clear();
  66. for (Node *P : selected_nodes) {
  67. GPUParticles2D *selected_particle = Object::cast_to<GPUParticles2D>(P);
  68. if (selected_particle != nullptr) {
  69. selected_particle->set_show_visibility_rect(true);
  70. selected_particles.push_back(selected_particle);
  71. }
  72. }
  73. }
  74. void GPUParticles2DEditorPlugin::_menu_callback(int p_idx) {
  75. switch (p_idx) {
  76. case MENU_GENERATE_VISIBILITY_RECT: {
  77. // Add one second to the default generation lifetime, since the progress is updated every second.
  78. generate_seconds->set_value(MAX(1.0, trunc(particles->get_lifetime()) + 1.0));
  79. if (generate_seconds->get_value() >= 11.0 + CMP_EPSILON) {
  80. // Only pop up the time dialog if the particle's lifetime is long enough to warrant shortening it.
  81. generate_visibility_rect->popup_centered();
  82. } else {
  83. // Generate the visibility rect immediately.
  84. _generate_visibility_rect();
  85. }
  86. } break;
  87. case MENU_LOAD_EMISSION_MASK: {
  88. file->popup_file_dialog();
  89. } break;
  90. case MENU_CLEAR_EMISSION_MASK: {
  91. emission_mask->popup_centered();
  92. } break;
  93. case MENU_OPTION_CONVERT_TO_CPU_PARTICLES: {
  94. CPUParticles2D *cpu_particles = memnew(CPUParticles2D);
  95. cpu_particles->convert_from_particles(particles);
  96. cpu_particles->set_name(particles->get_name());
  97. cpu_particles->set_transform(particles->get_transform());
  98. cpu_particles->set_visible(particles->is_visible());
  99. cpu_particles->set_process_mode(particles->get_process_mode());
  100. cpu_particles->set_z_index(particles->get_z_index());
  101. Ref<EditorUndoRedoManager> &ur = EditorNode::get_singleton()->get_undo_redo();
  102. ur->create_action(TTR("Convert to CPUParticles2D"));
  103. ur->add_do_method(SceneTreeDock::get_singleton(), "replace_node", particles, cpu_particles, true, false);
  104. ur->add_do_reference(cpu_particles);
  105. ur->add_undo_method(SceneTreeDock::get_singleton(), "replace_node", cpu_particles, particles, false, false);
  106. ur->add_undo_reference(particles);
  107. ur->commit_action();
  108. } break;
  109. case MENU_RESTART: {
  110. particles->restart();
  111. }
  112. }
  113. }
  114. void GPUParticles2DEditorPlugin::_generate_visibility_rect() {
  115. double time = generate_seconds->get_value();
  116. float running = 0.0;
  117. EditorProgress ep("gen_vrect", TTR("Generating Visibility Rect (Waiting for Particle Simulation)"), int(time));
  118. bool was_emitting = particles->is_emitting();
  119. if (!was_emitting) {
  120. particles->set_emitting(true);
  121. OS::get_singleton()->delay_usec(1000);
  122. }
  123. Rect2 rect;
  124. while (running < time) {
  125. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  126. ep.step("Generating...", int(running), true);
  127. OS::get_singleton()->delay_usec(1000);
  128. Rect2 capture = particles->capture_rect();
  129. if (rect == Rect2()) {
  130. rect = capture;
  131. } else {
  132. rect = rect.merge(capture);
  133. }
  134. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  135. }
  136. if (!was_emitting) {
  137. particles->set_emitting(false);
  138. }
  139. undo_redo->create_action(TTR("Generate Visibility Rect"));
  140. undo_redo->add_do_method(particles, "set_visibility_rect", rect);
  141. undo_redo->add_undo_method(particles, "set_visibility_rect", particles->get_visibility_rect());
  142. undo_redo->commit_action();
  143. }
  144. void GPUParticles2DEditorPlugin::_generate_emission_mask() {
  145. Ref<ParticleProcessMaterial> pm = particles->get_process_material();
  146. if (!pm.is_valid()) {
  147. EditorNode::get_singleton()->show_warning(TTR("Can only set point into a ParticleProcessMaterial process material"));
  148. return;
  149. }
  150. Ref<Image> img;
  151. img.instantiate();
  152. Error err = ImageLoader::load_image(source_emission_file, img);
  153. ERR_FAIL_COND_MSG(err != OK, "Error loading image '" + source_emission_file + "'.");
  154. if (img->is_compressed()) {
  155. img->decompress();
  156. }
  157. img->convert(Image::FORMAT_RGBA8);
  158. ERR_FAIL_COND(img->get_format() != Image::FORMAT_RGBA8);
  159. Size2i s = img->get_size();
  160. ERR_FAIL_COND(s.width == 0 || s.height == 0);
  161. Vector<Point2> valid_positions;
  162. Vector<Point2> valid_normals;
  163. Vector<uint8_t> valid_colors;
  164. valid_positions.resize(s.width * s.height);
  165. EmissionMode emode = (EmissionMode)emission_mask_mode->get_selected();
  166. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  167. valid_normals.resize(s.width * s.height);
  168. }
  169. bool capture_colors = emission_colors->is_pressed();
  170. if (capture_colors) {
  171. valid_colors.resize(s.width * s.height * 4);
  172. }
  173. int vpc = 0;
  174. {
  175. Vector<uint8_t> img_data = img->get_data();
  176. const uint8_t *r = img_data.ptr();
  177. for (int i = 0; i < s.width; i++) {
  178. for (int j = 0; j < s.height; j++) {
  179. uint8_t a = r[(j * s.width + i) * 4 + 3];
  180. if (a > 128) {
  181. if (emode == EMISSION_MODE_SOLID) {
  182. if (capture_colors) {
  183. valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  184. valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  185. valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  186. valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  187. }
  188. valid_positions.write[vpc++] = Point2(i, j);
  189. } else {
  190. bool on_border = false;
  191. for (int x = i - 1; x <= i + 1; x++) {
  192. for (int y = j - 1; y <= j + 1; y++) {
  193. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  194. on_border = true;
  195. break;
  196. }
  197. }
  198. if (on_border) {
  199. break;
  200. }
  201. }
  202. if (on_border) {
  203. valid_positions.write[vpc] = Point2(i, j);
  204. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  205. Vector2 normal;
  206. for (int x = i - 2; x <= i + 2; x++) {
  207. for (int y = j - 2; y <= j + 2; y++) {
  208. if (x == i && y == j) {
  209. continue;
  210. }
  211. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  212. normal += Vector2(x - i, y - j).normalized();
  213. }
  214. }
  215. }
  216. normal.normalize();
  217. valid_normals.write[vpc] = normal;
  218. }
  219. if (capture_colors) {
  220. valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  221. valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  222. valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  223. valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  224. }
  225. vpc++;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. valid_positions.resize(vpc);
  233. if (valid_normals.size()) {
  234. valid_normals.resize(vpc);
  235. }
  236. ERR_FAIL_COND_MSG(valid_positions.size() == 0, "No pixels with transparency > 128 in image...");
  237. Vector<uint8_t> texdata;
  238. int w = 2048;
  239. int h = (vpc / 2048) + 1;
  240. texdata.resize(w * h * 2 * sizeof(float));
  241. {
  242. uint8_t *tw = texdata.ptrw();
  243. float *twf = reinterpret_cast<float *>(tw);
  244. for (int i = 0; i < vpc; i++) {
  245. twf[i * 2 + 0] = valid_positions[i].x;
  246. twf[i * 2 + 1] = valid_positions[i].y;
  247. }
  248. }
  249. img.instantiate();
  250. img->create(w, h, false, Image::FORMAT_RGF, texdata);
  251. pm->set_emission_point_texture(ImageTexture::create_from_image(img));
  252. pm->set_emission_point_count(vpc);
  253. if (capture_colors) {
  254. Vector<uint8_t> colordata;
  255. colordata.resize(w * h * 4); //use RG texture
  256. {
  257. uint8_t *tw = colordata.ptrw();
  258. for (int i = 0; i < vpc * 4; i++) {
  259. tw[i] = valid_colors[i];
  260. }
  261. }
  262. img.instantiate();
  263. img->create(w, h, false, Image::FORMAT_RGBA8, colordata);
  264. pm->set_emission_color_texture(ImageTexture::create_from_image(img));
  265. }
  266. if (valid_normals.size()) {
  267. pm->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  268. Vector<uint8_t> normdata;
  269. normdata.resize(w * h * 2 * sizeof(float)); //use RG texture
  270. {
  271. uint8_t *tw = normdata.ptrw();
  272. float *twf = reinterpret_cast<float *>(tw);
  273. for (int i = 0; i < vpc; i++) {
  274. twf[i * 2 + 0] = valid_normals[i].x;
  275. twf[i * 2 + 1] = valid_normals[i].y;
  276. }
  277. }
  278. img.instantiate();
  279. img->create(w, h, false, Image::FORMAT_RGF, normdata);
  280. pm->set_emission_normal_texture(ImageTexture::create_from_image(img));
  281. } else {
  282. pm->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
  283. }
  284. }
  285. void GPUParticles2DEditorPlugin::_notification(int p_what) {
  286. switch (p_what) {
  287. case NOTIFICATION_ENTER_TREE: {
  288. menu->get_popup()->connect("id_pressed", callable_mp(this, &GPUParticles2DEditorPlugin::_menu_callback));
  289. menu->set_icon(menu->get_theme_icon(SNAME("GPUParticles2D"), SNAME("EditorIcons")));
  290. file->connect("file_selected", callable_mp(this, &GPUParticles2DEditorPlugin::_file_selected));
  291. EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", callable_mp(this, &GPUParticles2DEditorPlugin::_selection_changed));
  292. } break;
  293. }
  294. }
  295. void GPUParticles2DEditorPlugin::_bind_methods() {
  296. }
  297. GPUParticles2DEditorPlugin::GPUParticles2DEditorPlugin() {
  298. particles = nullptr;
  299. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  300. toolbar = memnew(HBoxContainer);
  301. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  302. toolbar->hide();
  303. toolbar->add_child(memnew(VSeparator));
  304. menu = memnew(MenuButton);
  305. menu->get_popup()->add_item(TTR("Restart"), MENU_RESTART);
  306. menu->get_popup()->add_item(TTR("Generate Visibility Rect"), MENU_GENERATE_VISIBILITY_RECT);
  307. menu->get_popup()->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
  308. // menu->get_popup()->add_item(TTR("Clear Emission Mask"), MENU_CLEAR_EMISSION_MASK);
  309. menu->get_popup()->add_item(TTR("Convert to CPUParticles2D"), MENU_OPTION_CONVERT_TO_CPU_PARTICLES);
  310. menu->set_text(TTR("GPUParticles2D"));
  311. menu->set_switch_on_hover(true);
  312. toolbar->add_child(menu);
  313. file = memnew(EditorFileDialog);
  314. List<String> ext;
  315. ImageLoader::get_recognized_extensions(&ext);
  316. for (const String &E : ext) {
  317. file->add_filter("*." + E, E.to_upper());
  318. }
  319. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  320. toolbar->add_child(file);
  321. epoints = memnew(SpinBox);
  322. epoints->set_min(1);
  323. epoints->set_max(8192);
  324. epoints->set_step(1);
  325. epoints->set_value(512);
  326. file->get_vbox()->add_margin_child(TTR("Generated Point Count:"), epoints);
  327. generate_visibility_rect = memnew(ConfirmationDialog);
  328. generate_visibility_rect->set_title(TTR("Generate Visibility Rect"));
  329. VBoxContainer *genvb = memnew(VBoxContainer);
  330. generate_visibility_rect->add_child(genvb);
  331. generate_seconds = memnew(SpinBox);
  332. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  333. generate_seconds->set_min(0.1);
  334. generate_seconds->set_max(25);
  335. generate_seconds->set_value(2);
  336. toolbar->add_child(generate_visibility_rect);
  337. generate_visibility_rect->connect("confirmed", callable_mp(this, &GPUParticles2DEditorPlugin::_generate_visibility_rect));
  338. emission_mask = memnew(ConfirmationDialog);
  339. emission_mask->set_title(TTR("Load Emission Mask"));
  340. VBoxContainer *emvb = memnew(VBoxContainer);
  341. emission_mask->add_child(emvb);
  342. emission_mask_mode = memnew(OptionButton);
  343. emvb->add_margin_child(TTR("Emission Mask"), emission_mask_mode);
  344. emission_mask_mode->add_item(TTR("Solid Pixels"), EMISSION_MODE_SOLID);
  345. emission_mask_mode->add_item(TTR("Border Pixels"), EMISSION_MODE_BORDER);
  346. emission_mask_mode->add_item(TTR("Directed Border Pixels"), EMISSION_MODE_BORDER_DIRECTED);
  347. emission_colors = memnew(CheckBox);
  348. emission_colors->set_text(TTR("Capture from Pixel"));
  349. emvb->add_margin_child(TTR("Emission Colors"), emission_colors);
  350. toolbar->add_child(emission_mask);
  351. emission_mask->connect("confirmed", callable_mp(this, &GPUParticles2DEditorPlugin::_generate_emission_mask));
  352. }
  353. GPUParticles2DEditorPlugin::~GPUParticles2DEditorPlugin() {
  354. }