export_texture.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. let export_texture_gamma: f32 = 1.0 / 2.2;
  2. function export_texture_run(path: string, bake_material: bool = false) {
  3. ///if is_paint
  4. if (bake_material) {
  5. export_texture_run_bake_material(path);
  6. }
  7. else if (context_raw.layers_export == export_mode_t.PER_UDIM_TILE) {
  8. let udim_tiles: string[] = [];
  9. for (let i: i32 = 0; i < project_layers.length; ++i) {
  10. let l: slot_layer_t = project_layers[i];
  11. if (slot_layer_get_object_mask(l) > 0) {
  12. let name: string = project_paint_objects[slot_layer_get_object_mask(l) - 1].base.name;
  13. if (substring(name, name.length - 5, 2) == ".1") { // tile.1001
  14. array_push(udim_tiles, substring(name, name.length - 5, name.length));
  15. }
  16. }
  17. }
  18. if (udim_tiles.length > 0) {
  19. for (let i: i32 = 0; i < udim_tiles.length; ++i) {
  20. let udim_tile: string = udim_tiles[i];
  21. export_texture_run_layers(path, project_layers, udim_tile);
  22. }
  23. }
  24. else {
  25. export_texture_run_layers(path, project_layers);
  26. }
  27. }
  28. else if (context_raw.layers_export == export_mode_t.PER_OBJECT) {
  29. let object_names: string[] = [];
  30. for (let i: i32 = 0; i < project_layers.length; ++i) {
  31. let l: slot_layer_t = project_layers[i];
  32. if (slot_layer_get_object_mask(l) > 0) {
  33. let name: string = project_paint_objects[slot_layer_get_object_mask(l) - 1].base.name;
  34. if (array_index_of(object_names, name) == -1) {
  35. array_push(object_names, name);
  36. }
  37. }
  38. }
  39. if (object_names.length > 0) {
  40. for (let i: i32 = 0; i < object_names.length; ++i) {
  41. let name: string = object_names[i];
  42. export_texture_run_layers(path, project_layers, name);
  43. }
  44. }
  45. else {
  46. export_texture_run_layers(path, project_layers);
  47. }
  48. }
  49. else { // Visible or selected
  50. let atlas_export: bool = false;
  51. if (project_atlas_objects != null) {
  52. for (let i: i32 = 1; i < project_atlas_objects.length; ++i) {
  53. if (project_atlas_objects[i - 1] != project_atlas_objects[i]) {
  54. atlas_export = true;
  55. break;
  56. }
  57. }
  58. }
  59. if (atlas_export) {
  60. for (let atlas_index: i32 = 0; atlas_index < project_atlas_objects.length; ++atlas_index) {
  61. let layers: slot_layer_t[] = [];
  62. for (let object_index: i32 = 0; object_index < project_atlas_objects.length; ++object_index) {
  63. if (project_atlas_objects[object_index] == atlas_index) {
  64. for (let i: i32 = 0; i < project_layers.length; ++i) {
  65. let l: slot_layer_t = project_layers[i];
  66. if (slot_layer_get_object_mask(l) == 0 || // shared object
  67. slot_layer_get_object_mask(l) - 1 == object_index) {
  68. array_push(layers, l);
  69. }
  70. }
  71. }
  72. }
  73. if (layers.length > 0) {
  74. export_texture_run_layers(path, layers, project_atlas_names[atlas_index]);
  75. }
  76. }
  77. }
  78. else {
  79. let layers: slot_layer_t[];
  80. if (context_raw.layers_export == export_mode_t.SELECTED) {
  81. if (slot_layer_is_group(context_raw.layer)) {
  82. layers = slot_layer_get_children(context_raw.layer);
  83. }
  84. else {
  85. layers = [context_raw.layer];
  86. }
  87. }
  88. else {
  89. layers = project_layers;
  90. }
  91. export_texture_run_layers(path, layers);
  92. }
  93. }
  94. ///end
  95. ///if is_lab
  96. let layers: slot_layer_t[] = [context_raw.brush_output_node_inst];
  97. export_texture_run_layers(path, layers);
  98. ///end
  99. ///if arm_ios
  100. console_info(tr("Textures exported") + " (\"Files/On My iPad/" + manifest_title + "\")");
  101. ///elseif arm_android
  102. console_info(tr("Textures exported") + " (\"Files/Internal storage/Pictures/" + manifest_title + "\")");
  103. ///else
  104. console_info(tr("Textures exported"));
  105. ///end
  106. ui_files_last_path = "";
  107. }
  108. function export_texture_run_bake_material(path: string) {
  109. if (render_path_paint_live_layer == null) {
  110. render_path_paint_live_layer = slot_layer_create("_live");
  111. }
  112. let _tool: workspace_tool_t = context_raw.tool;
  113. context_raw.tool = workspace_tool_t.FILL;
  114. make_material_parse_paint_material();
  115. let _paint_object: mesh_object_t = context_raw.paint_object;
  116. let planeo: mesh_object_t = scene_get_child(".Plane").ext;
  117. planeo.base.visible = true;
  118. context_raw.paint_object = planeo;
  119. context_raw.pdirty = 1;
  120. let _visibles: bool[] = [];
  121. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  122. let p: mesh_object_t = project_paint_objects[i];
  123. array_push(_visibles, p.base.visible);
  124. p.base.visible = false;
  125. }
  126. render_path_paint_use_live_layer(true);
  127. render_path_paint_commands_paint(false);
  128. render_path_paint_use_live_layer(false);
  129. context_raw.tool = _tool;
  130. make_material_parse_paint_material();
  131. context_raw.pdirty = 0;
  132. planeo.base.visible = false;
  133. context_raw.paint_object = _paint_object;
  134. for (let i: i32 = 0; i < project_paint_objects.length; ++i) {
  135. project_paint_objects[i].base.visible = _visibles[i];
  136. }
  137. let layers: slot_layer_t[] = [render_path_paint_live_layer];
  138. export_texture_run_layers(path, layers, "", true);
  139. }
  140. function export_texture_run_layers(path: string, layers: slot_layer_t[], object_name: string = "", bake_material: bool = false) {
  141. let texture_size_x: i32 = config_get_texture_res_x();
  142. let texture_size_y: i32 = config_get_texture_res_y();
  143. ///if (arm_android || arm_ios)
  144. let f: string = sys_title();
  145. ///else
  146. let f: string = ui_files_filename;
  147. ///end
  148. if (f == "") {
  149. f = tr("untitled");
  150. }
  151. let format_type: texture_ldr_format_t = context_raw.format_type;
  152. let bits: i32 = base_bits_handle.position == texture_bits_t.BITS8 ? 8 : 16;
  153. let ext: string = bits == 16 ? ".exr" : format_type == texture_ldr_format_t.PNG ? ".png" : ".jpg";
  154. if (ends_with(f, ext)) {
  155. f = substring(f, 0, f.length - 4);
  156. }
  157. ///if is_paint
  158. let is_udim: bool = context_raw.layers_export == export_mode_t.PER_UDIM_TILE;
  159. if (is_udim) {
  160. ext = object_name + ext;
  161. }
  162. layers_make_temp_img();
  163. layers_make_export_img();
  164. let rt: render_target_t = map_get(render_path_render_targets, "empty_white");
  165. let empty: gpu_texture_t = rt._image;
  166. // Append object mask name
  167. let export_selected: bool = context_raw.layers_export == export_mode_t.SELECTED;
  168. if (export_selected && slot_layer_get_object_mask(layers[0]) > 0) {
  169. f += "_" + project_paint_objects[slot_layer_get_object_mask(layers[0]) - 1].base.name;
  170. }
  171. if (!is_udim && !export_selected && object_name != "") {
  172. f += "_" + object_name;
  173. }
  174. // Clear export layer
  175. _gpu_begin(layers_expa, null, null, clear_flag_t.COLOR, color_from_floats(0.0, 0.0, 0.0, 0.0));
  176. gpu_end();
  177. _gpu_begin(layers_expb, null, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0));
  178. gpu_end();
  179. _gpu_begin(layers_expc, null, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.0, 0.0, 0.0));
  180. gpu_end();
  181. // Flatten layers
  182. for (let i: i32 = 0; i < layers.length; ++i) {
  183. let l1: slot_layer_t = layers[i];
  184. if (!export_selected && !slot_layer_is_visible(l1)) {
  185. continue;
  186. }
  187. if (!slot_layer_is_layer(l1)) {
  188. continue;
  189. }
  190. if (object_name != "" && slot_layer_get_object_mask(l1) > 0) {
  191. if (is_udim && !ends_with(project_paint_objects[slot_layer_get_object_mask(l1) - 1].base.name, object_name)) {
  192. continue;
  193. }
  194. let per_object: bool = context_raw.layers_export == export_mode_t.PER_OBJECT;
  195. if (per_object && project_paint_objects[slot_layer_get_object_mask(l1) - 1].base.name != object_name) {
  196. continue;
  197. }
  198. }
  199. let mask: gpu_texture_t = empty;
  200. let l1masks: slot_layer_t[] = slot_layer_get_masks(l1);
  201. if (l1masks != null && !bake_material) {
  202. if (l1masks.length > 1) {
  203. layers_make_temp_mask_img();
  204. draw_begin(pipes_temp_mask_image, true, 0x00000000);
  205. draw_end();
  206. let l1: slot_layer_t = {
  207. texpaint: pipes_temp_mask_image
  208. };
  209. for (let i: i32 = 0; i < l1masks.length; ++i) {
  210. layers_merge_layer(l1, l1masks[i]);
  211. }
  212. mask = pipes_temp_mask_image;
  213. }
  214. else {
  215. mask = l1masks[0].texpaint;
  216. }
  217. }
  218. if (l1.paint_base) {
  219. draw_begin(layers_temp_image); // Copy to temp
  220. draw_set_pipeline(pipes_copy);
  221. draw_image(layers_expa, 0, 0);
  222. draw_set_pipeline(null);
  223. draw_end();
  224. _gpu_begin(layers_expa);
  225. gpu_set_pipeline(pipes_merge);
  226. gpu_set_texture(pipes_tex0, l1.texpaint);
  227. gpu_set_texture(pipes_tex1, empty);
  228. gpu_set_texture(pipes_texmask, mask);
  229. gpu_set_texture(pipes_texa, layers_temp_image);
  230. gpu_set_float(pipes_opac, slot_layer_get_opacity(l1));
  231. gpu_set_float(pipes_tex1w, empty.width);
  232. gpu_set_int(pipes_blending, layers.length > 1 ? l1.blending : 0);
  233. gpu_set_vertex_buffer(const_data_screen_aligned_vb);
  234. gpu_set_index_buffer(const_data_screen_aligned_ib);
  235. gpu_draw();
  236. gpu_end();
  237. }
  238. if (l1.paint_nor) {
  239. draw_begin(layers_temp_image);
  240. draw_set_pipeline(pipes_copy);
  241. draw_image(layers_expb, 0, 0);
  242. draw_set_pipeline(null);
  243. draw_end();
  244. _gpu_begin(layers_expb);
  245. gpu_set_pipeline(pipes_merge);
  246. gpu_set_texture(pipes_tex0, l1.texpaint);
  247. gpu_set_texture(pipes_tex1, l1.texpaint_nor);
  248. gpu_set_texture(pipes_texmask, mask);
  249. gpu_set_texture(pipes_texa, layers_temp_image);
  250. gpu_set_float(pipes_opac, slot_layer_get_opacity(l1));
  251. gpu_set_float(pipes_tex1w, l1.texpaint_nor.width);
  252. gpu_set_int(pipes_blending, l1.paint_nor_blend ? 102 : 101);
  253. gpu_set_vertex_buffer(const_data_screen_aligned_vb);
  254. gpu_set_index_buffer(const_data_screen_aligned_ib);
  255. gpu_draw();
  256. gpu_end();
  257. }
  258. if (l1.paint_occ || l1.paint_rough || l1.paint_met || l1.paint_height) {
  259. draw_begin(layers_temp_image);
  260. draw_set_pipeline(pipes_copy);
  261. draw_image(layers_expc, 0, 0);
  262. draw_set_pipeline(null);
  263. draw_end();
  264. if (l1.paint_occ && l1.paint_rough && l1.paint_met && l1.paint_height) {
  265. layers_commands_merge_pack(pipes_merge, layers_expc, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask, l1.paint_height_blend ? 103 : 101);
  266. }
  267. else {
  268. if (l1.paint_occ) layers_commands_merge_pack(pipes_merge_r, layers_expc, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask);
  269. if (l1.paint_rough) layers_commands_merge_pack(pipes_merge_g, layers_expc, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask);
  270. if (l1.paint_met) layers_commands_merge_pack(pipes_merge_b, layers_expc, l1.texpaint, l1.texpaint_pack, slot_layer_get_opacity(l1), mask);
  271. }
  272. }
  273. }
  274. ///end
  275. let texpaint: gpu_texture_t = layers_expa;
  276. let texpaint_nor: gpu_texture_t = layers_expb;
  277. let texpaint_pack: gpu_texture_t = layers_expc;
  278. ///if is_lab
  279. texpaint = context_raw.brush_output_node_inst.texpaint;
  280. texpaint_nor = context_raw.brush_output_node_inst.texpaint_nor;
  281. texpaint_pack = context_raw.brush_output_node_inst.texpaint_pack;
  282. ///end
  283. let pixpaint: buffer_t = null;
  284. let pixpaint_nor: buffer_t = null;
  285. let pixpaint_pack: buffer_t = null;
  286. let preset: export_preset_t = box_export_preset;
  287. let pix: buffer_t = null;
  288. for (let i: i32 = 0; i < preset.textures.length; ++i) {
  289. let t: export_preset_texture_t = preset.textures[i];
  290. for (let i: i32 = 0; i < t.channels.length; ++i) {
  291. let c: string = t.channels[i];
  292. if ((c == "base_r" || c == "base_g" || c == "base_b" || c == "opac") && pixpaint == null) {
  293. pixpaint = gpu_get_texture_pixels(texpaint);
  294. }
  295. else if ((c == "nor_r" || c == "nor_g" || c == "nor_g_directx" || c == "nor_b" || c == "emis" || c == "subs") && pixpaint_nor == null) {
  296. pixpaint_nor = gpu_get_texture_pixels(texpaint_nor);
  297. }
  298. else if ((c == "occ" || c == "rough" || c == "metal" || c == "height" || c == "smooth") && pixpaint_pack == null) {
  299. pixpaint_pack = gpu_get_texture_pixels(texpaint_pack);
  300. }
  301. }
  302. }
  303. for (let i: i32 = 0; i < preset.textures.length; ++i) {
  304. let t: export_preset_texture_t = preset.textures[i];
  305. let c: string[] = t.channels;
  306. let tex_name: string = t.name != "" ? "_" + t.name : "";
  307. let single_channel: bool = c[0] == c[1] && c[1] == c[2] && c[3] == "1.0";
  308. if (c[0] == "base_r" && c[1] == "base_g" && c[2] == "base_b" && c[3] == "1.0" && t.color_space == "linear") {
  309. export_texture_write_texture(path + path_sep + f + tex_name + ext, pixpaint, 1);
  310. }
  311. else if (c[0] == "nor_r" && c[1] == "nor_g" && c[2] == "nor_b" && c[3] == "1.0" && t.color_space == "linear") {
  312. export_texture_write_texture(path + path_sep + f + tex_name + ext, pixpaint_nor, 1);
  313. }
  314. else if (c[0] == "occ" && c[1] == "rough" && c[2] == "metal" && c[3] == "1.0" && t.color_space == "linear") {
  315. export_texture_write_texture(path + path_sep + f + tex_name + ext, pixpaint_pack, 1);
  316. }
  317. else if (single_channel && c[0] == "occ" && t.color_space == "linear") {
  318. export_texture_write_texture(path + path_sep + f + tex_name + ext, pixpaint_pack, 2, 0);
  319. }
  320. else if (single_channel && c[0] == "rough" && t.color_space == "linear") {
  321. export_texture_write_texture(path + path_sep + f + tex_name + ext, pixpaint_pack, 2, 1);
  322. }
  323. else if (single_channel && c[0] == "metal" && t.color_space == "linear") {
  324. export_texture_write_texture(path + path_sep + f + tex_name + ext, pixpaint_pack, 2, 2);
  325. }
  326. else if (single_channel && c[0] == "height" && t.color_space == "linear") {
  327. export_texture_write_texture(path + path_sep + f + tex_name + ext, pixpaint_pack, 2, 3);
  328. }
  329. else if (single_channel && c[0] == "opac" && t.color_space == "linear") {
  330. export_texture_write_texture(path + path_sep + f + tex_name + ext, pixpaint, 2, 3);
  331. }
  332. else {
  333. if (pix == null) {
  334. pix = buffer_create(texture_size_x * texture_size_y * 4 * math_floor(bits / 8));
  335. }
  336. for (let i: i32 = 0; i < 4; ++i) {
  337. let c: string = t.channels[i];
  338. if (c == "base_r") {
  339. export_texture_copy_channel(pixpaint, 0, pix, i, t.color_space == "linear");
  340. }
  341. else if (c == "base_g") {
  342. export_texture_copy_channel(pixpaint, 1, pix, i, t.color_space == "linear");
  343. }
  344. else if (c == "base_b") {
  345. export_texture_copy_channel(pixpaint, 2, pix, i, t.color_space == "linear");
  346. }
  347. else if (c == "height") {
  348. export_texture_copy_channel(pixpaint_pack, 3, pix, i, t.color_space == "linear");
  349. }
  350. else if (c == "metal") {
  351. export_texture_copy_channel(pixpaint_pack, 2, pix, i, t.color_space == "linear");
  352. }
  353. else if (c == "nor_r") {
  354. export_texture_copy_channel(pixpaint_nor, 0, pix, i, t.color_space == "linear");
  355. }
  356. else if (c == "nor_g") {
  357. export_texture_copy_channel(pixpaint_nor, 1, pix, i, t.color_space == "linear");
  358. }
  359. else if (c == "nor_g_directx") {
  360. export_texture_copy_channel_inv(pixpaint_nor, 1, pix, i, t.color_space == "linear");
  361. }
  362. else if (c == "nor_b") {
  363. export_texture_copy_channel(pixpaint_nor, 2, pix, i, t.color_space == "linear");
  364. }
  365. else if (c == "occ") {
  366. export_texture_copy_channel(pixpaint_pack, 0, pix, i, t.color_space == "linear");
  367. }
  368. else if (c == "opac") {
  369. export_texture_copy_channel(pixpaint, 3, pix, i, t.color_space == "linear");
  370. }
  371. else if (c == "rough") {
  372. export_texture_copy_channel(pixpaint_pack, 1, pix, i, t.color_space == "linear");
  373. }
  374. else if (c == "smooth") {
  375. export_texture_copy_channel_inv(pixpaint_pack, 1, pix, i, t.color_space == "linear");
  376. }
  377. else if (c == "emis") {
  378. export_texture_extract_channel(pixpaint_nor, 3, pix, i, 3, 1, t.color_space == "linear");
  379. }
  380. else if (c == "subs") {
  381. export_texture_extract_channel(pixpaint_nor, 3, pix, i, 3, 2, t.color_space == "linear");
  382. }
  383. else if (c == "0.0") {
  384. export_texture_set_channel(0, pix, i);
  385. }
  386. else if (c == "1.0") {
  387. export_texture_set_channel(255, pix, i);
  388. }
  389. }
  390. export_texture_write_texture(path + path_sep + f + tex_name + ext, pix, 3);
  391. }
  392. }
  393. // Release staging memory allocated in gpu_get_texture_pixels()
  394. // texpaint.pixels = null;
  395. // texpaint_nor.pixels = null;
  396. // texpaint_pack.pixels = null;
  397. }
  398. function export_texture_write_texture(file: string, pixels: buffer_t, type: i32 = 1, off: i32 = 0) {
  399. let res_x: i32 = config_get_texture_res_x();
  400. let res_y: i32 = config_get_texture_res_y();
  401. let bits_handle: i32 = base_bits_handle.position;
  402. let bits: i32 = bits_handle == texture_bits_t.BITS8 ? 8 : bits_handle == texture_bits_t.BITS16 ? 16 : 32;
  403. let format: i32 = 0; // RGBA
  404. if (type == 1) {
  405. format = 2; // RGB1
  406. }
  407. if (type == 2 && off == 0) {
  408. format = 3; // RRR1
  409. }
  410. if (type == 2 && off == 1) {
  411. format = 4; // GGG1
  412. }
  413. if (type == 2 && off == 2) {
  414. format = 5; // BBB1
  415. }
  416. if (type == 2 && off == 3) {
  417. format = 6; // AAA1
  418. }
  419. if (context_raw.layers_destination == export_destination_t.PACKED) {
  420. let image: gpu_texture_t = gpu_create_texture_from_bytes(pixels, res_x, res_y);
  421. map_set(data_cached_images, file, image);
  422. let ar: string[] = string_split(file, path_sep);
  423. let name: string = ar[ar.length - 1];
  424. let asset: asset_t = {
  425. name: name,
  426. file: file,
  427. id: project_asset_id++
  428. };
  429. array_push(project_assets, asset);
  430. if (project_raw.assets == null) {
  431. project_raw.assets = [];
  432. }
  433. array_push(project_raw.assets, asset.file);
  434. array_push(project_asset_names, asset.name);
  435. map_set(project_asset_map, asset.id, image);
  436. let assets: asset_t[] = [asset];
  437. export_arm_pack_assets(project_raw, assets);
  438. return;
  439. }
  440. if (bits == 8 && context_raw.format_type == texture_ldr_format_t.PNG) {
  441. iron_write_png(file, pixels, res_x, res_y, format);
  442. }
  443. else if (bits == 8 && context_raw.format_type == texture_ldr_format_t.JPG) {
  444. iron_write_jpg(file, pixels, res_x, res_y, format, math_floor(context_raw.format_quality));
  445. }
  446. else { // Exr
  447. let b: buffer_t = parser_exr_run(res_x, res_y, pixels, bits, type, off);
  448. iron_file_save_bytes(file, b, b.length);
  449. }
  450. }
  451. ///if (arm_metal || arm_vulkan)
  452. function _export_texture_channel_bgra_swap(c: i32): i32 {
  453. return c == 0 ? 2 : c == 2 ? 0 : c;
  454. }
  455. ///end
  456. function export_texture_copy_channel(from: buffer_t, from_channel: i32, to: buffer_t, to_channel: i32, linear: bool = true) {
  457. ///if (arm_metal || arm_vulkan)
  458. from_channel = _export_texture_channel_bgra_swap(from_channel);
  459. ///end
  460. for (let i: i32 = 0; i < math_floor((to.length) / 4); ++i) {
  461. buffer_set_u8(to, i * 4 + to_channel, buffer_get_u8(from, i * 4 + from_channel));
  462. }
  463. if (!linear) {
  464. export_texture_to_srgb(to, to_channel);
  465. }
  466. }
  467. function export_texture_copy_channel_inv(from: buffer_t, from_channel: i32, to: buffer_t, to_channel: i32, linear: bool = true) {
  468. ///if (arm_metal || arm_vulkan)
  469. from_channel = _export_texture_channel_bgra_swap(from_channel);
  470. ///end
  471. for (let i: i32 = 0; i < math_floor((to.length) / 4); ++i) {
  472. buffer_set_u8(to, i * 4 + to_channel, 255 - buffer_get_u8(from, i * 4 + from_channel));
  473. }
  474. if (!linear) {
  475. export_texture_to_srgb(to, to_channel);
  476. }
  477. }
  478. function export_texture_extract_channel(from: buffer_t, from_channel: i32, to: buffer_t, to_channel: i32, step: i32, mask: i32, linear: bool = true) {
  479. ///if (arm_metal || arm_vulkan)
  480. from_channel = _export_texture_channel_bgra_swap(from_channel);
  481. ///end
  482. for (let i: i32 = 0; i < math_floor((to.length) / 4); ++i) {
  483. buffer_set_u8(to, i * 4 + to_channel, buffer_get_u8(from, i * 4 + from_channel) % step == mask ? 255 : 0);
  484. }
  485. if (!linear) {
  486. export_texture_to_srgb(to, to_channel);
  487. }
  488. }
  489. function export_texture_set_channel(value: i32, to: buffer_t, to_channel: i32, linear: bool = true) {
  490. for (let i: i32 = 0; i < math_floor((to.length) / 4); ++i) {
  491. buffer_set_u8(to, i * 4 + to_channel, value);
  492. }
  493. if (!linear) {
  494. export_texture_to_srgb(to, to_channel);
  495. }
  496. }
  497. function export_texture_to_srgb(to: buffer_t, to_channel: i32) {
  498. for (let i: i32 = 0; i < math_floor((to.length) / 4); ++i) {
  499. buffer_set_u8(to, i * 4 + to_channel, math_floor(math_pow(buffer_get_u8(to, i * 4 + to_channel) / 255, export_texture_gamma) * 255));
  500. }
  501. }
  502. type export_preset_t = {
  503. textures?: export_preset_texture_t[];
  504. };
  505. type export_preset_texture_t = {
  506. name?: string;
  507. channels?: string[];
  508. color_space?: string;
  509. };