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