particles_2d_editor_plugin.cpp 13 KB

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