2020-05-01 14:34:23 +02:00
|
|
|
|
/**************************************************************************/
|
2020-05-14 13:20:05 +02:00
|
|
|
|
/* texture_layered_editor_plugin.cpp */
|
2020-05-01 14:34:23 +02:00
|
|
|
|
/**************************************************************************/
|
|
|
|
|
/* This file is part of: */
|
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
|
/* https://godotengine.org */
|
|
|
|
|
/**************************************************************************/
|
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
|
/* */
|
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
|
/* the following conditions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "texture_layered_editor_plugin.h"
|
|
|
|
|
|
2024-05-30 13:53:26 +02:00
|
|
|
|
#include "editor/editor_string_names.h"
|
|
|
|
|
#include "editor/themes/editor_scale.h"
|
2022-11-19 12:45:49 +01:00
|
|
|
|
#include "scene/gui/label.h"
|
|
|
|
|
|
2024-05-30 13:53:26 +02:00
|
|
|
|
// Shader sources.
|
|
|
|
|
|
|
|
|
|
constexpr const char *array_2d_shader = R"(
|
|
|
|
|
// TextureLayeredEditor preview shader (2D array).
|
|
|
|
|
|
|
|
|
|
shader_type canvas_item;
|
|
|
|
|
|
|
|
|
|
uniform sampler2DArray tex;
|
|
|
|
|
uniform float layer;
|
|
|
|
|
|
|
|
|
|
void fragment() {
|
|
|
|
|
COLOR = textureLod(tex, vec3(UV, layer), 0.0);
|
|
|
|
|
}
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
|
|
constexpr const char *cubemap_shader = R"(
|
|
|
|
|
// TextureLayeredEditor preview shader (cubemap).
|
|
|
|
|
|
|
|
|
|
shader_type canvas_item;
|
|
|
|
|
|
|
|
|
|
uniform samplerCube tex;
|
|
|
|
|
uniform vec3 normal;
|
|
|
|
|
uniform mat3 rot;
|
|
|
|
|
|
|
|
|
|
void fragment() {
|
|
|
|
|
vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
|
|
|
|
|
COLOR = textureLod(tex, n, 0.0);
|
|
|
|
|
}
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
|
|
constexpr const char *cubemap_array_shader = R"(
|
|
|
|
|
// TextureLayeredEditor preview shader (cubemap array).
|
|
|
|
|
|
|
|
|
|
shader_type canvas_item;
|
|
|
|
|
uniform samplerCubeArray tex;
|
|
|
|
|
uniform vec3 normal;
|
|
|
|
|
uniform mat3 rot;
|
|
|
|
|
uniform float layer;
|
|
|
|
|
|
|
|
|
|
void fragment() {
|
|
|
|
|
vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
|
|
|
|
|
COLOR = textureLod(tex, vec4(n, layer), 0.0);
|
|
|
|
|
}
|
|
|
|
|
)";
|
|
|
|
|
|
2021-08-22 17:37:22 +02:00
|
|
|
|
void TextureLayeredEditor::gui_input(const Ref<InputEvent> &p_event) {
|
2021-04-05 08:52:21 +02:00
|
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
2023-01-08 00:55:54 +01:00
|
|
|
|
if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
|
2020-05-01 14:34:23 +02:00
|
|
|
|
y_rot += -mm->get_relative().x * 0.01;
|
2024-05-30 13:53:26 +02:00
|
|
|
|
x_rot += -mm->get_relative().y * 0.01;
|
|
|
|
|
|
|
|
|
|
_update_material(false);
|
2020-05-01 14:34:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextureLayeredEditor::_texture_rect_draw() {
|
|
|
|
|
texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 13:53:26 +02:00
|
|
|
|
void TextureLayeredEditor::_update_gui() {
|
|
|
|
|
if (texture.is_null()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_texture_rect_update_area();
|
|
|
|
|
|
|
|
|
|
const String format = Image::get_format_name(texture->get_format());
|
|
|
|
|
String texture_info;
|
|
|
|
|
|
|
|
|
|
switch (texture->get_layered_type()) {
|
|
|
|
|
case TextureLayered::LAYERED_TYPE_2D_ARRAY: {
|
|
|
|
|
layer->set_max(texture->get_layers() - 1);
|
|
|
|
|
|
|
|
|
|
texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"),
|
|
|
|
|
texture->get_width(),
|
|
|
|
|
texture->get_height(),
|
|
|
|
|
texture->get_layers(),
|
|
|
|
|
format);
|
|
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
case TextureLayered::LAYERED_TYPE_CUBEMAP: {
|
|
|
|
|
layer->hide();
|
|
|
|
|
|
|
|
|
|
texture_info = vformat(String::utf8("%d×%d %s\n"),
|
|
|
|
|
texture->get_width(),
|
|
|
|
|
texture->get_height(),
|
|
|
|
|
format);
|
|
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
case TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY: {
|
|
|
|
|
layer->set_max(texture->get_layers() / 6 - 1);
|
|
|
|
|
|
|
|
|
|
texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"),
|
|
|
|
|
texture->get_width(),
|
|
|
|
|
texture->get_height(),
|
|
|
|
|
texture->get_layers() / 6,
|
|
|
|
|
format);
|
|
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
default: {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (texture->has_mipmaps()) {
|
|
|
|
|
const int mip_count = Image::get_image_required_mipmaps(texture->get_width(), texture->get_height(), texture->get_format());
|
|
|
|
|
const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), texture->get_format(), true) * texture->get_layers();
|
|
|
|
|
|
|
|
|
|
texture_info += vformat(TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
|
|
|
|
|
mip_count,
|
|
|
|
|
String::humanize_size(memory));
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), texture->get_format(), false) * texture->get_layers();
|
|
|
|
|
|
|
|
|
|
texture_info += vformat(TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
|
|
|
|
|
String::humanize_size(memory));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info->set_text(texture_info);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
void TextureLayeredEditor::_notification(int p_what) {
|
2022-02-16 15:17:55 +01:00
|
|
|
|
switch (p_what) {
|
|
|
|
|
case NOTIFICATION_RESIZED: {
|
|
|
|
|
_texture_rect_update_area();
|
|
|
|
|
} break;
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
2022-02-16 15:17:55 +01:00
|
|
|
|
case NOTIFICATION_DRAW: {
|
2023-08-13 02:33:39 +02:00
|
|
|
|
Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
|
2022-02-16 15:17:55 +01:00
|
|
|
|
Size2 size = get_size();
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
2022-02-16 15:17:55 +01:00
|
|
|
|
draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
|
|
|
|
|
} break;
|
2024-05-30 13:53:26 +02:00
|
|
|
|
|
|
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
|
if (info) {
|
|
|
|
|
Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
|
|
|
|
|
info->add_theme_font_override(SceneStringName(font), metadata_label_font);
|
|
|
|
|
}
|
|
|
|
|
} break;
|
2020-05-01 14:34:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 21:18:45 +01:00
|
|
|
|
void TextureLayeredEditor::_texture_changed() {
|
2020-05-14 16:41:43 +02:00
|
|
|
|
if (!is_visible()) {
|
2020-05-01 14:34:23 +02:00
|
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
|
}
|
2024-05-30 13:53:26 +02:00
|
|
|
|
|
|
|
|
|
setting = true;
|
|
|
|
|
_update_gui();
|
|
|
|
|
setting = false;
|
|
|
|
|
|
|
|
|
|
_update_material(true);
|
2022-08-13 23:21:24 +02:00
|
|
|
|
queue_redraw();
|
2020-05-01 14:34:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 13:53:26 +02:00
|
|
|
|
void TextureLayeredEditor::_update_material(bool p_texture_changed) {
|
2022-08-27 11:22:43 +02:00
|
|
|
|
materials[0]->set_shader_parameter("layer", layer->get_value());
|
|
|
|
|
materials[2]->set_shader_parameter("layer", layer->get_value());
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
|
|
|
|
Vector3 v(1, 1, 1);
|
|
|
|
|
v.normalize();
|
|
|
|
|
|
|
|
|
|
Basis b;
|
|
|
|
|
b.rotate(Vector3(1, 0, 0), x_rot);
|
|
|
|
|
b.rotate(Vector3(0, 1, 0), y_rot);
|
|
|
|
|
|
2022-08-27 11:22:43 +02:00
|
|
|
|
materials[1]->set_shader_parameter("normal", v);
|
|
|
|
|
materials[1]->set_shader_parameter("rot", b);
|
|
|
|
|
materials[2]->set_shader_parameter("normal", v);
|
|
|
|
|
materials[2]->set_shader_parameter("rot", b);
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
2024-05-30 13:53:26 +02:00
|
|
|
|
if (p_texture_changed) {
|
|
|
|
|
materials[texture->get_layered_type()]->set_shader_parameter("tex", texture->get_rid());
|
2020-05-01 14:34:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextureLayeredEditor::_make_shaders() {
|
2021-06-18 00:03:09 +02:00
|
|
|
|
shaders[0].instantiate();
|
2024-05-30 13:53:26 +02:00
|
|
|
|
shaders[0]->set_code(array_2d_shader);
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
2021-06-18 00:03:09 +02:00
|
|
|
|
shaders[1].instantiate();
|
2024-05-30 13:53:26 +02:00
|
|
|
|
shaders[1]->set_code(cubemap_shader);
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
2021-06-18 00:03:09 +02:00
|
|
|
|
shaders[2].instantiate();
|
2024-05-30 13:53:26 +02:00
|
|
|
|
shaders[2]->set_code(cubemap_array_shader);
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2021-06-18 00:03:09 +02:00
|
|
|
|
materials[i].instantiate();
|
2020-05-01 14:34:23 +02:00
|
|
|
|
materials[i]->set_shader(shaders[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextureLayeredEditor::_texture_rect_update_area() {
|
|
|
|
|
Size2 size = get_size();
|
|
|
|
|
int tex_width = texture->get_width() * size.height / texture->get_height();
|
|
|
|
|
int tex_height = size.height;
|
|
|
|
|
|
|
|
|
|
if (tex_width > size.width) {
|
|
|
|
|
tex_width = size.width;
|
|
|
|
|
tex_height = texture->get_height() * tex_width / texture->get_width();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prevent the texture from being unpreviewable after the rescale, so that we can still see something
|
2020-05-14 16:41:43 +02:00
|
|
|
|
if (tex_height <= 0) {
|
2020-05-01 14:34:23 +02:00
|
|
|
|
tex_height = 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
|
}
|
|
|
|
|
if (tex_width <= 0) {
|
2020-05-01 14:34:23 +02:00
|
|
|
|
tex_width = 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
|
}
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
|
|
|
|
int ofs_x = (size.width - tex_width) / 2;
|
|
|
|
|
int ofs_y = (size.height - tex_height) / 2;
|
|
|
|
|
|
|
|
|
|
texture_rect->set_position(Vector2(ofs_x, ofs_y));
|
|
|
|
|
texture_rect->set_size(Vector2(tex_width, tex_height));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextureLayeredEditor::edit(Ref<TextureLayered> p_texture) {
|
2020-05-14 16:41:43 +02:00
|
|
|
|
if (!texture.is_null()) {
|
2023-07-03 21:29:37 +02:00
|
|
|
|
texture->disconnect_changed(callable_mp(this, &TextureLayeredEditor::_texture_changed));
|
2020-05-14 16:41:43 +02:00
|
|
|
|
}
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
|
|
|
|
texture = p_texture;
|
|
|
|
|
|
|
|
|
|
if (!texture.is_null()) {
|
|
|
|
|
if (shaders[0].is_null()) {
|
|
|
|
|
_make_shaders();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 21:29:37 +02:00
|
|
|
|
texture->connect_changed(callable_mp(this, &TextureLayeredEditor::_texture_changed));
|
2020-05-01 14:34:23 +02:00
|
|
|
|
texture_rect->set_material(materials[texture->get_layered_type()]);
|
2024-05-30 13:53:26 +02:00
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
setting = true;
|
2024-05-30 13:53:26 +02:00
|
|
|
|
layer->set_value(0);
|
|
|
|
|
layer->show();
|
|
|
|
|
_update_gui();
|
|
|
|
|
setting = false;
|
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
x_rot = 0;
|
|
|
|
|
y_rot = 0;
|
2024-05-30 13:53:26 +02:00
|
|
|
|
|
|
|
|
|
_update_material(true);
|
|
|
|
|
queue_redraw();
|
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
} else {
|
|
|
|
|
hide();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextureLayeredEditor::TextureLayeredEditor() {
|
|
|
|
|
set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
|
2024-05-30 13:53:26 +02:00
|
|
|
|
set_custom_minimum_size(Size2(0, 256.0) * EDSCALE);
|
2023-10-19 18:05:19 +02:00
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
texture_rect = memnew(Control);
|
|
|
|
|
texture_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
|
2024-05-13 16:56:03 +02:00
|
|
|
|
texture_rect->connect(SceneStringName(draw), callable_mp(this, &TextureLayeredEditor::_texture_rect_draw));
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
2024-05-30 13:53:26 +02:00
|
|
|
|
add_child(texture_rect);
|
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
layer = memnew(SpinBox);
|
|
|
|
|
layer->set_step(1);
|
|
|
|
|
layer->set_max(100);
|
2024-05-30 13:53:26 +02:00
|
|
|
|
|
2023-10-19 18:05:19 +02:00
|
|
|
|
layer->set_modulate(Color(1, 1, 1, 0.8));
|
2024-05-30 13:53:26 +02:00
|
|
|
|
layer->set_h_grow_direction(GROW_DIRECTION_BEGIN);
|
2020-12-22 17:24:29 +01:00
|
|
|
|
layer->set_anchor(SIDE_RIGHT, 1);
|
|
|
|
|
layer->set_anchor(SIDE_LEFT, 1);
|
2024-05-14 11:42:00 +02:00
|
|
|
|
layer->connect(SceneStringName(value_changed), callable_mp(this, &TextureLayeredEditor::_layer_changed));
|
2023-10-19 18:05:19 +02:00
|
|
|
|
|
2024-05-30 13:53:26 +02:00
|
|
|
|
add_child(layer);
|
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
info = memnew(Label);
|
2024-05-30 13:53:26 +02:00
|
|
|
|
info->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));
|
|
|
|
|
info->add_theme_color_override("font_shadow_color", Color(0, 0, 0));
|
|
|
|
|
info->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE);
|
|
|
|
|
info->add_theme_color_override("font_outline_color", Color(0, 0, 0));
|
|
|
|
|
info->add_theme_constant_override("outline_size", 8 * EDSCALE);
|
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
info->set_h_grow_direction(GROW_DIRECTION_BEGIN);
|
|
|
|
|
info->set_v_grow_direction(GROW_DIRECTION_BEGIN);
|
2024-05-30 13:53:26 +02:00
|
|
|
|
info->set_h_size_flags(Control::SIZE_SHRINK_END);
|
|
|
|
|
info->set_v_size_flags(Control::SIZE_SHRINK_END);
|
2023-10-19 18:05:19 +02:00
|
|
|
|
info->set_anchor(SIDE_RIGHT, 1);
|
|
|
|
|
info->set_anchor(SIDE_LEFT, 1);
|
|
|
|
|
info->set_anchor(SIDE_BOTTOM, 1);
|
|
|
|
|
info->set_anchor(SIDE_TOP, 1);
|
2024-05-30 13:53:26 +02:00
|
|
|
|
|
|
|
|
|
add_child(info);
|
2020-05-01 14:34:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextureLayeredEditor::~TextureLayeredEditor() {
|
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
|
bool EditorInspectorPluginLayeredTexture::can_handle(Object *p_object) {
|
|
|
|
|
return Object::cast_to<TextureLayered>(p_object) != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EditorInspectorPluginLayeredTexture::parse_begin(Object *p_object) {
|
|
|
|
|
TextureLayered *texture = Object::cast_to<TextureLayered>(p_object);
|
|
|
|
|
if (!texture) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Ref<TextureLayered> m(texture);
|
|
|
|
|
|
|
|
|
|
TextureLayeredEditor *editor = memnew(TextureLayeredEditor);
|
|
|
|
|
editor->edit(m);
|
|
|
|
|
add_custom_control(editor);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 10:36:51 +01:00
|
|
|
|
TextureLayeredEditorPlugin::TextureLayeredEditorPlugin() {
|
2020-05-01 14:34:23 +02:00
|
|
|
|
Ref<EditorInspectorPluginLayeredTexture> plugin;
|
2021-06-18 00:03:09 +02:00
|
|
|
|
plugin.instantiate();
|
2020-05-01 14:34:23 +02:00
|
|
|
|
add_inspector_plugin(plugin);
|
|
|
|
|
}
|