From 9ab64c23015fa11086540db8ac4e755b5507be2f Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 18 Jan 2022 22:21:50 +0100 Subject: [PATCH] Implement mechanism to transfer seams after a merge or cleanup. --- .../mesh_data_resource_editor/MIDGizmo.gd | 22 ++++- .../utilities/mdred_mesh_utils.gd | 83 +++++++++++++++++++ .../utilities/mesh_outline.gd | 3 + 3 files changed, 105 insertions(+), 3 deletions(-) diff --git a/game/addons/mesh_data_resource_editor/MIDGizmo.gd b/game/addons/mesh_data_resource_editor/MIDGizmo.gd index f6a5b24d..50c0ebe4 100644 --- a/game/addons/mesh_data_resource_editor/MIDGizmo.gd +++ b/game/addons/mesh_data_resource_editor/MIDGizmo.gd @@ -743,28 +743,44 @@ func remove_doubles(): if !_mdr: return + _mdr.disconnect("changed", self, "on_mdr_changed") + 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 - var merged_arrays : Array = MeshUtils.remove_doubles(mdr_arr) + var seam_points : PoolVector3Array = MDRMeshUtils.seams_to_points(_mdr) + var merged_arrays : Array = MeshUtils.remove_doubles(mdr_arr) _mdr.array = merged_arrays + + MDRMeshUtils.points_to_seams(_mdr, seam_points) + + _mdr.connect("changed", self, "on_mdr_changed") + on_mdr_changed() func merge_optimize(): if !_mdr: return + _mdr.disconnect("changed", self, "on_mdr_changed") + 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 - var merged_arrays : Array = MeshUtils.merge_mesh_array(mdr_arr) + var seam_points : PoolVector3Array = MDRMeshUtils.seams_to_points(_mdr) + var merged_arrays : Array = MeshUtils.merge_mesh_array(mdr_arr) _mdr.array = merged_arrays - + + MDRMeshUtils.points_to_seams(_mdr, seam_points) + + _mdr.connect("changed", self, "on_mdr_changed") + on_mdr_changed() + func onnect_to_first_selected(): if !_mdr: return 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 4a161d46..8610c0a9 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 @@ -724,3 +724,86 @@ static func remove_seam(mdr : MeshDataResource, index0 : int, index1 : int) -> v seams.remove(i) mdr.seams = seams return + +static func is_verts_equal(v0 : Vector3, v1 : Vector3) -> bool: + return is_equal_approx(v0.x, v1.x) && is_equal_approx(v0.y, v1.y) && is_equal_approx(v0.z, v1.z) + + +static func points_to_seams(mdr : MeshDataResource, points : PoolVector3Array) -> void: + var arrays : Array = mdr.get_array() + + if arrays.size() != ArrayMesh.ARRAY_MAX: + return + + if arrays[ArrayMesh.ARRAY_VERTEX] == null: + return + + var vertices : PoolVector3Array = arrays[ArrayMesh.ARRAY_VERTEX] + var indices : PoolIntArray = arrays[ArrayMesh.ARRAY_INDEX] + var seams : PoolIntArray = PoolIntArray() + + for i in range(0, points.size(), 2): + var p0 : Vector3 = points[i] + var p1 : Vector3 = points[i + 1] + + for j in range(0, indices.size(), 3): + var v0 : Vector3 = vertices[indices[j]] + var v1 : Vector3 = vertices[indices[j + 1]] + var v2 : Vector3 = vertices[indices[j + 2]] + + var p0_index : int = -1 + + if is_verts_equal(p0, v0): + p0_index = indices[j] + elif is_verts_equal(p0, v1): + p0_index = indices[j + 1] + elif is_verts_equal(p0, v2): + p0_index = indices[j + 2] + + if p0_index == -1: + continue + + var p1_index : int = -1 + + if is_verts_equal(p1, v0): + p1_index = indices[j] + elif is_verts_equal(p1, v1): + p1_index = indices[j + 1] + elif is_verts_equal(p1, v2): + p1_index = indices[j + 2] + + if p1_index == -1: + continue + + if p0_index == p1_index: + continue + + if p0_index > p1_index: + var t : int = p0_index + p0_index = p1_index + p1_index = t + + seams.push_back(p0_index) + seams.push_back(p1_index) + break + + mdr.seams = seams + +static func seams_to_points(mdr : MeshDataResource) -> PoolVector3Array: + var points : PoolVector3Array = PoolVector3Array() + + var arrays : Array = mdr.get_array() + + if arrays.size() != ArrayMesh.ARRAY_MAX: + return points + + if arrays[ArrayMesh.ARRAY_VERTEX] == null: + return points + + var vertices : PoolVector3Array = arrays[ArrayMesh.ARRAY_VERTEX] + var seams : PoolIntArray = mdr.seams + + for s in seams: + points.push_back(vertices[s]) + + return points diff --git a/game/addons/mesh_data_resource_editor/utilities/mesh_outline.gd b/game/addons/mesh_data_resource_editor/utilities/mesh_outline.gd index aced10f4..1dfc2fe1 100644 --- a/game/addons/mesh_data_resource_editor/utilities/mesh_outline.gd +++ b/game/addons/mesh_data_resource_editor/utilities/mesh_outline.gd @@ -45,6 +45,9 @@ func initialize() -> bool: return true func get_vertex(index : int) -> Vector3: + if index > _vertices.size(): + return Vector3() + var v : Vector3 = _vertices[index] # This should reduce z fighting