2
0

import_texture.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. type import_texture_data_t = {
  2. path: string;
  3. image: image_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_error1());
  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: image_t = data_get_image(path);
  19. let itd: import_texture_data_t = { path: path, image: image };
  20. app_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_info0());
  25. return;
  26. }
  27. }
  28. let ext: string = substring(path, string_last_index_of(path, ".") + 1, path.length);
  29. let importer: (s: string)=>image_t = map_get(path_texture_importers, ext);
  30. let cached: bool = map_get(data_cached_images, path) != null; // Already loaded or pink texture for missing file
  31. if (importer == null || cached) {
  32. importer = import_texture_default_importer;
  33. }
  34. let image: image_t = importer(path);
  35. map_set(data_cached_images, path, image);
  36. let ar: string[] = string_split(path, path_sep);
  37. let name: string = ar[ar.length - 1];
  38. let asset: asset_t = {name: name, file: path, id: project_asset_id++};
  39. array_push(project_assets, asset);
  40. if (context_raw.texture == null) {
  41. context_raw.texture = asset;
  42. }
  43. array_push(project_asset_names, name);
  44. map_set(project_asset_map, asset.id, image);
  45. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  46. console_info(tr("Texture imported:") + " " + name);
  47. // Set as envmap
  48. if (hdr_as_envmap && ends_with(to_lower_case(path), ".hdr")) {
  49. let itd: import_texture_data_t = { path: path, image: image };
  50. app_notify_on_next_frame(function (itd: import_texture_data_t) { // Make sure file browser process did finish
  51. import_envmap_run(itd.path, itd.image);
  52. }, itd);
  53. }
  54. }
  55. function import_texture_default_importer(path: string): image_t {
  56. return data_get_image(path);
  57. }