texture_editor_plugin.cpp 13 KB

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