summaryrefslogtreecommitdiff
path: root/editor
diff options
context:
space:
mode:
authorMrBlockers <1747505+MrBlockers@users.noreply.github.com>2022-10-23 11:35:29 -0400
committerMrBlockers <1747505+MrBlockers@users.noreply.github.com>2022-10-25 18:23:23 -0400
commit92ebbf2c0acc232ba0818a5dcb1d3dfc4dd5df67 (patch)
treea281b25a89999dbf01a0385f99c9eecf8cb2e591 /editor
parent5b84583b95b68531a578a4da02974978b759915e (diff)
Fix multiple issues with region editor
Make the filter mode of the texture preview match the node/resource being edited where applicable, and nearest neighbor with mipmaps as a fallback. Make the Edit Region button for Sprite3D only appear when region is enabled, to match behavior of Sprite2D. Fix the editor not correctly clearing reference to previously edited resources, resulting in a visual bug displaying the incorrect texture.
Diffstat (limited to 'editor')
-rw-r--r--editor/plugins/texture_region_editor_plugin.cpp83
-rw-r--r--editor/plugins/texture_region_editor_plugin.h2
2 files changed, 77 insertions, 8 deletions
diff --git a/editor/plugins/texture_region_editor_plugin.cpp b/editor/plugins/texture_region_editor_plugin.cpp
index 8e04391a94..7f703e486a 100644
--- a/editor/plugins/texture_region_editor_plugin.cpp
+++ b/editor/plugins/texture_region_editor_plugin.cpp
@@ -86,8 +86,8 @@ void TextureRegionEditor::_region_draw() {
mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
RS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), mtx);
- edit_draw->draw_rect(Rect2(Point2(), base_tex->get_size()), Color(0.5, 0.5, 0.5, 0.5), false);
- edit_draw->draw_texture(base_tex, Point2());
+ edit_draw->draw_rect(Rect2(Point2(), preview_tex->get_size()), Color(0.5, 0.5, 0.5, 0.5), false);
+ edit_draw->draw_texture(preview_tex, Point2());
RS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), Transform2D());
const Color color = get_theme_color(SNAME("mono_color"), SNAME("Editor"));
@@ -905,6 +905,13 @@ void TextureRegionEditor::edit(Object *p_obj) {
if (atlas_tex.is_valid()) {
atlas_tex->disconnect("changed", callable_mp(this, &TextureRegionEditor::_texture_changed));
}
+
+ node_sprite_2d = nullptr;
+ node_sprite_3d = nullptr;
+ node_ninepatch = nullptr;
+ obj_styleBox = Ref<StyleBoxTexture>(nullptr);
+ atlas_tex = Ref<AtlasTexture>(nullptr);
+
if (p_obj) {
node_sprite_2d = Object::cast_to<Sprite2D>(p_obj);
node_sprite_3d = Object::cast_to<Sprite3D>(p_obj);
@@ -926,13 +933,8 @@ void TextureRegionEditor::edit(Object *p_obj) {
p_obj->connect("texture_changed", callable_mp(this, &TextureRegionEditor::_texture_changed));
}
_edit_region();
- } else {
- node_sprite_2d = nullptr;
- node_sprite_3d = nullptr;
- node_ninepatch = nullptr;
- obj_styleBox = Ref<StyleBoxTexture>(nullptr);
- atlas_tex = Ref<AtlasTexture>(nullptr);
}
+
edit_draw->queue_redraw();
popup_centered_ratio(0.5);
request_center = true;
@@ -946,20 +948,80 @@ void TextureRegionEditor::_texture_changed() {
}
void TextureRegionEditor::_edit_region() {
+ CanvasItem::TextureFilter filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
+
Ref<Texture2D> texture = nullptr;
if (atlas_tex.is_valid()) {
texture = atlas_tex->get_atlas();
} else if (node_sprite_2d) {
texture = node_sprite_2d->get_texture();
+ filter = node_sprite_2d->get_texture_filter_in_tree();
} else if (node_sprite_3d) {
texture = node_sprite_3d->get_texture();
+
+ StandardMaterial3D::TextureFilter filter_3d = node_sprite_3d->get_texture_filter();
+
+ switch (filter_3d) {
+ case StandardMaterial3D::TEXTURE_FILTER_NEAREST:
+ filter = CanvasItem::TEXTURE_FILTER_NEAREST;
+ break;
+ case StandardMaterial3D::TEXTURE_FILTER_LINEAR:
+ filter = CanvasItem::TEXTURE_FILTER_LINEAR;
+ break;
+ case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
+ filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
+ break;
+ case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
+ filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
+ break;
+ case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC:
+ filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC;
+ break;
+ case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC:
+ filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC;
+ break;
+ default:
+ // fallback to project default
+ filter = CanvasItem::TEXTURE_FILTER_PARENT_NODE;
+ break;
+ }
} else if (node_ninepatch) {
texture = node_ninepatch->get_texture();
+ filter = node_ninepatch->get_texture_filter_in_tree();
} else if (obj_styleBox.is_valid()) {
texture = obj_styleBox->get_texture();
}
+ // occurs when get_texture_filter_in_tree reaches the scene root
+ if (filter == CanvasItem::TEXTURE_FILTER_PARENT_NODE) {
+ SubViewport *root = EditorNode::get_singleton()->get_scene_root();
+
+ if (root != nullptr) {
+ Viewport::DefaultCanvasItemTextureFilter filter_default = root->get_default_canvas_item_texture_filter();
+
+ // depending on default filter, set filter to match, otherwise fall back on nearest w/ mipmaps
+ switch (filter_default) {
+ case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST:
+ filter = CanvasItem::TEXTURE_FILTER_NEAREST;
+ break;
+ case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR:
+ filter = CanvasItem::TEXTURE_FILTER_LINEAR;
+ break;
+ case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
+ filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
+ break;
+ case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
+ default:
+ filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
+ break;
+ }
+ } else {
+ filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
+ }
+ }
+
if (texture.is_null()) {
+ preview_tex->set_diffuse_texture(nullptr);
_zoom_reset();
hscroll->hide();
vscroll->hide();
@@ -967,6 +1029,9 @@ void TextureRegionEditor::_edit_region() {
return;
}
+ preview_tex->set_texture_filter(filter);
+ preview_tex->set_diffuse_texture(texture);
+
if (cache_map.has(texture->get_rid())) {
autoslice_cache = cache_map[texture->get_rid()];
autoslice_is_dirty = false;
@@ -1002,6 +1067,8 @@ TextureRegionEditor::TextureRegionEditor() {
atlas_tex = Ref<AtlasTexture>(nullptr);
undo_redo = EditorNode::get_singleton()->get_undo_redo();
+ preview_tex = Ref<CanvasTexture>(memnew(CanvasTexture));
+
snap_step = Vector2(10, 10);
snap_separation = Vector2(0, 0);
snap_mode = SNAP_NONE;
diff --git a/editor/plugins/texture_region_editor_plugin.h b/editor/plugins/texture_region_editor_plugin.h
index e3bbaf49fc..7eda4f469f 100644
--- a/editor/plugins/texture_region_editor_plugin.h
+++ b/editor/plugins/texture_region_editor_plugin.h
@@ -86,6 +86,8 @@ class TextureRegionEditor : public AcceptDialog {
Ref<StyleBoxTexture> obj_styleBox;
Ref<AtlasTexture> atlas_tex;
+ Ref<CanvasTexture> preview_tex;
+
Rect2 rect;
Rect2 rect_prev;
float prev_margin = 0.0f;