texture_3d_editor_plugin.cpp 6.9 KB

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