| 1 | /**************************************************************************/ |
| 2 | /* texture_editor_plugin.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "texture_editor_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_string_names.h" |
| 34 | #include "editor/scene/texture/color_channel_selector.h" |
| 35 | #include "editor/themes/editor_scale.h" |
| 36 | #include "scene/gui/aspect_ratio_container.h" |
| 37 | #include "scene/gui/color_rect.h" |
| 38 | #include "scene/gui/label.h" |
| 39 | #include "scene/gui/texture_rect.h" |
| 40 | #include "scene/resources/animated_texture.h" |
| 41 | #include "scene/resources/atlas_texture.h" |
| 42 | #include "scene/resources/compressed_texture.h" |
| 43 | #include "scene/resources/dpi_texture.h" |
| 44 | #include "scene/resources/image_texture.h" |
| 45 | #include "scene/resources/portable_compressed_texture.h" |
| 46 | |
| 47 | constexpr const char *texture_2d_shader_code = R"( |
| 48 | shader_type canvas_item; |
| 49 | render_mode blend_mix; |
| 50 | |
| 51 | instance uniform vec4 u_channel_factors = vec4(1.0); |
| 52 | |
| 53 | vec4 filter_preview_colors(vec4 input_color, vec4 factors) { |
| 54 | // Filter RGB. |
| 55 | vec4 output_color = input_color * vec4(factors.rgb, input_color.a); |
| 56 | |
| 57 | // Remove transparency when alpha is not enabled. |
| 58 | output_color.a = mix(1.0, output_color.a, factors.a); |
| 59 | |
| 60 | // Switch to opaque grayscale when visualizing only one channel. |
| 61 | float csum = factors.r + factors.g + factors.b + factors.a; |
| 62 | float single = clamp(2.0 - csum, 0.0, 1.0); |
| 63 | for (int i = 0; i < 4; i++) { |
| 64 | float c = input_color[i]; |
| 65 | output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single); |
| 66 | } |
| 67 | |
| 68 | return output_color; |
| 69 | } |
| 70 | |
| 71 | void fragment() { |
| 72 | COLOR = filter_preview_colors(texture(TEXTURE, UV), u_channel_factors); |
| 73 | } |
| 74 | )"; |
| 75 | |
| 76 | void TexturePreview::init_shaders() { |
| 77 | texture_material.instantiate(); |
| 78 | |
| 79 | Ref<Shader> texture_shader; |
| 80 | texture_shader.instantiate(); |
| 81 | texture_shader->set_code(texture_2d_shader_code); |
| 82 | |
| 83 | texture_material->set_shader(texture_shader); |
| 84 | } |
| 85 | |
| 86 | void TexturePreview::finish_shaders() { |
| 87 | texture_material.unref(); |
| 88 | } |
| 89 | |
| 90 | TextureRect *TexturePreview::get_texture_display() { |
| 91 | return texture_display; |
| 92 | } |
| 93 | |
| 94 | void TexturePreview::_notification(int p_what) { |
| 95 | switch (p_what) { |
| 96 | case NOTIFICATION_THEME_CHANGED: { |
| 97 | if (!is_inside_tree()) { |
| 98 | // TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED` |
| 99 | // is getting called for some reason when the `TexturePreview` is |
| 100 | // getting destroyed, which causes `get_theme_font()` to return `nullptr`. |
| 101 | // See https://github.com/godotengine/godot/issues/50743. |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | if (metadata_label) { |
| 106 | Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts)); |
| 107 | metadata_label->add_theme_font_override(SceneStringName(font), metadata_label_font); |
| 108 | } |
| 109 | |
| 110 | bg_rect->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor))); |
| 111 | checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard"))); |
| 112 | theme_cache.outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor)); |
| 113 | } break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void TexturePreview::_draw_outline() { |
| 118 | const float outline_width = Math::round(EDSCALE); |
| 119 | const Rect2 outline_rect = Rect2(Vector2(), outline_overlay->get_size()).grow(outline_width * 0.5); |
| 120 | outline_overlay->draw_rect(outline_rect, theme_cache.outline_color, false, outline_width); |
| 121 | } |
| 122 | |
| 123 | void TexturePreview::_update_texture_display_ratio() { |
| 124 | if (texture_display->get_texture().is_valid()) { |
| 125 | centering_container->set_ratio(texture_display->get_texture()->get_size().aspect()); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static Image::Format get_texture_2d_format(const Ref<Texture2D> &p_texture) { |
| 130 | const Ref<ImageTexture> image_texture = p_texture; |
| 131 | if (image_texture.is_valid()) { |
| 132 | return image_texture->get_format(); |
| 133 | } |
| 134 | |
| 135 | const Ref<CompressedTexture2D> compressed_texture = p_texture; |
| 136 | if (compressed_texture.is_valid()) { |
| 137 | return compressed_texture->get_format(); |
| 138 | } |
| 139 | |
| 140 | const Ref<PortableCompressedTexture2D> portable_compressed_texture = p_texture; |
| 141 | if (portable_compressed_texture.is_valid()) { |
| 142 | return portable_compressed_texture->get_format(); |
| 143 | } |
| 144 | |
| 145 | // AtlasTexture? |
| 146 | |
| 147 | // Unknown |
| 148 | return Image::FORMAT_MAX; |
| 149 | } |
| 150 | |
| 151 | static int get_texture_mipmaps_count(const Ref<Texture2D> &p_texture) { |
| 152 | ERR_FAIL_COND_V(p_texture.is_null(), -1); |
| 153 | |
| 154 | // We are having to download the image only to get its mipmaps count. It would be nice if we didn't have to. |
| 155 | Ref<Image> image; |
| 156 | Ref<AtlasTexture> at = p_texture; |
| 157 | if (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. |
| 160 | Ref<Texture2D> atlas = at->get_atlas(); |
| 161 | if (atlas.is_valid()) { |
| 162 | image = atlas->get_image(); |
| 163 | } |
| 164 | } else { |
| 165 | image = p_texture->get_image(); |
| 166 | } |
| 167 | |
| 168 | if (image.is_valid()) { |
| 169 | return image->get_mipmap_count(); |
| 170 | } |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | void TexturePreview::_update_metadata_label_text() { |
| 175 | const Ref<Texture2D> texture = texture_display->get_texture(); |
| 176 | ERR_FAIL_COND(texture.is_null()); |
| 177 | |
| 178 | const Image::Format format = get_texture_2d_format(texture.ptr()); |
| 179 | |
| 180 | const String format_name = format != Image::FORMAT_MAX ? Image::get_format_name(format) : texture->get_class(); |
| 181 | |
| 182 | const Vector2i resolution = texture->get_size(); |
| 183 | const int mipmaps = get_texture_mipmaps_count(texture); |
| 184 | |
| 185 | if (format != Image::FORMAT_MAX) { |
| 186 | // Avoid signed integer overflow that could occur with huge texture sizes by casting everything to uint64_t. |
| 187 | uint64_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. |
| 189 | memory >>= Image::get_format_pixel_rshift(format); |
| 190 | |
| 191 | float mipmaps_multiplier = 1.0; |
| 192 | float mipmap_increase = 0.25; |
| 193 | for (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%. |
| 196 | mipmaps_multiplier += mipmap_increase; |
| 197 | mipmap_increase *= 0.25; |
| 198 | } |
| 199 | memory *= mipmaps_multiplier; |
| 200 | |
| 201 | if (mipmaps >= 1) { |
| 202 | metadata_label->set_text( |
| 203 | vformat(String::utf8("%d×%d %s\n") + TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"), |
| 204 | texture->get_width(), |
| 205 | texture->get_height(), |
| 206 | format_name, |
| 207 | mipmaps, |
| 208 | String::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. |
| 212 | metadata_label->set_text( |
| 213 | vformat(String::utf8("%d×%d %s\n") + TTR("No Mipmaps") + "\n" + TTR("Memory: %s"), |
| 214 | texture->get_width(), |
| 215 | texture->get_height(), |
| 216 | format_name, |
| 217 | String::humanize_size(memory))); |
| 218 | } |
| 219 | } else { |
| 220 | metadata_label->set_text( |
| 221 | vformat(String::utf8("%d×%d %s"), |
| 222 | texture->get_width(), |
| 223 | texture->get_height(), |
| 224 | format_name)); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void TexturePreview::on_selected_channels_changed() { |
| 229 | texture_display->set_instance_shader_parameter("u_channel_factors", channel_selector->get_selected_channel_factors()); |
| 230 | } |
| 231 | |
| 232 | TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) { |
| 233 | set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE); |
| 234 | |
| 235 | bg_rect = memnew(ColorRect); |
| 236 | |
| 237 | add_child(bg_rect); |
| 238 | |
| 239 | margin_container = memnew(MarginContainer); |
| 240 | const float outline_width = Math::round(EDSCALE); |
| 241 | margin_container->add_theme_constant_override("margin_right", outline_width); |
| 242 | margin_container->add_theme_constant_override("margin_top", outline_width); |
| 243 | margin_container->add_theme_constant_override("margin_left", outline_width); |
| 244 | margin_container->add_theme_constant_override("margin_bottom", outline_width); |
| 245 | add_child(margin_container); |
| 246 | |
| 247 | centering_container = memnew(AspectRatioContainer); |
| 248 | margin_container->add_child(centering_container); |
| 249 | |
| 250 | checkerboard = memnew(TextureRect); |
| 251 | checkerboard->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); |
| 252 | checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE); |
| 253 | checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED); |
| 254 | centering_container->add_child(checkerboard); |
| 255 | |
| 256 | texture_display = memnew(TextureRect); |
| 257 | texture_display->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS); |
| 258 | texture_display->set_texture(p_texture); |
| 259 | texture_display->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); |
| 260 | texture_display->set_material(texture_material); |
| 261 | texture_display->set_instance_shader_parameter("u_channel_factors", Vector4(1, 1, 1, 1)); |
| 262 | centering_container->add_child(texture_display); |
| 263 | |
| 264 | // Creating a separate control so it is not affected by the filtering shader. |
| 265 | outline_overlay = memnew(Control); |
| 266 | centering_container->add_child(outline_overlay); |
| 267 | |
| 268 | outline_overlay->connect(SceneStringName(draw), callable_mp(this, &TexturePreview::_draw_outline)); |
| 269 | |
| 270 | if (p_texture.is_valid()) { |
| 271 | _update_texture_display_ratio(); |
| 272 | p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_texture_display_ratio)); |
| 273 | } |
| 274 | |
| 275 | // Null can be passed by `Camera3DPreview` (which immediately after sets a texture anyways). |
| 276 | const Image::Format format = p_texture.is_valid() ? get_texture_2d_format(p_texture.ptr()) : Image::FORMAT_MAX; |
| 277 | const uint32_t components_mask = format != Image::FORMAT_MAX ? Image::get_format_component_mask(format) : 0xf; |
| 278 | |
| 279 | // Add color channel selector at the bottom left if more than 1 channel is available. |
| 280 | if (p_show_metadata && !is_power_of_2(components_mask)) { |
| 281 | channel_selector = memnew(ColorChannelSelector); |
| 282 | channel_selector->connect("selected_channels_changed", callable_mp(this, &TexturePreview::on_selected_channels_changed)); |
| 283 | channel_selector->set_h_size_flags(Control::SIZE_SHRINK_BEGIN); |
| 284 | channel_selector->set_v_size_flags(Control::SIZE_SHRINK_BEGIN); |
| 285 | channel_selector->set_available_channels_mask(components_mask); |
| 286 | add_child(channel_selector); |
| 287 | } |
| 288 | |
| 289 | if (p_show_metadata) { |
| 290 | metadata_label = memnew(Label); |
| 291 | metadata_label->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 292 | |
| 293 | if (p_texture.is_valid()) { |
| 294 | _update_metadata_label_text(); |
| 295 | p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text)); |
| 296 | } |
| 297 | |
| 298 | // It's okay that these colors are static since the grid color is static too. |
| 299 | metadata_label->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1)); |
| 300 | metadata_label->add_theme_color_override("font_shadow_color", Color(0, 0, 0)); |
| 301 | |
| 302 | metadata_label->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE); |
| 303 | metadata_label->add_theme_color_override("font_outline_color", Color(0, 0, 0)); |
| 304 | metadata_label->add_theme_constant_override("outline_size", 8 * EDSCALE); |
| 305 | |
| 306 | metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END); |
| 307 | metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END); |
| 308 | |
| 309 | add_child(metadata_label); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | bool EditorInspectorPluginTexture::can_handle(Object *p_object) { |
| 314 | return 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 | } |
| 316 | |
| 317 | void EditorInspectorPluginTexture::parse_begin(Object *p_object) { |
| 318 | Ref<Texture> texture(Object::cast_to<Texture>(p_object)); |
| 319 | if (texture.is_null()) { |
| 320 | Ref<Image> image(Object::cast_to<Image>(p_object)); |
| 321 | texture = ImageTexture::create_from_image(image); |
| 322 | |
| 323 | ERR_FAIL_COND_MSG(texture.is_null(), "Failed to create the texture from an invalid image."); |
| 324 | } |
| 325 | |
| 326 | add_custom_control(memnew(TexturePreview(texture, true))); |
| 327 | } |
| 328 | |
| 329 | TextureEditorPlugin::TextureEditorPlugin() { |
| 330 | Ref<EditorInspectorPluginTexture> plugin; |
| 331 | plugin.instantiate(); |
| 332 | add_inspector_plugin(plugin); |
| 333 | } |