texture_breakdown.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. let plugin = new arm.Plugin();
  2. let h1 = new zui.Handle();
  3. let h2 = new zui.Handle();
  4. let slots = ["base", "occ", "rough", "nor"];
  5. let breakdown = null;
  6. plugin.drawUI = function(ui) {
  7. if (ui.panel(h1, "Texture Breakdown")) {
  8. ui.g.end();
  9. drawBreakdown();
  10. ui.g.begin(false);
  11. // ui.row([1 / 4]);
  12. // ui.combo(h2, ["Material", "Viewport"], "Type");
  13. ui.image(breakdown);
  14. if (ui.isHovered && ui.inputReleasedR) {
  15. let x = ui.inputX - ui._windowX;
  16. let w = ui._windowW / slots.length;
  17. let i = (x / w) | 0;
  18. arm.UIMenu.draw(function(ui) {
  19. ui.text(slots[i], 2, ui.t.HIGHLIGHT_COL);
  20. if (ui.button("Delete", 0)) {
  21. slots.splice(i, 1);
  22. }
  23. }, 2);
  24. }
  25. ui.row([1 / 4, 1 / 4]);
  26. if (ui.button("Add")) {
  27. arm.UIMenu.draw(function(ui) {
  28. ui.text("Channel", 2, ui.t.HIGHLIGHT_COL);
  29. if (ui.button("Base Color", 0)) { slots.push("base"); }
  30. if (ui.button("Occlusion", 0)) { slots.push("occ"); }
  31. if (ui.button("Roughness", 0)) { slots.push("rough"); }
  32. if (ui.button("Metallic", 0)) { slots.push("metal"); }
  33. if (ui.button("Normal Map", 0)) { slots.push("nor"); }
  34. }, 6);
  35. }
  36. if (ui.button("Export")) {
  37. arm.UIFiles.show("png", true, false, function(path) {
  38. arm.App.notifyOnNextFrame(function() {
  39. var f = arm.UIFiles.filename;
  40. if (f === "") f = "untitled";
  41. if (!f.endsWith(".png")) f += ".png";
  42. Krom.writePng(path + arm.Path.sep + f, breakdown.getPixels().b.buffer, breakdown.get_width(), breakdown.get_height(), 2);
  43. });
  44. });
  45. }
  46. }
  47. }
  48. function drawBreakdown(type) {
  49. if (breakdown === null) {
  50. breakdown = core.Image.createRenderTarget(4096, 4096);
  51. }
  52. let g2 = breakdown.get_g2();
  53. g2.begin(true, 0xff000000);
  54. g2.disableScissor();
  55. if (h2.position === 0) { // Material
  56. var lay = arm.Context.layer;
  57. for (let i = 0; i < slots.length; ++i) {
  58. g2.set_pipeline(arm.UIView2D.pipe);
  59. let image = lay.texpaint;
  60. let channel = 0;
  61. if (slots[i] === "occ") {
  62. image = lay.texpaint_pack;
  63. channel = 1;
  64. }
  65. else if (slots[i] === "rough") {
  66. image = lay.texpaint_pack;
  67. channel = 2;
  68. }
  69. else if (slots[i] === "metal") {
  70. image = lay.texpaint_pack;
  71. channel = 3;
  72. }
  73. else if (slots[i] === "nor") {
  74. image = lay.texpaint_nor;
  75. channel = 5;
  76. }
  77. breakdown.get_g4().setInt(arm.UIView2D.channelLocation, channel);
  78. var step_source = image.get_width() / slots.length;
  79. var step_dest = breakdown.get_width() / slots.length;
  80. g2.drawScaledSubImage(image, step_source * i, 0, step_source, image.get_height(), step_dest * i, 0, step_dest, breakdown.get_height());
  81. g2.flush();
  82. }
  83. }
  84. else { // Viewport
  85. }
  86. g2.end();
  87. }