util_uv.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ///if (is_paint || is_sculpt)
  2. let util_uv_uvmap: image_t = null;
  3. let util_uv_uvmap_cached: bool = false;
  4. let util_uv_trianglemap: image_t = null;
  5. let util_uv_trianglemap_cached: bool = false;
  6. let util_uv_dilatemap: image_t = null;
  7. let util_uv_dilatemap_cached: bool = false;
  8. let util_uv_uvislandmap: image_t = null;
  9. let util_uv_uvislandmap_cached: bool = false;
  10. let util_uv_dilate_bytes: buffer_t = null;
  11. let util_uv_pipe_dilate: pipeline_t = null;
  12. function util_uv_cache_uv_map() {
  13. if (util_uv_uvmap != null && (util_uv_uvmap.width != config_get_texture_res_x() || util_uv_uvmap.height != config_get_texture_res_y())) {
  14. image_unload(util_uv_uvmap);
  15. util_uv_uvmap = null;
  16. util_uv_uvmap_cached = false;
  17. }
  18. if (util_uv_uvmap_cached) {
  19. return;
  20. }
  21. let res_x: i32 = config_get_texture_res_x();
  22. let res_y: i32 = config_get_texture_res_y();
  23. if (util_uv_uvmap == null) {
  24. util_uv_uvmap = image_create_render_target(res_x, res_y);
  25. }
  26. util_uv_uvmap_cached = true;
  27. let merged: mesh_object_t = context_raw.merged_object;
  28. let mesh: mesh_data_t = (context_raw.layer_filter == 0 && merged != null) ?
  29. merged.data : context_raw.paint_object.data;
  30. let texa: i16_array_t = mesh.vertex_arrays[2].values;
  31. let inda: u32_array_t = mesh.index_arrays[0].values;
  32. g2_begin(util_uv_uvmap);
  33. g2_clear(0x00000000);
  34. g2_set_color(0xffcccccc);
  35. let strength: f32 = res_x > 2048 ? 2.0 : 1.0;
  36. let f: f32 = (1 / 32767) * util_uv_uvmap.width;
  37. for (let i: i32 = 0; i < math_floor(inda.length / 3); ++i) {
  38. let x1: f32 = (texa[inda[i * 3 ] * 2 ]) * f;
  39. let x2: f32 = (texa[inda[i * 3 + 1] * 2 ]) * f;
  40. let x3: f32 = (texa[inda[i * 3 + 2] * 2 ]) * f;
  41. let y1: f32 = (texa[inda[i * 3 ] * 2 + 1]) * f;
  42. let y2: f32 = (texa[inda[i * 3 + 1] * 2 + 1]) * f;
  43. let y3: f32 = (texa[inda[i * 3 + 2] * 2 + 1]) * f;
  44. g2_draw_line(x1, y1, x2, y2, strength);
  45. g2_draw_line(x2, y2, x3, y3, strength);
  46. g2_draw_line(x3, y3, x1, y1, strength);
  47. }
  48. g2_end();
  49. }
  50. function util_uv_cache_triangle_map() {
  51. if (util_uv_trianglemap != null && (util_uv_trianglemap.width != config_get_texture_res_x() || util_uv_trianglemap.height != config_get_texture_res_y())) {
  52. image_unload(util_uv_trianglemap);
  53. util_uv_trianglemap = null;
  54. util_uv_trianglemap_cached = false;
  55. }
  56. if (util_uv_trianglemap_cached) {
  57. return;
  58. }
  59. if (util_uv_trianglemap == null) {
  60. util_uv_trianglemap = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y());
  61. }
  62. util_uv_trianglemap_cached = true;
  63. let merged: mesh_data_t = context_raw.merged_object != null ? context_raw.merged_object.data : context_raw.paint_object.data;
  64. let mesh: mesh_data_t = merged;
  65. let texa: i16_array_t = mesh.vertex_arrays[2].values;
  66. let inda: u32_array_t = mesh.index_arrays[0].values;
  67. g2_begin(util_uv_trianglemap);
  68. g2_clear(0xff000000);
  69. let f: f32 = (1 / 32767) * util_uv_trianglemap.width;
  70. let color: i32 = 0xff000001;
  71. for (let i: i32 = 0; i < math_floor(inda.length / 3); ++i) {
  72. if (color == 0xffffffff) color = 0xff000001;
  73. color++;
  74. g2_set_color(color);
  75. let x1: f32 = (texa[inda[i * 3 ] * 2 ]) * f;
  76. let x2: f32 = (texa[inda[i * 3 + 1] * 2 ]) * f;
  77. let x3: f32 = (texa[inda[i * 3 + 2] * 2 ]) * f;
  78. let y1: f32 = (texa[inda[i * 3 ] * 2 + 1]) * f;
  79. let y2: f32 = (texa[inda[i * 3 + 1] * 2 + 1]) * f;
  80. let y3: f32 = (texa[inda[i * 3 + 2] * 2 + 1]) * f;
  81. g2_fill_triangle(x1, y1, x2, y2, x3, y3);
  82. }
  83. g2_end();
  84. }
  85. function util_uv_cache_dilate_map() {
  86. if (util_uv_dilatemap != null && (util_uv_dilatemap.width != config_get_texture_res_x() || util_uv_dilatemap.height != config_get_texture_res_y())) {
  87. image_unload(util_uv_dilatemap);
  88. util_uv_dilatemap = null;
  89. util_uv_dilatemap_cached = false;
  90. }
  91. if (util_uv_dilatemap_cached) return;
  92. if (util_uv_dilatemap == null) {
  93. util_uv_dilatemap = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8);
  94. }
  95. if (util_uv_pipe_dilate == null) {
  96. util_uv_pipe_dilate = g4_pipeline_create();
  97. util_uv_pipe_dilate.vertex_shader = sys_get_shader("dilate_map.vert");
  98. util_uv_pipe_dilate.fragment_shader = sys_get_shader("dilate_map.frag");
  99. let vs: vertex_struct_t = g4_vertex_struct_create();
  100. ///if (krom_metal || krom_vulkan)
  101. g4_vertex_struct_add(vs, "tex", vertex_data_t.I16_2X_NORM);
  102. ///else
  103. g4_vertex_struct_add(vs, "pos", vertex_data_t.I16_4X_NORM);
  104. g4_vertex_struct_add(vs, "nor", vertex_data_t.I16_2X_NORM);
  105. g4_vertex_struct_add(vs, "tex", vertex_data_t.I16_2X_NORM);
  106. ///end
  107. util_uv_pipe_dilate.input_layout = [vs];
  108. util_uv_pipe_dilate.depth_write = false;
  109. util_uv_pipe_dilate.depth_mode = compare_mode_t.ALWAYS;
  110. util_uv_pipe_dilate.color_attachments[0] = tex_format_t.R8;
  111. g4_pipeline_compile(util_uv_pipe_dilate);
  112. // dilateTexUnpack = getConstantLocation(pipeDilate, "texUnpack");
  113. }
  114. let mask: i32 = context_object_mask_used() ? slot_layer_get_object_mask(context_raw.layer) : 0;
  115. if (context_layer_filter_used()) {
  116. mask = context_raw.layer_filter;
  117. }
  118. let geom: mesh_data_t = mask == 0 && context_raw.merged_object != null ? context_raw.merged_object.data : context_raw.paint_object.data;
  119. g4_begin(util_uv_dilatemap);
  120. g4_clear(0x00000000);
  121. g4_set_pipeline(util_uv_pipe_dilate);
  122. ///if (krom_metal || krom_vulkan)
  123. g4_set_vertex_buffer(mesh_data_get(geom, [{name: "tex", data: "short2norm"}]));
  124. ///else
  125. g4_set_vertex_buffer(geom._.vertex_buffer);
  126. ///end
  127. g4_set_index_buffer(geom._.index_buffers[0]);
  128. g4_draw();
  129. g4_end();
  130. util_uv_dilatemap_cached = true;
  131. util_uv_dilate_bytes = null;
  132. }
  133. function _util_uv_check(c: coord_t, w: i32, h: i32, r: i32, view: buffer_view_t, coords: coord_t[]) {
  134. if (c.x < 0 || c.x >= w || c.y < 0 || c.y >= h) {
  135. return;
  136. }
  137. if (buffer_view_get_u8(view, c.y * w + c.x) == 255) {
  138. return;
  139. }
  140. let dilate_view: buffer_view_t = buffer_view_create(util_uv_dilate_bytes);
  141. if (buffer_view_get_u8(dilate_view, c.y * r * util_uv_dilatemap.width + c.x * r) == 0) {
  142. return;
  143. }
  144. buffer_view_set_u8(view, c.y * w + c.x, 255);
  145. array_push(coords, { x: c.x + 1, y: c.y });
  146. array_push(coords, { x: c.x - 1, y: c.y });
  147. array_push(coords, { x: c.x, y: c.y + 1 });
  148. array_push(coords, { x: c.x, y: c.y - 1 });
  149. }
  150. function util_uv_cache_uv_island_map() {
  151. util_uv_cache_dilate_map();
  152. if (util_uv_dilate_bytes == null) {
  153. util_uv_dilate_bytes = image_get_pixels(util_uv_dilatemap);
  154. }
  155. util_render_pick_pos_nor_tex();
  156. let w: i32 = 2048; // config_get_texture_res_x()
  157. let h: i32 = 2048; // config_get_texture_res_y()
  158. let x: i32 = math_floor(context_raw.uvx_picked * w);
  159. let y: i32 = math_floor(context_raw.uvy_picked * h);
  160. let bytes: buffer_t = buffer_create(w * h);
  161. let view: buffer_view_t = buffer_view_create(bytes);
  162. let coords: coord_t[] = [{ x: x, y: y }];
  163. let r: i32 = math_floor(util_uv_dilatemap.width / w);
  164. while (coords.length > 0) {
  165. _util_uv_check(coords.pop(), w, h, r, view, coords);
  166. }
  167. if (util_uv_uvislandmap != null) {
  168. image_unload(util_uv_uvislandmap);
  169. }
  170. util_uv_uvislandmap = image_from_bytes(bytes, w, h, tex_format_t.R8);
  171. util_uv_uvislandmap_cached = true;
  172. }
  173. type coord_t = {
  174. x?: i32;
  175. y?: i32;
  176. }
  177. ///end