particles_2d_editor_plugin.cpp 12 KB

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