Path: blob/master/editor/scene/texture/texture_editor_plugin.cpp
9903 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/image_texture.h"43#include "scene/resources/portable_compressed_texture.h"44#include "scene/resources/style_box_flat.h"4546constexpr const char *texture_2d_shader = R"(47shader_type canvas_item;48render_mode blend_mix;4950uniform vec4 u_channel_factors = vec4(1.0);5152vec4 filter_preview_colors(vec4 input_color, vec4 factors) {53// Filter RGB.54vec4 output_color = input_color * vec4(factors.rgb, input_color.a);5556// Remove transparency when alpha is not enabled.57output_color.a = mix(1.0, output_color.a, factors.a);5859// Switch to opaque grayscale when visualizing only one channel.60float csum = factors.r + factors.g + factors.b + factors.a;61float single = clamp(2.0 - csum, 0.0, 1.0);62for (int i = 0; i < 4; i++) {63float c = input_color[i];64output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single);65}6667return output_color;68}6970void fragment() {71COLOR = filter_preview_colors(texture(TEXTURE, UV), u_channel_factors);72}73)";7475TextureRect *TexturePreview::get_texture_display() {76return texture_display;77}7879void TexturePreview::_notification(int p_what) {80switch (p_what) {81case NOTIFICATION_THEME_CHANGED: {82if (!is_inside_tree()) {83// TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED`84// is getting called for some reason when the `TexturePreview` is85// getting destroyed, which causes `get_theme_font()` to return `nullptr`.86// See https://github.com/godotengine/godot/issues/50743.87break;88}8990if (metadata_label) {91Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));92metadata_label->add_theme_font_override(SceneStringName(font), metadata_label_font);93}9495bg_rect->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor)));96checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard")));97theme_cache.outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor));98} break;99}100}101102void TexturePreview::_draw_outline() {103const float outline_width = Math::round(EDSCALE);104const Rect2 outline_rect = Rect2(Vector2(), outline_overlay->get_size()).grow(outline_width * 0.5);105outline_overlay->draw_rect(outline_rect, theme_cache.outline_color, false, outline_width);106}107108void TexturePreview::_update_texture_display_ratio() {109if (texture_display->get_texture().is_valid()) {110centering_container->set_ratio(texture_display->get_texture()->get_size().aspect());111}112}113114static Image::Format get_texture_2d_format(const Ref<Texture2D> &p_texture) {115const Ref<ImageTexture> image_texture = p_texture;116if (image_texture.is_valid()) {117return image_texture->get_format();118}119120const Ref<CompressedTexture2D> compressed_texture = p_texture;121if (compressed_texture.is_valid()) {122return compressed_texture->get_format();123}124125const Ref<PortableCompressedTexture2D> portable_compressed_texture = p_texture;126if (portable_compressed_texture.is_valid()) {127return portable_compressed_texture->get_format();128}129130// AtlasTexture?131132// Unknown133return Image::FORMAT_MAX;134}135136static int get_texture_mipmaps_count(const Ref<Texture2D> &p_texture) {137ERR_FAIL_COND_V(p_texture.is_null(), -1);138139// We are having to download the image only to get its mipmaps count. It would be nice if we didn't have to.140Ref<Image> image;141Ref<AtlasTexture> at = p_texture;142if (at.is_valid()) {143// The AtlasTexture tries to obtain the region from the atlas as an image,144// which will fail if it is a compressed format.145Ref<Texture2D> atlas = at->get_atlas();146if (atlas.is_valid()) {147image = atlas->get_image();148}149} else {150image = p_texture->get_image();151}152153if (image.is_valid()) {154return image->get_mipmap_count();155}156return -1;157}158159void TexturePreview::_update_metadata_label_text() {160const Ref<Texture2D> texture = texture_display->get_texture();161ERR_FAIL_COND(texture.is_null());162163const Image::Format format = get_texture_2d_format(texture.ptr());164165const String format_name = format != Image::FORMAT_MAX ? Image::get_format_name(format) : texture->get_class();166167const Vector2i resolution = texture->get_size();168const int mipmaps = get_texture_mipmaps_count(texture);169170if (format != Image::FORMAT_MAX) {171// Avoid signed integer overflow that could occur with huge texture sizes by casting everything to uint64_t.172uint64_t memory = uint64_t(resolution.x) * uint64_t(resolution.y) * uint64_t(Image::get_format_pixel_size(format));173// Handle VRAM-compressed formats that are stored with 4 bpp.174memory >>= Image::get_format_pixel_rshift(format);175176float mipmaps_multiplier = 1.0;177float mipmap_increase = 0.25;178for (int i = 0; i < mipmaps; i++) {179// Each mip adds 25% memory usage of the previous one.180// With a complete mipmap chain, memory usage increases by ~33%.181mipmaps_multiplier += mipmap_increase;182mipmap_increase *= 0.25;183}184memory *= mipmaps_multiplier;185186if (mipmaps >= 1) {187metadata_label->set_text(188vformat(String::utf8("%d×%d %s\n") + TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),189texture->get_width(),190texture->get_height(),191format_name,192mipmaps,193String::humanize_size(memory)));194} else {195// "No Mipmaps" is easier to distinguish than "0 Mipmaps",196// especially since 0, 6, and 8 look quite close with the default code font.197metadata_label->set_text(198vformat(String::utf8("%d×%d %s\n") + TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),199texture->get_width(),200texture->get_height(),201format_name,202String::humanize_size(memory)));203}204} else {205metadata_label->set_text(206vformat(String::utf8("%d×%d %s"),207texture->get_width(),208texture->get_height(),209format_name));210}211}212213void TexturePreview::on_selected_channels_changed() {214material->set_shader_parameter("u_channel_factors", channel_selector->get_selected_channel_factors());215}216217TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {218set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);219220bg_rect = memnew(ColorRect);221222add_child(bg_rect);223224margin_container = memnew(MarginContainer);225const float outline_width = Math::round(EDSCALE);226margin_container->add_theme_constant_override("margin_right", outline_width);227margin_container->add_theme_constant_override("margin_top", outline_width);228margin_container->add_theme_constant_override("margin_left", outline_width);229margin_container->add_theme_constant_override("margin_bottom", outline_width);230add_child(margin_container);231232centering_container = memnew(AspectRatioContainer);233margin_container->add_child(centering_container);234235checkerboard = memnew(TextureRect);236checkerboard->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);237checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);238checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);239centering_container->add_child(checkerboard);240241{242Ref<Shader> shader;243shader.instantiate();244shader->set_code(texture_2d_shader);245246material.instantiate();247material->set_shader(shader);248material->set_shader_parameter("u_channel_factors", Vector4(1, 1, 1, 1));249}250251texture_display = memnew(TextureRect);252texture_display->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);253texture_display->set_texture(p_texture);254texture_display->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);255texture_display->set_material(material);256centering_container->add_child(texture_display);257258// Creating a separate control so it is not affected by the filtering shader.259outline_overlay = memnew(Control);260centering_container->add_child(outline_overlay);261262outline_overlay->connect(SceneStringName(draw), callable_mp(this, &TexturePreview::_draw_outline));263264if (p_texture.is_valid()) {265_update_texture_display_ratio();266p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_texture_display_ratio));267}268269// Null can be passed by `Camera3DPreview` (which immediately after sets a texture anyways).270const Image::Format format = p_texture.is_valid() ? get_texture_2d_format(p_texture.ptr()) : Image::FORMAT_MAX;271const uint32_t components_mask = format != Image::FORMAT_MAX ? Image::get_format_component_mask(format) : 0xf;272273// Add color channel selector at the bottom left if more than 1 channel is available.274if (p_show_metadata && !is_power_of_2(components_mask)) {275channel_selector = memnew(ColorChannelSelector);276channel_selector->connect("selected_channels_changed", callable_mp(this, &TexturePreview::on_selected_channels_changed));277channel_selector->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);278channel_selector->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);279channel_selector->set_available_channels_mask(components_mask);280add_child(channel_selector);281}282283if (p_show_metadata) {284metadata_label = memnew(Label);285metadata_label->set_focus_mode(FOCUS_ACCESSIBILITY);286287if (p_texture.is_valid()) {288_update_metadata_label_text();289p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text));290}291292// It's okay that these colors are static since the grid color is static too.293metadata_label->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));294metadata_label->add_theme_color_override("font_shadow_color", Color(0, 0, 0));295296metadata_label->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE);297metadata_label->add_theme_color_override("font_outline_color", Color(0, 0, 0));298metadata_label->add_theme_constant_override("outline_size", 8 * EDSCALE);299300metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END);301metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END);302303add_child(metadata_label);304}305}306307bool EditorInspectorPluginTexture::can_handle(Object *p_object) {308return 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<Image>(p_object) != nullptr;309}310311void EditorInspectorPluginTexture::parse_begin(Object *p_object) {312Ref<Texture> texture(Object::cast_to<Texture>(p_object));313if (texture.is_null()) {314Ref<Image> image(Object::cast_to<Image>(p_object));315texture = ImageTexture::create_from_image(image);316317ERR_FAIL_COND_MSG(texture.is_null(), "Failed to create the texture from an invalid image.");318}319320add_custom_control(memnew(TexturePreview(texture, true)));321}322323TextureEditorPlugin::TextureEditorPlugin() {324Ref<EditorInspectorPluginTexture> plugin;325plugin.instantiate();326add_inspector_plugin(plugin);327}328329330