texture_3d_editor_plugin.cpp 8.4 KB

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