texture_editor_plugin.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include "texture_editor_plugin.h"
  31. #include "editor/editor_string_names.h"
  32. #include "editor/plugins/color_channel_selector.h"
  33. #include "editor/themes/editor_scale.h"
  34. #include "scene/gui/aspect_ratio_container.h"
  35. #include "scene/gui/color_rect.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/texture_rect.h"
  38. #include "scene/resources/animated_texture.h"
  39. #include "scene/resources/atlas_texture.h"
  40. #include "scene/resources/compressed_texture.h"
  41. #include "scene/resources/image_texture.h"
  42. #include "scene/resources/portable_compressed_texture.h"
  43. #include "scene/resources/style_box_flat.h"
  44. constexpr const char *texture_2d_shader = R"(
  45. shader_type canvas_item;
  46. render_mode blend_mix;
  47. uniform vec4 u_channel_factors = vec4(1.0);
  48. vec4 filter_preview_colors(vec4 input_color, vec4 factors) {
  49. // Filter RGB.
  50. vec4 output_color = input_color * vec4(factors.rgb, input_color.a);
  51. // Remove transparency when alpha is not enabled.
  52. output_color.a = mix(1.0, output_color.a, factors.a);
  53. // Switch to opaque grayscale when visualizing only one channel.
  54. float csum = factors.r + factors.g + factors.b + factors.a;
  55. float single = clamp(2.0 - csum, 0.0, 1.0);
  56. for (int i = 0; i < 4; i++) {
  57. float c = input_color[i];
  58. output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single);
  59. }
  60. return output_color;
  61. }
  62. void fragment() {
  63. COLOR = filter_preview_colors(texture(TEXTURE, UV), u_channel_factors);
  64. }
  65. )";
  66. TextureRect *TexturePreview::get_texture_display() {
  67. return texture_display;
  68. }
  69. void TexturePreview::_notification(int p_what) {
  70. switch (p_what) {
  71. case NOTIFICATION_ENTER_TREE:
  72. case NOTIFICATION_THEME_CHANGED: {
  73. if (!is_inside_tree()) {
  74. // TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED`
  75. // is getting called for some reason when the `TexturePreview` is
  76. // getting destroyed, which causes `get_theme_font()` to return `nullptr`.
  77. // See https://github.com/godotengine/godot/issues/50743.
  78. break;
  79. }
  80. if (metadata_label) {
  81. Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
  82. metadata_label->add_theme_font_override(SceneStringName(font), metadata_label_font);
  83. }
  84. bg_rect->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor)));
  85. checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard")));
  86. cached_outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor));
  87. } break;
  88. }
  89. }
  90. void TexturePreview::_draw_outline() {
  91. const float outline_width = Math::round(EDSCALE);
  92. const Rect2 outline_rect = Rect2(Vector2(), outline_overlay->get_size()).grow(outline_width * 0.5);
  93. outline_overlay->draw_rect(outline_rect, cached_outline_color, false, outline_width);
  94. }
  95. void TexturePreview::_update_texture_display_ratio() {
  96. if (texture_display->get_texture().is_valid()) {
  97. centering_container->set_ratio(texture_display->get_texture()->get_size().aspect());
  98. }
  99. }
  100. static Image::Format get_texture_2d_format(const Ref<Texture2D> &p_texture) {
  101. const Ref<ImageTexture> image_texture = p_texture;
  102. if (image_texture.is_valid()) {
  103. return image_texture->get_format();
  104. }
  105. const Ref<CompressedTexture2D> compressed_texture = p_texture;
  106. if (compressed_texture.is_valid()) {
  107. return compressed_texture->get_format();
  108. }
  109. // AtlasTexture?
  110. // Unknown
  111. return Image::FORMAT_MAX;
  112. }
  113. static int get_texture_mipmaps_count(const Ref<Texture2D> &p_texture) {
  114. ERR_FAIL_COND_V(p_texture.is_null(), -1);
  115. // We are having to download the image only to get its mipmaps count. It would be nice if we didn't have to.
  116. Ref<Image> image;
  117. Ref<AtlasTexture> at = p_texture;
  118. if (at.is_valid()) {
  119. // The AtlasTexture tries to obtain the region from the atlas as an image,
  120. // which will fail if it is a compressed format.
  121. Ref<Texture2D> atlas = at->get_atlas();
  122. if (atlas.is_valid()) {
  123. image = atlas->get_image();
  124. }
  125. } else {
  126. image = p_texture->get_image();
  127. }
  128. if (image.is_valid()) {
  129. return image->get_mipmap_count();
  130. }
  131. return -1;
  132. }
  133. void TexturePreview::_update_metadata_label_text() {
  134. const Ref<Texture2D> texture = texture_display->get_texture();
  135. ERR_FAIL_COND(texture.is_null());
  136. const Image::Format format = get_texture_2d_format(texture.ptr());
  137. const String format_name = format != Image::FORMAT_MAX ? Image::get_format_name(format) : texture->get_class();
  138. const Vector2i resolution = texture->get_size();
  139. const int mipmaps = get_texture_mipmaps_count(texture);
  140. if (format != Image::FORMAT_MAX) {
  141. // Avoid signed integer overflow that could occur with huge texture sizes by casting everything to uint64_t.
  142. uint64_t memory = uint64_t(resolution.x) * uint64_t(resolution.y) * uint64_t(Image::get_format_pixel_size(format));
  143. // Handle VRAM-compressed formats that are stored with 4 bpp.
  144. memory >>= Image::get_format_pixel_rshift(format);
  145. float mipmaps_multiplier = 1.0;
  146. float mipmap_increase = 0.25;
  147. for (int i = 0; i < mipmaps; i++) {
  148. // Each mip adds 25% memory usage of the previous one.
  149. // With a complete mipmap chain, memory usage increases by ~33%.
  150. mipmaps_multiplier += mipmap_increase;
  151. mipmap_increase *= 0.25;
  152. }
  153. memory *= mipmaps_multiplier;
  154. if (mipmaps >= 1) {
  155. metadata_label->set_text(
  156. vformat(String::utf8("%d×%d %s\n") + TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
  157. texture->get_width(),
  158. texture->get_height(),
  159. format_name,
  160. mipmaps,
  161. String::humanize_size(memory)));
  162. } else {
  163. // "No Mipmaps" is easier to distinguish than "0 Mipmaps",
  164. // especially since 0, 6, and 8 look quite close with the default code font.
  165. metadata_label->set_text(
  166. vformat(String::utf8("%d×%d %s\n") + TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
  167. texture->get_width(),
  168. texture->get_height(),
  169. format_name,
  170. String::humanize_size(memory)));
  171. }
  172. } else {
  173. metadata_label->set_text(
  174. vformat(String::utf8("%d×%d %s"),
  175. texture->get_width(),
  176. texture->get_height(),
  177. format_name));
  178. }
  179. }
  180. void TexturePreview::on_selected_channels_changed() {
  181. material->set_shader_parameter("u_channel_factors", channel_selector->get_selected_channel_factors());
  182. }
  183. TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {
  184. set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);
  185. bg_rect = memnew(ColorRect);
  186. add_child(bg_rect);
  187. margin_container = memnew(MarginContainer);
  188. const float outline_width = Math::round(EDSCALE);
  189. margin_container->add_theme_constant_override("margin_right", outline_width);
  190. margin_container->add_theme_constant_override("margin_top", outline_width);
  191. margin_container->add_theme_constant_override("margin_left", outline_width);
  192. margin_container->add_theme_constant_override("margin_bottom", outline_width);
  193. add_child(margin_container);
  194. centering_container = memnew(AspectRatioContainer);
  195. margin_container->add_child(centering_container);
  196. checkerboard = memnew(TextureRect);
  197. checkerboard->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  198. checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
  199. checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
  200. centering_container->add_child(checkerboard);
  201. {
  202. Ref<Shader> shader;
  203. shader.instantiate();
  204. shader->set_code(texture_2d_shader);
  205. material.instantiate();
  206. material->set_shader(shader);
  207. material->set_shader_parameter("u_channel_factors", Vector4(1, 1, 1, 1));
  208. }
  209. texture_display = memnew(TextureRect);
  210. texture_display->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  211. texture_display->set_texture(p_texture);
  212. texture_display->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  213. texture_display->set_material(material);
  214. centering_container->add_child(texture_display);
  215. // Creating a separate control so it is not affected by the filtering shader.
  216. outline_overlay = memnew(Control);
  217. centering_container->add_child(outline_overlay);
  218. outline_overlay->connect(SceneStringName(draw), callable_mp(this, &TexturePreview::_draw_outline));
  219. if (p_texture.is_valid()) {
  220. _update_texture_display_ratio();
  221. p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_texture_display_ratio));
  222. }
  223. // Null can be passed by `Camera3DPreview` (which immediately after sets a texture anyways).
  224. const Image::Format format = p_texture.is_valid() ? get_texture_2d_format(p_texture.ptr()) : Image::FORMAT_MAX;
  225. const uint32_t components_mask = format != Image::FORMAT_MAX ? Image::get_format_component_mask(format) : 0xf;
  226. // Add color channel selector at the bottom left if more than 1 channel is available.
  227. if (p_show_metadata && !is_power_of_2(components_mask)) {
  228. channel_selector = memnew(ColorChannelSelector);
  229. channel_selector->connect("selected_channels_changed", callable_mp(this, &TexturePreview::on_selected_channels_changed));
  230. channel_selector->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);
  231. channel_selector->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
  232. channel_selector->set_available_channels_mask(components_mask);
  233. add_child(channel_selector);
  234. }
  235. if (p_show_metadata) {
  236. metadata_label = memnew(Label);
  237. if (p_texture.is_valid()) {
  238. _update_metadata_label_text();
  239. p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text));
  240. }
  241. // It's okay that these colors are static since the grid color is static too.
  242. metadata_label->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));
  243. metadata_label->add_theme_color_override("font_shadow_color", Color(0, 0, 0));
  244. metadata_label->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE);
  245. metadata_label->add_theme_color_override("font_outline_color", Color(0, 0, 0));
  246. metadata_label->add_theme_constant_override("outline_size", 8 * EDSCALE);
  247. metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END);
  248. metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END);
  249. add_child(metadata_label);
  250. }
  251. }
  252. bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
  253. 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<Image>(p_object) != nullptr;
  254. }
  255. void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
  256. Ref<Texture> texture(Object::cast_to<Texture>(p_object));
  257. if (texture.is_null()) {
  258. Ref<Image> image(Object::cast_to<Image>(p_object));
  259. texture = ImageTexture::create_from_image(image);
  260. ERR_FAIL_COND_MSG(texture.is_null(), "Failed to create the texture from an invalid image.");
  261. }
  262. add_custom_control(memnew(TexturePreview(texture, true)));
  263. }
  264. TextureEditorPlugin::TextureEditorPlugin() {
  265. Ref<EditorInspectorPluginTexture> plugin;
  266. plugin.instantiate();
  267. add_inspector_plugin(plugin);
  268. }