From 0cd86b089ca815dc9abee5f0e18ad8439e581990 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 27 Jan 2022 16:39:23 +0100 Subject: [PATCH] Added back the extrude button to mdr ed's sidebar, and implemented an extrude algorithm. I have high hopes for it, however it does not work at the moment, because SurfaceTool messes up the order of the triangles in the mesh after a normal generation, which I will fix soon. --- .../addons/mesh_data_resource_editor/MDIEd.gd | 2 +- .../mesh_data_resource_editor/MDIEd.tscn | 15 ++-- .../mesh_data_resource_editor/MIDGizmo.gd | 87 ++++++++++++++++++- .../mesh_data_resource_editor/plugin.gd | 4 + 4 files changed, 98 insertions(+), 10 deletions(-) diff --git a/game/addons/mesh_data_resource_editor/MDIEd.gd b/game/addons/mesh_data_resource_editor/MDIEd.gd index 6bead367..1d5ec17b 100644 --- a/game/addons/mesh_data_resource_editor/MDIEd.gd +++ b/game/addons/mesh_data_resource_editor/MDIEd.gd @@ -147,7 +147,7 @@ func set_selection_mode_face() -> void: func _on_Extrude_pressed(): - pass # Replace with function body. + _plugin.extrude() func _on_AddBox_pressed(): _plugin.add_box() diff --git a/game/addons/mesh_data_resource_editor/MDIEd.tscn b/game/addons/mesh_data_resource_editor/MDIEd.tscn index f9c3a1b1..b9e1d98c 100644 --- a/game/addons/mesh_data_resource_editor/MDIEd.tscn +++ b/game/addons/mesh_data_resource_editor/MDIEd.tscn @@ -203,7 +203,7 @@ scroll_horizontal_enabled = false [node name="VBoxContainer2" type="VBoxContainer" parent="VBoxContainer/ScrollContainer"] margin_right = 998.0 -margin_bottom = 690.0 +margin_bottom = 698.0 size_flags_horizontal = 3 [node name="HBoxContainer6" type="HBoxContainer" parent="VBoxContainer/ScrollContainer/VBoxContainer2"] @@ -459,6 +459,11 @@ margin_top = 18.0 margin_right = 998.0 margin_bottom = 98.0 +[node name="Extrude" type="Button" parent="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations"] +margin_right = 1010.0 +margin_bottom = 20.0 +text = "Extrude" + [node name="Label" type="Label" parent="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations"] margin_right = 998.0 margin_bottom = 14.0 @@ -485,12 +490,6 @@ margin_bottom = 20.0 size_flags_horizontal = 3 text = "Quad" -[node name="Extrude" type="Button" parent="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations"] -visible = false -margin_right = 1010.0 -margin_bottom = 20.0 -text = "Extrude" - [node name="SeamLabel" type="Label" parent="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations"] margin_top = 42.0 margin_right = 998.0 @@ -744,9 +743,9 @@ size_flags_vertical = 3 [connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/VertexOps/Operations/HBoxContainer/ConnectToFirst" to="." method="_on_connect_to_first_selected_pressed"] [connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/VertexOps/Operations/HBoxContainer/ConnectMed" to="." method="_on_connect_to_avg_pressed"] [connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/VertexOps/Operations/HBoxContainer/ConnectToLast" to="." method="_on_connect_to_last_selected_pressed"] +[connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations/Extrude" to="." method="_on_Extrude_pressed"] [connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations/HBoxContainer2/TriAt" to="." method="_on_add_triangle_at_pressed"] [connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations/HBoxContainer2/QuadAt" to="." method="_on_add_auad_at_pressed"] -[connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations/Extrude" to="." method="_on_Extrude_pressed"] [connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations/HBoxContainer/Mark" to="." method="_on_mark_seam_pressed"] [connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/EdgeOps/Operations/HBoxContainer/Unmark" to="." method="_on_unmark_seam_pressed"] [connection signal="pressed" from="VBoxContainer/ScrollContainer/VBoxContainer2/FaceOps/Operations/Delete" to="." method="_on_delete_pressed"] diff --git a/game/addons/mesh_data_resource_editor/MIDGizmo.gd b/game/addons/mesh_data_resource_editor/MIDGizmo.gd index 8da2c037..48844fed 100644 --- a/game/addons/mesh_data_resource_editor/MIDGizmo.gd +++ b/game/addons/mesh_data_resource_editor/MIDGizmo.gd @@ -939,7 +939,92 @@ func add_quad_at() -> void: add_mesh_change_undo_redo(orig_arr, _mdr.array, "Add Quad At") enable_change_event() else: - add_triangle() + add_quad() + +func extrude() -> void: + if !_mdr: + return + + if _mdr.array.size() != ArrayMesh.ARRAY_MAX || _mdr.array[ArrayMesh.ARRAY_VERTEX] == null: + return + + if selection_mode == SelectionMode.SELECTION_MODE_VERTEX: + pass + elif selection_mode == SelectionMode.SELECTION_MODE_EDGE: + disable_change_event() + var orig_arr = copy_arrays(_mdr.array) + var original_size : int = orig_arr[ArrayMesh.ARRAY_VERTEX].size() + + for sp in _selected_points: + add_quad_to_edge(sp) + + var arr : Array = _mdr.array + + # Note: This algorithm depends heavily depends on the inner workings of add_quad_to_edge! + var new_verts : PoolVector3Array = arr[ArrayMesh.ARRAY_VERTEX] + + # every 4 vertex is a quad + # 1 ---- 2 + # | | + # | | + # 0 ---- 3 + # vertex 1, and 2 are the created new ones, 0, and 3 are duplicated from the original edge + + # Don't reallocate it every time + var found_verts : PoolIntArray = PoolIntArray() + + # Go through every new created 0th vertex + for i in range(original_size, new_verts.size(), 4): + var v0 : Vector3 = new_verts[i] + + found_verts.resize(0) + + # Find a pair for it (has to be the 3th). + for j in range(original_size, new_verts.size(), 4): + if i == j: + continue + + # +3 offset to 3rd vert + var v3 : Vector3 = new_verts[j + 3] + + if is_verts_equal(v0, v3): + # +2 offset to 2rd vert + found_verts.append(j + 2) + + if found_verts.size() == 0: + continue + + # Also append the first vertex index to simplify logic + found_verts.append(i + 1) + + # Calculate avg + var vavg : Vector3 = Vector3() + for ind in found_verts: + vavg += new_verts[ind] + + vavg /= found_verts.size() + + # set back + for ind in found_verts: + new_verts[ind] = vavg + + arr[ArrayMesh.ARRAY_VERTEX] = new_verts + _mdr.array = arr + + _selected_points.resize(0) + add_mesh_change_undo_redo(orig_arr, _mdr.array, "Extrude") + enable_change_event() + + # The selection alo will take care of the duplicates + var new_handle_points : PoolVector3Array = PoolVector3Array() + for i in range(original_size, new_verts.size() - 4, 4): + new_handle_points.append(new_verts[i + 1]) + new_handle_points.append(new_verts[i + 2]) + + # select new ones + # TODO + else: + add_quad() func add_box() -> void: if _mdr: diff --git a/game/addons/mesh_data_resource_editor/plugin.gd b/game/addons/mesh_data_resource_editor/plugin.gd index 36dada6f..5ca67c79 100644 --- a/game/addons/mesh_data_resource_editor/plugin.gd +++ b/game/addons/mesh_data_resource_editor/plugin.gd @@ -272,3 +272,7 @@ func handle_selection_type_back(): func handle_selection_type_all(): if current_mesh_data_instance && current_mesh_data_instance.gizmo: current_mesh_data_instance.gizmo.handle_selection_type_all() + +func extrude(): + if current_mesh_data_instance && current_mesh_data_instance.gizmo: + current_mesh_data_instance.gizmo.extrude()