particles_2d_editor_plugin.cpp 14 KB

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