texture_layered_editor_plugin.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**************************************************************************/
  2. /* texture_layered_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_layered_editor_plugin.h"
  31. #include "scene/gui/label.h"
  32. void TextureLayeredEditor::gui_input(const Ref<InputEvent> &p_event) {
  33. ERR_FAIL_COND(p_event.is_null());
  34. Ref<InputEventMouseMotion> mm = p_event;
  35. if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  36. y_rot += -mm->get_relative().x * 0.01;
  37. x_rot += mm->get_relative().y * 0.01;
  38. _update_material();
  39. }
  40. }
  41. void TextureLayeredEditor::_texture_rect_draw() {
  42. texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
  43. }
  44. void TextureLayeredEditor::_notification(int p_what) {
  45. switch (p_what) {
  46. case NOTIFICATION_RESIZED: {
  47. _texture_rect_update_area();
  48. } break;
  49. case NOTIFICATION_DRAW: {
  50. Ref<Texture2D> checkerboard = get_theme_icon(SNAME("Checkerboard"), SNAME("EditorIcons"));
  51. Size2 size = get_size();
  52. draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
  53. } break;
  54. }
  55. }
  56. void TextureLayeredEditor::_texture_changed() {
  57. if (!is_visible()) {
  58. return;
  59. }
  60. queue_redraw();
  61. }
  62. void TextureLayeredEditor::_update_material() {
  63. materials[0]->set_shader_parameter("layer", layer->get_value());
  64. materials[2]->set_shader_parameter("layer", layer->get_value());
  65. materials[texture->get_layered_type()]->set_shader_parameter("tex", texture->get_rid());
  66. Vector3 v(1, 1, 1);
  67. v.normalize();
  68. Basis b;
  69. b.rotate(Vector3(1, 0, 0), x_rot);
  70. b.rotate(Vector3(0, 1, 0), y_rot);
  71. materials[1]->set_shader_parameter("normal", v);
  72. materials[1]->set_shader_parameter("rot", b);
  73. materials[2]->set_shader_parameter("normal", v);
  74. materials[2]->set_shader_parameter("rot", b);
  75. String format = Image::get_format_name(texture->get_format());
  76. String text;
  77. if (texture->get_layered_type() == TextureLayered::LAYERED_TYPE_2D_ARRAY) {
  78. text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " (x " + itos(texture->get_layers()) + ")" + format;
  79. } else if (texture->get_layered_type() == TextureLayered::LAYERED_TYPE_CUBEMAP) {
  80. text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " " + format;
  81. } else if (texture->get_layered_type() == TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY) {
  82. text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " (x " + itos(texture->get_layers() / 6) + ")" + format;
  83. }
  84. info->set_text(text);
  85. }
  86. void TextureLayeredEditor::_make_shaders() {
  87. shaders[0].instantiate();
  88. shaders[0]->set_code(R"(
  89. // TextureLayeredEditor preview shader (2D array).
  90. shader_type canvas_item;
  91. uniform sampler2DArray tex;
  92. uniform float layer;
  93. void fragment() {
  94. COLOR = textureLod(tex, vec3(UV, layer), 0.0);
  95. }
  96. )");
  97. shaders[1].instantiate();
  98. shaders[1]->set_code(R"(
  99. // TextureLayeredEditor preview shader (cubemap).
  100. shader_type canvas_item;
  101. uniform samplerCube tex;
  102. uniform vec3 normal;
  103. uniform mat3 rot;
  104. void fragment() {
  105. vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
  106. COLOR = textureLod(tex, n, 0.0);
  107. }
  108. )");
  109. shaders[2].instantiate();
  110. shaders[2]->set_code(R"(
  111. // TextureLayeredEditor preview shader (cubemap array).
  112. shader_type canvas_item;
  113. uniform samplerCubeArray tex;
  114. uniform vec3 normal;
  115. uniform mat3 rot;
  116. uniform float layer;
  117. void fragment() {
  118. vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
  119. COLOR = textureLod(tex, vec4(n, layer), 0.0);
  120. }
  121. )");
  122. for (int i = 0; i < 3; i++) {
  123. materials[i].instantiate();
  124. materials[i]->set_shader(shaders[i]);
  125. }
  126. }
  127. void TextureLayeredEditor::_texture_rect_update_area() {
  128. Size2 size = get_size();
  129. int tex_width = texture->get_width() * size.height / texture->get_height();
  130. int tex_height = size.height;
  131. if (tex_width > size.width) {
  132. tex_width = size.width;
  133. tex_height = texture->get_height() * tex_width / texture->get_width();
  134. }
  135. // Prevent the texture from being unpreviewable after the rescale, so that we can still see something
  136. if (tex_height <= 0) {
  137. tex_height = 1;
  138. }
  139. if (tex_width <= 0) {
  140. tex_width = 1;
  141. }
  142. int ofs_x = (size.width - tex_width) / 2;
  143. int ofs_y = (size.height - tex_height) / 2;
  144. texture_rect->set_position(Vector2(ofs_x, ofs_y));
  145. texture_rect->set_size(Vector2(tex_width, tex_height));
  146. }
  147. void TextureLayeredEditor::edit(Ref<TextureLayered> p_texture) {
  148. if (!texture.is_null()) {
  149. texture->disconnect("changed", callable_mp(this, &TextureLayeredEditor::_texture_changed));
  150. }
  151. texture = p_texture;
  152. if (!texture.is_null()) {
  153. if (shaders[0].is_null()) {
  154. _make_shaders();
  155. }
  156. texture->connect("changed", callable_mp(this, &TextureLayeredEditor::_texture_changed));
  157. queue_redraw();
  158. texture_rect->set_material(materials[texture->get_layered_type()]);
  159. setting = true;
  160. if (texture->get_layered_type() == TextureLayered::LAYERED_TYPE_2D_ARRAY) {
  161. layer->set_max(texture->get_layers() - 1);
  162. layer->set_value(0);
  163. layer->show();
  164. } else if (texture->get_layered_type() == TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY) {
  165. layer->set_max(texture->get_layers() / 6 - 1);
  166. layer->set_value(0);
  167. layer->show();
  168. } else {
  169. layer->hide();
  170. }
  171. x_rot = 0;
  172. y_rot = 0;
  173. _update_material();
  174. setting = false;
  175. _texture_rect_update_area();
  176. } else {
  177. hide();
  178. }
  179. }
  180. TextureLayeredEditor::TextureLayeredEditor() {
  181. set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
  182. set_custom_minimum_size(Size2(1, 150));
  183. texture_rect = memnew(Control);
  184. texture_rect->connect("draw", callable_mp(this, &TextureLayeredEditor::_texture_rect_draw));
  185. texture_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
  186. add_child(texture_rect);
  187. layer = memnew(SpinBox);
  188. layer->set_step(1);
  189. layer->set_max(100);
  190. add_child(layer);
  191. layer->set_anchor(SIDE_RIGHT, 1);
  192. layer->set_anchor(SIDE_LEFT, 1);
  193. layer->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  194. layer->set_modulate(Color(1, 1, 1, 0.8));
  195. info = memnew(Label);
  196. add_child(info);
  197. info->set_anchor(SIDE_RIGHT, 1);
  198. info->set_anchor(SIDE_LEFT, 1);
  199. info->set_anchor(SIDE_BOTTOM, 1);
  200. info->set_anchor(SIDE_TOP, 1);
  201. info->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  202. info->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  203. info->add_theme_color_override("font_color", Color(1, 1, 1, 1));
  204. info->add_theme_color_override("font_shadow_color", Color(0, 0, 0, 0.5));
  205. info->add_theme_constant_override("shadow_outline_size", 1);
  206. info->add_theme_constant_override("shadow_offset_x", 2);
  207. info->add_theme_constant_override("shadow_offset_y", 2);
  208. setting = false;
  209. layer->connect("value_changed", callable_mp(this, &TextureLayeredEditor::_layer_changed));
  210. }
  211. TextureLayeredEditor::~TextureLayeredEditor() {
  212. }
  213. //
  214. bool EditorInspectorPluginLayeredTexture::can_handle(Object *p_object) {
  215. return Object::cast_to<TextureLayered>(p_object) != nullptr;
  216. }
  217. void EditorInspectorPluginLayeredTexture::parse_begin(Object *p_object) {
  218. TextureLayered *texture = Object::cast_to<TextureLayered>(p_object);
  219. if (!texture) {
  220. return;
  221. }
  222. Ref<TextureLayered> m(texture);
  223. TextureLayeredEditor *editor = memnew(TextureLayeredEditor);
  224. editor->edit(m);
  225. add_custom_control(editor);
  226. }
  227. TextureLayeredEditorPlugin::TextureLayeredEditorPlugin() {
  228. Ref<EditorInspectorPluginLayeredTexture> plugin;
  229. plugin.instantiate();
  230. add_inspector_plugin(plugin);
  231. }