gpu_particles_2d_editor_plugin.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*************************************************************************/
  2. /* gpu_particles_2d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "gpu_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 GPUParticles2DEditorPlugin::edit(Object *p_object) {
  37. particles = Object::cast_to<GPUParticles2D>(p_object);
  38. }
  39. bool GPUParticles2DEditorPlugin::handles(Object *p_object) const {
  40. return p_object->is_class("GPUParticles2D");
  41. }
  42. void GPUParticles2DEditorPlugin::make_visible(bool p_visible) {
  43. if (p_visible) {
  44. toolbar->show();
  45. } else {
  46. toolbar->hide();
  47. }
  48. }
  49. void GPUParticles2DEditorPlugin::_file_selected(const String &p_file) {
  50. source_emission_file = p_file;
  51. emission_mask->popup_centered();
  52. }
  53. void GPUParticles2DEditorPlugin::_menu_callback(int p_idx) {
  54. switch (p_idx) {
  55. case MENU_GENERATE_VISIBILITY_RECT: {
  56. float gen_time = particles->get_lifetime();
  57. if (gen_time < 1.0) {
  58. generate_seconds->set_value(1.0);
  59. } else {
  60. generate_seconds->set_value(trunc(gen_time) + 1.0);
  61. }
  62. generate_visibility_rect->popup_centered();
  63. } break;
  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_OPTION_CONVERT_TO_CPU_PARTICLES: {
  71. CPUParticles2D *cpu_particles = memnew(CPUParticles2D);
  72. cpu_particles->convert_from_particles(particles);
  73. cpu_particles->set_name(particles->get_name());
  74. cpu_particles->set_transform(particles->get_transform());
  75. cpu_particles->set_visible(particles->is_visible());
  76. cpu_particles->set_process_mode(particles->get_process_mode());
  77. cpu_particles->set_z_index(particles->get_z_index());
  78. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  79. ur->create_action(TTR("Convert to CPUParticles2D"));
  80. ur->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", particles, cpu_particles, true, false);
  81. ur->add_do_reference(cpu_particles);
  82. ur->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", cpu_particles, particles, false, false);
  83. ur->add_undo_reference(particles);
  84. ur->commit_action();
  85. } break;
  86. case MENU_RESTART: {
  87. particles->restart();
  88. }
  89. }
  90. }
  91. void GPUParticles2DEditorPlugin::_generate_visibility_rect() {
  92. float time = generate_seconds->get_value();
  93. float running = 0.0;
  94. EditorProgress ep("gen_vrect", TTR("Generating Visibility Rect"), int(time));
  95. bool was_emitting = particles->is_emitting();
  96. if (!was_emitting) {
  97. particles->set_emitting(true);
  98. OS::get_singleton()->delay_usec(1000);
  99. }
  100. Rect2 rect;
  101. while (running < time) {
  102. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  103. ep.step("Generating...", int(running), true);
  104. OS::get_singleton()->delay_usec(1000);
  105. Rect2 capture = particles->capture_rect();
  106. if (rect == Rect2()) {
  107. rect = capture;
  108. } else {
  109. rect = rect.merge(capture);
  110. }
  111. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  112. }
  113. if (!was_emitting) {
  114. particles->set_emitting(false);
  115. }
  116. undo_redo->create_action(TTR("Generate Visibility Rect"));
  117. undo_redo->add_do_method(particles, "set_visibility_rect", rect);
  118. undo_redo->add_undo_method(particles, "set_visibility_rect", particles->get_visibility_rect());
  119. undo_redo->commit_action();
  120. }
  121. void GPUParticles2DEditorPlugin::_generate_emission_mask() {
  122. Ref<ParticlesMaterial> pm = particles->get_process_material();
  123. if (!pm.is_valid()) {
  124. EditorNode::get_singleton()->show_warning(TTR("Can only set point into a ParticlesMaterial process material"));
  125. return;
  126. }
  127. Ref<Image> img;
  128. img.instantiate();
  129. Error err = ImageLoader::load_image(source_emission_file, img);
  130. ERR_FAIL_COND_MSG(err != OK, "Error loading image '" + source_emission_file + "'.");
  131. if (img->is_compressed()) {
  132. img->decompress();
  133. }
  134. img->convert(Image::FORMAT_RGBA8);
  135. ERR_FAIL_COND(img->get_format() != Image::FORMAT_RGBA8);
  136. Size2i s = Size2(img->get_width(), img->get_height());
  137. ERR_FAIL_COND(s.width == 0 || s.height == 0);
  138. Vector<Point2> valid_positions;
  139. Vector<Point2> valid_normals;
  140. Vector<uint8_t> valid_colors;
  141. valid_positions.resize(s.width * s.height);
  142. EmissionMode emode = (EmissionMode)emission_mask_mode->get_selected();
  143. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  144. valid_normals.resize(s.width * s.height);
  145. }
  146. bool capture_colors = emission_colors->is_pressed();
  147. if (capture_colors) {
  148. valid_colors.resize(s.width * s.height * 4);
  149. }
  150. int vpc = 0;
  151. {
  152. Vector<uint8_t> data = img->get_data();
  153. const uint8_t *r = data.ptr();
  154. for (int i = 0; i < s.width; i++) {
  155. for (int j = 0; j < s.height; j++) {
  156. uint8_t a = r[(j * s.width + i) * 4 + 3];
  157. if (a > 128) {
  158. if (emode == EMISSION_MODE_SOLID) {
  159. if (capture_colors) {
  160. valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  161. valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  162. valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  163. valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  164. }
  165. valid_positions.write[vpc++] = Point2(i, j);
  166. } else {
  167. bool on_border = false;
  168. for (int x = i - 1; x <= i + 1; x++) {
  169. for (int y = j - 1; y <= j + 1; y++) {
  170. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  171. on_border = true;
  172. break;
  173. }
  174. }
  175. if (on_border) {
  176. break;
  177. }
  178. }
  179. if (on_border) {
  180. valid_positions.write[vpc] = Point2(i, j);
  181. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  182. Vector2 normal;
  183. for (int x = i - 2; x <= i + 2; x++) {
  184. for (int y = j - 2; y <= j + 2; y++) {
  185. if (x == i && y == j) {
  186. continue;
  187. }
  188. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  189. normal += Vector2(x - i, y - j).normalized();
  190. }
  191. }
  192. }
  193. normal.normalize();
  194. valid_normals.write[vpc] = normal;
  195. }
  196. if (capture_colors) {
  197. valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  198. valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  199. valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  200. valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  201. }
  202. vpc++;
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. valid_positions.resize(vpc);
  210. if (valid_normals.size()) {
  211. valid_normals.resize(vpc);
  212. }
  213. ERR_FAIL_COND_MSG(valid_positions.size() == 0, "No pixels with transparency > 128 in image...");
  214. Vector<uint8_t> texdata;
  215. int w = 2048;
  216. int h = (vpc / 2048) + 1;
  217. texdata.resize(w * h * 2 * sizeof(float));
  218. {
  219. uint8_t *tw = texdata.ptrw();
  220. float *twf = (float *)tw;
  221. for (int i = 0; i < vpc; i++) {
  222. twf[i * 2 + 0] = valid_positions[i].x;
  223. twf[i * 2 + 1] = valid_positions[i].y;
  224. }
  225. }
  226. img.instantiate();
  227. img->create(w, h, false, Image::FORMAT_RGF, texdata);
  228. Ref<ImageTexture> imgt;
  229. imgt.instantiate();
  230. imgt->create_from_image(img);
  231. pm->set_emission_point_texture(imgt);
  232. pm->set_emission_point_count(vpc);
  233. if (capture_colors) {
  234. Vector<uint8_t> colordata;
  235. colordata.resize(w * h * 4); //use RG texture
  236. {
  237. uint8_t *tw = colordata.ptrw();
  238. for (int i = 0; i < vpc * 4; i++) {
  239. tw[i] = valid_colors[i];
  240. }
  241. }
  242. img.instantiate();
  243. img->create(w, h, false, Image::FORMAT_RGBA8, colordata);
  244. imgt.instantiate();
  245. imgt->create_from_image(img);
  246. pm->set_emission_color_texture(imgt);
  247. }
  248. if (valid_normals.size()) {
  249. pm->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  250. Vector<uint8_t> normdata;
  251. normdata.resize(w * h * 2 * sizeof(float)); //use RG texture
  252. {
  253. uint8_t *tw = normdata.ptrw();
  254. float *twf = (float *)tw;
  255. for (int i = 0; i < vpc; i++) {
  256. twf[i * 2 + 0] = valid_normals[i].x;
  257. twf[i * 2 + 1] = valid_normals[i].y;
  258. }
  259. }
  260. img.instantiate();
  261. img->create(w, h, false, Image::FORMAT_RGF, normdata);
  262. imgt.instantiate();
  263. imgt->create_from_image(img);
  264. pm->set_emission_normal_texture(imgt);
  265. } else {
  266. pm->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_POINTS);
  267. }
  268. }
  269. void GPUParticles2DEditorPlugin::_notification(int p_what) {
  270. if (p_what == NOTIFICATION_ENTER_TREE) {
  271. menu->get_popup()->connect("id_pressed", callable_mp(this, &GPUParticles2DEditorPlugin::_menu_callback));
  272. menu->set_icon(menu->get_theme_icon(SNAME("GPUParticles2D"), SNAME("EditorIcons")));
  273. file->connect("file_selected", callable_mp(this, &GPUParticles2DEditorPlugin::_file_selected));
  274. }
  275. }
  276. void GPUParticles2DEditorPlugin::_bind_methods() {
  277. }
  278. GPUParticles2DEditorPlugin::GPUParticles2DEditorPlugin(EditorNode *p_node) {
  279. particles = nullptr;
  280. editor = p_node;
  281. undo_redo = editor->get_undo_redo();
  282. toolbar = memnew(HBoxContainer);
  283. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  284. toolbar->hide();
  285. toolbar->add_child(memnew(VSeparator));
  286. menu = memnew(MenuButton);
  287. menu->get_popup()->add_item(TTR("Restart"), MENU_RESTART);
  288. menu->get_popup()->add_item(TTR("Generate Visibility Rect"), MENU_GENERATE_VISIBILITY_RECT);
  289. menu->get_popup()->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
  290. // menu->get_popup()->add_item(TTR("Clear Emission Mask"), MENU_CLEAR_EMISSION_MASK);
  291. menu->get_popup()->add_item(TTR("Convert to CPUParticles2D"), MENU_OPTION_CONVERT_TO_CPU_PARTICLES);
  292. menu->set_text(TTR("GPUParticles2D"));
  293. menu->set_switch_on_hover(true);
  294. toolbar->add_child(menu);
  295. file = memnew(EditorFileDialog);
  296. List<String> ext;
  297. ImageLoader::get_recognized_extensions(&ext);
  298. for (const String &E : ext) {
  299. file->add_filter("*." + E + "; " + E.to_upper());
  300. }
  301. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  302. toolbar->add_child(file);
  303. epoints = memnew(SpinBox);
  304. epoints->set_min(1);
  305. epoints->set_max(8192);
  306. epoints->set_step(1);
  307. epoints->set_value(512);
  308. file->get_vbox()->add_margin_child(TTR("Generated Point Count:"), epoints);
  309. generate_visibility_rect = memnew(ConfirmationDialog);
  310. generate_visibility_rect->set_title(TTR("Generate Visibility Rect"));
  311. VBoxContainer *genvb = memnew(VBoxContainer);
  312. generate_visibility_rect->add_child(genvb);
  313. generate_seconds = memnew(SpinBox);
  314. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  315. generate_seconds->set_min(0.1);
  316. generate_seconds->set_max(25);
  317. generate_seconds->set_value(2);
  318. toolbar->add_child(generate_visibility_rect);
  319. generate_visibility_rect->connect("confirmed", callable_mp(this, &GPUParticles2DEditorPlugin::_generate_visibility_rect));
  320. emission_mask = memnew(ConfirmationDialog);
  321. emission_mask->set_title(TTR("Load Emission Mask"));
  322. VBoxContainer *emvb = memnew(VBoxContainer);
  323. emission_mask->add_child(emvb);
  324. emission_mask_mode = memnew(OptionButton);
  325. emvb->add_margin_child(TTR("Emission Mask"), emission_mask_mode);
  326. emission_mask_mode->add_item(TTR("Solid Pixels"), EMISSION_MODE_SOLID);
  327. emission_mask_mode->add_item(TTR("Border Pixels"), EMISSION_MODE_BORDER);
  328. emission_mask_mode->add_item(TTR("Directed Border Pixels"), EMISSION_MODE_BORDER_DIRECTED);
  329. emission_colors = memnew(CheckBox);
  330. emission_colors->set_text(TTR("Capture from Pixel"));
  331. emvb->add_margin_child(TTR("Emission Colors"), emission_colors);
  332. toolbar->add_child(emission_mask);
  333. emission_mask->connect("confirmed", callable_mp(this, &GPUParticles2DEditorPlugin::_generate_emission_mask));
  334. }
  335. GPUParticles2DEditorPlugin::~GPUParticles2DEditorPlugin() {
  336. }