From 8a2da58680c398d3a8be160dcb2f80e7f0888a02 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 31 Dec 2021 14:37:07 +0100 Subject: [PATCH] Implemented face selection mode. --- .../mesh_data_resource_editor/MIDGizmo.gd | 8 +-- .../utilities/mesh_decompose.gd | 71 ++++++++++++++++++- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/game/addons/mesh_data_resource_editor/MIDGizmo.gd b/game/addons/mesh_data_resource_editor/MIDGizmo.gd index c7205e35..f7738501 100644 --- a/game/addons/mesh_data_resource_editor/MIDGizmo.gd +++ b/game/addons/mesh_data_resource_editor/MIDGizmo.gd @@ -343,10 +343,10 @@ func recalculate_handle_points() -> void: _handle_points = result[0] _handle_to_vertex_map = result[1] elif selection_mode == SelectionMode.SELECTION_MODE_FACE: - #todo - var merged_arrays : Array = MeshUtils.merge_mesh_array(arr) - _handle_points = merged_arrays[ArrayMesh.ARRAY_VERTEX] - _handle_to_vertex_map = MeshDecompose.get_handle_vertex_to_vertex_map(arr, _handle_points) + var result : Array = MeshDecompose.get_handle_face_to_vertex_map(arr) + + _handle_points = result[0] + _handle_to_vertex_map = result[1] func on_mesh_data_resource_changed(mdr : MeshDataResource) -> void: if _mdr: diff --git a/game/addons/mesh_data_resource_editor/utilities/mesh_decompose.gd b/game/addons/mesh_data_resource_editor/utilities/mesh_decompose.gd index 5d04a589..2c9d5c43 100644 --- a/game/addons/mesh_data_resource_editor/utilities/mesh_decompose.gd +++ b/game/addons/mesh_data_resource_editor/utilities/mesh_decompose.gd @@ -118,8 +118,75 @@ static func get_handle_edge_to_vertex_map(arrays : Array) -> Array: return [ handle_points, handle_to_vertex_map ] -static func get_handle_face_to_vertex_map() -> Array: - return Array() +#returns an array: +#index 0 is the handle_points +#index 1 is the handle_to_vertex_map +static func get_handle_face_to_vertex_map(arrays : Array) -> Array: + var handle_to_vertex_map : Array + var handle_points : PoolVector3Array + + if arrays.size() != ArrayMesh.ARRAY_MAX || arrays[ArrayMesh.ARRAY_INDEX] == null: + return [ handle_points, handle_to_vertex_map ] + + var vertices : PoolVector3Array = arrays[ArrayMesh.ARRAY_VERTEX] + + if vertices.size() == 0: + return [ handle_points, handle_to_vertex_map ] + + var arr : Array = Array() + arr.resize(ArrayMesh.ARRAY_MAX) + arr[ArrayMesh.ARRAY_VERTEX] = arrays[ArrayMesh.ARRAY_VERTEX] + arr[ArrayMesh.ARRAY_INDEX] = arrays[ArrayMesh.ARRAY_INDEX] + + var optimized_arrays : Array = MeshUtils.merge_mesh_array(arr) + var optimized_verts : PoolVector3Array = optimized_arrays[ArrayMesh.ARRAY_VERTEX] + var optimized_indices : PoolIntArray = optimized_arrays[ArrayMesh.ARRAY_INDEX] + + var vert_to_optimized_vert_map : Array = get_handle_vertex_to_vertex_map(arrays, optimized_verts) + + for i in range(0, optimized_indices.size(), 3): + var i0 : int = optimized_indices[i + 0] + var i1 : int = optimized_indices[i + 1] + var i2 : int = optimized_indices[i + 2] + + var v0 : Vector3 = optimized_verts[i0] + var v1 : Vector3 = optimized_verts[i1] + var v2 : Vector3 = optimized_verts[i2] + + var pmid : Vector3 = v0 + v1 + v2 + pmid /= 3 + handle_points.append(pmid) + + var vm0 : PoolIntArray = vert_to_optimized_vert_map[i0] + var vm1 : PoolIntArray = vert_to_optimized_vert_map[i1] + var vm2 : PoolIntArray = vert_to_optimized_vert_map[i2] + + var vm : PoolIntArray = PoolIntArray() + vm.append_array(vm0) + + for vi in vm1: + var found : bool = false + for vit in vm: + if vi == vit: + found = true + break + + if !found: + vm.append(vi) + + for vi in vm2: + var found : bool = false + for vit in vm: + if vi == vit: + found = true + break + + if !found: + vm.append(vi) + + handle_to_vertex_map.append(vm) + + return [ handle_points, handle_to_vertex_map ] static func calculate_map_midpoints(mesh : Array, vertex_map : Array) -> PoolVector3Array: return PoolVector3Array()