texture_3d_editor_plugin.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include "texture_3d_editor_plugin.h"
  31. #include "editor/editor_string_names.h"
  32. #include "editor/scene/texture/color_channel_selector.h"
  33. #include "editor/themes/editor_scale.h"
  34. #include "scene/gui/label.h"
  35. // Shader sources.
  36. constexpr const char *texture_3d_shader = R"(
  37. // Texture3DEditor preview shader.
  38. shader_type canvas_item;
  39. uniform sampler3D tex;
  40. uniform float layer;
  41. uniform vec4 u_channel_factors = vec4(1.0);
  42. vec4 filter_preview_colors(vec4 input_color, vec4 factors) {
  43. // Filter RGB.
  44. vec4 output_color = input_color * vec4(factors.rgb, input_color.a);
  45. // Remove transparency when alpha is not enabled.
  46. output_color.a = mix(1.0, output_color.a, factors.a);
  47. // Switch to opaque grayscale when visualizing only one channel.
  48. float csum = factors.r + factors.g + factors.b + factors.a;
  49. float single = clamp(2.0 - csum, 0.0, 1.0);
  50. for (int i = 0; i < 4; i++) {
  51. float c = input_color[i];
  52. output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single);
  53. }
  54. return output_color;
  55. }
  56. void fragment() {
  57. COLOR = textureLod(tex, vec3(UV, layer), 0.0);
  58. COLOR = filter_preview_colors(COLOR, u_channel_factors);
  59. }
  60. )";
  61. void Texture3DEditor::_texture_rect_draw() {
  62. texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
  63. }
  64. void Texture3DEditor::_notification(int p_what) {
  65. switch (p_what) {
  66. case NOTIFICATION_RESIZED: {
  67. _texture_rect_update_area();
  68. } break;
  69. case NOTIFICATION_DRAW: {
  70. Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
  71. draw_texture_rect(checkerboard, texture_rect->get_rect(), true);
  72. _draw_outline();
  73. } break;
  74. case NOTIFICATION_THEME_CHANGED: {
  75. if (info) {
  76. Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
  77. info->add_theme_font_override(SceneStringName(font), metadata_label_font);
  78. }
  79. theme_cache.outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor));
  80. } break;
  81. }
  82. }
  83. void Texture3DEditor::_texture_changed() {
  84. if (!is_visible()) {
  85. return;
  86. }
  87. setting = true;
  88. _update_gui();
  89. setting = false;
  90. _update_material(true);
  91. queue_redraw();
  92. }
  93. void Texture3DEditor::_update_material(bool p_texture_changed) {
  94. material->set_shader_parameter("layer", (layer->get_value() + 0.5) / texture->get_depth());
  95. if (p_texture_changed) {
  96. material->set_shader_parameter("tex", texture->get_rid());
  97. }
  98. material->set_shader_parameter("u_channel_factors", channel_selector->get_selected_channel_factors());
  99. }
  100. void Texture3DEditor::_draw_outline() {
  101. const float outline_width = Math::round(EDSCALE);
  102. const Rect2 outline_rect = texture_rect->get_rect().grow(outline_width * 0.5);
  103. draw_rect(outline_rect, theme_cache.outline_color, false, outline_width);
  104. }
  105. void Texture3DEditor::_make_shaders() {
  106. shader.instantiate();
  107. shader->set_code(texture_3d_shader);
  108. material.instantiate();
  109. material->set_shader(shader);
  110. }
  111. void Texture3DEditor::_texture_rect_update_area() {
  112. Size2 size = get_size();
  113. int tex_width = texture->get_width() * size.height / texture->get_height();
  114. int tex_height = size.height;
  115. if (tex_width > size.width) {
  116. tex_width = size.width;
  117. tex_height = texture->get_height() * tex_width / texture->get_width();
  118. }
  119. // Prevent the texture from being unpreviewable after the rescale, so that we can still see something
  120. if (tex_height <= 0) {
  121. tex_height = 1;
  122. }
  123. if (tex_width <= 0) {
  124. tex_width = 1;
  125. }
  126. int ofs_x = (size.width - tex_width) / 2;
  127. int ofs_y = (size.height - tex_height) / 2;
  128. texture_rect->set_position(Vector2(ofs_x, ofs_y - Math::round(EDSCALE)));
  129. texture_rect->set_size(Vector2(tex_width, tex_height));
  130. }
  131. void Texture3DEditor::_update_gui() {
  132. if (texture.is_null()) {
  133. return;
  134. }
  135. _texture_rect_update_area();
  136. layer->set_max(texture->get_depth() - 1);
  137. const Image::Format format = texture->get_format();
  138. const String format_name = Image::get_format_name(format);
  139. if (texture->has_mipmaps()) {
  140. const int mip_count = Image::get_image_required_mipmaps(texture->get_width(), texture->get_height(), format);
  141. const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, true) * texture->get_depth();
  142. info->set_text(vformat(String::utf8("%d×%d×%d %s\n") + TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
  143. texture->get_width(),
  144. texture->get_height(),
  145. texture->get_depth(),
  146. format_name,
  147. mip_count,
  148. String::humanize_size(memory)));
  149. } else {
  150. const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, false) * texture->get_depth();
  151. info->set_text(vformat(String::utf8("%d×%d×%d %s\n") + TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
  152. texture->get_width(),
  153. texture->get_height(),
  154. texture->get_depth(),
  155. format_name,
  156. String::humanize_size(memory)));
  157. }
  158. const uint32_t components_mask = Image::get_format_component_mask(format);
  159. if (is_power_of_2(components_mask)) {
  160. // Only one channel available, no point in showing a channel selector.
  161. channel_selector->hide();
  162. } else {
  163. channel_selector->show();
  164. channel_selector->set_available_channels_mask(components_mask);
  165. }
  166. }
  167. void Texture3DEditor::on_selected_channels_changed() {
  168. _update_material(false);
  169. }
  170. void Texture3DEditor::edit(Ref<Texture3D> p_texture) {
  171. if (texture.is_valid()) {
  172. texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
  173. }
  174. texture = p_texture;
  175. if (texture.is_valid()) {
  176. if (shader.is_null()) {
  177. _make_shaders();
  178. }
  179. texture->connect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
  180. texture_rect->set_material(material);
  181. setting = true;
  182. layer->set_value(0);
  183. layer->show();
  184. _update_gui();
  185. setting = false;
  186. _update_material(true);
  187. queue_redraw();
  188. } else {
  189. hide();
  190. }
  191. }
  192. Texture3DEditor::Texture3DEditor() {
  193. set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
  194. set_custom_minimum_size(Size2(1, 256.0) * EDSCALE);
  195. texture_rect = memnew(Control);
  196. texture_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
  197. texture_rect->connect(SceneStringName(draw), callable_mp(this, &Texture3DEditor::_texture_rect_draw));
  198. add_child(texture_rect);
  199. layer = memnew(SpinBox);
  200. layer->set_step(1);
  201. layer->set_max(100);
  202. layer->set_modulate(Color(1, 1, 1, 0.8));
  203. layer->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  204. layer->set_anchor(SIDE_RIGHT, 1);
  205. layer->set_anchor(SIDE_LEFT, 1);
  206. layer->connect(SceneStringName(value_changed), callable_mp(this, &Texture3DEditor::_layer_changed));
  207. add_child(layer);
  208. channel_selector = memnew(ColorChannelSelector);
  209. channel_selector->connect("selected_channels_changed", callable_mp(this, &Texture3DEditor::on_selected_channels_changed));
  210. channel_selector->set_anchors_preset(Control::PRESET_TOP_LEFT);
  211. add_child(channel_selector);
  212. info = memnew(Label);
  213. info->set_focus_mode(FOCUS_ACCESSIBILITY);
  214. info->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));
  215. info->add_theme_color_override("font_shadow_color", Color(0, 0, 0));
  216. info->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE);
  217. info->add_theme_color_override("font_outline_color", Color(0, 0, 0));
  218. info->add_theme_constant_override("outline_size", 8 * EDSCALE);
  219. info->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  220. info->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  221. info->set_h_size_flags(Control::SIZE_SHRINK_END);
  222. info->set_v_size_flags(Control::SIZE_SHRINK_END);
  223. info->set_anchor(SIDE_RIGHT, 1);
  224. info->set_anchor(SIDE_LEFT, 1);
  225. info->set_anchor(SIDE_BOTTOM, 1);
  226. info->set_anchor(SIDE_TOP, 1);
  227. add_child(info);
  228. }
  229. Texture3DEditor::~Texture3DEditor() {
  230. if (texture.is_valid()) {
  231. texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
  232. }
  233. }
  234. bool EditorInspectorPlugin3DTexture::can_handle(Object *p_object) {
  235. return Object::cast_to<Texture3D>(p_object) != nullptr;
  236. }
  237. void EditorInspectorPlugin3DTexture::parse_begin(Object *p_object) {
  238. Texture3D *texture = Object::cast_to<Texture3D>(p_object);
  239. if (!texture) {
  240. return;
  241. }
  242. Ref<Texture3D> m(texture);
  243. Texture3DEditor *editor = memnew(Texture3DEditor);
  244. editor->edit(m);
  245. add_custom_control(editor);
  246. }
  247. Texture3DEditorPlugin::Texture3DEditorPlugin() {
  248. Ref<EditorInspectorPlugin3DTexture> plugin;
  249. plugin.instantiate();
  250. add_inspector_plugin(plugin);
  251. }