texture_layered_editor_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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/scene/texture/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. if (!use_rotation) {
  119. return;
  120. }
  121. Ref<InputEventMouseMotion> mm = p_event;
  122. if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::RIGHT)) {
  123. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) {
  124. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  125. }
  126. y_rot += mm->get_relative().x * 0.01;
  127. x_rot = CLAMP(x_rot - mm->get_relative().y * 0.01, -Math::PI * 0.5f, Math::PI * 0.5f);
  128. _update_material(false);
  129. }
  130. Ref<InputEventMouseButton> mb = p_event;
  131. if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT) {
  132. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
  133. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  134. Input::get_singleton()->warp_mouse(original_mouse_pos);
  135. } else if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) {
  136. original_mouse_pos = mb->get_global_position();
  137. }
  138. }
  139. }
  140. void TextureLayeredEditor::_texture_rect_draw() {
  141. texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
  142. }
  143. void TextureLayeredEditor::_update_gui() {
  144. if (texture.is_null()) {
  145. return;
  146. }
  147. _texture_rect_update_area();
  148. const Image::Format format = texture->get_format();
  149. const String format_name = Image::get_format_name(format);
  150. String texture_info;
  151. switch (texture->get_layered_type()) {
  152. case TextureLayered::LAYERED_TYPE_2D_ARRAY: {
  153. layer->set_max(texture->get_layers() - 1);
  154. texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"),
  155. texture->get_width(),
  156. texture->get_height(),
  157. texture->get_layers(),
  158. format_name);
  159. } break;
  160. case TextureLayered::LAYERED_TYPE_CUBEMAP: {
  161. layer->hide();
  162. texture_info = vformat(String::utf8("%d×%d %s\n"),
  163. texture->get_width(),
  164. texture->get_height(),
  165. format_name);
  166. } break;
  167. case TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY: {
  168. layer->set_max(texture->get_layers() / 6 - 1);
  169. texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"),
  170. texture->get_width(),
  171. texture->get_height(),
  172. texture->get_layers() / 6,
  173. format_name);
  174. } break;
  175. default: {
  176. }
  177. }
  178. if (texture->has_mipmaps()) {
  179. const int mip_count = Image::get_image_required_mipmaps(texture->get_width(), texture->get_height(), format);
  180. const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, true) * texture->get_layers();
  181. texture_info += vformat(TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
  182. mip_count,
  183. String::humanize_size(memory));
  184. } else {
  185. const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), format, false) * texture->get_layers();
  186. texture_info += vformat(TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
  187. String::humanize_size(memory));
  188. }
  189. info->set_text(texture_info);
  190. const uint32_t components_mask = Image::get_format_component_mask(format);
  191. if (is_power_of_2(components_mask)) {
  192. // Only one channel available, no point in showing a channel selector.
  193. channel_selector->hide();
  194. } else {
  195. channel_selector->show();
  196. channel_selector->set_available_channels_mask(components_mask);
  197. }
  198. }
  199. void TextureLayeredEditor::_notification(int p_what) {
  200. switch (p_what) {
  201. case NOTIFICATION_RESIZED: {
  202. _texture_rect_update_area();
  203. } break;
  204. case NOTIFICATION_DRAW: {
  205. Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
  206. draw_texture_rect(checkerboard, texture_rect->get_rect(), true);
  207. _draw_outline();
  208. } break;
  209. case NOTIFICATION_THEME_CHANGED: {
  210. if (info) {
  211. Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
  212. info->add_theme_font_override(SceneStringName(font), metadata_label_font);
  213. }
  214. theme_cache.outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor));
  215. } break;
  216. }
  217. }
  218. void TextureLayeredEditor::_texture_changed() {
  219. if (!is_visible()) {
  220. return;
  221. }
  222. setting = true;
  223. _update_gui();
  224. setting = false;
  225. _update_material(true);
  226. queue_redraw();
  227. }
  228. void TextureLayeredEditor::_update_material(bool p_texture_changed) {
  229. materials[0]->set_shader_parameter("layer", layer->get_value());
  230. materials[2]->set_shader_parameter("layer", layer->get_value());
  231. Vector3 v(-1, -1, -1);
  232. v.normalize();
  233. Basis b;
  234. b.rotate(Vector3(1, 0, 0), x_rot);
  235. b.rotate(Vector3(0, 1, 0), y_rot);
  236. materials[1]->set_shader_parameter("normal", v);
  237. materials[1]->set_shader_parameter("rot", b);
  238. materials[2]->set_shader_parameter("normal", v);
  239. materials[2]->set_shader_parameter("rot", b);
  240. if (p_texture_changed) {
  241. const TextureLayered::LayeredType type = texture->get_layered_type();
  242. use_rotation = type == TextureLayered::LAYERED_TYPE_CUBEMAP || type == TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY;
  243. materials[texture->get_layered_type()]->set_shader_parameter("tex", texture->get_rid());
  244. }
  245. const Vector4 channel_factors = channel_selector->get_selected_channel_factors();
  246. for (unsigned int i = 0; i < 3; ++i) {
  247. materials[i]->set_shader_parameter("u_channel_factors", channel_factors);
  248. }
  249. }
  250. void TextureLayeredEditor::on_selected_channels_changed() {
  251. _update_material(false);
  252. }
  253. void TextureLayeredEditor::_draw_outline() {
  254. const float outline_width = Math::round(EDSCALE);
  255. const Rect2 outline_rect = texture_rect->get_rect().grow(outline_width * 0.5);
  256. draw_rect(outline_rect, theme_cache.outline_color, false, outline_width);
  257. }
  258. void TextureLayeredEditor::_make_materials() {
  259. for (int i = 0; i < 3; i++) {
  260. materials[i].instantiate();
  261. materials[i]->set_shader(shaders[i]);
  262. }
  263. }
  264. void TextureLayeredEditor::_texture_rect_update_area() {
  265. Size2 size = get_size();
  266. int tex_width = texture->get_width() * size.height / texture->get_height();
  267. int tex_height = size.height;
  268. if (tex_width > size.width) {
  269. tex_width = size.width;
  270. tex_height = texture->get_height() * tex_width / texture->get_width();
  271. }
  272. // Prevent the texture from being unpreviewable after the rescale, so that we can still see something
  273. if (tex_height <= 0) {
  274. tex_height = 1;
  275. }
  276. if (tex_width <= 0) {
  277. tex_width = 1;
  278. }
  279. int ofs_x = (size.width - tex_width) / 2;
  280. int ofs_y = (size.height - tex_height) / 2;
  281. texture_rect->set_position(Vector2(ofs_x, ofs_y - Math::round(EDSCALE)));
  282. texture_rect->set_size(Vector2(tex_width, tex_height));
  283. }
  284. void TextureLayeredEditor::init_shaders() {
  285. shaders[0].instantiate();
  286. shaders[0]->set_code(array_2d_shader);
  287. shaders[1].instantiate();
  288. shaders[1]->set_code(cubemap_shader);
  289. shaders[2].instantiate();
  290. shaders[2]->set_code(cubemap_array_shader);
  291. }
  292. void TextureLayeredEditor::finish_shaders() {
  293. shaders[0].unref();
  294. shaders[1].unref();
  295. shaders[2].unref();
  296. }
  297. void TextureLayeredEditor::edit(Ref<TextureLayered> p_texture) {
  298. if (texture.is_valid()) {
  299. texture->disconnect_changed(callable_mp(this, &TextureLayeredEditor::_texture_changed));
  300. }
  301. texture = p_texture;
  302. if (texture.is_valid()) {
  303. if (materials[0].is_null()) {
  304. _make_materials();
  305. }
  306. texture->connect_changed(callable_mp(this, &TextureLayeredEditor::_texture_changed));
  307. texture_rect->set_material(materials[texture->get_layered_type()]);
  308. setting = true;
  309. layer->set_value(0);
  310. layer->show();
  311. _update_gui();
  312. setting = false;
  313. x_rot = 0;
  314. y_rot = 0;
  315. _update_material(true);
  316. queue_redraw();
  317. } else {
  318. hide();
  319. }
  320. }
  321. TextureLayeredEditor::TextureLayeredEditor() {
  322. set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
  323. set_custom_minimum_size(Size2(0, 256.0) * EDSCALE);
  324. texture_rect = memnew(Control);
  325. texture_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
  326. texture_rect->connect(SceneStringName(draw), callable_mp(this, &TextureLayeredEditor::_texture_rect_draw));
  327. add_child(texture_rect);
  328. layer = memnew(SpinBox);
  329. layer->set_step(1);
  330. layer->set_max(100);
  331. layer->set_modulate(Color(1, 1, 1, 0.8));
  332. layer->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  333. layer->set_anchor(SIDE_RIGHT, 1);
  334. layer->set_anchor(SIDE_LEFT, 1);
  335. layer->connect(SceneStringName(value_changed), callable_mp(this, &TextureLayeredEditor::_layer_changed));
  336. add_child(layer);
  337. channel_selector = memnew(ColorChannelSelector);
  338. channel_selector->connect("selected_channels_changed", callable_mp(this, &TextureLayeredEditor::on_selected_channels_changed));
  339. channel_selector->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT);
  340. add_child(channel_selector);
  341. info = memnew(Label);
  342. info->set_focus_mode(FOCUS_ACCESSIBILITY);
  343. info->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));
  344. info->add_theme_color_override("font_shadow_color", Color(0, 0, 0));
  345. info->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE);
  346. info->add_theme_color_override("font_outline_color", Color(0, 0, 0));
  347. info->add_theme_constant_override("outline_size", 8 * EDSCALE);
  348. info->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  349. info->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  350. info->set_h_size_flags(Control::SIZE_SHRINK_END);
  351. info->set_v_size_flags(Control::SIZE_SHRINK_END);
  352. info->set_anchor(SIDE_RIGHT, 1);
  353. info->set_anchor(SIDE_LEFT, 1);
  354. info->set_anchor(SIDE_BOTTOM, 1);
  355. info->set_anchor(SIDE_TOP, 1);
  356. add_child(info);
  357. }
  358. bool EditorInspectorPluginLayeredTexture::can_handle(Object *p_object) {
  359. return Object::cast_to<TextureLayered>(p_object) != nullptr;
  360. }
  361. void EditorInspectorPluginLayeredTexture::parse_begin(Object *p_object) {
  362. TextureLayered *texture = Object::cast_to<TextureLayered>(p_object);
  363. if (!texture) {
  364. return;
  365. }
  366. Ref<TextureLayered> m(texture);
  367. TextureLayeredEditor *editor = memnew(TextureLayeredEditor);
  368. editor->edit(m);
  369. add_custom_control(editor);
  370. }
  371. TextureLayeredEditorPlugin::TextureLayeredEditorPlugin() {
  372. Ref<EditorInspectorPluginLayeredTexture> plugin;
  373. plugin.instantiate();
  374. add_inspector_plugin(plugin);
  375. }