Path: blob/master/editor/scene/texture/texture_editor_plugin.cpp
20829 views
/**************************************************************************/1/* texture_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "texture_editor_plugin.h"3132#include "editor/editor_string_names.h"33#include "editor/scene/texture/color_channel_selector.h"34#include "editor/themes/editor_scale.h"35#include "scene/gui/aspect_ratio_container.h"36#include "scene/gui/color_rect.h"37#include "scene/gui/label.h"38#include "scene/gui/texture_rect.h"39#include "scene/resources/animated_texture.h"40#include "scene/resources/atlas_texture.h"41#include "scene/resources/compressed_texture.h"42#include "scene/resources/dpi_texture.h"43#include "scene/resources/image_texture.h"44#include "scene/resources/material.h"45#include "scene/resources/portable_compressed_texture.h"4647constexpr const char *texture_2d_shader_code = R"(48shader_type canvas_item;49render_mode blend_mix;5051instance uniform vec4 u_channel_factors = vec4(1.0);5253vec4 filter_preview_colors(vec4 input_color, vec4 factors) {54// Filter RGB.55vec4 output_color = input_color * vec4(factors.rgb, input_color.a);5657// Remove transparency when alpha is not enabled.58output_color.a = mix(1.0, output_color.a, factors.a);5960// Switch to opaque grayscale when visualizing only one channel.61float csum = factors.r + factors.g + factors.b + factors.a;62float single = clamp(2.0 - csum, 0.0, 1.0);63for (int i = 0; i < 4; i++) {64float c = input_color[i];65output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single);66}6768return output_color;69}7071void fragment() {72COLOR = filter_preview_colors(texture(TEXTURE, UV), u_channel_factors);73}74)";7576void TexturePreview::init_shaders() {77texture_material.instantiate();7879Ref<Shader> texture_shader;80texture_shader.instantiate();81texture_shader->set_code(texture_2d_shader_code);8283texture_material->set_shader(texture_shader);84}8586void TexturePreview::finish_shaders() {87texture_material.unref();88}8990TextureRect *TexturePreview::get_texture_display() {91return texture_display;92}9394void TexturePreview::_notification(int p_what) {95switch (p_what) {96case NOTIFICATION_THEME_CHANGED: {97if (!is_inside_tree()) {98// TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED`99// is getting called for some reason when the `TexturePreview` is100// getting destroyed, which causes `get_theme_font()` to return `nullptr`.101// See https://github.com/godotengine/godot/issues/50743.102break;103}104105if (metadata_label) {106Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));107metadata_label->add_theme_font_override(SceneStringName(font), metadata_label_font);108}109110bg_rect->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor)));111checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard")));112theme_cache.outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor));113} break;114}115}116117void TexturePreview::_draw_outline() {118const float outline_width = Math::round(EDSCALE);119const Rect2 outline_rect = Rect2(Vector2(), outline_overlay->get_size()).grow(outline_width * 0.5);120outline_overlay->draw_rect(outline_rect, theme_cache.outline_color, false, outline_width);121}122123void TexturePreview::_update_texture_display_ratio() {124if (texture_display->get_texture().is_valid()) {125centering_container->set_ratio(texture_display->get_texture()->get_size().aspect());126}127}128129static Image::Format get_texture_2d_format(const Ref<Texture2D> &p_texture) {130const Ref<ImageTexture> image_texture = p_texture;131if (image_texture.is_valid()) {132return image_texture->get_format();133}134135const Ref<CompressedTexture2D> compressed_texture = p_texture;136if (compressed_texture.is_valid()) {137return compressed_texture->get_format();138}139140const Ref<PortableCompressedTexture2D> portable_compressed_texture = p_texture;141if (portable_compressed_texture.is_valid()) {142return portable_compressed_texture->get_format();143}144145// AtlasTexture?146147// Unknown148return Image::FORMAT_MAX;149}150151static int get_texture_mipmaps_count(const Ref<Texture2D> &p_texture) {152ERR_FAIL_COND_V(p_texture.is_null(), -1);153154// We are having to download the image only to get its mipmaps count. It would be nice if we didn't have to.155Ref<Image> image;156Ref<AtlasTexture> at = p_texture;157if (at.is_valid()) {158// The AtlasTexture tries to obtain the region from the atlas as an image,159// which will fail if it is a compressed format.160Ref<Texture2D> atlas = at->get_atlas();161if (atlas.is_valid()) {162image = atlas->get_image();163}164} else {165image = p_texture->get_image();166}167168if (image.is_valid()) {169return image->get_mipmap_count();170}171return -1;172}173174void TexturePreview::_update_metadata_label_text() {175const Ref<Texture2D> texture = texture_display->get_texture();176ERR_FAIL_COND(texture.is_null());177178const Image::Format format = get_texture_2d_format(texture.ptr());179180const String format_name = format != Image::FORMAT_MAX ? Image::get_format_name(format) : texture->get_class();181182const Vector2i resolution = texture->get_size();183const int mipmaps = get_texture_mipmaps_count(texture);184185if (format != Image::FORMAT_MAX) {186// Avoid signed integer overflow that could occur with huge texture sizes by casting everything to uint64_t.187uint64_t memory = uint64_t(resolution.x) * uint64_t(resolution.y) * uint64_t(Image::get_format_pixel_size(format));188// Handle VRAM-compressed formats that are stored with 4 bpp.189memory >>= Image::get_format_pixel_rshift(format);190191float mipmaps_multiplier = 1.0;192float mipmap_increase = 0.25;193for (int i = 0; i < mipmaps; i++) {194// Each mip adds 25% memory usage of the previous one.195// With a complete mipmap chain, memory usage increases by ~33%.196mipmaps_multiplier += mipmap_increase;197mipmap_increase *= 0.25;198}199memory *= mipmaps_multiplier;200201if (mipmaps >= 1) {202metadata_label->set_text(203vformat(String::utf8("%d×%d %s\n") + TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),204texture->get_width(),205texture->get_height(),206format_name,207mipmaps,208String::humanize_size(memory)));209} else {210// "No Mipmaps" is easier to distinguish than "0 Mipmaps",211// especially since 0, 6, and 8 look quite close with the default code font.212metadata_label->set_text(213vformat(String::utf8("%d×%d %s\n") + TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),214texture->get_width(),215texture->get_height(),216format_name,217String::humanize_size(memory)));218}219} else {220metadata_label->set_text(221vformat(String::utf8("%d×%d %s"),222texture->get_width(),223texture->get_height(),224format_name));225}226}227228void TexturePreview::on_selected_channels_changed() {229texture_display->set_instance_shader_parameter("u_channel_factors", channel_selector->get_selected_channel_factors());230}231232TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {233set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);234235bg_rect = memnew(ColorRect);236237add_child(bg_rect);238239margin_container = memnew(MarginContainer);240const float outline_width = Math::round(EDSCALE);241margin_container->add_theme_constant_override("margin_right", outline_width);242margin_container->add_theme_constant_override("margin_top", outline_width);243margin_container->add_theme_constant_override("margin_left", outline_width);244margin_container->add_theme_constant_override("margin_bottom", outline_width);245add_child(margin_container);246247centering_container = memnew(AspectRatioContainer);248margin_container->add_child(centering_container);249250checkerboard = memnew(TextureRect);251checkerboard->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);252checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);253checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);254centering_container->add_child(checkerboard);255256texture_display = memnew(TextureRect);257texture_display->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);258texture_display->set_texture(p_texture);259texture_display->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);260texture_display->set_material(texture_material);261texture_display->set_instance_shader_parameter("u_channel_factors", Vector4(1, 1, 1, 1));262centering_container->add_child(texture_display);263264// Creating a separate control so it is not affected by the filtering shader.265outline_overlay = memnew(Control);266centering_container->add_child(outline_overlay);267268outline_overlay->connect(SceneStringName(draw), callable_mp(this, &TexturePreview::_draw_outline));269270if (p_texture.is_valid()) {271_update_texture_display_ratio();272p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_texture_display_ratio));273}274275// Null can be passed by `Camera3DPreview` (which immediately after sets a texture anyways).276const Image::Format format = p_texture.is_valid() ? get_texture_2d_format(p_texture.ptr()) : Image::FORMAT_MAX;277const uint32_t components_mask = format != Image::FORMAT_MAX ? Image::get_format_component_mask(format) : 0xf;278279// Add color channel selector at the bottom left if more than 1 channel is available.280if (p_show_metadata && !is_power_of_2(components_mask)) {281channel_selector = memnew(ColorChannelSelector);282channel_selector->connect("selected_channels_changed", callable_mp(this, &TexturePreview::on_selected_channels_changed));283channel_selector->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);284channel_selector->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);285channel_selector->set_available_channels_mask(components_mask);286add_child(channel_selector);287}288289if (p_show_metadata) {290metadata_label = memnew(Label);291metadata_label->set_focus_mode(FOCUS_ACCESSIBILITY);292293if (p_texture.is_valid()) {294_update_metadata_label_text();295p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text));296}297298// It's okay that these colors are static since the grid color is static too.299metadata_label->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));300metadata_label->add_theme_color_override("font_shadow_color", Color(0, 0, 0));301302metadata_label->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE);303metadata_label->add_theme_color_override("font_outline_color", Color(0, 0, 0));304metadata_label->add_theme_constant_override("outline_size", 8 * EDSCALE);305306metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END);307metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END);308309add_child(metadata_label);310}311}312313bool EditorInspectorPluginTexture::can_handle(Object *p_object) {314return Object::cast_to<ImageTexture>(p_object) != nullptr || Object::cast_to<AtlasTexture>(p_object) != nullptr || Object::cast_to<CompressedTexture2D>(p_object) != nullptr || Object::cast_to<PortableCompressedTexture2D>(p_object) != nullptr || Object::cast_to<AnimatedTexture>(p_object) != nullptr || Object::cast_to<DPITexture>(p_object) != nullptr || Object::cast_to<Image>(p_object) != nullptr;315}316317void EditorInspectorPluginTexture::parse_begin(Object *p_object) {318Ref<Texture> texture(Object::cast_to<Texture>(p_object));319if (texture.is_null()) {320Ref<Image> image(Object::cast_to<Image>(p_object));321texture = ImageTexture::create_from_image(image);322323ERR_FAIL_COND_MSG(texture.is_null(), "Failed to create the texture from an invalid image.");324}325326add_custom_control(memnew(TexturePreview(texture, true)));327}328329TextureEditorPlugin::TextureEditorPlugin() {330Ref<EditorInspectorPluginTexture> plugin;331plugin.instantiate();332add_inspector_plugin(plugin);333}334335336