cpu_particles_2d_editor_plugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/gui/editor_file_dialog.h"
  37. #include "editor/scene_tree_dock.h"
  38. #include "scene/2d/cpu_particles_2d.h"
  39. #include "scene/2d/gpu_particles_2d.h"
  40. #include "scene/gui/check_box.h"
  41. #include "scene/gui/menu_button.h"
  42. #include "scene/gui/option_button.h"
  43. #include "scene/gui/separator.h"
  44. #include "scene/gui/spin_box.h"
  45. #include "scene/resources/particle_process_material.h"
  46. void CPUParticles2DEditorPlugin::edit(Object *p_object) {
  47. particles = Object::cast_to<CPUParticles2D>(p_object);
  48. }
  49. bool CPUParticles2DEditorPlugin::handles(Object *p_object) const {
  50. return p_object->is_class("CPUParticles2D");
  51. }
  52. void CPUParticles2DEditorPlugin::make_visible(bool p_visible) {
  53. if (p_visible) {
  54. toolbar->show();
  55. } else {
  56. toolbar->hide();
  57. }
  58. }
  59. void CPUParticles2DEditorPlugin::_file_selected(const String &p_file) {
  60. source_emission_file = p_file;
  61. emission_mask->popup_centered();
  62. }
  63. void CPUParticles2DEditorPlugin::_menu_callback(int p_idx) {
  64. switch (p_idx) {
  65. case MENU_LOAD_EMISSION_MASK: {
  66. file->popup_file_dialog();
  67. } break;
  68. case MENU_CLEAR_EMISSION_MASK: {
  69. emission_mask->popup_centered();
  70. } break;
  71. case MENU_RESTART: {
  72. particles->restart();
  73. } break;
  74. case MENU_CONVERT_TO_GPU_PARTICLES: {
  75. GPUParticles2D *gpu_particles = memnew(GPUParticles2D);
  76. gpu_particles->convert_from_particles(particles);
  77. gpu_particles->set_name(particles->get_name());
  78. gpu_particles->set_transform(particles->get_transform());
  79. gpu_particles->set_visible(particles->is_visible());
  80. gpu_particles->set_process_mode(particles->get_process_mode());
  81. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  82. ur->create_action(TTR("Convert to GPUParticles3D"), UndoRedo::MERGE_DISABLE, particles);
  83. SceneTreeDock::get_singleton()->replace_node(particles, gpu_particles);
  84. ur->commit_action(false);
  85. } break;
  86. }
  87. }
  88. void CPUParticles2DEditorPlugin::_generate_emission_mask() {
  89. Ref<Image> img;
  90. img.instantiate();
  91. Error err = ImageLoader::load_image(source_emission_file, img);
  92. ERR_FAIL_COND_MSG(err != OK, "Error loading image '" + source_emission_file + "'.");
  93. if (img->is_compressed()) {
  94. img->decompress();
  95. }
  96. img->convert(Image::FORMAT_RGBA8);
  97. ERR_FAIL_COND(img->get_format() != Image::FORMAT_RGBA8);
  98. Size2i s = img->get_size();
  99. ERR_FAIL_COND(s.width == 0 || s.height == 0);
  100. Vector<Point2> valid_positions;
  101. Vector<Point2> valid_normals;
  102. Vector<uint8_t> valid_colors;
  103. valid_positions.resize(s.width * s.height);
  104. EmissionMode emode = (EmissionMode)emission_mask_mode->get_selected();
  105. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  106. valid_normals.resize(s.width * s.height);
  107. }
  108. bool capture_colors = emission_colors->is_pressed();
  109. if (capture_colors) {
  110. valid_colors.resize(s.width * s.height * 4);
  111. }
  112. int vpc = 0;
  113. {
  114. Vector<uint8_t> img_data = img->get_data();
  115. const uint8_t *r = img_data.ptr();
  116. for (int i = 0; i < s.width; i++) {
  117. for (int j = 0; j < s.height; j++) {
  118. uint8_t a = r[(j * s.width + i) * 4 + 3];
  119. if (a > 128) {
  120. if (emode == EMISSION_MODE_SOLID) {
  121. if (capture_colors) {
  122. valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  123. valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  124. valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  125. valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  126. }
  127. valid_positions.write[vpc++] = Point2(i, j);
  128. } else {
  129. bool on_border = false;
  130. for (int x = i - 1; x <= i + 1; x++) {
  131. for (int y = j - 1; y <= j + 1; y++) {
  132. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  133. on_border = true;
  134. break;
  135. }
  136. }
  137. if (on_border) {
  138. break;
  139. }
  140. }
  141. if (on_border) {
  142. valid_positions.write[vpc] = Point2(i, j);
  143. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  144. Vector2 normal;
  145. for (int x = i - 2; x <= i + 2; x++) {
  146. for (int y = j - 2; y <= j + 2; y++) {
  147. if (x == i && y == j) {
  148. continue;
  149. }
  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.write[vpc] = normal;
  157. }
  158. if (capture_colors) {
  159. valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  160. valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  161. valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  162. valid_colors.write[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_FAIL_COND_MSG(valid_positions.is_empty(), "No pixels with transparency > 128 in image...");
  176. if (capture_colors) {
  177. PackedColorArray pca;
  178. pca.resize(vpc);
  179. Color *pcaw = pca.ptrw();
  180. for (int i = 0; i < vpc; i += 1) {
  181. Color color;
  182. color.r = valid_colors[i * 4 + 0] / 255.0f;
  183. color.g = valid_colors[i * 4 + 1] / 255.0f;
  184. color.b = valid_colors[i * 4 + 2] / 255.0f;
  185. color.a = valid_colors[i * 4 + 3] / 255.0f;
  186. pcaw[i] = color;
  187. }
  188. particles->set_emission_colors(pca);
  189. }
  190. if (valid_normals.size()) {
  191. particles->set_emission_shape(CPUParticles2D::EMISSION_SHAPE_DIRECTED_POINTS);
  192. PackedVector2Array norms;
  193. norms.resize(valid_normals.size());
  194. Vector2 *normsw = norms.ptrw();
  195. for (int i = 0; i < valid_normals.size(); i += 1) {
  196. normsw[i] = valid_normals[i];
  197. }
  198. particles->set_emission_normals(norms);
  199. } else {
  200. particles->set_emission_shape(CPUParticles2D::EMISSION_SHAPE_POINTS);
  201. }
  202. {
  203. Vector2 offset;
  204. if (emission_mask_centered->is_pressed()) {
  205. offset = Vector2(-s.width * 0.5, -s.height * 0.5);
  206. }
  207. PackedVector2Array points;
  208. points.resize(valid_positions.size());
  209. Vector2 *pointsw = points.ptrw();
  210. for (int i = 0; i < valid_positions.size(); i += 1) {
  211. pointsw[i] = valid_positions[i] + offset;
  212. }
  213. particles->set_emission_points(points);
  214. }
  215. }
  216. void CPUParticles2DEditorPlugin::_notification(int p_what) {
  217. switch (p_what) {
  218. case NOTIFICATION_ENTER_TREE: {
  219. menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &CPUParticles2DEditorPlugin::_menu_callback));
  220. menu->set_icon(file->get_editor_theme_icon(SNAME("CPUParticles2D")));
  221. file->connect("file_selected", callable_mp(this, &CPUParticles2DEditorPlugin::_file_selected));
  222. } break;
  223. }
  224. }
  225. CPUParticles2DEditorPlugin::CPUParticles2DEditorPlugin() {
  226. particles = nullptr;
  227. toolbar = memnew(HBoxContainer);
  228. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  229. toolbar->hide();
  230. menu = memnew(MenuButton);
  231. menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("particles/restart_emission"), MENU_RESTART);
  232. menu->get_popup()->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
  233. menu->get_popup()->add_item(TTR("Convert to GPUParticles2D"), MENU_CONVERT_TO_GPU_PARTICLES);
  234. menu->set_text(TTR("CPUParticles2D"));
  235. menu->set_switch_on_hover(true);
  236. toolbar->add_child(menu);
  237. file = memnew(EditorFileDialog);
  238. List<String> ext;
  239. ImageLoader::get_recognized_extensions(&ext);
  240. for (const String &E : ext) {
  241. file->add_filter("*." + E, E.to_upper());
  242. }
  243. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  244. toolbar->add_child(file);
  245. emission_mask = memnew(ConfirmationDialog);
  246. emission_mask->set_title(TTR("Load Emission Mask"));
  247. VBoxContainer *emvb = memnew(VBoxContainer);
  248. emission_mask->add_child(emvb);
  249. emission_mask_mode = memnew(OptionButton);
  250. emvb->add_margin_child(TTR("Emission Mask"), emission_mask_mode);
  251. emission_mask_mode->add_item(TTR("Solid Pixels"), EMISSION_MODE_SOLID);
  252. emission_mask_mode->add_item(TTR("Border Pixels"), EMISSION_MODE_BORDER);
  253. emission_mask_mode->add_item(TTR("Directed Border Pixels"), EMISSION_MODE_BORDER_DIRECTED);
  254. VBoxContainer *optionsvb = memnew(VBoxContainer);
  255. emvb->add_margin_child(TTR("Options"), optionsvb);
  256. emission_mask_centered = memnew(CheckBox);
  257. emission_mask_centered->set_text(TTR("Centered"));
  258. optionsvb->add_child(emission_mask_centered);
  259. emission_colors = memnew(CheckBox);
  260. emission_colors->set_text(TTR("Capture Colors from Pixel"));
  261. optionsvb->add_child(emission_colors);
  262. toolbar->add_child(emission_mask);
  263. emission_mask->connect(SceneStringName(confirmed), callable_mp(this, &CPUParticles2DEditorPlugin::_generate_emission_mask));
  264. }
  265. CPUParticles2DEditorPlugin::~CPUParticles2DEditorPlugin() {
  266. }