texture_3d_editor_plugin.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/plugins/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. Size2 size = get_size();
  72. draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
  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. } break;
  80. }
  81. }
  82. void Texture3DEditor::_texture_changed() {
  83. if (!is_visible()) {
  84. return;
  85. }
  86. setting = true;
  87. _update_gui();
  88. setting = false;
  89. _update_material(true);
  90. queue_redraw();
  91. }
  92. void Texture3DEditor::_update_material(bool p_texture_changed) {
  93. material->set_shader_parameter("layer", (layer->get_value() + 0.5) / texture->get_depth());
  94. if (p_texture_changed) {
  95. material->set_shader_parameter("tex", texture->get_rid());
  96. }
  97. material->set_shader_parameter("u_channel_factors", channel_selector->get_selected_channel_factors());
  98. }
  99. void Texture3DEditor::_make_shaders() {
  100. shader.instantiate();
  101. shader->set_code(texture_3d_shader);
  102. material.instantiate();
  103. material->set_shader(shader);
  104. }
  105. void Texture3DEditor::_texture_rect_update_area() {
  106. Size2 size = get_size();
  107. int tex_width = texture->get_width() * size.height / texture->get_height();
  108. int tex_height = size.height;
  109. if (tex_width > size.width) {
  110. tex_width = size.width;
  111. tex_height = texture->get_height() * tex_width / texture->get_width();
  112. }
  113. // Prevent the texture from being unpreviewable after the rescale, so that we can still see something
  114. if (tex_height <= 0) {
  115. tex_height = 1;
  116. }
  117. if (tex_width <= 0) {
  118. tex_width = 1;
  119. }
  120. int ofs_x = (size.width - tex_width) / 2;
  121. int ofs_y = (size.height - tex_height) / 2;
  122. texture_rect->set_position(Vector2(ofs_x, ofs_y));
  123. texture_rect->set_size(Vector2(tex_width, tex_height));
  124. }
  125. void Texture3DEditor::_update_gui() {
  126. if (texture.is_null()) {
  127. return;
  128. }
  129. _texture_rect_update_area();
  130. layer->set_max(texture->get_depth() - 1);
  131. const Image::Format format = texture->get_format();
  132. const String format_name = Image::get_format_name(format);
  133. if (texture->has_mipmaps()) {
  134. const int mip_count = Image::get_image_required_mipmaps(texture->get_width(), texture->get_height(), format);
  135. const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, true) * texture->get_depth();
  136. info->set_text(vformat(String::utf8("%d×%d×%d %s\n") + TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
  137. texture->get_width(),
  138. texture->get_height(),
  139. texture->get_depth(),
  140. format_name,
  141. mip_count,
  142. String::humanize_size(memory)));
  143. } else {
  144. const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, false) * texture->get_depth();
  145. info->set_text(vformat(String::utf8("%d×%d×%d %s\n") + TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
  146. texture->get_width(),
  147. texture->get_height(),
  148. texture->get_depth(),
  149. format_name,
  150. String::humanize_size(memory)));
  151. }
  152. const uint32_t components_mask = Image::get_format_component_mask(format);
  153. if (is_power_of_2(components_mask)) {
  154. // Only one channel available, no point in showing a channel selector.
  155. channel_selector->hide();
  156. } else {
  157. channel_selector->show();
  158. channel_selector->set_available_channels_mask(components_mask);
  159. }
  160. }
  161. void Texture3DEditor::on_selected_channels_changed() {
  162. _update_material(false);
  163. }
  164. void Texture3DEditor::edit(Ref<Texture3D> p_texture) {
  165. if (texture.is_valid()) {
  166. texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
  167. }
  168. texture = p_texture;
  169. if (texture.is_valid()) {
  170. if (shader.is_null()) {
  171. _make_shaders();
  172. }
  173. texture->connect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
  174. texture_rect->set_material(material);
  175. setting = true;
  176. layer->set_value(0);
  177. layer->show();
  178. _update_gui();
  179. setting = false;
  180. _update_material(true);
  181. queue_redraw();
  182. } else {
  183. hide();
  184. }
  185. }
  186. Texture3DEditor::Texture3DEditor() {
  187. set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
  188. set_custom_minimum_size(Size2(1, 256.0) * EDSCALE);
  189. texture_rect = memnew(Control);
  190. texture_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
  191. texture_rect->connect(SceneStringName(draw), callable_mp(this, &Texture3DEditor::_texture_rect_draw));
  192. add_child(texture_rect);
  193. layer = memnew(SpinBox);
  194. layer->set_step(1);
  195. layer->set_max(100);
  196. layer->set_modulate(Color(1, 1, 1, 0.8));
  197. layer->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  198. layer->set_anchor(SIDE_RIGHT, 1);
  199. layer->set_anchor(SIDE_LEFT, 1);
  200. layer->connect(SceneStringName(value_changed), callable_mp(this, &Texture3DEditor::_layer_changed));
  201. add_child(layer);
  202. channel_selector = memnew(ColorChannelSelector);
  203. channel_selector->connect("selected_channels_changed", callable_mp(this, &Texture3DEditor::on_selected_channels_changed));
  204. channel_selector->set_anchors_preset(Control::PRESET_TOP_LEFT);
  205. add_child(channel_selector);
  206. info = memnew(Label);
  207. info->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));
  208. info->add_theme_color_override("font_shadow_color", Color(0, 0, 0));
  209. info->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE);
  210. info->add_theme_color_override("font_outline_color", Color(0, 0, 0));
  211. info->add_theme_constant_override("outline_size", 8 * EDSCALE);
  212. info->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  213. info->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  214. info->set_h_size_flags(Control::SIZE_SHRINK_END);
  215. info->set_v_size_flags(Control::SIZE_SHRINK_END);
  216. info->set_anchor(SIDE_RIGHT, 1);
  217. info->set_anchor(SIDE_LEFT, 1);
  218. info->set_anchor(SIDE_BOTTOM, 1);
  219. info->set_anchor(SIDE_TOP, 1);
  220. add_child(info);
  221. }
  222. Texture3DEditor::~Texture3DEditor() {
  223. if (texture.is_valid()) {
  224. texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
  225. }
  226. }
  227. bool EditorInspectorPlugin3DTexture::can_handle(Object *p_object) {
  228. return Object::cast_to<Texture3D>(p_object) != nullptr;
  229. }
  230. void EditorInspectorPlugin3DTexture::parse_begin(Object *p_object) {
  231. Texture3D *texture = Object::cast_to<Texture3D>(p_object);
  232. if (!texture) {
  233. return;
  234. }
  235. Ref<Texture3D> m(texture);
  236. Texture3DEditor *editor = memnew(Texture3DEditor);
  237. editor->edit(m);
  238. add_custom_control(editor);
  239. }
  240. Texture3DEditorPlugin::Texture3DEditorPlugin() {
  241. Ref<EditorInspectorPlugin3DTexture> plugin;
  242. plugin.instantiate();
  243. add_inspector_plugin(plugin);
  244. }