particles_2d_editor_plugin.cpp 13 KB

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