import_texture.ts 2.2 KB

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