texture_3d_editor_plugin.cpp 6.9 KB

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