7#include "hellfire/assets/models/ModelImporter.h"
8#include "hellfire/serializers/ModelSerializer.h"
9#include "hellfire/serializers/TextureSerializer.h"
18 std::string lower_name = name;
19 std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower);
21 if (lower_name.find(
"normal") != std::string::npos ||
22 lower_name.find(
"nrm") != std::string::npos ||
23 lower_name.find(
"_n.") != std::string::npos) {
26 if (lower_name.find(
"rough") != std::string::npos) {
29 if (lower_name.find(
"metal") != std::string::npos) {
32 if (lower_name.find(
"ao") != std::string::npos ||
33 lower_name.find(
"occlusion") != std::string::npos) {
36 if (lower_name.find(
"emissive") != std::string::npos ||
37 lower_name.find(
"emission") != std::string::npos) {
40 if (lower_name.find(
"spec") != std::string::npos) {
48 const std::filesystem::path &project_root) :
registry_(registry),
55 std::cout <<
"=== Scanning for assets to import ===" << std::endl;
60 std::cout <<
"=== Import complete ===" << std::endl;
66 std::vector<AssetMetadata> to_import;
68 for (
const auto &meta: models) {
70 std::string ext = meta.filepath.extension().string();
71 std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
73 if (ext ==
".hfmodel" || ext ==
".hfmesh")
continue;
75 if (needs_import(meta.uuid)) {
76 to_import.push_back(meta);
81 std::mutex output_mutex;
82 std::mutex registry_mutex;
87 std::lock_guard lock(output_mutex);
89 std::cout <<
"Imported: " << meta
.name << std::endl;
91 std::cerr <<
"Failed: " << meta
.name << std::endl;
95 std::vector<std::future<
void>> futures;
96 for (
const auto& meta : to_import) {
97 futures.push_back(std::async(std::launch::async, worker, meta));
99 for (
auto& f : futures) f.get();
105 for (
const auto &meta: textures) {
107 auto meta_path = registry_.get_absolute_path(meta.uuid).string() +
".meta";
109 if (!std::filesystem::exists(meta_path)) {
110 std::cout <<
"Creating texture metadata: " << meta.name << std::endl;
113 TextureMetadata tex_meta;
114 tex_meta.type = infer_texture_type(meta.name);
115 tex_meta.generate_mipmaps =
true;
116 tex_meta.srgb = (tex_meta.type == TextureType::DIFFUSE ||
117 tex_meta.type == TextureType::EMISSIVE);
119 TextureSerializer::save_metadata(
120 registry_.get_absolute_path(meta.uuid),
129 if (!meta)
return false;
131 switch (meta->type) {
143 if (!meta)
return false;
147 if (!exists(imported_path)) {
153 auto source_time = last_write_time(source_path);
154 auto imported_time = last_write_time(imported_path);
156 return source_time > imported_time;
162 if (!std::filesystem::exists(source_path)) {
163 std::cerr <<
"Source file not found: " << source_path << std::endl;
169 std::filesystem::create_directories(model_output_dir);
181 std::cerr <<
"Failed to import model: " << meta
.name
187 auto model_path = model_output_dir / (meta
.name +
".hfmodel");
189 std::cerr <<
"Failed to save .hfmodel: " << model_path << std::endl;
196 std::cout <<
" Created: " << model_path.filename() << std::endl;
197 std::cout <<
" Meshes: " << result.created_mesh_assets.size() << std::endl;
198 std::cout <<
" Materials: " << result.created_material_assets.size() << std::endl;
199 std::cout <<
" Textures: " << result.created_texture_assets.size() << std::endl;
206 std::mutex& registry_mutex)
210 if (!std::filesystem::exists(source_path)) {
211 std::cerr <<
"Source file not found: " << source_path << std::endl;
217 std::filesystem::create_directories(model_output_dir);
229 std::cerr <<
"Failed to import model: " << meta
.name
235 auto model_path = model_output_dir / (meta
.name +
".hfmodel");
237 std::cerr <<
"Failed to save .hfmodel: " << model_path << std::endl;
242 std::lock_guard<std::mutex> lock(registry_mutex);
266 const std::string &extension)
const {
std::filesystem::path get_imported_path(const AssetMetadata &meta, const std::string &extension) const
void import_all_pending()
Import all unprocessed assets in registry.
bool import_model(const AssetMetadata &meta)
bool needs_import(AssetID id) const
AssetImportManager(AssetRegistry ®istry, AssetManager &asset_manager, const std::filesystem::path &project_root)
bool import_asset(AssetID id)
void import_all_textures()
std::filesystem::path project_root_
bool has_imported_mesh(AssetID original_id) const
AssetManager & asset_manager_
bool import_model_threaded(const AssetMetadata &meta, std::mutex ®istry_mutex)
bool import_texture(const AssetMetadata &meta)
std::filesystem::path import_output_dir_
AssetRegistry & registry_
std::shared_ptr< Mesh > get_mesh(AssetID id)
Registry for storing assets.
std::filesystem::path get_absolute_path(AssetID uuid)
Converts external model formats (FBX, GLTF, OBJ) into internal assets this runs once during asset imp...
ImportResult import(const std::filesystem::path &source_path, const ImportSettings &settings={})
Serializes the ImportResult to a .hfmodel file.
static bool save(const std::filesystem::path &filepath, const ImportResult &result)
static bool save_metadata(const std::filesystem::path &texture_path, const TextureMetadata &meta)
TextureType infer_texture_type(const std::string &name)
Complete result of importing a model file.
std::string error_message
Metadata for texture assets.