gpu_particles_2d_editor_plugin.cpp 15 KB

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