texture_breakdown.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. let plugin = plugin_create();
  2. let h1 = zui_handle_create();
  3. let h2 = zui_handle_create();
  4. let slots = ["base", "occ", "rough", "nor"];
  5. let breakdown = null;
  6. plugin.draw_ui = function(ui) {
  7. if (zui_panel(h1, "Texture Breakdown")) {
  8. g2_end();
  9. draw_breakdown();
  10. g2_begin(ui);
  11. // zui_row([1 / 4]);
  12. // zui_combo(h2, ["Material", "Viewport"], "Type");
  13. zui_image(breakdown);
  14. if (ui.is_hovered && ui.input_released_r) {
  15. let x = ui.input_x - ui._window_x;
  16. let w = ui._window_w / slots.length;
  17. let i = (x / w) | 0;
  18. ui_menu_draw(function(ui) {
  19. zui_text(slots[i], 2, ui.t.HIGHLIGHT_COL);
  20. if (zui_button("Delete", 0)) {
  21. slots.splice(i, 1);
  22. }
  23. }, 2);
  24. }
  25. zui_row([1 / 4, 1 / 4]);
  26. if (zui_button("Add")) {
  27. ui_menu_draw(function(ui) {
  28. zui_text("Channel", 2, ui.t.HIGHLIGHT_COL);
  29. if (zui_button("Base Color", 0)) {
  30. slots.push("base");
  31. }
  32. if (zui_button("Occlusion", 0)) {
  33. slots.push("occ");
  34. }
  35. if (zui_button("Roughness", 0)) {
  36. slots.push("rough");
  37. }
  38. if (zui_button("Metallic", 0)) {
  39. slots.push("metal");
  40. }
  41. if (zui_button("Normal Map", 0)) {
  42. slots.push("nor");
  43. }
  44. }, 6);
  45. }
  46. if (zui_button("Export")) {
  47. ui_files_show("png", true, false, function(path) {
  48. base_notify_on_next_frame(function() {
  49. var f = ui_files_filename;
  50. if (f === "") {
  51. f = "untitled";
  52. }
  53. if (!f.endsWith(".png")) {
  54. f += ".png";
  55. }
  56. krom_write_png(path + path_sep + f, image_get_pixels(breakdown), breakdown.width, breakdown.height, 2);
  57. });
  58. });
  59. }
  60. }
  61. }
  62. function draw_breakdown(type) {
  63. if (breakdown === null) {
  64. breakdown = image_create_render_target(4096, 4096);
  65. }
  66. g2_begin(breakdown);
  67. g2_clear(0xff000000);
  68. g2_disable_scissor();
  69. if (h2.position === 0) { // Material
  70. var lay = brush_output_node_inst;
  71. for (let i = 0; i < slots.length; ++i) {
  72. g2_set_pipeline(ui_view2d_pipe);
  73. let image = lay.texpaint;
  74. let channel = 0;
  75. if (slots[i] === "occ") {
  76. image = lay.texpaint_pack;
  77. channel = 1;
  78. }
  79. else if (slots[i] === "rough") {
  80. image = lay.texpaint_pack;
  81. channel = 2;
  82. }
  83. else if (slots[i] === "metal") {
  84. image = lay.texpaint_pack;
  85. channel = 3;
  86. }
  87. else if (slots[i] === "nor") {
  88. image = lay.texpaint_nor;
  89. channel = 5;
  90. }
  91. g4_set_int(ui_view2d_channel_location, channel);
  92. var step_source = image.width / slots.length;
  93. var step_dest = breakdown.width / slots.length;
  94. g2_draw_scaled_sub_image(image, step_source * i, 0, step_source, image.height, step_dest * i, 0, step_dest, breakdown.height);
  95. g2_end(); // Flush
  96. g2_begin();
  97. }
  98. }
  99. else { // Viewport
  100. }
  101. g2_end();
  102. }