texture_resource.vala 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. using Gee;
  6. namespace Crown
  7. {
  8. public enum TextureFormat
  9. {
  10. BC1,
  11. BC2,
  12. BC3,
  13. BC4,
  14. BC5,
  15. PTC14,
  16. RGB8,
  17. RGBA8,
  18. COUNT;
  19. public string to_key()
  20. {
  21. switch (this) {
  22. case BC1:
  23. return "BC1";
  24. case BC2:
  25. return "BC2";
  26. case BC3:
  27. return "BC3";
  28. case BC4:
  29. return "BC4";
  30. case BC5:
  31. return "BC5";
  32. case PTC14:
  33. return "PTC14";
  34. case RGB8:
  35. return "RGB8";
  36. case RGBA8:
  37. return "RGBA8";
  38. default:
  39. return "BC1";
  40. }
  41. }
  42. }
  43. public struct TextureResource
  44. {
  45. public Database _db;
  46. public Guid _id;
  47. public TextureResource(Database db
  48. , Guid id
  49. , string source_image
  50. , TextureFormat output_format
  51. , bool generate_mips
  52. , bool is_normal_map
  53. )
  54. {
  55. _db = db;
  56. _id = id;
  57. _db.create(_id, "texture");
  58. for (int tp = 0; tp < TargetPlatform.COUNT; ++tp) {
  59. string platform = ((TargetPlatform)tp).to_key();
  60. _db.set_property_string(_id, "source", source_image);
  61. _db.set_property_string(_id, "output." + platform + ".format", output_format.to_key());
  62. _db.set_property_bool(_id, "output." + platform + ".generate_mips", generate_mips);
  63. _db.set_property_bool(_id, "output." + platform + ".normal_map", is_normal_map);
  64. _db.set_property_double(_id, "output." + platform + ".mip_skip_smallest", 0);
  65. }
  66. }
  67. public TextureResource.color_map(Database db, Guid texture_id, string source_image)
  68. {
  69. this(db, texture_id, source_image, TextureFormat.BC1, true, false);
  70. }
  71. public TextureResource.normal_map(Database db, Guid texture_id, string source_image)
  72. {
  73. this(db, texture_id, source_image, TextureFormat.BC5, true, true);
  74. }
  75. public TextureResource.font_atlas(Database db, Guid texture_id, string source_image)
  76. {
  77. this(db, texture_id, source_image, TextureFormat.BC3, false, false);
  78. }
  79. public TextureResource.sprite(Database db, Guid texture_id, string source_image)
  80. {
  81. this(db, texture_id, source_image, TextureFormat.RGBA8, false, false);
  82. }
  83. public int save(Project project, string resource_name)
  84. {
  85. return _db.save(project.absolute_path(resource_name) + ".texture", _id);
  86. }
  87. public static ImportResult import(ProjectStore project_store, string destination_dir, SList<string> filenames)
  88. {
  89. Project project = project_store._project;
  90. foreach (unowned string filename_i in filenames) {
  91. GLib.File file_src = File.new_for_path(filename_i);
  92. GLib.File file_dst = File.new_for_path(Path.build_filename(destination_dir, file_src.get_basename()));
  93. string resource_filename = project.resource_filename(file_dst.get_path());
  94. string resource_path = ResourceId.normalize(resource_filename);
  95. string resource_name = ResourceId.name(resource_path);
  96. try {
  97. file_src.copy(file_dst, FileCopyFlags.OVERWRITE);
  98. } catch (Error e) {
  99. loge(e.message);
  100. return ImportResult.ERROR;
  101. }
  102. Database db = new Database(project);
  103. var texture_resource = TextureResource.color_map(db, Guid.new_guid(), resource_path);
  104. texture_resource.save(project, resource_name);
  105. }
  106. return ImportResult.SUCCESS;
  107. }
  108. }
  109. } /* namespace Crown */