From 24d0e671ef6ac3c56c977003e40c04ed635242a2 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 13 Feb 2022 13:45:33 +0100 Subject: [PATCH] Implemented rotation for the uv editor. --- .../uv_editor/RectView.gd | 4 +- .../uv_editor/RectViewNode.gd | 71 +++++++++++++++++-- .../uv_editor/UVEditor.tscn | 1 - 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/game/addons/mesh_data_resource_editor/uv_editor/RectView.gd b/game/addons/mesh_data_resource_editor/uv_editor/RectView.gd index 460031f7..a4877ee2 100644 --- a/game/addons/mesh_data_resource_editor/uv_editor/RectView.gd +++ b/game/addons/mesh_data_resource_editor/uv_editor/RectView.gd @@ -72,14 +72,14 @@ func on_mirror_vertical_button_pressed() -> void: func on_rotate_left_button_button_pressed() -> void: if selected_rect && is_instance_valid(selected_rect): - selected_rect.rotate_uvs(rotation_amount) + selected_rect.rotate_uvs(-rotation_amount) func on_rotate_amount_spinbox_changed(val : float) -> void: rotation_amount = val func on_rotate_right_button_button_pressed() -> void: if selected_rect && is_instance_valid(selected_rect): - selected_rect.rotate_uvs(-rotation_amount) + selected_rect.rotate_uvs(rotation_amount) func set_plugin(plugin : EditorPlugin) -> void: _plugin = plugin diff --git a/game/addons/mesh_data_resource_editor/uv_editor/RectViewNode.gd b/game/addons/mesh_data_resource_editor/uv_editor/RectViewNode.gd index 1325c727..fa5eb106 100644 --- a/game/addons/mesh_data_resource_editor/uv_editor/RectViewNode.gd +++ b/game/addons/mesh_data_resource_editor/uv_editor/RectViewNode.gd @@ -23,8 +23,8 @@ var edited_resource_parent_size : Vector2 = Vector2() var _edited_resource_rect_border_color : Color = Color(0.8, 0.8, 0.8, 0.5) var _edited_resource_rect_color : Color = Color(0.5, 0.5, 0.5, 0.2) -var _edited_resource_rect_selected_border_color : Color = Color(0.9, 0.9, 0.9, 0.5) -var _edited_resource_rect_selected_color : Color = Color(0.6, 0.6, 0.6, 0.2) +var _edited_resource_rect_selected_border_color : Color = Color(0.9, 0.9, 0.9, 0.8) +var _edited_resource_rect_selected_color : Color = Color(0.5, 0.5, 0.5, 0.4) var _edited_resource_uv_mesh_color : Color = Color(1, 1, 1, 1) var _editor_rect_border_size : int = 2 var _edited_resource_font_color : Color = Color(0, 0, 0, 1) @@ -97,7 +97,30 @@ func mirror_vertical() -> void: update() func rotate_uvs(amount : float) -> void: - pass + var t : Transform2D = Transform2D(deg2rad(amount), Vector2()) + + var pia : PoolIntArray = PoolIntArray() + for index in _indices: + var found : bool = false + + for i in pia: + if i == index: + found = true + break + + if found: + continue + + pia.append(index) + + var uv : Vector2 = _uvs[index] + uv = t.xform(uv) + _uvs.set(index, uv) + + + re_normalize_uvs() + apply_uv() + update() func refresh() -> void: if !_mdr: @@ -171,7 +194,48 @@ func set_up_base_rect() -> void: _uv_max = vmax normalize_uvs() + +func re_normalize_uvs() -> void: + if _uvs.size() == 0: + return + var vmin : Vector2 = _uvs[_indices[0]] + var vmax : Vector2 = vmin + for i in range(1, _indices.size()): + var uv : Vector2 = _uvs[_indices[i]] + + if uv.x < vmin.x: + vmin.x = uv.x + + if uv.x > vmax.x: + vmax.x = uv.x + + if uv.y < vmin.y: + vmin.y = uv.y + + if uv.y > vmax.y: + vmax.y = uv.y + + var xmm : float = vmax.x - vmin.x + var ymm : float = vmax.y - vmin.y + + if xmm == 0: + xmm = 0.0000001 + + if ymm == 0: + ymm = 0.0000001 + + for i in range(_uvs.size()): + var uv : Vector2 = _uvs[i] + + uv.x -= vmin.x + uv.x /= xmm + + uv.y -= vmin.y + uv.y /= ymm + + _uvs[i] = uv + func normalize_uvs() -> void: var xmm : float = _uv_max.x - _uv_min.x var ymm : float = _uv_max.y - _uv_min.y @@ -193,7 +257,6 @@ func normalize_uvs() -> void: _uvs[i] = uv - func apply_uv() -> void: if !_mdr: return diff --git a/game/addons/mesh_data_resource_editor/uv_editor/UVEditor.tscn b/game/addons/mesh_data_resource_editor/uv_editor/UVEditor.tscn index ac8ce1dc..2603514e 100644 --- a/game/addons/mesh_data_resource_editor/uv_editor/UVEditor.tscn +++ b/game/addons/mesh_data_resource_editor/uv_editor/UVEditor.tscn @@ -109,7 +109,6 @@ margin_right = 305.0 margin_bottom = 24.0 hint_tooltip = "Rotate amount in degrees." max_value = 360.0 -step = 0.0 value = 45.0 allow_greater = true allow_lesser = true