texture_layered_editor_plugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 "editor/editor_string_names.h"
  32. #include "editor/plugins/color_channel_selector.h"
  33. #include "editor/themes/editor_scale.h"
  34. #include "scene/gui/label.h"
  35. // Shader sources.
  36. constexpr const char *array_2d_shader = R"(
  37. // TextureLayeredEditor preview shader (2D array).
  38. shader_type canvas_item;
  39. uniform sampler2DArray tex;
  40. uniform float layer;
  41. uniform vec4 u_channel_factors = vec4(1.0);
  42. vec4 filter_preview_colors(vec4 input_color, vec4 factors) {
  43. // Filter RGB.
  44. vec4 output_color = input_color * vec4(factors.rgb, input_color.a);
  45. // Remove transparency when alpha is not enabled.
  46. output_color.a = mix(1.0, output_color.a, factors.a);
  47. // Switch to opaque grayscale when visualizing only one channel.
  48. float csum = factors.r + factors.g + factors.b + factors.a;
  49. float single = clamp(2.0 - csum, 0.0, 1.0);
  50. for (int i = 0; i < 4; i++) {
  51. float c = input_color[i];
  52. output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single);
  53. }
  54. return output_color;
  55. }
  56. void fragment() {
  57. COLOR = textureLod(tex, vec3(UV, layer), 0.0);
  58. COLOR = filter_preview_colors(COLOR, u_channel_factors);
  59. }
  60. )";
  61. constexpr const char *cubemap_shader = R"(
  62. // TextureLayeredEditor preview shader (cubemap).
  63. shader_type canvas_item;
  64. uniform samplerCube tex;
  65. uniform vec3 normal;
  66. uniform mat3 rot;
  67. uniform vec4 u_channel_factors = vec4(1.0);
  68. vec4 filter_preview_colors(vec4 input_color, vec4 factors) {
  69. // Filter RGB.
  70. vec4 output_color = input_color * vec4(factors.rgb, input_color.a);
  71. // Remove transparency when alpha is not enabled.
  72. output_color.a = mix(1.0, output_color.a, factors.a);
  73. // Switch to opaque grayscale when visualizing only one channel.
  74. float csum = factors.r + factors.g + factors.b + factors.a;
  75. float single = clamp(2.0 - csum, 0.0, 1.0);
  76. for (int i = 0; i < 4; i++) {
  77. float c = input_color[i];
  78. output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single);
  79. }
  80. return output_color;
  81. }
  82. void fragment() {
  83. vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
  84. COLOR = textureLod(tex, n, 0.0);
  85. COLOR = filter_preview_colors(COLOR, u_channel_factors);
  86. }
  87. )";
  88. constexpr const char *cubemap_array_shader = R"(
  89. // TextureLayeredEditor preview shader (cubemap array).
  90. shader_type canvas_item;
  91. uniform samplerCubeArray tex;
  92. uniform vec3 normal;
  93. uniform mat3 rot;
  94. uniform float layer;
  95. uniform vec4 u_channel_factors = vec4(1.0);
  96. vec4 filter_preview_colors(vec4 input_color, vec4 factors) {
  97. // Filter RGB.
  98. vec4 output_color = input_color * vec4(factors.rgb, input_color.a);
  99. // Remove transparency when alpha is not enabled.
  100. output_color.a = mix(1.0, output_color.a, factors.a);
  101. // Switch to opaque grayscale when visualizing only one channel.
  102. float csum = factors.r + factors.g + factors.b + factors.a;
  103. float single = clamp(2.0 - csum, 0.0, 1.0);
  104. for (int i = 0; i < 4; i++) {
  105. float c = input_color[i];
  106. output_color = mix(output_color, vec4(c, c, c, 1.0), factors[i] * single);
  107. }
  108. return output_color;
  109. }
  110. void fragment() {
  111. vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
  112. COLOR = textureLod(tex, vec4(n, layer), 0.0);
  113. COLOR = filter_preview_colors(COLOR, u_channel_factors);
  114. }
  115. )";
  116. void TextureLayeredEditor::gui_input(const Ref<InputEvent> &p_event) {
  117. ERR_FAIL_COND(p_event.is_null());
  118. Ref<InputEventMouseMotion> mm = p_event;
  119. if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  120. y_rot += -mm->get_relative().x * 0.01;
  121. x_rot += -mm->get_relative().y * 0.01;
  122. _update_material(false);
  123. }
  124. }
  125. void TextureLayeredEditor::_texture_rect_draw() {
  126. texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
  127. }
  128. void TextureLayeredEditor::_update_gui() {
  129. if (texture.is_null()) {
  130. return;
  131. }
  132. _texture_rect_update_area();
  133. const Image::Format format = texture->get_format();
  134. const String format_name = Image::get_format_name(format);
  135. String texture_info;
  136. switch (texture->get_layered_type()) {
  137. case TextureLayered::LAYERED_TYPE_2D_ARRAY: {
  138. layer->set_max(texture->get_layers() - 1);
  139. texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"),
  140. texture->get_width(),
  141. texture->get_height(),
  142. texture->get_layers(),
  143. format_name);
  144. } break;
  145. case TextureLayered::LAYERED_TYPE_CUBEMAP: {
  146. layer->hide();
  147. texture_info = vformat(String::utf8("%d×%d %s\n"),
  148. texture->get_width(),
  149. texture->get_height(),
  150. format_name);
  151. } break;
  152. case TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY: {
  153. layer->set_max(texture->get_layers() / 6 - 1);
  154. texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"),
  155. texture->get_width(),
  156. texture->get_height(),
  157. texture->get_layers() / 6,
  158. format_name);
  159. } break;
  160. default: {
  161. }
  162. }
  163. if (texture->has_mipmaps()) {
  164. const int mip_count = Image::get_image_required_mipmaps(texture->get_width(), texture->get_height(), format);
  165. const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, true) * texture->get_layers();
  166. texture_info += vformat(TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
  167. mip_count,
  168. String::humanize_size(memory));
  169. } else {
  170. const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, false) * texture->get_layers();
  171. texture_info += vformat(TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
  172. String::humanize_size(memory));
  173. }
  174. info->set_text(texture_info);
  175. const uint32_t components_mask = Image::get_format_component_mask(format);
  176. if (is_power_of_2(components_mask)) {
  177. // Only one channel available, no point in showing a channel selector.
  178. channel_selector->hide();
  179. } else {
  180. channel_selector->show();
  181. channel_selector->set_available_channels_mask(components_mask);
  182. }
  183. }
  184. void TextureLayeredEditor::_notification(int p_what) {
  185. switch (p_what) {
  186. case NOTIFICATION_RESIZED: {
  187. _texture_rect_update_area();
  188. } break;
  189. case NOTIFICATION_DRAW: {
  190. Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
  191. Size2 size = get_size();
  192. draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
  193. } break;
  194. case NOTIFICATION_THEME_CHANGED: {
  195. if (info) {
  196. Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
  197. info->add_theme_font_override(SceneStringName(font), metadata_label_font);
  198. }
  199. } break;
  200. }
  201. }
  202. void TextureLayeredEditor::_texture_changed() {
  203. if (!is_visible()) {
  204. return;
  205. }
  206. setting = true;
  207. _update_gui();
  208. setting = false;
  209. _update_material(true);
  210. queue_redraw();
  211. }
  212. void TextureLayeredEditor::_update_material(bool p_texture_changed) {
  213. materials[0]->set_shader_parameter("layer", layer->get_value());
  214. materials[2]->set_shader_parameter("layer", layer->get_value());
  215. Vector3 v(1, 1, 1);
  216. v.normalize();
  217. Basis b;
  218. b.rotate(Vector3(1, 0, 0), x_rot);
  219. b.rotate(Vector3(0, 1, 0), y_rot);
  220. materials[1]->set_shader_parameter("normal", v);
  221. materials[1]->set_shader_parameter("rot", b);
  222. materials[2]->set_shader_parameter("normal", v);
  223. materials[2]->set_shader_parameter("rot", b);
  224. if (p_texture_changed) {
  225. materials[texture->get_layered_type()]->set_shader_parameter("tex", texture->get_rid());
  226. }
  227. const Vector4 channel_factors = channel_selector->get_selected_channel_factors();
  228. for (unsigned int i = 0; i < 3; ++i) {
  229. materials[i]->set_shader_parameter("u_channel_factors", channel_factors);
  230. }
  231. }
  232. void TextureLayeredEditor::on_selected_channels_changed() {
  233. _update_material(false);
  234. }
  235. void TextureLayeredEditor::_make_shaders() {
  236. shaders[0].instantiate();
  237. shaders[0]->set_code(array_2d_shader);
  238. shaders[1].instantiate();
  239. shaders[1]->set_code(cubemap_shader);
  240. shaders[2].instantiate();
  241. shaders[2]->set_code(cubemap_array_shader);
  242. for (int i = 0; i < 3; i++) {
  243. materials[i].instantiate();
  244. materials[i]->set_shader(shaders[i]);
  245. }
  246. }
  247. void TextureLayeredEditor::_texture_rect_update_area() {
  248. Size2 size = get_size();
  249. int tex_width = texture->get_width() * size.height / texture->get_height();
  250. int tex_height = size.height;
  251. if (tex_width > size.width) {
  252. tex_width = size.width;
  253. tex_height = texture->get_height() * tex_width / texture->get_width();
  254. }
  255. // Prevent the texture from being unpreviewable after the rescale, so that we can still see something
  256. if (tex_height <= 0) {
  257. tex_height = 1;
  258. }
  259. if (tex_width <= 0) {
  260. tex_width = 1;
  261. }
  262. int ofs_x = (size.width - tex_width) / 2;
  263. int ofs_y = (size.height - tex_height) / 2;
  264. texture_rect->set_position(Vector2(ofs_x, ofs_y));
  265. texture_rect->set_size(Vector2(tex_width, tex_height));
  266. }
  267. void TextureLayeredEditor::edit(Ref<TextureLayered> p_texture) {
  268. if (texture.is_valid()) {
  269. texture->disconnect_changed(callable_mp(this, &TextureLayeredEditor::_texture_changed));
  270. }
  271. texture = p_texture;
  272. if (texture.is_valid()) {
  273. if (shaders[0].is_null()) {
  274. _make_shaders();
  275. }
  276. texture->connect_changed(callable_mp(this, &TextureLayeredEditor::_texture_changed));
  277. texture_rect->set_material(materials[texture->get_layered_type()]);
  278. setting = true;
  279. layer->set_value(0);
  280. layer->show();
  281. _update_gui();
  282. setting = false;
  283. x_rot = 0;
  284. y_rot = 0;
  285. _update_material(true);
  286. queue_redraw();
  287. } else {
  288. hide();
  289. }
  290. }
  291. TextureLayeredEditor::TextureLayeredEditor() {
  292. set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
  293. set_custom_minimum_size(Size2(0, 256.0) * EDSCALE);
  294. texture_rect = memnew(Control);
  295. texture_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
  296. texture_rect->connect(SceneStringName(draw), callable_mp(this, &TextureLayeredEditor::_texture_rect_draw));
  297. add_child(texture_rect);
  298. layer = memnew(SpinBox);
  299. layer->set_step(1);
  300. layer->set_max(100);
  301. layer->set_modulate(Color(1, 1, 1, 0.8));
  302. layer->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  303. layer->set_anchor(SIDE_RIGHT, 1);
  304. layer->set_anchor(SIDE_LEFT, 1);
  305. layer->connect(SceneStringName(value_changed), callable_mp(this, &TextureLayeredEditor::_layer_changed));
  306. add_child(layer);
  307. channel_selector = memnew(ColorChannelSelector);
  308. channel_selector->connect("selected_channels_changed", callable_mp(this, &TextureLayeredEditor::on_selected_channels_changed));
  309. channel_selector->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT);
  310. add_child(channel_selector);
  311. info = memnew(Label);
  312. info->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));
  313. info->add_theme_color_override("font_shadow_color", Color(0, 0, 0));
  314. info->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE);
  315. info->add_theme_color_override("font_outline_color", Color(0, 0, 0));
  316. info->add_theme_constant_override("outline_size", 8 * EDSCALE);
  317. info->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  318. info->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  319. info->set_h_size_flags(Control::SIZE_SHRINK_END);
  320. info->set_v_size_flags(Control::SIZE_SHRINK_END);
  321. info->set_anchor(SIDE_RIGHT, 1);
  322. info->set_anchor(SIDE_LEFT, 1);
  323. info->set_anchor(SIDE_BOTTOM, 1);
  324. info->set_anchor(SIDE_TOP, 1);
  325. add_child(info);
  326. }
  327. TextureLayeredEditor::~TextureLayeredEditor() {
  328. }
  329. bool EditorInspectorPluginLayeredTexture::can_handle(Object *p_object) {
  330. return Object::cast_to<TextureLayered>(p_object) != nullptr;
  331. }
  332. void EditorInspectorPluginLayeredTexture::parse_begin(Object *p_object) {
  333. TextureLayered *texture = Object::cast_to<TextureLayered>(p_object);
  334. if (!texture) {
  335. return;
  336. }
  337. Ref<TextureLayered> m(texture);
  338. TextureLayeredEditor *editor = memnew(TextureLayeredEditor);
  339. editor->edit(m);
  340. add_custom_control(editor);
  341. }
  342. TextureLayeredEditorPlugin::TextureLayeredEditorPlugin() {
  343. Ref<EditorInspectorPluginLayeredTexture> plugin;
  344. plugin.instantiate();
  345. add_inspector_plugin(plugin);
  346. }