export_texture.ts 19 KB

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