diff options
author | Juan Linietsky <juan@godotengine.org> | 2018-02-21 09:38:21 -0300 |
---|---|---|
committer | Juan Linietsky <juan@godotengine.org> | 2018-02-21 09:39:09 -0300 |
commit | 9e3a1e5401f9f807085547de0ecc3f527610daa4 (patch) | |
tree | ac58cd4f27e9dfe8ed550dfce9bfc442f29cd05a /editor | |
parent | 1c77fdcc8516cf2f0c5083d4840a684b9ad90958 (diff) |
Add base support for 2D meshes in Godot, including Sprite -> Mesh2D conversion.
Diffstat (limited to 'editor')
-rw-r--r-- | editor/editor_node.cpp | 4 | ||||
-rw-r--r-- | editor/plugins/sprite_editor_plugin.cpp | 396 | ||||
-rw-r--r-- | editor/plugins/sprite_editor_plugin.h | 73 | ||||
-rw-r--r-- | editor/scene_tree_dock.cpp | 109 | ||||
-rw-r--r-- | editor/scene_tree_dock.h | 2 |
5 files changed, 532 insertions, 52 deletions
diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 6d94aedfa9..3ef6d65ab4 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -100,6 +100,7 @@ #include "editor/plugins/shader_editor_plugin.h" #include "editor/plugins/shader_graph_editor_plugin.h" #include "editor/plugins/spatial_editor_plugin.h" +#include "editor/plugins/sprite_editor_plugin.h" #include "editor/plugins/sprite_frames_editor_plugin.h" #include "editor/plugins/style_box_editor_plugin.h" #include "editor/plugins/texture_editor_plugin.h" @@ -5671,6 +5672,7 @@ EditorNode::EditorNode() { add_editor_plugin(memnew(AnimationTreeEditorPlugin(this))); add_editor_plugin(memnew(MeshLibraryEditorPlugin(this))); add_editor_plugin(memnew(StyleBoxEditorPlugin(this))); + add_editor_plugin(memnew(SpriteEditorPlugin(this))); add_editor_plugin(memnew(ParticlesEditorPlugin(this))); add_editor_plugin(memnew(ResourcePreloaderEditorPlugin(this))); add_editor_plugin(memnew(ItemListEditorPlugin(this))); @@ -5693,7 +5695,7 @@ EditorNode::EditorNode() { add_editor_plugin(memnew(CollisionShape2DEditorPlugin(this))); add_editor_plugin(memnew(CurveEditorPlugin(this))); add_editor_plugin(memnew(TextureEditorPlugin(this))); - add_editor_plugin(memnew(MeshEditorPlugin(this))); + add_editor_plugin(memnew(AudioBusesEditorPlugin(audio_bus_editor))); add_editor_plugin(memnew(AudioBusesEditorPlugin(audio_bus_editor))); add_editor_plugin(memnew(NavigationMeshEditorPlugin(this))); diff --git a/editor/plugins/sprite_editor_plugin.cpp b/editor/plugins/sprite_editor_plugin.cpp new file mode 100644 index 0000000000..77ad89a22b --- /dev/null +++ b/editor/plugins/sprite_editor_plugin.cpp @@ -0,0 +1,396 @@ +#include "sprite_editor_plugin.h" + +#include "canvas_item_editor_plugin.h" +#include "scene/2d/mesh_instance_2d.h" +#include "scene/gui/box_container.h" +#include "thirdparty/misc/clipper.hpp" + +void SpriteEditor::_node_removed(Node *p_node) { + + if (p_node == node) { + node = NULL; + options->hide(); + } +} + +void SpriteEditor::edit(Sprite *p_sprite) { + + node = p_sprite; +} + +#define PRECISION 10.0 + +Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float epsilon = 2.0) { + int size = points.size(); + ERR_FAIL_COND_V(size < 2, Vector<Vector2>()); + + ClipperLib::Path subj; + ClipperLib::PolyTree solution; + ClipperLib::PolyTree out; + + for (int i = 0; i < points.size(); i++) { + + subj << ClipperLib::IntPoint(points[i].x * PRECISION, points[i].y * PRECISION); + } + ClipperLib::ClipperOffset co; + co.AddPath(subj, ClipperLib::jtMiter, ClipperLib::etClosedPolygon); + co.Execute(solution, epsilon * PRECISION); + + ClipperLib::PolyNode *p = solution.GetFirst(); + + ERR_FAIL_COND_V(!p, points); + + while (p->IsHole()) { + p = p->GetNext(); + } + + //turn the result into simply polygon (AKA, fix overlap) + + //clamp into the specified rect + ClipperLib::Clipper cl; + cl.StrictlySimple(true); + cl.AddPath(p->Contour, ClipperLib::ptSubject, true); + //create the clipping rect + ClipperLib::Path clamp; + clamp.push_back(ClipperLib::IntPoint(0, 0)); + clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, 0)); + clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, rect.size.height * PRECISION)); + clamp.push_back(ClipperLib::IntPoint(0, rect.size.height * PRECISION)); + cl.AddPath(clamp, ClipperLib::ptClip, true); + cl.Execute(ClipperLib::ctIntersection, out); + + Vector<Vector2> outPoints; + ClipperLib::PolyNode *p2 = out.GetFirst(); + while (p2->IsHole()) { + p2 = p2->GetNext(); + } + + int lasti = p2->Contour.size() - 1; + Vector2 prev = Vector2(p2->Contour[lasti].X / PRECISION, p2->Contour[lasti].Y / PRECISION); + for (int i = 0; i < p2->Contour.size(); i++) { + + Vector2 cur = Vector2(p2->Contour[i].X / PRECISION, p2->Contour[i].Y / PRECISION); + if (cur.distance_to(prev) > 0.5) { + outPoints.push_back(cur); + prev = cur; + } + } + return outPoints; +} + +void SpriteEditor::_menu_option(int p_option) { + + switch (p_option) { + case MENU_OPTION_CREATE_MESH_2D: { + + _update_mesh_data(); + debug_uv_dialog->popup_centered(); + debug_uv->update(); + + } break; + } +} + +void SpriteEditor::_update_mesh_data() { + + Ref<Texture> texture = node->get_texture(); + if (texture.is_null()) { + err_dialog->set_text(TTR("Sprite is empty!")); + err_dialog->popup_centered_minsize(); + return; + } + + if (node->get_hframes() > 1 || node->get_vframes() > 1) { + err_dialog->set_text(TTR("Can't convert a sprite using animation frames to mesh.")); + err_dialog->popup_centered_minsize(); + return; + } + Ref<Image> image = texture->get_data(); + ERR_FAIL_COND(image.is_null()); + Rect2 rect; + if (node->is_region()) + rect = node->get_region_rect(); + else + rect.size = Size2(image->get_width(), image->get_height()); + + Ref<BitMap> bm; + bm.instance(); + bm->create_from_image_alpha(image); + + int grow = island_merging->get_value(); + if (grow > 0) { + bm->grow_mask(grow, rect); + } + + float epsilon = simplification->get_value(); + + Vector<Vector<Vector2> > lines = bm->clip_opaque_to_polygons(rect, epsilon); + + print_line("lines: " + itos(lines.size())); + uv_lines.clear(); + + computed_vertices.clear(); + computed_indices.clear(); + + Size2 img_size = Vector2(image->get_width(), image->get_height()); + for (int j = 0; j < lines.size(); j++) { + lines[j] = expand(lines[j], rect, epsilon); + + int index_ofs = computed_vertices.size(); + + for (int i = 0; i < lines[j].size(); i++) { + Vector2 vtx = lines[j][i]; + computed_uv.push_back(vtx / img_size); + + vtx -= rect.position; //offset by rect position + + //flip if flipped + if (node->is_flipped_h()) + vtx.x = rect.size.x - vtx.x - 1.0; + if (node->is_flipped_v()) + vtx.y = rect.size.y - vtx.y - 1.0; + + if (node->is_centered()) + vtx -= rect.size / 2.0; + + computed_vertices.push_back(vtx); + } +#if 0 + Vector<Vector<Vector2> > polys = Geometry::decompose_polygon(lines[j]); + print_line("polygon: " + itos(polys.size())); + + for (int i = 0; i < polys.size(); i++) { + for (int k = 0; k < polys[i].size(); k++) { + + int idxn = (k + 1) % polys[i].size(); + uv_lines.push_back(polys[i][k]); + uv_lines.push_back(polys[i][idxn]); + } + } +#endif + +#if 1 + + Vector<int> poly = Geometry::triangulate_polygon(lines[j]); + + for (int i = 0; i < poly.size(); i += 3) { + for (int k = 0; k < 3; k++) { + int idx = i + k; + int idxn = i + (k + 1) % 3; + uv_lines.push_back(lines[j][poly[idx]]); + uv_lines.push_back(lines[j][poly[idxn]]); + + computed_indices.push_back(poly[idx] + index_ofs); + } + } +#endif + +#if 0 + for (int i = 0; i < lines[j].size() - 1; i++) { + uv_lines.push_back(lines[j][i]); + uv_lines.push_back(lines[j][i + 1]); + } +#endif + } + + debug_uv->update(); +} + +void SpriteEditor::_create_mesh_node() { + + if (computed_vertices.size() < 3) { + err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh.")); + err_dialog->popup_centered_minsize(); + return; + } + + Ref<ArrayMesh> mesh; + mesh.instance(); + + Array a; + a.resize(Mesh::ARRAY_MAX); + a[Mesh::ARRAY_VERTEX] = computed_vertices; + a[Mesh::ARRAY_TEX_UV] = computed_uv; + a[Mesh::ARRAY_INDEX] = computed_indices; + + mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Mesh::ARRAY_FLAG_USE_2D_VERTICES); + + MeshInstance2D *mesh_instance = memnew(MeshInstance2D); + mesh_instance->set_mesh(mesh); + EditorNode::get_singleton()->get_scene_tree_dock()->replace_node(node, mesh_instance); +} + +#if 0 +void SpriteEditor::_create_uv_lines() { + + Ref<Mesh> sprite = node->get_sprite(); + ERR_FAIL_COND(!sprite.is_valid()); + + Set<SpriteEditorEdgeSort> edges; + uv_lines.clear(); + for (int i = 0; i < sprite->get_surface_count(); i++) { + if (sprite->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) + continue; + Array a = sprite->surface_get_arrays(i); + + PoolVector<Vector2> uv = a[p_layer == 0 ? Mesh::ARRAY_TEX_UV : Mesh::ARRAY_TEX_UV2]; + if (uv.size() == 0) { + err_dialog->set_text(TTR("Model has no UV in this layer")); + err_dialog->popup_centered_minsize(); + return; + } + + PoolVector<Vector2>::Read r = uv.read(); + + PoolVector<int> indices = a[Mesh::ARRAY_INDEX]; + PoolVector<int>::Read ri; + + int ic; + bool use_indices; + + if (indices.size()) { + ic = indices.size(); + ri = indices.read(); + use_indices = true; + } else { + ic = uv.size(); + use_indices = false; + } + + for (int j = 0; j < ic; j += 3) { + + for (int k = 0; k < 3; k++) { + + SpriteEditorEdgeSort edge; + if (use_indices) { + edge.a = r[ri[j + k]]; + edge.b = r[ri[j + ((k + 1) % 3)]]; + } else { + edge.a = r[j + k]; + edge.b = r[j + ((k + 1) % 3)]; + } + + if (edges.has(edge)) + continue; + + uv_lines.push_back(edge.a); + uv_lines.push_back(edge.b); + edges.insert(edge); + } + } + } + + debug_uv_dialog->popup_centered_minsize(); +} +#endif +void SpriteEditor::_debug_uv_draw() { + + if (uv_lines.size() == 0) + return; + + Ref<Texture> tex = node->get_texture(); + ERR_FAIL_COND(!tex.is_valid()); + debug_uv->set_clip_contents(true); + debug_uv->draw_texture(tex, Point2()); + debug_uv->set_custom_minimum_size(tex->get_size()); + //debug_uv->draw_set_transform(Vector2(), 0, debug_uv->get_size()); + debug_uv->draw_multiline(uv_lines, Color(1.0, 0.8, 0.7)); +} + +void SpriteEditor::_bind_methods() { + + ClassDB::bind_method("_menu_option", &SpriteEditor::_menu_option); + ClassDB::bind_method("_debug_uv_draw", &SpriteEditor::_debug_uv_draw); + ClassDB::bind_method("_update_mesh_data", &SpriteEditor::_update_mesh_data); + ClassDB::bind_method("_create_mesh_node", &SpriteEditor::_create_mesh_node); +} + +SpriteEditor::SpriteEditor() { + + options = memnew(MenuButton); + + CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options); + + options->set_text(TTR("Sprite")); + options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Sprite", "EditorIcons")); + + options->get_popup()->add_item(TTR("Convert to 2D Mesh"), MENU_OPTION_CREATE_MESH_2D); + + options->get_popup()->connect("id_pressed", this, "_menu_option"); + + err_dialog = memnew(AcceptDialog); + add_child(err_dialog); + + debug_uv_dialog = memnew(ConfirmationDialog); + debug_uv_dialog->get_ok()->set_text(TTR("Create 2D Mesh")); + debug_uv_dialog->set_title("Mesh 2D Preview"); + VBoxContainer *vb = memnew(VBoxContainer); + debug_uv_dialog->add_child(vb); + ScrollContainer *scroll = memnew(ScrollContainer); + scroll->set_custom_minimum_size(Size2(800, 500) * EDSCALE); + scroll->set_enable_h_scroll(true); + scroll->set_enable_v_scroll(true); + vb->add_margin_child(TTR("Preview:"), scroll, true); + debug_uv = memnew(Control); + debug_uv->connect("draw", this, "_debug_uv_draw"); + scroll->add_child(debug_uv); + debug_uv_dialog->connect("confirmed", this, "_create_mesh_node"); + + HBoxContainer *hb = memnew(HBoxContainer); + hb->add_child(memnew(Label(TTR("Simplification: ")))); + simplification = memnew(SpinBox); + simplification->set_min(0.01); + simplification->set_max(10.00); + simplification->set_step(0.01); + simplification->set_value(2); + hb->add_child(simplification); + hb->add_spacer(); + hb->add_child(memnew(Label(TTR("Grow (Pixels): ")))); + island_merging = memnew(SpinBox); + island_merging->set_min(0); + island_merging->set_max(10); + island_merging->set_step(1); + island_merging->set_value(2); + hb->add_child(island_merging); + hb->add_spacer(); + update_preview = memnew(Button); + update_preview->set_text(TTR("Update Preview")); + update_preview->connect("pressed", this, "_update_mesh_data"); + hb->add_child(update_preview); + vb->add_margin_child(TTR("Settings:"), hb); + + add_child(debug_uv_dialog); +} + +void SpriteEditorPlugin::edit(Object *p_object) { + + sprite_editor->edit(Object::cast_to<Sprite>(p_object)); +} + +bool SpriteEditorPlugin::handles(Object *p_object) const { + + return p_object->is_class("Sprite"); +} + +void SpriteEditorPlugin::make_visible(bool p_visible) { + + if (p_visible) { + sprite_editor->options->show(); + } else { + + sprite_editor->options->hide(); + sprite_editor->edit(NULL); + } +} + +SpriteEditorPlugin::SpriteEditorPlugin(EditorNode *p_node) { + + editor = p_node; + sprite_editor = memnew(SpriteEditor); + editor->get_viewport()->add_child(sprite_editor); + + //sprite_editor->options->hide(); +} + +SpriteEditorPlugin::~SpriteEditorPlugin() { +} diff --git a/editor/plugins/sprite_editor_plugin.h b/editor/plugins/sprite_editor_plugin.h new file mode 100644 index 0000000000..17aa3eb1f9 --- /dev/null +++ b/editor/plugins/sprite_editor_plugin.h @@ -0,0 +1,73 @@ +#ifndef SPRITE_EDITOR_PLUGIN_H +#define SPRITE_EDITOR_PLUGIN_H + +#include "editor/editor_node.h" +#include "editor/editor_plugin.h" +#include "scene/2d/sprite.h" +#include "scene/gui/spin_box.h" + +class SpriteEditor : public Control { + + GDCLASS(SpriteEditor, Control); + + enum Menu { + MENU_OPTION_CREATE_MESH_2D, + }; + + Sprite *node; + + MenuButton *options; + + ConfirmationDialog *outline_dialog; + + AcceptDialog *err_dialog; + + ConfirmationDialog *debug_uv_dialog; + Control *debug_uv; + Vector<Vector2> uv_lines; + + Vector<Vector2> computed_vertices; + Vector<Vector2> computed_uv; + Vector<int> computed_indices; + + SpinBox *simplification; + SpinBox *island_merging; + Button *update_preview; + + void _menu_option(int p_option); + + //void _create_uv_lines(); + friend class SpriteEditorPlugin; + + void _debug_uv_draw(); + void _update_mesh_data(); + void _create_mesh_node(); + +protected: + void _node_removed(Node *p_node); + static void _bind_methods(); + +public: + void edit(Sprite *p_sprite); + SpriteEditor(); +}; + +class SpriteEditorPlugin : public EditorPlugin { + + GDCLASS(SpriteEditorPlugin, EditorPlugin); + + SpriteEditor *sprite_editor; + EditorNode *editor; + +public: + virtual String get_name() const { return "Sprite"; } + bool has_main_screen() const { return false; } + virtual void edit(Object *p_object); + virtual bool handles(Object *p_object) const; + virtual void make_visible(bool p_visible); + + SpriteEditorPlugin(EditorNode *p_node); + ~SpriteEditorPlugin(); +}; + +#endif // SPRITE_EDITOR_PLUGIN_H diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index 45622d43e8..9f0f62592b 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -1402,70 +1402,77 @@ void SceneTreeDock::_create() { Node *newnode = Object::cast_to<Node>(c); ERR_FAIL_COND(!newnode); - List<PropertyInfo> pinfo; - n->get_property_list(&pinfo); - - for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) { - if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) - continue; - if (E->get().name == "__meta__") - continue; - newnode->set(E->get().name, n->get(E->get().name)); - } + replace_node(n, newnode); + } + } +} - editor->push_item(NULL); +void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node) { - //reconnect signals - List<MethodInfo> sl; + Node *n = p_node; + Node *newnode = p_by_node; + List<PropertyInfo> pinfo; + n->get_property_list(&pinfo); - n->get_signal_list(&sl); - for (List<MethodInfo>::Element *E = sl.front(); E; E = E->next()) { + for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) + continue; + if (E->get().name == "__meta__") + continue; + newnode->set(E->get().name, n->get(E->get().name)); + } - List<Object::Connection> cl; - n->get_signal_connection_list(E->get().name, &cl); + editor->push_item(NULL); - for (List<Object::Connection>::Element *F = cl.front(); F; F = F->next()) { + //reconnect signals + List<MethodInfo> sl; - Object::Connection &c = F->get(); - if (!(c.flags & Object::CONNECT_PERSIST)) - continue; - newnode->connect(c.signal, c.target, c.method, varray(), Object::CONNECT_PERSIST); - } - } + n->get_signal_list(&sl); + for (List<MethodInfo>::Element *E = sl.front(); E; E = E->next()) { - String newname = n->get_name(); + List<Object::Connection> cl; + n->get_signal_connection_list(E->get().name, &cl); - List<Node *> to_erase; - for (int i = 0; i < n->get_child_count(); i++) { - if (n->get_child(i)->get_owner() == NULL && n->is_owned_by_parent()) { - to_erase.push_back(n->get_child(i)); - } - } - n->replace_by(newnode, true); + for (List<Object::Connection>::Element *F = cl.front(); F; F = F->next()) { - if (n == edited_scene) { - edited_scene = newnode; - editor->set_edited_scene(newnode); - newnode->set_editable_instances(n->get_editable_instances()); - } + Object::Connection &c = F->get(); + if (!(c.flags & Object::CONNECT_PERSIST)) + continue; + newnode->connect(c.signal, c.target, c.method, varray(), Object::CONNECT_PERSIST); + } + } - //small hack to make collisionshapes and other kind of nodes to work - for (int i = 0; i < newnode->get_child_count(); i++) { - Node *c = newnode->get_child(i); - c->call("set_transform", c->call("get_transform")); - } - editor_data->get_undo_redo().clear_history(); - newnode->set_name(newname); + String newname = n->get_name(); + + List<Node *> to_erase; + for (int i = 0; i < n->get_child_count(); i++) { + if (n->get_child(i)->get_owner() == NULL && n->is_owned_by_parent()) { + to_erase.push_back(n->get_child(i)); + } + } + n->replace_by(newnode, true); - editor->push_item(newnode); + if (n == edited_scene) { + edited_scene = newnode; + editor->set_edited_scene(newnode); + newnode->set_editable_instances(n->get_editable_instances()); + } - memdelete(n); + //small hack to make collisionshapes and other kind of nodes to work + for (int i = 0; i < newnode->get_child_count(); i++) { + Node *c = newnode->get_child(i); + c->call("set_transform", c->call("get_transform")); + } + editor_data->get_undo_redo().clear_history(); + newnode->set_name(newname); - while (to_erase.front()) { - memdelete(to_erase.front()->get()); - to_erase.pop_front(); - } - } + editor->push_item(newnode); + + memdelete(n); + + while (to_erase.front()) { + memdelete(to_erase.front()->get()); + to_erase.pop_front(); } } diff --git a/editor/scene_tree_dock.h b/editor/scene_tree_dock.h index 0a68aa7dc2..a4f36e31ee 100644 --- a/editor/scene_tree_dock.h +++ b/editor/scene_tree_dock.h @@ -208,6 +208,8 @@ public: void show_tab_buttons(); void hide_tab_buttons(); + void replace_node(Node *p_node, Node *p_by_node); + void open_script_dialog(Node *p_for_node); SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSelection *p_editor_selection, EditorData &p_editor_data); }; |