From 0e948fa7e0b23266d2e67a7d09c39ae37699cbe4 Mon Sep 17 00:00:00 2001 From: BlueCube3310 <53150244+BlueCube3310@users.noreply.github.com> Date: Sun, 21 Jul 2024 22:11:43 +0200 Subject: [PATCH] Add image previews for 3D and layered textures --- editor/plugins/editor_preview_plugins.cpp | 42 +++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp index 7ac924571d36..a2c36b1f3ce6 100644 --- a/editor/plugins/editor_preview_plugins.cpp +++ b/editor/plugins/editor_preview_plugins.cpp @@ -76,7 +76,7 @@ void post_process_preview(Ref p_image) { } bool EditorTexturePreviewPlugin::handles(const String &p_type) const { - return ClassDB::is_parent_class(p_type, "Texture2D"); + return ClassDB::is_parent_class(p_type, "Texture2D") || ClassDB::is_parent_class(p_type, "Texture3D") || ClassDB::is_parent_class(p_type, "TextureLayered"); } bool EditorTexturePreviewPlugin::generate_small_preview_automatically() const { @@ -85,9 +85,13 @@ bool EditorTexturePreviewPlugin::generate_small_preview_automatically() const { Ref EditorTexturePreviewPlugin::generate(const Ref &p_from, const Size2 &p_size, Dictionary &p_metadata) const { Ref img; - Ref atex = p_from; - if (atex.is_valid()) { - Ref tex = atex->get_atlas(); + + Ref tex_atlas = p_from; + Ref tex_3d = p_from; + Ref tex_lyr = p_from; + + if (tex_atlas.is_valid()) { + Ref tex = tex_atlas->get_atlas(); if (!tex.is_valid()) { return Ref(); } @@ -97,11 +101,36 @@ Ref EditorTexturePreviewPlugin::generate(const Ref &p_from, return Ref(); } - if (!atex->get_region().has_area()) { + if (!tex_atlas->get_region().has_area()) { + return Ref(); + } + + img = atlas->get_region(tex_atlas->get_region()); + + } else if (tex_3d.is_valid()) { + if (tex_3d->get_depth() == 0) { + return Ref(); + } + + const int mid_depth = (tex_3d->get_depth() - 1) / 2; + + Vector> data = tex_3d->get_data(); + if (!data.is_empty() && data[mid_depth].is_valid()) { + img = data[mid_depth]->duplicate(); + } + + } else if (tex_lyr.is_valid()) { + if (tex_lyr->get_layers() == 0) { return Ref(); } - img = atlas->get_region(atex->get_region()); + const int mid_layer = (tex_lyr->get_layers() - 1) / 2; + + Ref data = tex_lyr->get_layer_data(mid_layer); + if (data.is_valid()) { + img = data->duplicate(); + } + } else { Ref tex = p_from; if (tex.is_valid()) { @@ -115,6 +144,7 @@ Ref EditorTexturePreviewPlugin::generate(const Ref &p_from, if (img.is_null() || img->is_empty()) { return Ref(); } + p_metadata["dimensions"] = img->get_size(); img->clear_mipmaps();