import_mesh.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ///if (is_paint || is_sculpt)
  2. let import_mesh_clear_layers: bool = true;
  3. ///end
  4. let import_mesh_meshes_to_unwrap: any[] = null;
  5. ///if (is_paint || is_sculpt)
  6. function import_mesh_run(path: string, _clear_layers: bool = true, replace_existing: bool = true) {
  7. ///end
  8. ///if is_lab
  9. function import_mesh_run(path: string, replace_existing: bool = true) {
  10. ///end
  11. if (!path_is_mesh(path)) {
  12. if (!context_enable_import_plugin(path)) {
  13. console_error(strings_error1());
  14. return;
  15. }
  16. }
  17. ///if (is_paint || is_sculpt)
  18. import_mesh_clear_layers = _clear_layers;
  19. context_raw.layer_filter = 0;
  20. ///end
  21. import_mesh_meshes_to_unwrap = null;
  22. let p: string = to_lower_case(path);
  23. if (ends_with(p, ".obj")) {
  24. import_obj_run(path, replace_existing);
  25. }
  26. else if (ends_with(p, ".blend")) {
  27. import_blend_mesh_run(path, replace_existing);
  28. }
  29. else {
  30. let ext: string = substring(path, string_last_index_of(path, ".") + 1, path.length);
  31. let importer: (s: string)=>any = map_get(path_mesh_importers, ext);
  32. let mesh: any = importer(path);
  33. replace_existing ? import_mesh_make_mesh(mesh, path) : import_mesh_add_mesh(mesh);
  34. let has_next: bool = mesh.has_next;
  35. while (has_next) {
  36. let mesh: any = importer(path);
  37. has_next = mesh.has_next;
  38. import_mesh_add_mesh(mesh);
  39. // let m: mat4_t = fromFloat32Array(mesh.transform);
  40. // paintObjects[paintObjects.length - 1].transform.localOnly = true;
  41. // paintObjects[paintObjects.length - 1].transform.setMatrix(m);
  42. }
  43. }
  44. project_mesh_assets = [path];
  45. ///if (krom_android || krom_ios)
  46. sys_title_set(substring(path, string_last_index_of(path, path_sep) + 1, string_last_index_of(path, ".")));
  47. ///end
  48. }
  49. function import_mesh_finish_import() {
  50. if (context_raw.merged_object != null) {
  51. mesh_data_delete(context_raw.merged_object.data);
  52. mesh_object_remove(context_raw.merged_object);
  53. context_raw.merged_object = null;
  54. }
  55. context_select_paint_object(context_main_object());
  56. if (project_paint_objects.length > 1) {
  57. // Sort by name
  58. array_sort(project_paint_objects, function (a: mesh_object_t, b: mesh_object_t): i32 {
  59. if (a.base.name < b.base.name) {
  60. return -1;
  61. }
  62. else if (a.base.name > b.base.name) {
  63. return 1;
  64. }
  65. return 0;
  66. });
  67. // No mask by default
  68. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  69. let p: mesh_object_t = project_paint_objects[i];
  70. p.base.visible = true;
  71. }
  72. if (context_raw.merged_object == null) {
  73. util_mesh_merge();
  74. }
  75. context_raw.paint_object.skip_context = "paint";
  76. context_raw.merged_object.base.visible = true;
  77. }
  78. viewport_scale_to_bounds();
  79. if (context_raw.paint_object.base.name == "") {
  80. context_raw.paint_object.base.name = "Object";
  81. }
  82. make_material_parse_paint_material();
  83. make_material_parse_mesh_material();
  84. ///if (is_paint || is_sculpt)
  85. ui_view2d_hwnd.redraws = 2;
  86. ///end
  87. ///if (krom_direct3d12 || krom_vulkan || krom_metal)
  88. render_path_raytrace_ready = false;
  89. ///end
  90. ///if arm_physics
  91. context_raw.paint_body = null;
  92. ///end
  93. }
  94. function _import_mesh_make_mesh(mesh: any) {
  95. let raw: mesh_data_t = import_mesh_raw_mesh(mesh);
  96. if (mesh.cola != null) {
  97. array_push(raw.vertex_arrays, { values: mesh.cola, attrib: "col", data: "short4norm" });
  98. }
  99. let md: mesh_data_t = mesh_data_create(raw);
  100. context_raw.paint_object = context_main_object();
  101. context_select_paint_object(context_main_object());
  102. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  103. let p: mesh_object_t = project_paint_objects[i];
  104. if (p == context_raw.paint_object) {
  105. continue;
  106. }
  107. data_delete_mesh(p.data._.handle);
  108. mesh_object_remove(p);
  109. }
  110. let handle: string = context_raw.paint_object.data._.handle;
  111. if (handle != "SceneSphere" && handle != "ScenePlane") {
  112. data_delete_mesh(handle);
  113. }
  114. mesh_object_set_data(context_raw.paint_object, md);
  115. context_raw.paint_object.base.name = mesh.name;
  116. project_paint_objects = [context_raw.paint_object];
  117. md._.handle = raw.name;
  118. map_set(data_cached_meshes, md._.handle, md);
  119. context_raw.ddirty = 4;
  120. ///if (is_paint || is_sculpt)
  121. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  122. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  123. util_uv_uvmap_cached = false;
  124. util_uv_trianglemap_cached = false;
  125. util_uv_dilatemap_cached = false;
  126. ///end
  127. ///if (is_paint || is_sculpt)
  128. if (import_mesh_clear_layers) {
  129. while (project_layers.length > 0) {
  130. let l: slot_layer_t = project_layers.pop();
  131. slot_layer_unload(l);
  132. }
  133. base_new_layer(false);
  134. app_notify_on_init(base_init_layers);
  135. history_reset();
  136. }
  137. ///end
  138. // Wait for addMesh calls to finish
  139. if (import_mesh_meshes_to_unwrap != null) {
  140. app_notify_on_next_frame(import_mesh_finish_import);
  141. }
  142. else {
  143. app_notify_on_init(import_mesh_finish_import);
  144. }
  145. }
  146. function import_mesh_make_mesh(mesh: any, path: string) {
  147. if (mesh == null || mesh.posa == null || mesh.nora == null || mesh.inda == null || mesh.posa.length == 0) {
  148. console_error(strings_error3());
  149. return;
  150. }
  151. if (mesh.texa == null) {
  152. if (import_mesh_meshes_to_unwrap == null) {
  153. import_mesh_meshes_to_unwrap = [];
  154. }
  155. let first_unwrap_done = function (mesh: any) {
  156. _import_mesh_make_mesh(mesh);
  157. for (let i: i32 = 0; i < import_mesh_meshes_to_unwrap.length; ++i) {
  158. let mesh: any = import_mesh_meshes_to_unwrap[i];
  159. project_unwrap_mesh_box(mesh, _import_mesh_add_mesh, true);
  160. }
  161. }
  162. project_unwrap_mesh_box(mesh, first_unwrap_done);
  163. }
  164. else {
  165. _import_mesh_make_mesh(mesh);
  166. }
  167. }
  168. function _import_mesh_add_mesh(mesh: any) {
  169. let raw: mesh_data_t = import_mesh_raw_mesh(mesh);
  170. if (mesh.cola != null) {
  171. array_push(raw.vertex_arrays, { values: mesh.cola, attrib: "col", data: "short4norm" });
  172. }
  173. let md: mesh_data_t = mesh_data_create(raw);
  174. let object: mesh_object_t = scene_add_mesh_object(md, context_raw.paint_object.materials, context_raw.paint_object.base);
  175. object.base.name = mesh.name;
  176. object.skip_context = "paint";
  177. // Ensure unique names
  178. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  179. let p: mesh_object_t = project_paint_objects[i];
  180. if (p.base.name == object.base.name) {
  181. p.base.name += ".001";
  182. p.data._.handle += ".001";
  183. map_set(data_cached_meshes, p.data._.handle, p.data);
  184. }
  185. }
  186. array_push(project_paint_objects, object);
  187. md._.handle = raw.name;
  188. map_set(data_cached_meshes, md._.handle, md);
  189. context_raw.ddirty = 4;
  190. ///if (is_paint || is_sculpt)
  191. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  192. util_uv_uvmap_cached = false;
  193. util_uv_trianglemap_cached = false;
  194. util_uv_dilatemap_cached = false;
  195. ///end
  196. }
  197. function import_mesh_add_mesh(mesh: any) {
  198. if (mesh.texa == null) {
  199. if (import_mesh_meshes_to_unwrap != null) {
  200. array_push(import_mesh_meshes_to_unwrap, mesh);
  201. }
  202. else {
  203. project_unwrap_mesh_box(mesh, _import_mesh_add_mesh);
  204. }
  205. }
  206. else {
  207. _import_mesh_add_mesh(mesh);
  208. }
  209. }
  210. function import_mesh_raw_mesh(mesh: any): mesh_data_t {
  211. return {
  212. name: mesh.name,
  213. vertex_arrays: [
  214. { values: mesh.posa, attrib: "pos", data: "short4norm" },
  215. { values: mesh.nora, attrib: "nor", data: "short2norm" },
  216. { values: mesh.texa, attrib: "tex", data: "short2norm" }
  217. ],
  218. index_arrays: [
  219. { values: mesh.inda, material: 0 }
  220. ],
  221. scale_pos: mesh.scale_pos,
  222. scale_tex: mesh.scale_tex
  223. };
  224. }