texture_layered_editor_plugin.cpp 9.2 KB

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