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