| 1 | /**************************************************************************/ |
| 2 | /* texture_layered_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_layered_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/label.h" |
| 37 | |
| 38 | // Shader sources. |
| 39 | |
| 40 | constexpr const char *array_2d_shader = R"( |
| 41 | // TextureLayeredEditor preview shader (2D array). |
| 42 | |
| 43 | shader_type canvas_item; |
| 44 | |
| 45 | uniform sampler2DArray tex; |
| 46 | uniform float layer; |
| 47 | uniform vec4 u_channel_factors = vec4(1.0); |
| 48 | |
| 49 | vec4 filter_preview_colors(vec4 input_color, vec4 factors) { |
| 50 | // Filter RGB. |
| 51 | vec4 output_color = input_color * vec4(factors.rgb, input_color.a); |
| 52 | |
| 53 | // Remove transparency when alpha is not enabled. |
| 54 | output_color.a = mix(1.0, output_color.a, factors.a); |
| 55 | |
| 56 | // Switch to opaque grayscale when visualizing only one channel. |
| 57 | float csum = factors.r + factors.g + factors.b + factors.a; |
| 58 | float single = clamp(2.0 - csum, 0.0, 1.0); |
| 59 | for (int i = 0; i < 4; i++) { |
| 60 | float c = input_color[i]; |
| 61 | output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single); |
| 62 | } |
| 63 | |
| 64 | return output_color; |
| 65 | } |
| 66 | |
| 67 | void fragment() { |
| 68 | COLOR = textureLod(tex, vec3(UV, layer), 0.0); |
| 69 | COLOR = filter_preview_colors(COLOR, u_channel_factors); |
| 70 | } |
| 71 | )"; |
| 72 | |
| 73 | constexpr const char *cubemap_shader = R"( |
| 74 | // TextureLayeredEditor preview shader (cubemap). |
| 75 | |
| 76 | shader_type canvas_item; |
| 77 | |
| 78 | uniform samplerCube tex; |
| 79 | uniform vec3 normal; |
| 80 | uniform mat3 rot; |
| 81 | |
| 82 | uniform vec4 u_channel_factors = vec4(1.0); |
| 83 | |
| 84 | vec4 filter_preview_colors(vec4 input_color, vec4 factors) { |
| 85 | // Filter RGB. |
| 86 | vec4 output_color = input_color * vec4(factors.rgb, input_color.a); |
| 87 | |
| 88 | // Remove transparency when alpha is not enabled. |
| 89 | output_color.a = mix(1.0, output_color.a, factors.a); |
| 90 | |
| 91 | // Switch to opaque grayscale when visualizing only one channel. |
| 92 | float csum = factors.r + factors.g + factors.b + factors.a; |
| 93 | float single = clamp(2.0 - csum, 0.0, 1.0); |
| 94 | for (int i = 0; i < 4; i++) { |
| 95 | float c = input_color[i]; |
| 96 | output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single); |
| 97 | } |
| 98 | |
| 99 | return output_color; |
| 100 | } |
| 101 | |
| 102 | void fragment() { |
| 103 | vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z)); |
| 104 | COLOR = textureLod(tex, n, 0.0); |
| 105 | COLOR = filter_preview_colors(COLOR, u_channel_factors); |
| 106 | } |
| 107 | )"; |
| 108 | |
| 109 | constexpr const char *cubemap_array_shader = R"( |
| 110 | // TextureLayeredEditor preview shader (cubemap array). |
| 111 | |
| 112 | shader_type canvas_item; |
| 113 | uniform samplerCubeArray tex; |
| 114 | uniform vec3 normal; |
| 115 | uniform mat3 rot; |
| 116 | uniform float layer; |
| 117 | |
| 118 | uniform vec4 u_channel_factors = vec4(1.0); |
| 119 | |
| 120 | vec4 filter_preview_colors(vec4 input_color, vec4 factors) { |
| 121 | // Filter RGB. |
| 122 | vec4 output_color = input_color * vec4(factors.rgb, input_color.a); |
| 123 | |
| 124 | // Remove transparency when alpha is not enabled. |
| 125 | output_color.a = mix(1.0, output_color.a, factors.a); |
| 126 | |
| 127 | // Switch to opaque grayscale when visualizing only one channel. |
| 128 | float csum = factors.r + factors.g + factors.b + factors.a; |
| 129 | float single = clamp(2.0 - csum, 0.0, 1.0); |
| 130 | for (int i = 0; i < 4; i++) { |
| 131 | float c = input_color[i]; |
| 132 | output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single); |
| 133 | } |
| 134 | |
| 135 | return output_color; |
| 136 | } |
| 137 | |
| 138 | void fragment() { |
| 139 | vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z)); |
| 140 | COLOR = textureLod(tex, vec4(n, layer), 0.0); |
| 141 | COLOR = filter_preview_colors(COLOR, u_channel_factors); |
| 142 | } |
| 143 | )"; |
| 144 | |
| 145 | void TextureLayeredEditor::gui_input(const Ref<InputEvent> &p_event) { |
| 146 | ERR_FAIL_COND(p_event.is_null()); |
| 147 | |
| 148 | if (!use_rotation) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | Ref<InputEventMouseMotion> mm = p_event; |
| 153 | if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::RIGHT)) { |
| 154 | if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) { |
| 155 | Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); |
| 156 | } |
| 157 | |
| 158 | y_rot += mm->get_relative().x * 0.01; |
| 159 | x_rot = CLAMP(x_rot - mm->get_relative().y * 0.01, -Math::PI * 0.5f, Math::PI * 0.5f); |
| 160 | |
| 161 | _update_material(false); |
| 162 | } |
| 163 | |
| 164 | Ref<InputEventMouseButton> mb = p_event; |
| 165 | if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT) { |
| 166 | if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) { |
| 167 | Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE); |
| 168 | Input::get_singleton()->warp_mouse(original_mouse_pos); |
| 169 | } else if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) { |
| 170 | original_mouse_pos = mb->get_global_position(); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void TextureLayeredEditor::_texture_rect_draw() { |
| 176 | texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1)); |
| 177 | } |
| 178 | |
| 179 | void TextureLayeredEditor::_update_gui() { |
| 180 | if (texture.is_null()) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | _texture_rect_update_area(); |
| 185 | |
| 186 | const Image::Format format = texture->get_format(); |
| 187 | const String format_name = Image::get_format_name(format); |
| 188 | String texture_info; |
| 189 | |
| 190 | switch (texture->get_layered_type()) { |
| 191 | case TextureLayered::LAYERED_TYPE_2D_ARRAY: { |
| 192 | layer->set_max(texture->get_layers() - 1); |
| 193 | |
| 194 | texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"), |
| 195 | texture->get_width(), |
| 196 | texture->get_height(), |
| 197 | texture->get_layers(), |
| 198 | format_name); |
| 199 | |
| 200 | } break; |
| 201 | case TextureLayered::LAYERED_TYPE_CUBEMAP: { |
| 202 | layer->hide(); |
| 203 | |
| 204 | texture_info = vformat(String::utf8("%d×%d %s\n"), |
| 205 | texture->get_width(), |
| 206 | texture->get_height(), |
| 207 | format_name); |
| 208 | |
| 209 | } break; |
| 210 | case TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY: { |
| 211 | layer->set_max(texture->get_layers() / 6 - 1); |
| 212 | |
| 213 | texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"), |
| 214 | texture->get_width(), |
| 215 | texture->get_height(), |
| 216 | texture->get_layers() / 6, |
| 217 | format_name); |
| 218 | |
| 219 | } break; |
| 220 | |
| 221 | default: { |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (texture->has_mipmaps()) { |
| 226 | const int mip_count = Image::get_image_required_mipmaps(texture->get_width(), texture->get_height(), format); |
| 227 | const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, true) * texture->get_layers(); |
| 228 | |
| 229 | texture_info += vformat(TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"), |
| 230 | mip_count, |
| 231 | String::humanize_size(memory)); |
| 232 | |
| 233 | } else { |
| 234 | const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, false) * texture->get_layers(); |
| 235 | |
| 236 | texture_info += vformat(TTR("No Mipmaps") + "\n" + TTR("Memory: %s"), |
| 237 | String::humanize_size(memory)); |
| 238 | } |
| 239 | |
| 240 | info->set_text(texture_info); |
| 241 | |
| 242 | const uint32_t components_mask = Image::get_format_component_mask(format); |
| 243 | if (is_power_of_2(components_mask)) { |
| 244 | // Only one channel available, no point in showing a channel selector. |
| 245 | channel_selector->hide(); |
| 246 | } else { |
| 247 | channel_selector->show(); |
| 248 | channel_selector->set_available_channels_mask(components_mask); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void TextureLayeredEditor::_notification(int p_what) { |
| 253 | switch (p_what) { |
| 254 | case NOTIFICATION_RESIZED: { |
| 255 | _texture_rect_update_area(); |
| 256 | } break; |
| 257 | |
| 258 | case NOTIFICATION_DRAW: { |
| 259 | Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard")); |
| 260 | draw_texture_rect(checkerboard, texture_rect->get_rect(), true); |
| 261 | _draw_outline(); |
| 262 | } break; |
| 263 | |
| 264 | case NOTIFICATION_THEME_CHANGED: { |
| 265 | if (info) { |
| 266 | Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts)); |
| 267 | info->add_theme_font_override(SceneStringName(font), metadata_label_font); |
| 268 | } |
| 269 | theme_cache.outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor)); |
| 270 | } break; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | void TextureLayeredEditor::_texture_changed() { |
| 275 | if (!is_visible()) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | setting = true; |
| 280 | _update_gui(); |
| 281 | setting = false; |
| 282 | |
| 283 | _update_material(true); |
| 284 | queue_redraw(); |
| 285 | } |
| 286 | |
| 287 | void TextureLayeredEditor::_update_material(bool p_texture_changed) { |
| 288 | materials[0]->set_shader_parameter("layer", layer->get_value()); |
| 289 | materials[2]->set_shader_parameter("layer", layer->get_value()); |
| 290 | |
| 291 | Vector3 v(-1, -1, -1); |
| 292 | v.normalize(); |
| 293 | |
| 294 | Basis b; |
| 295 | b.rotate(Vector3(1, 0, 0), x_rot); |
| 296 | b.rotate(Vector3(0, 1, 0), y_rot); |
| 297 | |
| 298 | materials[1]->set_shader_parameter("normal", v); |
| 299 | materials[1]->set_shader_parameter("rot", b); |
| 300 | materials[2]->set_shader_parameter("normal", v); |
| 301 | materials[2]->set_shader_parameter("rot", b); |
| 302 | |
| 303 | if (p_texture_changed) { |
| 304 | const TextureLayered::LayeredType type = texture->get_layered_type(); |
| 305 | use_rotation = type == TextureLayered::LAYERED_TYPE_CUBEMAP || type == TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY; |
| 306 | |
| 307 | materials[texture->get_layered_type()]->set_shader_parameter("tex", texture->get_rid()); |
| 308 | } |
| 309 | |
| 310 | const Vector4 channel_factors = channel_selector->get_selected_channel_factors(); |
| 311 | for (unsigned int i = 0; i < 3; ++i) { |
| 312 | materials[i]->set_shader_parameter("u_channel_factors", channel_factors); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void TextureLayeredEditor::on_selected_channels_changed() { |
| 317 | _update_material(false); |
| 318 | } |
| 319 | |
| 320 | void TextureLayeredEditor::_draw_outline() { |
| 321 | const float outline_width = Math::round(EDSCALE); |
| 322 | const Rect2 outline_rect = texture_rect->get_rect().grow(outline_width * 0.5); |
| 323 | draw_rect(outline_rect, theme_cache.outline_color, false, outline_width); |
| 324 | } |
| 325 | |
| 326 | void TextureLayeredEditor::_make_materials() { |
| 327 | for (int i = 0; i < 3; i++) { |
| 328 | materials[i].instantiate(); |
| 329 | materials[i]->set_shader(shaders[i]); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void TextureLayeredEditor::_texture_rect_update_area() { |
| 334 | Size2 size = get_size(); |
| 335 | int tex_width = texture->get_width() * size.height / texture->get_height(); |
| 336 | int tex_height = size.height; |
| 337 | |
| 338 | if (tex_width > size.width) { |
| 339 | tex_width = size.width; |
| 340 | tex_height = texture->get_height() * tex_width / texture->get_width(); |
| 341 | } |
| 342 | |
| 343 | // Prevent the texture from being unpreviewable after the rescale, so that we can still see something |
| 344 | if (tex_height <= 0) { |
| 345 | tex_height = 1; |
| 346 | } |
| 347 | if (tex_width <= 0) { |
| 348 | tex_width = 1; |
| 349 | } |
| 350 | |
| 351 | int ofs_x = (size.width - tex_width) / 2; |
| 352 | int ofs_y = (size.height - tex_height) / 2; |
| 353 | |
| 354 | texture_rect->set_position(Vector2(ofs_x, ofs_y - Math::round(EDSCALE))); |
| 355 | texture_rect->set_size(Vector2(tex_width, tex_height)); |
| 356 | } |
| 357 | |
| 358 | void TextureLayeredEditor::init_shaders() { |
| 359 | shaders[0].instantiate(); |
| 360 | shaders[0]->set_code(array_2d_shader); |
| 361 | |
| 362 | shaders[1].instantiate(); |
| 363 | shaders[1]->set_code(cubemap_shader); |
| 364 | |
| 365 | shaders[2].instantiate(); |
| 366 | shaders[2]->set_code(cubemap_array_shader); |
| 367 | } |
| 368 | |
| 369 | void TextureLayeredEditor::finish_shaders() { |
| 370 | shaders[0].unref(); |
| 371 | shaders[1].unref(); |
| 372 | shaders[2].unref(); |
| 373 | } |
| 374 | |
| 375 | void TextureLayeredEditor::edit(Ref<TextureLayered> p_texture) { |
| 376 | if (texture.is_valid()) { |
| 377 | texture->disconnect_changed(callable_mp(this, &TextureLayeredEditor::_texture_changed)); |
| 378 | } |
| 379 | |
| 380 | texture = p_texture; |
| 381 | |
| 382 | if (texture.is_valid()) { |
| 383 | if (materials[0].is_null()) { |
| 384 | _make_materials(); |
| 385 | } |
| 386 | |
| 387 | texture->connect_changed(callable_mp(this, &TextureLayeredEditor::_texture_changed)); |
| 388 | texture_rect->set_material(materials[texture->get_layered_type()]); |
| 389 | |
| 390 | setting = true; |
| 391 | layer->set_value(0); |
| 392 | layer->show(); |
| 393 | _update_gui(); |
| 394 | setting = false; |
| 395 | |
| 396 | x_rot = 0; |
| 397 | y_rot = 0; |
| 398 | |
| 399 | _update_material(true); |
| 400 | queue_redraw(); |
| 401 | |
| 402 | } else { |
| 403 | hide(); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | TextureLayeredEditor::TextureLayeredEditor() { |
| 408 | set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED); |
| 409 | set_custom_minimum_size(Size2(0, 256.0) * EDSCALE); |
| 410 | |
| 411 | texture_rect = memnew(Control); |
| 412 | texture_rect->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 413 | texture_rect->connect(SceneStringName(draw), callable_mp(this, &TextureLayeredEditor::_texture_rect_draw)); |
| 414 | |
| 415 | add_child(texture_rect); |
| 416 | |
| 417 | layer = memnew(SpinBox); |
| 418 | layer->set_step(1); |
| 419 | layer->set_max(100); |
| 420 | |
| 421 | layer->set_modulate(Color(1, 1, 1, 0.8)); |
| 422 | layer->set_h_grow_direction(GROW_DIRECTION_BEGIN); |
| 423 | layer->set_anchor(SIDE_RIGHT, 1); |
| 424 | layer->set_anchor(SIDE_LEFT, 1); |
| 425 | layer->connect(SceneStringName(value_changed), callable_mp(this, &TextureLayeredEditor::_layer_changed)); |
| 426 | |
| 427 | add_child(layer); |
| 428 | |
| 429 | channel_selector = memnew(ColorChannelSelector); |
| 430 | channel_selector->connect("selected_channels_changed", callable_mp(this, &TextureLayeredEditor::on_selected_channels_changed)); |
| 431 | channel_selector->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT); |
| 432 | add_child(channel_selector); |
| 433 | |
| 434 | info = memnew(Label); |
| 435 | info->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 436 | info->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1)); |
| 437 | info->add_theme_color_override("font_shadow_color", Color(0, 0, 0)); |
| 438 | info->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE); |
| 439 | info->add_theme_color_override("font_outline_color", Color(0, 0, 0)); |
| 440 | info->add_theme_constant_override("outline_size", 8 * EDSCALE); |
| 441 | |
| 442 | info->set_h_grow_direction(GROW_DIRECTION_BEGIN); |
| 443 | info->set_v_grow_direction(GROW_DIRECTION_BEGIN); |
| 444 | info->set_h_size_flags(Control::SIZE_SHRINK_END); |
| 445 | info->set_v_size_flags(Control::SIZE_SHRINK_END); |
| 446 | info->set_anchor(SIDE_RIGHT, 1); |
| 447 | info->set_anchor(SIDE_LEFT, 1); |
| 448 | info->set_anchor(SIDE_BOTTOM, 1); |
| 449 | info->set_anchor(SIDE_TOP, 1); |
| 450 | |
| 451 | add_child(info); |
| 452 | } |
| 453 | |
| 454 | bool EditorInspectorPluginLayeredTexture::can_handle(Object *p_object) { |
| 455 | return Object::cast_to<TextureLayered>(p_object) != nullptr; |
| 456 | } |
| 457 | |
| 458 | void EditorInspectorPluginLayeredTexture::parse_begin(Object *p_object) { |
| 459 | TextureLayered *texture = Object::cast_to<TextureLayered>(p_object); |
| 460 | if (!texture) { |
| 461 | return; |
| 462 | } |
| 463 | Ref<TextureLayered> m(texture); |
| 464 | |
| 465 | TextureLayeredEditor *editor = memnew(TextureLayeredEditor); |
| 466 | editor->edit(m); |
| 467 | add_custom_control(editor); |
| 468 | } |
| 469 | |
| 470 | TextureLayeredEditorPlugin::TextureLayeredEditorPlugin() { |
| 471 | Ref<EditorInspectorPluginLayeredTexture> plugin; |
| 472 | plugin.instantiate(); |
| 473 | add_inspector_plugin(plugin); |
| 474 | } |