From 0019d2f784235cf4271aaa6e289682aaf2c4b15a Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 15 Jan 2022 01:29:09 +0100 Subject: [PATCH] Also implemented add quad to edge operation. --- .../mesh_data_resource_editor/MIDGizmo.gd | 54 ++++++++++++++++++- .../utilities/mdred_mesh_utils.gd | 48 +++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/game/addons/mesh_data_resource_editor/MIDGizmo.gd b/game/addons/mesh_data_resource_editor/MIDGizmo.gd index ba59ab24..6fc96d41 100644 --- a/game/addons/mesh_data_resource_editor/MIDGizmo.gd +++ b/game/addons/mesh_data_resource_editor/MIDGizmo.gd @@ -521,7 +521,49 @@ func add_triangle_to_edge(edge : int) -> void: ei1 = t MDRMeshUtils.append_triangle_to_tri_edge(_mdr, _vertices[ei0], _vertices[ei1], _vertices[erefind]) + +func add_quad_to_edge(edge : int) -> void: + var triangle_index : int = find_first_triangle_for_edge(edge) + var inds : int = triangle_index * 3 + + var ti0 : int = _indices[inds] + var ti1 : int = _indices[inds + 1] + var ti2 : int = _indices[inds + 2] + + var ps : PoolIntArray = _handle_to_vertex_map[edge] + + if ps.size() == 0: + return + + var ei0 : int = 0 + var ei1 : int = 0 + var erefind : int = 0 + + if !pool_int_arr_contains(ps, ti0): + ei0 = ti1 + ei1 = ti2 + erefind = ti0 + elif !pool_int_arr_contains(ps, ti1): + ei0 = ti0 + ei1 = ti2 + erefind = ti1 + elif !pool_int_arr_contains(ps, ti2): + ei0 = ti0 + ei1 = ti1 + erefind = ti2 + + var fo : Vector3 = MDRMeshUtils.get_face_normal(_vertices[ti0], _vertices[ti1], _vertices[ti2]) + var fn : Vector3 = MDRMeshUtils.get_face_normal(_vertices[ei0], _vertices[ei1], _vertices[erefind]) + + if fo.dot(fn) < 0: + var t : int = ei0 + ei0 = ei1 + ei1 = t + + MDRMeshUtils.append_quad_to_tri_edge(_mdr, _vertices[ei0], _vertices[ei1], _vertices[erefind]) + + func add_triangle_at() -> void: if !_mdr: return @@ -536,7 +578,17 @@ func add_triangle_at() -> void: add_triangle() func add_quad_at() -> void: - pass + if !_mdr: + return + + if selection_mode == SelectionMode.SELECTION_MODE_VERTEX: + #todo + pass + elif selection_mode == SelectionMode.SELECTION_MODE_EDGE: + for sp in _selected_points: + add_quad_to_edge(sp) + else: + add_triangle() func add_box() -> void: if _mdr: diff --git a/game/addons/mesh_data_resource_editor/utilities/mdred_mesh_utils.gd b/game/addons/mesh_data_resource_editor/utilities/mdred_mesh_utils.gd index 6993d97f..5a365e9c 100644 --- a/game/addons/mesh_data_resource_editor/utilities/mdred_mesh_utils.gd +++ b/game/addons/mesh_data_resource_editor/utilities/mdred_mesh_utils.gd @@ -51,7 +51,55 @@ static func add_triangle(mdr : MeshDataResource) -> void: st.generate_normals() merge_in_surface_tool(mdr, st) + +# Appends a quad to the mesh. It's created to the opposite side of v2 to the ev0, and ev1 edge +static func append_quad_to_tri_edge(mdr : MeshDataResource, ev0 : Vector3, ev1 : Vector3, v2 : Vector3) -> void: + var vref : Vector3 = reflect_vertex(ev0, ev1, v2) + var vproj : Vector3 = (vref - ev0).project(ev1 - ev0) + var eoffs : Vector3 = (vref - ev0) - vproj + var qv0 : Vector3 = ev0 + var qv1 : Vector3 = ev0 + eoffs + var qv2 : Vector3 = ev1 + eoffs + var qv3 : Vector3 = ev1 + + add_quad_at(mdr, qv0, qv1, qv2, qv3, false) + +static func add_quad_at(mdr : MeshDataResource, v0 : Vector3, v1 : Vector3, v2 : Vector3, v3 : Vector3, flip : bool = false) -> void: + var st : SurfaceTool = SurfaceTool.new() + + st.begin(Mesh.PRIMITIVE_TRIANGLES) + + st.add_uv(Vector2(0, 1)) + st.add_vertex(v0) + st.add_uv(Vector2(0, 0)) + st.add_vertex(v1) + st.add_uv(Vector2(1, 0)) + st.add_vertex(v2) + st.add_uv(Vector2(1, 1)) + st.add_vertex(v3) + + + if !flip: + st.add_index(0) + st.add_index(1) + st.add_index(2) + + st.add_index(0) + st.add_index(2) + st.add_index(3) + else: + st.add_index(2) + st.add_index(1) + st.add_index(0) + + st.add_index(3) + st.add_index(2) + st.add_index(0) + + st.generate_normals() + + merge_in_surface_tool(mdr, st) static func add_quad(mdr : MeshDataResource) -> void: var st : SurfaceTool = SurfaceTool.new()