cpu_particles_2d_editor_plugin.cpp 11 KB

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