Implemented a very crude face creation algorithm. Also did the gui for it. I't not perfect, it does depend on the selection order if you have more than 3 vertices selected. I'll implement a better one later.

This commit is contained in:
Relintai 2022-01-15 13:44:14 +01:00
parent 0019d2f784
commit 18583e66f9
5 changed files with 101 additions and 19 deletions

View File

@ -156,3 +156,6 @@ func _on_add_triangle_at_pressed():
func _on_add_auad_at_pressed():
plugin.add_quad_at()
func _oncreate_face_pressed():
plugin.create_face()

View File

@ -181,7 +181,7 @@ margin_bottom = 94.0
[node name="VertexOps" type="VBoxContainer" parent="VBoxContainer"]
margin_top = 98.0
margin_right = 1010.0
margin_bottom = 208.0
margin_bottom = 232.0
[node name="OperationsLabel" type="Label" parent="VBoxContainer/VertexOps"]
margin_right = 1010.0
@ -193,29 +193,35 @@ valign = 1
[node name="Operations" type="VBoxContainer" parent="VBoxContainer/VertexOps"]
margin_top = 18.0
margin_right = 1010.0
margin_bottom = 110.0
margin_bottom = 134.0
[node name="Merge" type="Button" parent="VBoxContainer/VertexOps/Operations"]
[node name="AddFace" type="Button" parent="VBoxContainer/VertexOps/Operations"]
margin_right = 1010.0
margin_bottom = 20.0
text = "Merge"
text = "Create Face"
[node name="Split" type="Button" parent="VBoxContainer/VertexOps/Operations"]
[node name="Merge" type="Button" parent="VBoxContainer/VertexOps/Operations"]
margin_top = 24.0
margin_right = 1010.0
margin_bottom = 44.0
text = "Split"
text = "Merge"
[node name="Connect" type="Button" parent="VBoxContainer/VertexOps/Operations"]
[node name="Split" type="Button" parent="VBoxContainer/VertexOps/Operations"]
margin_top = 48.0
margin_right = 1010.0
margin_bottom = 68.0
text = "Connect"
text = "Split"
[node name="Disconnect" type="Button" parent="VBoxContainer/VertexOps/Operations"]
[node name="Connect" type="Button" parent="VBoxContainer/VertexOps/Operations"]
margin_top = 72.0
margin_right = 1010.0
margin_bottom = 92.0
text = "Connect"
[node name="Disconnect" type="Button" parent="VBoxContainer/VertexOps/Operations"]
margin_top = 96.0
margin_right = 1010.0
margin_bottom = 116.0
text = "Disconnect"
[node name="EdgeOps" type="VBoxContainer" parent="VBoxContainer"]
@ -307,9 +313,9 @@ margin_bottom = 92.0
text = "Disconnect"
[node name="Operations" type="VBoxContainer" parent="VBoxContainer"]
margin_top = 212.0
margin_top = 236.0
margin_right = 1010.0
margin_bottom = 322.0
margin_bottom = 346.0
[node name="OperationsLabel" type="Label" parent="VBoxContainer/Operations"]
margin_right = 1010.0
@ -347,14 +353,14 @@ margin_bottom = 92.0
text = "Rem Doubles"
[node name="HSeparator3" type="HSeparator" parent="VBoxContainer"]
margin_top = 326.0
margin_top = 350.0
margin_right = 1010.0
margin_bottom = 330.0
margin_bottom = 354.0
[node name="Add" type="VBoxContainer" parent="VBoxContainer"]
margin_top = 334.0
margin_top = 358.0
margin_right = 1010.0
margin_bottom = 420.0
margin_bottom = 444.0
[node name="AddLabel" type="Label" parent="VBoxContainer/Add"]
margin_right = 1010.0
@ -410,12 +416,12 @@ margin_bottom = 20.0
text = "At"
[node name="HSeparator2" type="HSeparator" parent="VBoxContainer"]
margin_top = 424.0
margin_top = 448.0
margin_right = 1010.0
margin_bottom = 428.0
margin_bottom = 452.0
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer"]
margin_top = 432.0
margin_top = 456.0
margin_right = 1010.0
margin_bottom = 586.0
size_flags_horizontal = 3
@ -431,7 +437,7 @@ valign = 1
[node name="UVEditorScrollContainer" type="ScrollContainer" parent="VBoxContainer/VBoxContainer"]
margin_top = 18.0
margin_right = 1010.0
margin_bottom = 154.0
margin_bottom = 130.0
size_flags_horizontal = 3
size_flags_vertical = 3
__meta__ = {
@ -456,6 +462,7 @@ script = ExtResource( 3 )
[connection signal="toggled" from="VBoxContainer/Actions/Actions/VBoxContainer2/HBoxContainer3/Edge" to="VBoxContainer/EdgeOps" method="set_visible"]
[connection signal="toggled" from="VBoxContainer/Actions/Actions/VBoxContainer2/HBoxContainer3/Face" to="." method="on_selection_mode_face_toggled"]
[connection signal="toggled" from="VBoxContainer/Actions/Actions/VBoxContainer2/HBoxContainer3/Face" to="VBoxContainer/FaceOps" method="set_visible"]
[connection signal="pressed" from="VBoxContainer/VertexOps/Operations/AddFace" to="." method="_oncreate_face_pressed"]
[connection signal="pressed" from="VBoxContainer/VertexOps/Operations/Merge" to="." method="_on_merge_pressed"]
[connection signal="pressed" from="VBoxContainer/VertexOps/Operations/Split" to="." method="_on_split_pressed"]
[connection signal="pressed" from="VBoxContainer/VertexOps/Operations/Connect" to="." method="_on_connect_pressed"]

View File

@ -31,6 +31,7 @@ var axis_constraint : int = AxisConstraint.X | AxisConstraint.Y | AxisConstraint
var selection_mode : int = SelectionMode.SELECTION_MODE_VERTEX
var previous_point : Vector2
var is_dragging : bool = false
var _last_known_camera_facing : Vector3 = Vector3(0, 0, -1)
var _mdr : MeshDataResource = null
@ -155,6 +156,8 @@ func apply() -> void:
_mdr.connect("changed", self, "on_mdr_changed")
func forward_spatial_gui_input(index, camera, event):
_last_known_camera_facing = camera.transform.basis.xform(Vector3(0, 0, -1))
if event is InputEventMouseButton:
var gt : Transform = get_spatial_node().global_transform
var ray_from : Vector3 = camera.global_transform.origin
@ -635,3 +638,29 @@ func connect_action():
func disconnect_action():
pass
func create_face():
if !_mdr:
return
if _selected_points.size() <= 2:
return
var mdr_arr : Array = _mdr.array
if mdr_arr.size() != ArrayMesh.ARRAY_MAX || mdr_arr[ArrayMesh.ARRAY_VERTEX] == null || mdr_arr[ArrayMesh.ARRAY_VERTEX].size() == 0:
return
if selection_mode == SelectionMode.SELECTION_MODE_VERTEX:
var points : PoolVector3Array = PoolVector3Array()
for sp in _selected_points:
points.push_back(_handle_points[sp])
MDRMeshUtils.add_triangulated_mesh_from_points(_mdr, points, _last_known_camera_facing)
elif selection_mode == SelectionMode.SELECTION_MODE_EDGE:
pass
elif selection_mode == SelectionMode.SELECTION_MODE_FACE:
pass

View File

@ -205,3 +205,7 @@ func connect_action():
func disconnect_action():
for g in active_gizmos:
g.disconnect_action()
func create_face():
for g in active_gizmos:
g.create_face()

View File

@ -1,6 +1,45 @@
tool
extends Object
static func add_triangulated_mesh_from_points(mdr : MeshDataResource, selected_points : PoolVector3Array, last_known_camera_facing : Vector3) -> void:
if selected_points.size() < 3:
return
var st : SurfaceTool = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
var v0 : Vector3 = selected_points[0]
var v1 : Vector3 = selected_points[1]
for i in range(2, selected_points.size()):
var v2 : Vector3 = selected_points[i]
st.add_uv(Vector2(0, 1))
st.add_vertex(v0)
st.add_uv(Vector2(0.5, 0))
st.add_vertex(v1)
st.add_uv(Vector2(1, 1))
st.add_vertex(v2)
var flip : bool = is_normal_similar(v0, v1, v2, last_known_camera_facing)
var im3 : int = (i - 2) * 3
if !flip:
st.add_index(im3)
st.add_index(im3 + 1)
st.add_index(im3 + 2)
else:
st.add_index(im3 + 2)
st.add_index(im3 + 1)
st.add_index(im3)
st.generate_normals()
merge_in_surface_tool(mdr, st)
# Appends a triangle to the mesh. It's created from miroring v2 to the ev0, and ev1 edge
static func append_triangle_to_tri_edge(mdr : MeshDataResource, ev0 : Vector3, ev1 : Vector3, v2 : Vector3) -> void:
var vref : Vector3 = reflect_vertex(ev0, ev1, v2)