2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* texture_3d_editor_plugin.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2020-09-09 17:40:51 +02:00
|
|
|
|
|
|
|
#include "texture_3d_editor_plugin.h"
|
|
|
|
|
2022-11-19 12:45:49 +01:00
|
|
|
#include "scene/gui/label.h"
|
|
|
|
|
2020-09-09 17:40:51 +02:00
|
|
|
void Texture3DEditor::_texture_rect_draw() {
|
|
|
|
texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture3DEditor::_notification(int p_what) {
|
2022-02-16 15:17:55 +01:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_RESIZED: {
|
|
|
|
_texture_rect_update_area();
|
|
|
|
} break;
|
2020-09-09 17:40:51 +02:00
|
|
|
|
2022-02-16 15:17:55 +01:00
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
Ref<Texture2D> checkerboard = get_theme_icon(SNAME("Checkerboard"), SNAME("EditorIcons"));
|
|
|
|
Size2 size = get_size();
|
2020-09-09 17:40:51 +02:00
|
|
|
|
2022-02-16 15:17:55 +01:00
|
|
|
draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
|
|
|
|
} break;
|
2020-09-09 17:40:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 21:18:45 +01:00
|
|
|
void Texture3DEditor::_texture_changed() {
|
2020-09-09 17:40:51 +02:00
|
|
|
if (!is_visible()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-13 23:21:24 +02:00
|
|
|
queue_redraw();
|
2020-09-09 17:40:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Texture3DEditor::_update_material() {
|
2022-08-27 11:22:43 +02:00
|
|
|
material->set_shader_parameter("layer", (layer->get_value() + 0.5) / texture->get_depth());
|
|
|
|
material->set_shader_parameter("tex", texture->get_rid());
|
2020-09-09 17:40:51 +02:00
|
|
|
|
|
|
|
String format = Image::get_format_name(texture->get_format());
|
|
|
|
|
|
|
|
String text;
|
|
|
|
text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + "x" + itos(texture->get_depth()) + " " + format;
|
|
|
|
|
|
|
|
info->set_text(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture3DEditor::_make_shaders() {
|
2021-06-18 00:03:09 +02:00
|
|
|
shader.instantiate();
|
2021-07-19 08:06:51 +02:00
|
|
|
shader->set_code(R"(
|
2021-08-18 03:09:22 +02:00
|
|
|
// Texture3DEditor preview shader.
|
|
|
|
|
2021-07-19 08:06:51 +02:00
|
|
|
shader_type canvas_item;
|
|
|
|
|
|
|
|
uniform sampler3D tex;
|
|
|
|
uniform float layer;
|
|
|
|
|
|
|
|
void fragment() {
|
|
|
|
COLOR = textureLod(tex, vec3(UV, layer), 0.0);
|
|
|
|
}
|
|
|
|
)");
|
2021-06-18 00:03:09 +02:00
|
|
|
material.instantiate();
|
2020-09-09 17:40:51 +02:00
|
|
|
material->set_shader(shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture3DEditor::_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
|
|
|
|
if (tex_height <= 0) {
|
|
|
|
tex_height = 1;
|
|
|
|
}
|
|
|
|
if (tex_width <= 0) {
|
|
|
|
tex_width = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 Texture3DEditor::edit(Ref<Texture3D> p_texture) {
|
|
|
|
if (!texture.is_null()) {
|
2023-07-03 21:29:37 +02:00
|
|
|
texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
|
2020-09-09 17:40:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
texture = p_texture;
|
|
|
|
|
|
|
|
if (!texture.is_null()) {
|
|
|
|
if (shader.is_null()) {
|
|
|
|
_make_shaders();
|
|
|
|
}
|
|
|
|
|
2023-07-03 21:29:37 +02:00
|
|
|
texture->connect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
|
2022-08-13 23:21:24 +02:00
|
|
|
queue_redraw();
|
2020-09-09 17:40:51 +02:00
|
|
|
texture_rect->set_material(material);
|
|
|
|
setting = true;
|
|
|
|
layer->set_max(texture->get_depth() - 1);
|
|
|
|
layer->set_value(0);
|
|
|
|
layer->show();
|
|
|
|
_update_material();
|
|
|
|
setting = false;
|
|
|
|
_texture_rect_update_area();
|
|
|
|
} else {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture3DEditor::Texture3DEditor() {
|
|
|
|
set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
|
|
|
|
set_custom_minimum_size(Size2(1, 150));
|
|
|
|
texture_rect = memnew(Control);
|
|
|
|
texture_rect->connect("draw", callable_mp(this, &Texture3DEditor::_texture_rect_draw));
|
|
|
|
texture_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
|
|
|
|
add_child(texture_rect);
|
|
|
|
|
|
|
|
layer = memnew(SpinBox);
|
|
|
|
layer->set_step(1);
|
|
|
|
layer->set_max(100);
|
|
|
|
add_child(layer);
|
2020-12-22 17:24:29 +01:00
|
|
|
layer->set_anchor(SIDE_RIGHT, 1);
|
|
|
|
layer->set_anchor(SIDE_LEFT, 1);
|
2020-09-09 17:40:51 +02:00
|
|
|
layer->set_h_grow_direction(GROW_DIRECTION_BEGIN);
|
|
|
|
layer->set_modulate(Color(1, 1, 1, 0.8));
|
|
|
|
info = memnew(Label);
|
|
|
|
add_child(info);
|
2020-12-22 17:24:29 +01: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);
|
2020-09-09 17:40:51 +02:00
|
|
|
info->set_h_grow_direction(GROW_DIRECTION_BEGIN);
|
|
|
|
info->set_v_grow_direction(GROW_DIRECTION_BEGIN);
|
2022-02-08 10:14:58 +01:00
|
|
|
info->add_theme_color_override("font_color", Color(1, 1, 1, 1));
|
|
|
|
info->add_theme_color_override("font_shadow_color", Color(0, 0, 0, 0.5));
|
|
|
|
info->add_theme_constant_override("shadow_outline_size", 1);
|
|
|
|
info->add_theme_constant_override("shadow_offset_x", 2);
|
|
|
|
info->add_theme_constant_override("shadow_offset_y", 2);
|
2020-09-09 17:40:51 +02:00
|
|
|
|
|
|
|
setting = false;
|
2020-12-06 16:09:18 +01:00
|
|
|
layer->connect("value_changed", callable_mp(this, &Texture3DEditor::_layer_changed));
|
2020-09-09 17:40:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Texture3DEditor::~Texture3DEditor() {
|
|
|
|
if (!texture.is_null()) {
|
2023-07-03 21:29:37 +02:00
|
|
|
texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
|
2020-09-09 17:40:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
bool EditorInspectorPlugin3DTexture::can_handle(Object *p_object) {
|
|
|
|
return Object::cast_to<Texture3D>(p_object) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorInspectorPlugin3DTexture::parse_begin(Object *p_object) {
|
|
|
|
Texture3D *texture = Object::cast_to<Texture3D>(p_object);
|
|
|
|
if (!texture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ref<Texture3D> m(texture);
|
|
|
|
|
|
|
|
Texture3DEditor *editor = memnew(Texture3DEditor);
|
|
|
|
editor->edit(m);
|
|
|
|
add_custom_control(editor);
|
|
|
|
}
|
|
|
|
|
2022-01-27 10:36:51 +01:00
|
|
|
Texture3DEditorPlugin::Texture3DEditorPlugin() {
|
2020-09-09 17:40:51 +02:00
|
|
|
Ref<EditorInspectorPlugin3DTexture> plugin;
|
2021-06-18 00:03:09 +02:00
|
|
|
plugin.instantiate();
|
2020-09-09 17:40:51 +02:00
|
|
|
add_inspector_plugin(plugin);
|
|
|
|
}
|