import_texture.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. type import_texture_data_t = {
  2. path: string; image : gpu_texture_t;
  3. };
  4. function import_texture_run(path: string, hdr_as_envmap: bool = true) {
  5. if (!path_is_texture(path)) {
  6. if (!context_enable_import_plugin(path)) {
  7. console_error(strings_unknown_asset_format());
  8. return;
  9. }
  10. }
  11. for (let i: i32 = 0; i < project_assets.length; ++i) {
  12. let a: asset_t = project_assets[i];
  13. // Already imported
  14. if (a.file == path) {
  15. // Set as envmap
  16. if (hdr_as_envmap && ends_with(to_lower_case(path), ".hdr")) {
  17. let image: gpu_texture_t = data_get_image(path);
  18. let itd: import_texture_data_t = {path : path, image : image};
  19. sys_notify_on_next_frame(function(itd: import_texture_data_t) { // Make sure file browser process did finish
  20. import_envmap_run(itd.path, itd.image);
  21. }, itd);
  22. }
  23. console_info(strings_asset_already_imported());
  24. return;
  25. }
  26. }
  27. let ext: string = substring(path, string_last_index_of(path, ".") + 1, path.length);
  28. let importer: any = map_get(path_texture_importers, ext); // JSValue -> (s: string)=>gpu_texture_t
  29. let cached: bool = map_get(data_cached_images, path) != null; // Already loaded or pink texture for missing file
  30. let image: gpu_texture_t;
  31. if (importer == null || cached) {
  32. image = import_texture_default_importer(path);
  33. }
  34. else {
  35. image = js_pcall_str(importer, path);
  36. }
  37. if (image == null) {
  38. return;
  39. }
  40. map_set(data_cached_images, path, image);
  41. let ar: string[] = string_split(path, path_sep);
  42. let name: string = ar[ar.length - 1];
  43. let asset: asset_t = {name : name, file : path, id : project_asset_id++};
  44. array_push(project_assets, asset);
  45. if (context_raw.texture == null) {
  46. context_raw.texture = asset;
  47. }
  48. array_push(project_asset_names, name);
  49. map_set(project_asset_map, asset.id, image);
  50. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  51. console_info(tr("Texture imported:") + " " + name);
  52. // Set as envmap
  53. if (hdr_as_envmap && ends_with(to_lower_case(path), ".hdr")) {
  54. let itd: import_texture_data_t = {path : path, image : image};
  55. sys_notify_on_next_frame(function(itd: import_texture_data_t) { // Make sure file browser process did finish
  56. import_envmap_run(itd.path, itd.image);
  57. }, itd);
  58. }
  59. }
  60. function import_texture_default_importer(path: string): gpu_texture_t {
  61. return data_get_image(path);
  62. }