export_arm.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. function export_arm_run_mesh(path: string, paint_objects: mesh_object_t[]) {
  2. let mesh_datas: mesh_data_t[] = [];
  3. for (let i: i32 = 0; i < paint_objects.length; ++i) {
  4. let p: mesh_object_t = paint_objects[i];
  5. array_push(mesh_datas, p.data);
  6. }
  7. let raw: scene_t = {
  8. mesh_datas: mesh_datas
  9. };
  10. let b: buffer_t = util_encode_scene(raw);
  11. if (!ends_with(path, ".arm")) {
  12. path += ".arm";
  13. }
  14. iron_file_save_bytes(path, b, b.length + 1);
  15. }
  16. function export_arm_run_project() {
  17. ///if is_paint
  18. let mnodes: ui_node_canvas_t[] = [];
  19. for (let i: i32 = 0; i < project_materials.length; ++i) {
  20. let m: slot_material_t = project_materials[i];
  21. let c: ui_node_canvas_t = util_clone_canvas(m.canvas);
  22. for (let i: i32 = 0; i < c.nodes.length; ++i) {
  23. let n: ui_node_t = c.nodes[i];
  24. export_arm_export_node(n);
  25. }
  26. array_push(mnodes, c);
  27. }
  28. let bnodes: ui_node_canvas_t[] = [];
  29. for (let i: i32 = 0; i < project_brushes.length; ++i) {
  30. let b: slot_brush_t = project_brushes[i];
  31. array_push(bnodes, b.canvas);
  32. }
  33. ///end
  34. ///if is_lab
  35. let c: ui_node_canvas_t = util_clone_canvas(project_canvas);
  36. for (let i: i32 = 0; i < c.nodes.length; ++i) {
  37. let n: ui_node_t = c.nodes[i];
  38. export_arm_export_node(n);
  39. }
  40. ///end
  41. let mgroups: ui_node_canvas_t[] = null;
  42. if (project_material_groups.length > 0) {
  43. mgroups = [];
  44. for (let i: i32 = 0; i < project_material_groups.length; ++i) {
  45. let g: node_group_t = project_material_groups[i];
  46. let c: ui_node_canvas_t = util_clone_canvas(g.canvas);
  47. for (let i: i32 = 0; i < c.nodes.length; ++i) {
  48. let n: ui_node_t = c.nodes[i];
  49. export_arm_export_node(n);
  50. }
  51. array_push(mgroups, c);
  52. }
  53. }
  54. ///if is_paint
  55. let md: mesh_data_t[] = [];
  56. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  57. let p: mesh_object_t = project_paint_objects[i];
  58. array_push(md, p.data);
  59. }
  60. ///end
  61. ///if is_lab
  62. let md: mesh_data_t = project_paint_objects[0].data;
  63. ///end
  64. let texture_files: string[] = export_arm_assets_to_files(project_filepath, project_assets);
  65. ///if is_paint
  66. let font_files: string[] = export_arm_fonts_to_files(project_filepath, project_fonts);
  67. let mesh_files: string[] = export_arm_meshes_to_files(project_filepath);
  68. let bits_pos: i32 = base_bits_handle.position;
  69. let bpp: i32 = bits_pos == texture_bits_t.BITS8 ? 8 : bits_pos == texture_bits_t.BITS16 ? 16 : 32;
  70. let ld: layer_data_t[] = [];
  71. for (let i: i32 = 0; i < project_layers.length; ++i) {
  72. let l: slot_layer_t = project_layers[i];
  73. let d: layer_data_t = {
  74. name: l.name,
  75. res: l.texpaint != null ? l.texpaint.width : project_layers[0].texpaint.width,
  76. bpp: bpp,
  77. texpaint: l.texpaint != null ? lz4_encode(gpu_get_texture_pixels(l.texpaint)) : null,
  78. uv_scale: l.scale,
  79. uv_rot: l.angle,
  80. uv_type: l.uv_type,
  81. decal_mat: l.uv_type == uv_type_t.PROJECT ? mat4_to_f32_array(l.decal_mat) : null,
  82. opacity_mask: l.mask_opacity,
  83. fill_layer: l.fill_layer != null ? array_index_of(project_materials, l.fill_layer) : -1,
  84. object_mask: l.object_mask,
  85. blending: l.blending,
  86. parent: l.parent != null ? array_index_of(project_layers, l.parent) : -1,
  87. visible: l.visible,
  88. ///if is_paint
  89. texpaint_nor: l.texpaint_nor != null ? lz4_encode(gpu_get_texture_pixels(l.texpaint_nor)) : null,
  90. texpaint_pack: l.texpaint_pack != null ? lz4_encode(gpu_get_texture_pixels(l.texpaint_pack)) : null,
  91. paint_base: l.paint_base,
  92. paint_opac: l.paint_opac,
  93. paint_occ: l.paint_occ,
  94. paint_rough: l.paint_rough,
  95. paint_met: l.paint_met,
  96. paint_nor: l.paint_nor,
  97. paint_nor_blend: l.paint_nor_blend,
  98. paint_height: l.paint_height,
  99. paint_height_blend: l.paint_height_blend,
  100. paint_emis: l.paint_emis,
  101. paint_subs: l.paint_subs
  102. ///end
  103. };
  104. array_push(ld, d);
  105. }
  106. ///end
  107. let packed_assets: packed_asset_t[] = (project_raw.packed_assets == null || project_raw.packed_assets.length == 0) ? null : project_raw.packed_assets;
  108. ///if arm_ios
  109. let same_drive: bool = false;
  110. ///else
  111. let same_drive: bool = project_raw.envmap != null ? char_at(project_filepath, 0) == char_at(project_raw.envmap, 0) : true;
  112. ///end
  113. project_raw.version = manifest_version;
  114. project_raw.material_groups = mgroups;
  115. project_raw.assets = texture_files;
  116. project_raw.packed_assets = packed_assets;
  117. project_raw.swatches = project_raw.swatches;
  118. project_raw.envmap = project_raw.envmap != null ?
  119. (same_drive ?
  120. path_to_relative(project_filepath, project_raw.envmap) :
  121. project_raw.envmap) :
  122. null;
  123. project_raw.envmap_strength = scene_world.strength;
  124. project_raw.camera_world = mat4_to_f32_array(scene_camera.base.transform.local);
  125. project_raw.camera_origin = export_arm_vec3f32(camera_origins[0].v);
  126. project_raw.camera_fov = scene_camera.data.fov;
  127. ///if is_paint
  128. // project_raw.mesh_datas = md; // TODO: fix GC ref
  129. if (project_raw.mesh_datas == null) {
  130. project_raw.mesh_datas = md;
  131. }
  132. else {
  133. project_raw.mesh_datas.length = 0;
  134. for (let i: i32 = 0; i < md.length; ++i) {
  135. array_push(project_raw.mesh_datas, md[i]);
  136. }
  137. }
  138. project_raw.material_nodes = mnodes;
  139. project_raw.brush_nodes = bnodes;
  140. project_raw.layer_datas = ld;
  141. project_raw.font_assets = font_files;
  142. project_raw.mesh_assets = mesh_files;
  143. project_raw.atlas_objects = project_atlas_objects;
  144. project_raw.atlas_names = project_atlas_names;
  145. ///end
  146. ///if is_lab
  147. project_raw.mesh_data = md;
  148. project_raw.material = c;
  149. ///end
  150. ///if (arm_metal || arm_vulkan)
  151. project_raw.is_bgra = true;
  152. ///else
  153. project_raw.is_bgra = false;
  154. ///end
  155. ///if (arm_android || arm_ios)
  156. let rt: render_target_t = map_get(render_path_render_targets, "buf");
  157. let tex: gpu_texture_t = rt._image;
  158. let mesh_icon: gpu_texture_t = gpu_create_render_target(256, 256);
  159. let r: f32 = sys_w() / sys_h();
  160. draw_begin(mesh_icon);
  161. draw_scaled_image(tex, -(256 * r - 256) / 2, 0, 256 * r, 256);
  162. draw_end();
  163. let mesh_icon_pixels: buffer_t = gpu_get_texture_pixels(mesh_icon);
  164. let u8a: u8_array_t = mesh_icon_pixels;
  165. for (let i: i32 = 0; i < 256 * 256 * 4; ++i) {
  166. u8a[i] = math_floor(math_pow(u8a[i] / 255, 1.0 / 2.2) * 255);
  167. }
  168. ///if (arm_metal || arm_vulkan)
  169. export_arm_bgra_swap(mesh_icon_pixels);
  170. ///end
  171. iron_delete_texture(mesh_icon);
  172. // raw.mesh_icons =
  173. // ///if (arm_metal || arm_vulkan)
  174. // [encode(bgra_swap(mesh_icon_pixels)];
  175. // ///else
  176. // [encode(mesh_icon_pixels)];
  177. // ///end
  178. iron_write_png(substring(project_filepath, 0, project_filepath.length - 4) + "_icon.png", mesh_icon_pixels, 256, 256, 0);
  179. ///end
  180. ///if is_paint
  181. let is_packed: bool = ends_with(project_filepath, "_packed_.arm");
  182. if (is_packed) { // Pack textures
  183. export_arm_pack_assets(project_raw, project_assets);
  184. }
  185. ///end
  186. let buffer: buffer_t = util_encode_project(project_raw);
  187. iron_file_save_bytes(project_filepath, buffer, buffer.length + 1);
  188. // Save to recent
  189. ///if arm_ios
  190. let recent_path: string = substring(project_filepath, string_last_index_of(project_filepath, "/") + 1, project_filepath.length);
  191. ///else
  192. let recent_path: string = project_filepath;
  193. ///end
  194. ///if arm_windows
  195. recent_path = string_replace_all(recent_path, "\\", "/");
  196. ///end
  197. let recent: string[] = config_raw.recent_projects;
  198. array_remove(recent, recent_path);
  199. array_insert(recent, 0, recent_path);
  200. config_save();
  201. console_info(tr("Project saved"));
  202. }
  203. function export_arm_texture_node_name(): string {
  204. ///if is_paint
  205. return "TEX_IMAGE";
  206. ///else
  207. return "image_texture_node";
  208. ///end
  209. }
  210. function export_arm_export_node(n: ui_node_t, assets: asset_t[] = null) {
  211. if (n.type == export_arm_texture_node_name()) {
  212. let index: i32 = n.buttons[0].default_value[0];
  213. n.buttons[0].data = u8_array_create_from_string(base_enum_texts(n.type)[index]);
  214. if (assets != null) {
  215. let asset: asset_t = project_assets[index];
  216. if (array_index_of(assets, asset) == -1) {
  217. array_push(assets, asset);
  218. }
  219. }
  220. }
  221. }
  222. function export_arm_run_material(path: string) {
  223. if (!ends_with(path, ".arm")) {
  224. path += ".arm";
  225. }
  226. let mnodes: ui_node_canvas_t[] = [];
  227. let mgroups: ui_node_canvas_t[] = null;
  228. let m: slot_material_t = context_raw.material;
  229. let c: ui_node_canvas_t = util_clone_canvas(m.canvas);
  230. let assets: asset_t[] = [];
  231. if (ui_nodes_has_group(c)) {
  232. mgroups = [];
  233. ui_nodes_traverse_group(mgroups, c);
  234. for (let i: i32 = 0; i < mgroups.length; ++i) {
  235. let gc: ui_node_canvas_t = mgroups[i];
  236. for (let i: i32 = 0; i < gc.nodes.length; ++i) {
  237. let n: ui_node_t = gc.nodes[i];
  238. export_arm_export_node(n, assets);
  239. }
  240. }
  241. }
  242. for (let i: i32 = 0; i < c.nodes.length; ++i) {
  243. let n: ui_node_t = c.nodes[i];
  244. export_arm_export_node(n, assets);
  245. }
  246. array_push(mnodes, c);
  247. let texture_files: string[] = export_arm_assets_to_files(path, assets);
  248. let is_cloud: bool = ends_with(path, "_cloud_.arm");
  249. if (is_cloud) {
  250. path = string_replace_all(path, "_cloud_", "");
  251. }
  252. let packed_assets: packed_asset_t[] = null;
  253. if (!context_raw.pack_assets_on_export) {
  254. packed_assets = export_arm_get_packed_assets(path, texture_files);
  255. }
  256. let micons: buffer_t[] = null;
  257. if (!is_cloud) {
  258. ///if (arm_metal || arm_vulkan)
  259. let buf: buffer_t = lz4_encode(export_arm_bgra_swap(gpu_get_texture_pixels(m.image)));
  260. ///else
  261. let buf: buffer_t = lz4_encode(gpu_get_texture_pixels(m.image));
  262. ///end
  263. micons = [buf];
  264. }
  265. let raw: project_format_t = {
  266. version: manifest_version,
  267. material_nodes: mnodes,
  268. material_groups: mgroups,
  269. material_icons: micons,
  270. assets: texture_files,
  271. packed_assets: packed_assets
  272. };
  273. if (context_raw.write_icon_on_export) { // Separate icon files
  274. iron_write_png(substring(path, 0, path.length - 4) + "_icon.png", gpu_get_texture_pixels(m.image), m.image.width, m.image.height, 0);
  275. if (is_cloud) {
  276. iron_write_jpg(substring(path, 0, path.length - 4) + "_icon.jpg", gpu_get_texture_pixels(m.image), m.image.width, m.image.height, 0, 50);
  277. }
  278. }
  279. if (context_raw.pack_assets_on_export) { // Pack textures
  280. export_arm_pack_assets(raw, assets);
  281. }
  282. let buffer: buffer_t = util_encode_project(raw);
  283. iron_file_save_bytes(path, buffer, buffer.length + 1);
  284. }
  285. function export_arm_bgra_swap(buffer: buffer_t): buffer_t {
  286. for (let i: i32 = 0; i < math_floor((buffer.length) / 4); ++i) {
  287. let r: i32 = buffer[i * 4];
  288. buffer[i * 4] = buffer[i * 4 + 2];
  289. buffer[i * 4 + 2] = r;
  290. }
  291. return buffer;
  292. }
  293. function export_arm_run_brush(path: string) {
  294. if (!ends_with(path, ".arm")) {
  295. path += ".arm";
  296. }
  297. let bnodes: ui_node_canvas_t[] = [];
  298. let b: slot_brush_t = context_raw.brush;
  299. let c: ui_node_canvas_t = util_clone_canvas(b.canvas);
  300. let assets: asset_t[] = [];
  301. for (let i: i32 = 0; i < c.nodes.length; ++i) {
  302. let n: ui_node_t = c.nodes[i];
  303. export_arm_export_node(n, assets);
  304. }
  305. array_push(bnodes, c);
  306. let texture_files: string[] = export_arm_assets_to_files(path, assets);
  307. let is_cloud: bool = ends_with(path, "_cloud_.arm");
  308. if (is_cloud) {
  309. path = string_replace_all(path, "_cloud_", "");
  310. }
  311. let packed_assets: packed_asset_t[] = null;
  312. if (!context_raw.pack_assets_on_export) {
  313. packed_assets = export_arm_get_packed_assets(path, texture_files);
  314. }
  315. let bicons: buffer_t[] = null;
  316. if (!is_cloud) {
  317. ///if (arm_metal || arm_vulkan)
  318. let buf: buffer_t = lz4_encode(export_arm_bgra_swap(gpu_get_texture_pixels(b.image)));
  319. ///else
  320. let buf: buffer_t = lz4_encode(gpu_get_texture_pixels(b.image));
  321. ///end
  322. bicons = [buf];
  323. }
  324. let raw: project_format_t = {
  325. version: manifest_version,
  326. brush_nodes: bnodes,
  327. brush_icons: bicons,
  328. assets: texture_files,
  329. packed_assets: packed_assets
  330. };
  331. if (context_raw.write_icon_on_export) { // Separate icon file
  332. iron_write_png(substring(path, 0, path.length - 4) + "_icon.png", gpu_get_texture_pixels(b.image), b.image.width, b.image.height, 0);
  333. }
  334. if (context_raw.pack_assets_on_export) { // Pack textures
  335. export_arm_pack_assets(raw, assets);
  336. }
  337. let buffer: buffer_t = util_encode_project(raw);
  338. iron_file_save_bytes(path, buffer, buffer.length + 1);
  339. }
  340. function export_arm_assets_to_files(project_path: string, assets: asset_t[]): string[] {
  341. let texture_files: string[] = [];
  342. for (let i: i32 = 0; i < assets.length; ++i) {
  343. let a: asset_t = assets[i];
  344. ///if arm_ios
  345. let same_drive: bool = false;
  346. ///else
  347. let same_drive: bool = char_at(project_path, 0) == char_at(a.file, 0);
  348. ///end
  349. // Convert image path from absolute to relative
  350. if (same_drive) {
  351. array_push(texture_files, path_to_relative(project_path, a.file));
  352. }
  353. else {
  354. array_push(texture_files, a.file);
  355. }
  356. }
  357. return texture_files;
  358. }
  359. function export_arm_meshes_to_files(project_path: string): string[] {
  360. let mesh_files: string[] = [];
  361. for (let i: i32 = 0; i < project_mesh_assets.length; ++i) {
  362. let file: string = project_mesh_assets[i];
  363. ///if arm_ios
  364. let same_drive: bool = false;
  365. ///else
  366. let same_drive: bool = char_at(project_path, 0) == char_at(file, 0);
  367. ///end
  368. // Convert mesh path from absolute to relative
  369. if (same_drive) {
  370. array_push(mesh_files, path_to_relative(project_path, file));
  371. }
  372. else {
  373. array_push(mesh_files, file);
  374. }
  375. }
  376. return mesh_files;
  377. }
  378. function export_arm_fonts_to_files(project_path: string, fonts: slot_font_t[]): string[] {
  379. let font_files: string[] = [];
  380. for (let i: i32 = 1; i < fonts.length; ++i) {
  381. let f: slot_font_t = fonts[i];
  382. ///if arm_ios
  383. let same_drive: bool = false;
  384. ///else
  385. let same_drive: bool = char_at(project_path, 0) == char_at(f.file, 0);
  386. ///end
  387. // Convert font path from absolute to relative
  388. if (same_drive) {
  389. array_push(font_files, path_to_relative(project_path, f.file));
  390. }
  391. else {
  392. array_push(font_files, f.file);
  393. }
  394. }
  395. return font_files;
  396. }
  397. function export_arm_get_packed_assets(project_path: string, texture_files: string[]): packed_asset_t[] {
  398. let packed_assets: packed_asset_t[] = null;
  399. if (project_raw.packed_assets != null) {
  400. for (let i: i32 = 0; i < project_raw.packed_assets.length; ++i) {
  401. let pa: packed_asset_t = project_raw.packed_assets[i];
  402. ///if arm_ios
  403. let same_drive: bool = false;
  404. ///else
  405. let same_drive: bool = char_at(project_path, 0) == char_at(pa.name, 0);
  406. ///end
  407. // Convert path from absolute to relative
  408. pa.name = same_drive ? path_to_relative(project_path, pa.name) : pa.name;
  409. for (let i: i32 = 0; i < texture_files.length; ++i) {
  410. let tf: string = texture_files[i];
  411. if (pa.name == tf) {
  412. if (packed_assets == null) {
  413. packed_assets = [];
  414. }
  415. array_push(packed_assets, pa);
  416. break;
  417. }
  418. }
  419. }
  420. }
  421. return packed_assets;
  422. }
  423. function export_arm_pack_assets(raw: project_format_t, assets: asset_t[]) {
  424. if (raw.packed_assets == null) {
  425. raw.packed_assets = [];
  426. }
  427. let temp_images: gpu_texture_t[] = [];
  428. for (let i: i32 = 0; i < assets.length; ++i) {
  429. if (!project_packed_asset_exists(raw.packed_assets, assets[i].file)) {
  430. let image: gpu_texture_t = project_get_image(assets[i]);
  431. let temp: gpu_texture_t = gpu_create_render_target(image.width, image.height);
  432. draw_begin(temp);
  433. draw_image(image, 0, 0);
  434. draw_end();
  435. array_push(temp_images, temp);
  436. let pa: packed_asset_t = {
  437. name: assets[i].file,
  438. bytes: ends_with(assets[i].file, ".jpg") ?
  439. iron_encode_jpg(gpu_get_texture_pixels(temp), temp.width, temp.height, 0, 80) :
  440. iron_encode_png(gpu_get_texture_pixels(temp), temp.width, temp.height, 0)
  441. };
  442. array_push(raw.packed_assets, pa);
  443. }
  444. }
  445. for (let i: i32 = 0; i < temp_images.length; ++i) {
  446. let image: gpu_texture_t = temp_images[i];
  447. iron_delete_texture(image);
  448. }
  449. }
  450. function export_arm_run_swatches(path: string) {
  451. if (!ends_with(path, ".arm")) {
  452. path += ".arm";
  453. }
  454. let raw: project_format_t = {
  455. version: manifest_version,
  456. swatches: project_raw.swatches
  457. };
  458. let buffer: buffer_t = util_encode_project(raw);
  459. iron_file_save_bytes(path, buffer, buffer.length + 1);
  460. }
  461. function export_arm_vec3f32(v: vec4_t): f32_array_t {
  462. let res: f32_array_t = f32_array_create(3);
  463. res[0] = v.x;
  464. res[1] = v.y;
  465. res[2] = v.z;
  466. return res;
  467. }