cpu_particles_2d_editor_plugin.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**************************************************************************/
  2. /* cpu_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 "cpu_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 CPUParticles2DEditorPlugin::edit(Object *p_object) {
  37. particles = Object::cast_to<CPUParticles2D>(p_object);
  38. }
  39. bool CPUParticles2DEditorPlugin::handles(Object *p_object) const {
  40. return p_object->is_class("CPUParticles2D");
  41. }
  42. void CPUParticles2DEditorPlugin::make_visible(bool p_visible) {
  43. if (p_visible) {
  44. toolbar->show();
  45. } else {
  46. toolbar->hide();
  47. }
  48. }
  49. void CPUParticles2DEditorPlugin::_file_selected(const String &p_file) {
  50. source_emission_file = p_file;
  51. emission_mask->popup_centered_minsize();
  52. }
  53. void CPUParticles2DEditorPlugin::_menu_callback(int p_idx) {
  54. switch (p_idx) {
  55. case MENU_LOAD_EMISSION_MASK: {
  56. file->popup_centered_ratio();
  57. } break;
  58. case MENU_CLEAR_EMISSION_MASK: {
  59. emission_mask->popup_centered_minsize();
  60. } break;
  61. case MENU_RESTART: {
  62. particles->restart();
  63. }
  64. }
  65. }
  66. void CPUParticles2DEditorPlugin::_generate_emission_mask() {
  67. Ref<Image> img;
  68. img.instance();
  69. Error err = ImageLoader::load_image(source_emission_file, img);
  70. ERR_FAIL_COND_MSG(err != OK, "Error loading image '" + source_emission_file + "'.");
  71. if (img->is_compressed()) {
  72. img->decompress();
  73. }
  74. img->convert(Image::FORMAT_RGBA8);
  75. ERR_FAIL_COND(img->get_format() != Image::FORMAT_RGBA8);
  76. Size2i s = Size2(img->get_width(), img->get_height());
  77. ERR_FAIL_COND(s.width == 0 || s.height == 0);
  78. Vector<Point2> valid_positions;
  79. Vector<Point2> valid_normals;
  80. Vector<uint8_t> valid_colors;
  81. valid_positions.resize(s.width * s.height);
  82. EmissionMode emode = (EmissionMode)emission_mask_mode->get_selected();
  83. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  84. valid_normals.resize(s.width * s.height);
  85. }
  86. bool capture_colors = emission_colors->is_pressed();
  87. if (capture_colors) {
  88. valid_colors.resize(s.width * s.height * 4);
  89. }
  90. int vpc = 0;
  91. {
  92. PoolVector<uint8_t> data = img->get_data();
  93. PoolVector<uint8_t>::Read r = data.read();
  94. for (int i = 0; i < s.width; i++) {
  95. for (int j = 0; j < s.height; j++) {
  96. uint8_t a = r[(j * s.width + i) * 4 + 3];
  97. if (a > 128) {
  98. if (emode == EMISSION_MODE_SOLID) {
  99. if (capture_colors) {
  100. valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  101. valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  102. valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  103. valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  104. }
  105. valid_positions.write[vpc++] = Point2(i, j);
  106. } else {
  107. bool on_border = false;
  108. for (int x = i - 1; x <= i + 1; x++) {
  109. for (int y = j - 1; y <= j + 1; y++) {
  110. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  111. on_border = true;
  112. break;
  113. }
  114. }
  115. if (on_border) {
  116. break;
  117. }
  118. }
  119. if (on_border) {
  120. valid_positions.write[vpc] = Point2(i, j);
  121. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  122. Vector2 normal;
  123. for (int x = i - 2; x <= i + 2; x++) {
  124. for (int y = j - 2; y <= j + 2; y++) {
  125. if (x == i && y == j) {
  126. continue;
  127. }
  128. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  129. normal += Vector2(x - i, y - j).normalized();
  130. }
  131. }
  132. }
  133. normal.normalize();
  134. valid_normals.write[vpc] = normal;
  135. }
  136. if (capture_colors) {
  137. valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  138. valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  139. valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  140. valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  141. }
  142. vpc++;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. valid_positions.resize(vpc);
  150. if (valid_normals.size()) {
  151. valid_normals.resize(vpc);
  152. }
  153. ERR_FAIL_COND_MSG(valid_positions.size() == 0, "No pixels with transparency > 128 in image...");
  154. if (capture_colors) {
  155. PoolColorArray pca;
  156. pca.resize(vpc);
  157. PoolColorArray::Write pcaw = pca.write();
  158. for (int i = 0; i < vpc; i += 1) {
  159. Color color;
  160. color.r = valid_colors[i * 4 + 0] / 255.0f;
  161. color.g = valid_colors[i * 4 + 1] / 255.0f;
  162. color.b = valid_colors[i * 4 + 2] / 255.0f;
  163. color.a = valid_colors[i * 4 + 3] / 255.0f;
  164. pcaw[i] = color;
  165. }
  166. particles->set_emission_colors(pca);
  167. }
  168. if (valid_normals.size()) {
  169. particles->set_emission_shape(CPUParticles2D::EMISSION_SHAPE_DIRECTED_POINTS);
  170. PoolVector2Array norms;
  171. norms.resize(valid_normals.size());
  172. PoolVector2Array::Write normsw = norms.write();
  173. for (int i = 0; i < valid_normals.size(); i += 1) {
  174. normsw[i] = valid_normals[i];
  175. }
  176. particles->set_emission_normals(norms);
  177. } else {
  178. particles->set_emission_shape(CPUParticles2D::EMISSION_SHAPE_POINTS);
  179. }
  180. {
  181. PoolVector2Array points;
  182. points.resize(valid_positions.size());
  183. PoolVector2Array::Write pointsw = points.write();
  184. for (int i = 0; i < valid_positions.size(); i += 1) {
  185. pointsw[i] = valid_positions[i];
  186. }
  187. particles->set_emission_points(points);
  188. }
  189. }
  190. void CPUParticles2DEditorPlugin::_notification(int p_what) {
  191. if (p_what == NOTIFICATION_ENTER_TREE) {
  192. menu->get_popup()->connect("id_pressed", this, "_menu_callback");
  193. menu->set_icon(menu->get_popup()->get_icon("Particles2D", "EditorIcons"));
  194. file->connect("file_selected", this, "_file_selected");
  195. }
  196. }
  197. void CPUParticles2DEditorPlugin::_bind_methods() {
  198. ClassDB::bind_method(D_METHOD("_menu_callback"), &CPUParticles2DEditorPlugin::_menu_callback);
  199. ClassDB::bind_method(D_METHOD("_file_selected"), &CPUParticles2DEditorPlugin::_file_selected);
  200. ClassDB::bind_method(D_METHOD("_generate_emission_mask"), &CPUParticles2DEditorPlugin::_generate_emission_mask);
  201. }
  202. CPUParticles2DEditorPlugin::CPUParticles2DEditorPlugin(EditorNode *p_node) {
  203. particles = nullptr;
  204. editor = p_node;
  205. undo_redo = editor->get_undo_redo();
  206. toolbar = memnew(HBoxContainer);
  207. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  208. toolbar->hide();
  209. toolbar->add_child(memnew(VSeparator));
  210. menu = memnew(MenuButton);
  211. menu->get_popup()->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
  212. menu->get_popup()->add_separator();
  213. menu->get_popup()->add_item(TTR("Restart"), MENU_RESTART);
  214. // menu->get_popup()->add_item(TTR("Clear Emission Mask"), MENU_CLEAR_EMISSION_MASK);
  215. menu->set_text(TTR("Particles"));
  216. menu->set_switch_on_hover(true);
  217. toolbar->add_child(menu);
  218. file = memnew(EditorFileDialog);
  219. List<String> ext;
  220. ImageLoader::get_recognized_extensions(&ext);
  221. for (List<String>::Element *E = ext.front(); E; E = E->next()) {
  222. file->add_filter("*." + E->get() + "; " + E->get().to_upper());
  223. }
  224. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  225. toolbar->add_child(file);
  226. epoints = memnew(SpinBox);
  227. epoints->set_min(1);
  228. epoints->set_max(8192);
  229. epoints->set_step(1);
  230. epoints->set_value(512);
  231. file->get_vbox()->add_margin_child(TTR("Generated Point Count:"), epoints);
  232. emission_mask = memnew(ConfirmationDialog);
  233. emission_mask->set_title(TTR("Load Emission Mask"));
  234. VBoxContainer *emvb = memnew(VBoxContainer);
  235. emission_mask->add_child(emvb);
  236. emission_mask_mode = memnew(OptionButton);
  237. emvb->add_margin_child(TTR("Emission Mask"), emission_mask_mode);
  238. emission_mask_mode->add_item(TTR("Solid Pixels"), EMISSION_MODE_SOLID);
  239. emission_mask_mode->add_item(TTR("Border Pixels"), EMISSION_MODE_BORDER);
  240. emission_mask_mode->add_item(TTR("Directed Border Pixels"), EMISSION_MODE_BORDER_DIRECTED);
  241. emission_colors = memnew(CheckBox);
  242. emission_colors->set_text(TTR("Capture from Pixel"));
  243. emvb->add_margin_child(TTR("Emission Colors"), emission_colors);
  244. toolbar->add_child(emission_mask);
  245. emission_mask->connect("confirmed", this, "_generate_emission_mask");
  246. }
  247. CPUParticles2DEditorPlugin::~CPUParticles2DEditorPlugin() {
  248. }