cpu_particles_2d_editor_plugin.cpp 9.7 KB

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