sprite_editor_plugin.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include "sprite_editor_plugin.h"
  2. #include "canvas_item_editor_plugin.h"
  3. #include "scene/2d/mesh_instance_2d.h"
  4. #include "scene/gui/box_container.h"
  5. #include "thirdparty/misc/clipper.hpp"
  6. void SpriteEditor::_node_removed(Node *p_node) {
  7. if (p_node == node) {
  8. node = NULL;
  9. options->hide();
  10. }
  11. }
  12. void SpriteEditor::edit(Sprite *p_sprite) {
  13. node = p_sprite;
  14. }
  15. #define PRECISION 10.0
  16. Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float epsilon = 2.0) {
  17. int size = points.size();
  18. ERR_FAIL_COND_V(size < 2, Vector<Vector2>());
  19. ClipperLib::Path subj;
  20. ClipperLib::PolyTree solution;
  21. ClipperLib::PolyTree out;
  22. for (int i = 0; i < points.size(); i++) {
  23. subj << ClipperLib::IntPoint(points[i].x * PRECISION, points[i].y * PRECISION);
  24. }
  25. ClipperLib::ClipperOffset co;
  26. co.AddPath(subj, ClipperLib::jtMiter, ClipperLib::etClosedPolygon);
  27. co.Execute(solution, epsilon * PRECISION);
  28. ClipperLib::PolyNode *p = solution.GetFirst();
  29. ERR_FAIL_COND_V(!p, points);
  30. while (p->IsHole()) {
  31. p = p->GetNext();
  32. }
  33. //turn the result into simply polygon (AKA, fix overlap)
  34. //clamp into the specified rect
  35. ClipperLib::Clipper cl;
  36. cl.StrictlySimple(true);
  37. cl.AddPath(p->Contour, ClipperLib::ptSubject, true);
  38. //create the clipping rect
  39. ClipperLib::Path clamp;
  40. clamp.push_back(ClipperLib::IntPoint(0, 0));
  41. clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, 0));
  42. clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, rect.size.height * PRECISION));
  43. clamp.push_back(ClipperLib::IntPoint(0, rect.size.height * PRECISION));
  44. cl.AddPath(clamp, ClipperLib::ptClip, true);
  45. cl.Execute(ClipperLib::ctIntersection, out);
  46. Vector<Vector2> outPoints;
  47. ClipperLib::PolyNode *p2 = out.GetFirst();
  48. while (p2->IsHole()) {
  49. p2 = p2->GetNext();
  50. }
  51. int lasti = p2->Contour.size() - 1;
  52. Vector2 prev = Vector2(p2->Contour[lasti].X / PRECISION, p2->Contour[lasti].Y / PRECISION);
  53. for (int i = 0; i < p2->Contour.size(); i++) {
  54. Vector2 cur = Vector2(p2->Contour[i].X / PRECISION, p2->Contour[i].Y / PRECISION);
  55. if (cur.distance_to(prev) > 0.5) {
  56. outPoints.push_back(cur);
  57. prev = cur;
  58. }
  59. }
  60. return outPoints;
  61. }
  62. void SpriteEditor::_menu_option(int p_option) {
  63. if (!node) {
  64. return;
  65. }
  66. switch (p_option) {
  67. case MENU_OPTION_CREATE_MESH_2D: {
  68. _update_mesh_data();
  69. debug_uv_dialog->popup_centered();
  70. debug_uv->update();
  71. } break;
  72. }
  73. }
  74. void SpriteEditor::_update_mesh_data() {
  75. Ref<Texture> texture = node->get_texture();
  76. if (texture.is_null()) {
  77. err_dialog->set_text(TTR("Sprite is empty!"));
  78. err_dialog->popup_centered_minsize();
  79. return;
  80. }
  81. if (node->get_hframes() > 1 || node->get_vframes() > 1) {
  82. err_dialog->set_text(TTR("Can't convert a sprite using animation frames to mesh."));
  83. err_dialog->popup_centered_minsize();
  84. return;
  85. }
  86. Ref<Image> image = texture->get_data();
  87. ERR_FAIL_COND(image.is_null());
  88. Rect2 rect;
  89. if (node->is_region())
  90. rect = node->get_region_rect();
  91. else
  92. rect.size = Size2(image->get_width(), image->get_height());
  93. Ref<BitMap> bm;
  94. bm.instance();
  95. bm->create_from_image_alpha(image);
  96. int grow = island_merging->get_value();
  97. if (grow > 0) {
  98. bm->grow_mask(grow, rect);
  99. }
  100. float epsilon = simplification->get_value();
  101. Vector<Vector<Vector2> > lines = bm->clip_opaque_to_polygons(rect, epsilon);
  102. print_line("lines: " + itos(lines.size()));
  103. uv_lines.clear();
  104. computed_vertices.clear();
  105. computed_uv.clear();
  106. computed_indices.clear();
  107. Size2 img_size = Vector2(image->get_width(), image->get_height());
  108. for (int j = 0; j < lines.size(); j++) {
  109. lines[j] = expand(lines[j], rect, epsilon);
  110. int index_ofs = computed_vertices.size();
  111. for (int i = 0; i < lines[j].size(); i++) {
  112. Vector2 vtx = lines[j][i];
  113. computed_uv.push_back(vtx / img_size);
  114. vtx -= rect.position; //offset by rect position
  115. //flip if flipped
  116. if (node->is_flipped_h())
  117. vtx.x = rect.size.x - vtx.x - 1.0;
  118. if (node->is_flipped_v())
  119. vtx.y = rect.size.y - vtx.y - 1.0;
  120. if (node->is_centered())
  121. vtx -= rect.size / 2.0;
  122. computed_vertices.push_back(vtx);
  123. }
  124. #if 0
  125. Vector<Vector<Vector2> > polys = Geometry::decompose_polygon(lines[j]);
  126. print_line("polygon: " + itos(polys.size()));
  127. for (int i = 0; i < polys.size(); i++) {
  128. for (int k = 0; k < polys[i].size(); k++) {
  129. int idxn = (k + 1) % polys[i].size();
  130. uv_lines.push_back(polys[i][k]);
  131. uv_lines.push_back(polys[i][idxn]);
  132. }
  133. }
  134. #endif
  135. #if 1
  136. Vector<int> poly = Geometry::triangulate_polygon(lines[j]);
  137. for (int i = 0; i < poly.size(); i += 3) {
  138. for (int k = 0; k < 3; k++) {
  139. int idx = i + k;
  140. int idxn = i + (k + 1) % 3;
  141. uv_lines.push_back(lines[j][poly[idx]]);
  142. uv_lines.push_back(lines[j][poly[idxn]]);
  143. computed_indices.push_back(poly[idx] + index_ofs);
  144. }
  145. }
  146. #endif
  147. #if 0
  148. for (int i = 0; i < lines[j].size() - 1; i++) {
  149. uv_lines.push_back(lines[j][i]);
  150. uv_lines.push_back(lines[j][i + 1]);
  151. }
  152. #endif
  153. }
  154. debug_uv->update();
  155. }
  156. void SpriteEditor::_create_mesh_node() {
  157. if (computed_vertices.size() < 3) {
  158. err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh."));
  159. err_dialog->popup_centered_minsize();
  160. return;
  161. }
  162. Ref<ArrayMesh> mesh;
  163. mesh.instance();
  164. Array a;
  165. a.resize(Mesh::ARRAY_MAX);
  166. a[Mesh::ARRAY_VERTEX] = computed_vertices;
  167. a[Mesh::ARRAY_TEX_UV] = computed_uv;
  168. a[Mesh::ARRAY_INDEX] = computed_indices;
  169. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  170. MeshInstance2D *mesh_instance = memnew(MeshInstance2D);
  171. mesh_instance->set_mesh(mesh);
  172. EditorNode::get_singleton()->get_scene_tree_dock()->replace_node(node, mesh_instance);
  173. }
  174. #if 0
  175. void SpriteEditor::_create_uv_lines() {
  176. Ref<Mesh> sprite = node->get_sprite();
  177. ERR_FAIL_COND(!sprite.is_valid());
  178. Set<SpriteEditorEdgeSort> edges;
  179. uv_lines.clear();
  180. for (int i = 0; i < sprite->get_surface_count(); i++) {
  181. if (sprite->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  182. continue;
  183. Array a = sprite->surface_get_arrays(i);
  184. PoolVector<Vector2> uv = a[p_layer == 0 ? Mesh::ARRAY_TEX_UV : Mesh::ARRAY_TEX_UV2];
  185. if (uv.size() == 0) {
  186. err_dialog->set_text(TTR("Model has no UV in this layer"));
  187. err_dialog->popup_centered_minsize();
  188. return;
  189. }
  190. PoolVector<Vector2>::Read r = uv.read();
  191. PoolVector<int> indices = a[Mesh::ARRAY_INDEX];
  192. PoolVector<int>::Read ri;
  193. int ic;
  194. bool use_indices;
  195. if (indices.size()) {
  196. ic = indices.size();
  197. ri = indices.read();
  198. use_indices = true;
  199. } else {
  200. ic = uv.size();
  201. use_indices = false;
  202. }
  203. for (int j = 0; j < ic; j += 3) {
  204. for (int k = 0; k < 3; k++) {
  205. SpriteEditorEdgeSort edge;
  206. if (use_indices) {
  207. edge.a = r[ri[j + k]];
  208. edge.b = r[ri[j + ((k + 1) % 3)]];
  209. } else {
  210. edge.a = r[j + k];
  211. edge.b = r[j + ((k + 1) % 3)];
  212. }
  213. if (edges.has(edge))
  214. continue;
  215. uv_lines.push_back(edge.a);
  216. uv_lines.push_back(edge.b);
  217. edges.insert(edge);
  218. }
  219. }
  220. }
  221. debug_uv_dialog->popup_centered_minsize();
  222. }
  223. #endif
  224. void SpriteEditor::_debug_uv_draw() {
  225. if (uv_lines.size() == 0)
  226. return;
  227. Ref<Texture> tex = node->get_texture();
  228. ERR_FAIL_COND(!tex.is_valid());
  229. debug_uv->set_clip_contents(true);
  230. debug_uv->draw_texture(tex, Point2());
  231. debug_uv->set_custom_minimum_size(tex->get_size());
  232. //debug_uv->draw_set_transform(Vector2(), 0, debug_uv->get_size());
  233. debug_uv->draw_multiline(uv_lines, Color(1.0, 0.8, 0.7));
  234. }
  235. void SpriteEditor::_bind_methods() {
  236. ClassDB::bind_method("_menu_option", &SpriteEditor::_menu_option);
  237. ClassDB::bind_method("_debug_uv_draw", &SpriteEditor::_debug_uv_draw);
  238. ClassDB::bind_method("_update_mesh_data", &SpriteEditor::_update_mesh_data);
  239. ClassDB::bind_method("_create_mesh_node", &SpriteEditor::_create_mesh_node);
  240. }
  241. SpriteEditor::SpriteEditor() {
  242. options = memnew(MenuButton);
  243. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
  244. options->set_text(TTR("Sprite"));
  245. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Sprite", "EditorIcons"));
  246. options->get_popup()->add_item(TTR("Convert to 2D Mesh"), MENU_OPTION_CREATE_MESH_2D);
  247. options->get_popup()->connect("id_pressed", this, "_menu_option");
  248. err_dialog = memnew(AcceptDialog);
  249. add_child(err_dialog);
  250. debug_uv_dialog = memnew(ConfirmationDialog);
  251. debug_uv_dialog->get_ok()->set_text(TTR("Create 2D Mesh"));
  252. debug_uv_dialog->set_title("Mesh 2D Preview");
  253. VBoxContainer *vb = memnew(VBoxContainer);
  254. debug_uv_dialog->add_child(vb);
  255. ScrollContainer *scroll = memnew(ScrollContainer);
  256. scroll->set_custom_minimum_size(Size2(800, 500) * EDSCALE);
  257. scroll->set_enable_h_scroll(true);
  258. scroll->set_enable_v_scroll(true);
  259. vb->add_margin_child(TTR("Preview:"), scroll, true);
  260. debug_uv = memnew(Control);
  261. debug_uv->connect("draw", this, "_debug_uv_draw");
  262. scroll->add_child(debug_uv);
  263. debug_uv_dialog->connect("confirmed", this, "_create_mesh_node");
  264. HBoxContainer *hb = memnew(HBoxContainer);
  265. hb->add_child(memnew(Label(TTR("Simplification: "))));
  266. simplification = memnew(SpinBox);
  267. simplification->set_min(0.01);
  268. simplification->set_max(10.00);
  269. simplification->set_step(0.01);
  270. simplification->set_value(2);
  271. hb->add_child(simplification);
  272. hb->add_spacer();
  273. hb->add_child(memnew(Label(TTR("Grow (Pixels): "))));
  274. island_merging = memnew(SpinBox);
  275. island_merging->set_min(0);
  276. island_merging->set_max(10);
  277. island_merging->set_step(1);
  278. island_merging->set_value(2);
  279. hb->add_child(island_merging);
  280. hb->add_spacer();
  281. update_preview = memnew(Button);
  282. update_preview->set_text(TTR("Update Preview"));
  283. update_preview->connect("pressed", this, "_update_mesh_data");
  284. hb->add_child(update_preview);
  285. vb->add_margin_child(TTR("Settings:"), hb);
  286. add_child(debug_uv_dialog);
  287. }
  288. void SpriteEditorPlugin::edit(Object *p_object) {
  289. sprite_editor->edit(Object::cast_to<Sprite>(p_object));
  290. }
  291. bool SpriteEditorPlugin::handles(Object *p_object) const {
  292. return p_object->is_class("Sprite");
  293. }
  294. void SpriteEditorPlugin::make_visible(bool p_visible) {
  295. if (p_visible) {
  296. sprite_editor->options->show();
  297. } else {
  298. sprite_editor->options->hide();
  299. sprite_editor->edit(NULL);
  300. }
  301. }
  302. SpriteEditorPlugin::SpriteEditorPlugin(EditorNode *p_node) {
  303. editor = p_node;
  304. sprite_editor = memnew(SpriteEditor);
  305. editor->get_viewport()->add_child(sprite_editor);
  306. make_visible(false);
  307. //sprite_editor->options->hide();
  308. }
  309. SpriteEditorPlugin::~SpriteEditorPlugin() {
  310. }