1/**************************************************************************/
2/* texture_3d_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_3d_editor_plugin.h"
32
33#include "scene/gui/label.h"
34
35void Texture3DEditor::_texture_rect_draw() {
36 texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
37}
38
39void Texture3DEditor::_notification(int p_what) {
40 switch (p_what) {
41 case NOTIFICATION_RESIZED: {
42 _texture_rect_update_area();
43 } break;
44
45 case NOTIFICATION_DRAW: {
46 Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
47 Size2 size = get_size();
48
49 draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
50 } break;
51 }
52}
53
54void Texture3DEditor::_texture_changed() {
55 if (!is_visible()) {
56 return;
57 }
58 queue_redraw();
59}
60
61void Texture3DEditor::_update_material() {
62 material->set_shader_parameter("layer", (layer->get_value() + 0.5) / texture->get_depth());
63 material->set_shader_parameter("tex", texture->get_rid());
64
65 String format = Image::get_format_name(texture->get_format());
66
67 String text;
68 text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + "x" + itos(texture->get_depth()) + " " + format;
69
70 info->set_text(text);
71}
72
73void Texture3DEditor::_make_shaders() {
74 shader.instantiate();
75 shader->set_code(R"(
76// Texture3DEditor preview shader.
77
78shader_type canvas_item;
79
80uniform sampler3D tex;
81uniform float layer;
82
83void fragment() {
84 COLOR = textureLod(tex, vec3(UV, layer), 0.0);
85}
86)");
87 material.instantiate();
88 material->set_shader(shader);
89}
90
91void Texture3DEditor::_texture_rect_update_area() {
92 Size2 size = get_size();
93 int tex_width = texture->get_width() * size.height / texture->get_height();
94 int tex_height = size.height;
95
96 if (tex_width > size.width) {
97 tex_width = size.width;
98 tex_height = texture->get_height() * tex_width / texture->get_width();
99 }
100
101 // Prevent the texture from being unpreviewable after the rescale, so that we can still see something
102 if (tex_height <= 0) {
103 tex_height = 1;
104 }
105 if (tex_width <= 0) {
106 tex_width = 1;
107 }
108
109 int ofs_x = (size.width - tex_width) / 2;
110 int ofs_y = (size.height - tex_height) / 2;
111
112 texture_rect->set_position(Vector2(ofs_x, ofs_y));
113 texture_rect->set_size(Vector2(tex_width, tex_height));
114}
115
116void Texture3DEditor::edit(Ref<Texture3D> p_texture) {
117 if (!texture.is_null()) {
118 texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
119 }
120
121 texture = p_texture;
122
123 if (!texture.is_null()) {
124 if (shader.is_null()) {
125 _make_shaders();
126 }
127
128 texture->connect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
129 queue_redraw();
130 texture_rect->set_material(material);
131 setting = true;
132 layer->set_max(texture->get_depth() - 1);
133 layer->set_value(0);
134 layer->show();
135 _update_material();
136 setting = false;
137 _texture_rect_update_area();
138 } else {
139 hide();
140 }
141}
142
143Texture3DEditor::Texture3DEditor() {
144 set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
145 set_custom_minimum_size(Size2(1, 150));
146 texture_rect = memnew(Control);
147 texture_rect->connect("draw", callable_mp(this, &Texture3DEditor::_texture_rect_draw));
148 texture_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
149 add_child(texture_rect);
150
151 layer = memnew(SpinBox);
152 layer->set_step(1);
153 layer->set_max(100);
154 add_child(layer);
155 layer->set_anchor(SIDE_RIGHT, 1);
156 layer->set_anchor(SIDE_LEFT, 1);
157 layer->set_h_grow_direction(GROW_DIRECTION_BEGIN);
158 layer->set_modulate(Color(1, 1, 1, 0.8));
159 info = memnew(Label);
160 add_child(info);
161 info->set_anchor(SIDE_RIGHT, 1);
162 info->set_anchor(SIDE_LEFT, 1);
163 info->set_anchor(SIDE_BOTTOM, 1);
164 info->set_anchor(SIDE_TOP, 1);
165 info->set_h_grow_direction(GROW_DIRECTION_BEGIN);
166 info->set_v_grow_direction(GROW_DIRECTION_BEGIN);
167 info->add_theme_color_override("font_color", Color(1, 1, 1, 1));
168 info->add_theme_color_override("font_shadow_color", Color(0, 0, 0, 0.5));
169 info->add_theme_constant_override("shadow_outline_size", 1);
170 info->add_theme_constant_override("shadow_offset_x", 2);
171 info->add_theme_constant_override("shadow_offset_y", 2);
172
173 setting = false;
174 layer->connect("value_changed", callable_mp(this, &Texture3DEditor::_layer_changed));
175}
176
177Texture3DEditor::~Texture3DEditor() {
178 if (!texture.is_null()) {
179 texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
180 }
181}
182
183//
184bool EditorInspectorPlugin3DTexture::can_handle(Object *p_object) {
185 return Object::cast_to<Texture3D>(p_object) != nullptr;
186}
187
188void EditorInspectorPlugin3DTexture::parse_begin(Object *p_object) {
189 Texture3D *texture = Object::cast_to<Texture3D>(p_object);
190 if (!texture) {
191 return;
192 }
193 Ref<Texture3D> m(texture);
194
195 Texture3DEditor *editor = memnew(Texture3DEditor);
196 editor->edit(m);
197 add_custom_control(editor);
198}
199
200Texture3DEditorPlugin::Texture3DEditorPlugin() {
201 Ref<EditorInspectorPlugin3DTexture> plugin;
202 plugin.instantiate();
203 add_inspector_plugin(plugin);
204}
205