tiling_node.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. type tiling_node_t = {
  2. base?: logic_node_t;
  3. result?: image_t;
  4. };
  5. let tiling_node_image: image_t = null;
  6. let tiling_node_prompt: string = "";
  7. let tiling_node_strength: f32 = 0.5;
  8. let tiling_node_auto: bool = true;
  9. function tiling_node_create(arg: any): tiling_node_t {
  10. let n: float_node_t = {};
  11. n.base = logic_node_create();
  12. n.base.get_as_image = tiling_node_get_as_image;
  13. n.base.get_cached_image = tiling_node_get_cached_image;
  14. tiling_node_init();
  15. return n;
  16. }
  17. function tiling_node_init() {
  18. if (tiling_node_image == null) {
  19. tiling_node_image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y());
  20. }
  21. }
  22. function tiling_node_buttons(ui: zui_t, nodes: zui_nodes_t, node: zui_node_t) {
  23. tiling_node_auto = node.buttons[0].default_value == 0 ? false : true;
  24. if (!tiling_node_auto) {
  25. let tiling_node_strength_handle: zui_handle_t = zui_handle(__ID__);
  26. if (tiling_node_strength_handle.init) {
  27. tiling_node_strength_handle.value = tiling_node_strength;
  28. }
  29. tiling_node_strength = zui_slider(tiling_node_strength_handle, tr("strength"), 0, 1, true);
  30. tiling_node_prompt = zui_text_area(zui_handle(__ID__), zui_align_t.LEFT, true, tr("prompt"), true);
  31. node.buttons[1].height = 1 + string_split(tiling_node_prompt, "\n").length;
  32. }
  33. else {
  34. node.buttons[1].height = 0;
  35. }
  36. }
  37. function tiling_node_get_as_image(self: tiling_node_t, from: i32): image_t {
  38. let source: image_t = logic_node_input_get_as_image(self.base.inputs[0]);
  39. g2_begin(tiling_node_image);
  40. g2_draw_scaled_image(source, 0, 0, config_get_texture_res_x(), config_get_texture_res_y());
  41. g2_end();
  42. console_progress(tr("Processing") + " - " + tr("Tiling"));
  43. krom_g4_swap_buffers();
  44. if (tiling_node_auto){
  45. self.result = inpaint_node_texsynth_inpaint(tiling_node_image, true, null);
  46. }
  47. else {
  48. self.result = tiling_node_sd_tiling(tiling_node_image, -1);
  49. }
  50. return self.result;
  51. }
  52. function tiling_node_get_cached_image(self: tiling_node_t): image_t {
  53. return self.result;
  54. }
  55. function tiling_node_sd_tiling(image: image_t, seed: i32): image_t {
  56. text_to_photo_node_tiling = false;
  57. let tile = image_create_render_target(512, 512);
  58. g2_begin(tile);
  59. g2_draw_scaled_image(image, -256, -256, 512, 512);
  60. g2_draw_scaled_image(image, 256, -256, 512, 512);
  61. g2_draw_scaled_image(image, -256, 256, 512, 512);
  62. g2_draw_scaled_image(image, 256, 256, 512, 512);
  63. g2_end();
  64. let u8a = u8_array_create(512 * 512);
  65. for (let i: i32 = 0; i < 512 * 512; ++i) {
  66. let x = i % 512;
  67. let y = math_floor(i / 512);
  68. let l = y < 256 ? y : (511 - y);
  69. u8a[i] = (x > 256 - l && x < 256 + l) ? 0 : 255;
  70. }
  71. // for (let i: i32 = 0; i < 512 * 512; ++i) u8a[i] = 255;
  72. // for (let x: i32 = (256 - 32); x < (256 + 32); ++x) {
  73. // for (let y: i32 = 0; y < 512; ++y) {
  74. // u8a[y * 512 + x] = 0;
  75. // }
  76. // }
  77. // for (let x: i32 = 0; x < 512; ++x) {
  78. // for (let y: i32 = (256 - 32); y < 256 + 32); ++y) {
  79. // u8a[y * 512 + x] = 0;
  80. // }
  81. // }
  82. let mask = image_from_bytes(u8a.buffer, 512, 512, tex_format_t.R8);
  83. inpaint_node_prompt = tiling_node_prompt;
  84. inpaint_node_strength = tiling_node_strength;
  85. if (seed >= 0) {
  86. random_node_set_seed(seed);
  87. }
  88. return inpaint_node_sd_inpaint(tile, mask);
  89. }
  90. let tiling_node_def: zui_node_t = {
  91. id: 0,
  92. name: _tr("Tiling"),
  93. type: "tiling_node",
  94. x: 0,
  95. y: 0,
  96. color: 0xff4982a0,
  97. inputs: [
  98. {
  99. id: 0,
  100. node_id: 0,
  101. name: _tr("Color"),
  102. type: "RGBA",
  103. color: 0xffc7c729,
  104. default_value: f32_array_create_xyzw(0.0, 0.0, 0.0, 1.0),
  105. min: 0.0,
  106. max: 1.0,
  107. precision: 100,
  108. display: 0
  109. }
  110. ],
  111. outputs: [
  112. {
  113. id: 0,
  114. node_id: 0,
  115. name: _tr("Color"),
  116. type: "RGBA",
  117. color: 0xffc7c729,
  118. default_value: f32_array_create_xyzw(0.0, 0.0, 0.0, 1.0),
  119. min: 0.0,
  120. max: 1.0,
  121. precision: 100,
  122. display: 0
  123. }
  124. ],
  125. buttons: [
  126. {
  127. name: _tr("auto"),
  128. type: "BOOL",
  129. output: 0,
  130. default_value: f32_array_create_x(1),
  131. data: null,
  132. min: 0.0,
  133. max: 1.0,
  134. precision: 100,
  135. height: 0
  136. },
  137. {
  138. name: "tiling_node_buttons",
  139. type: "CUSTOM",
  140. output: -1,
  141. default_value: null,
  142. data: null,
  143. min: 0.0,
  144. max: 1.0,
  145. precision: 100,
  146. height: 0
  147. }
  148. ],
  149. width: 0
  150. };