import_folder.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function import_folder_run(path: string) {
  2. let files: string[] = file_read_directory(path);
  3. let mapbase: string = "";
  4. let mapopac: string = "";
  5. let mapnor: string = "";
  6. let mapocc: string = "";
  7. let maprough: string = "";
  8. let mapmet: string = "";
  9. let mapheight: string = "";
  10. let found_texture: bool = false;
  11. // Import maps
  12. for (let i: i32 = 0; i < files.length; ++i) {
  13. let f: string = files[i];
  14. if (!path_is_texture(f)) {
  15. continue;
  16. }
  17. // TODO: handle -albedo
  18. let base: string = to_lower_case(substring(f, 0, string_last_index_of(f, ".")));
  19. let valid: bool = false;
  20. if (mapbase == "" && path_is_base_color_tex(base)) {
  21. mapbase = f;
  22. valid = true;
  23. }
  24. if (mapopac == "" && path_is_opacity_tex(base)) {
  25. mapopac = f;
  26. valid = true;
  27. }
  28. if (mapnor == "" && path_is_normal_map_tex(base)) {
  29. mapnor = f;
  30. valid = true;
  31. }
  32. if (mapocc == "" && path_is_occlusion_tex(base)) {
  33. mapocc = f;
  34. valid = true;
  35. }
  36. if (maprough == "" && path_is_roughness_tex(base)) {
  37. maprough = f;
  38. valid = true;
  39. }
  40. if (mapmet == "" && path_is_metallic_tex(base)) {
  41. mapmet = f;
  42. valid = true;
  43. }
  44. if (mapheight == "" && path_is_displacement_tex(base)) {
  45. mapheight = f;
  46. valid = true;
  47. }
  48. if (valid) {
  49. import_texture_run(path + path_sep + f, false);
  50. found_texture = true;
  51. }
  52. }
  53. if (!found_texture) {
  54. console_info(tr("Folder does not contain textures"));
  55. return;
  56. }
  57. // Create material
  58. context_raw.material = slot_material_create(project_materials[0].data);
  59. array_push(project_materials, context_raw.material);
  60. let nodes: zui_nodes_t = context_raw.material.nodes;
  61. let canvas: zui_node_canvas_t = context_raw.material.canvas;
  62. let dirs: string[] = string_split(path, path_sep);
  63. canvas.name = dirs[dirs.length - 1];
  64. let nout: zui_node_t = null;
  65. for (let i: i32 = 0; i < canvas.nodes.length; ++i) {
  66. let n: zui_node_t = canvas.nodes[i];
  67. if (n.type == "OUTPUT_MATERIAL_PBR") {
  68. nout = n;
  69. break;
  70. }
  71. }
  72. for (let i: i32 = 0; i < canvas.nodes.length; ++i) {
  73. let n: zui_node_t = canvas.nodes[i];
  74. if (n.name == "RGB") {
  75. zui_remove_node(n, canvas);
  76. break;
  77. }
  78. }
  79. // Place nodes
  80. let pos: i32 = 0;
  81. let start_y: i32 = 100;
  82. let node_h: i32 = 164;
  83. if (mapbase != "") {
  84. import_folder_place_image_node(nodes, canvas, mapbase, start_y + node_h * pos, nout.id, 0);
  85. pos++;
  86. }
  87. if (mapopac != "") {
  88. import_folder_place_image_node(nodes, canvas, mapopac, start_y + node_h * pos, nout.id, 1);
  89. pos++;
  90. }
  91. if (mapocc != "") {
  92. import_folder_place_image_node(nodes, canvas, mapocc, start_y + node_h * pos, nout.id, 2);
  93. pos++;
  94. }
  95. if (maprough != "") {
  96. import_folder_place_image_node(nodes, canvas, maprough, start_y + node_h * pos, nout.id, 3);
  97. pos++;
  98. }
  99. if (mapmet != "") {
  100. import_folder_place_image_node(nodes, canvas, mapmet, start_y + node_h * pos, nout.id, 4);
  101. pos++;
  102. }
  103. if (mapnor != "") {
  104. import_folder_place_image_node(nodes, canvas, mapnor, start_y + node_h * pos, nout.id, 5);
  105. pos++;
  106. }
  107. if (mapheight != "") {
  108. import_folder_place_image_node(nodes, canvas, mapheight, start_y + node_h * pos, nout.id, 7);
  109. pos++;
  110. }
  111. make_material_parse_paint_material();
  112. util_render_make_material_preview();
  113. ui_base_hwnds[1].redraws = 2;
  114. history_new_material();
  115. }
  116. function import_folder_place_image_node(nodes: zui_nodes_t, canvas: zui_node_canvas_t, asset: string, ny: i32, to_id: i32, to_socket: i32) {
  117. let n: zui_node_t = nodes_material_create_node("TEX_IMAGE");
  118. n.buttons[0].default_value = f32_array_create_x(base_get_asset_index(asset));
  119. n.x = 72;
  120. n.y = ny;
  121. let l: zui_node_link_t = { id: zui_get_link_id(canvas.links), from_id: n.id, from_socket: 0, to_id: to_id, to_socket: to_socket };
  122. array_push(canvas.links, l);
  123. }