7#include "hellfire/ecs/RenderableComponent.h"
8#include "hellfire/ecs/TransformComponent.h"
9#include "hellfire/ecs/components/MeshComponent.h"
10#include "hellfire/serializers/ModelSerializer.h"
19 auto meta =
registry_.get_asset(model_asset_id);
21 std::cerr <<
"ModelInstantiator: Asset not found: " << model_asset_id << std::endl;
29 const std::filesystem::path& hfmodel_path,
33 std::cerr <<
"ModelInstantiator: Failed to load model: " << hfmodel_path << std::endl;
37 return instantiate(scene, *model_opt, parent);
43 if (!model
.success || model.nodes.empty()) {
44 std::cerr <<
"ModelInstantiator: Invalid model data" << std::endl;
55 const auto& node = model.nodes[node_index];
58 EntityID entity_id = scene.create_entity(node.name);
64 transform->set_position(node.position);
65 transform->set_rotation(node.rotation);
66 transform->set_scale(node.scale);
75 if (node.mesh_indices.size() == 1) {
77 const auto& mesh_ref = model.meshes[node.mesh_indices[0]];
80 else if (node.mesh_indices.size() > 1) {
82 for (size_t mesh_idx : node.mesh_indices) {
83 const auto& mesh_ref = model.meshes[mesh_idx];
85 EntityID mesh_entity_id = scene.create_entity(mesh_ref.name);
86 Entity* mesh_entity = scene.get_entity(mesh_entity_id);
87 scene.set_parent(mesh_entity_id, entity_id);
89 attach_mesh_components(mesh_entity, mesh_ref);
94 for (size_t child_idx : node.child_indices) {
95 instantiate_node(scene, model, child_idx, entity_id);
103 auto* mesh_comp = entity->add_component<MeshComponent>();
107 if (
auto mesh = assets_.get_mesh(mesh_ref.mesh_asset)) {
108 mesh_comp->set_mesh(mesh);
112 auto* renderable = entity->add_component<RenderableComponent>();
117 if (
auto material = assets_.get_material(mesh_ref.material_asset)) {
118 renderable->set_material(material);
static constexpr hellfire::EntityID INVALID_ENTITY
Registry for storing assets.
std::filesystem::path get_absolute_path(AssetID uuid)
Creates scene entities from an ImportResult.
EntityID instantiate(Scene &scene, const ImportResult &model, EntityID parent=INVALID_ENTITY)
AssetRegistry & registry_
ModelInstantiator(AssetManager &assets, AssetRegistry ®istry)
EntityID instantiate(Scene &scene, const std::filesystem::path &hfmodel_path, EntityID parent=INVALID_ENTITY)
EntityID instantiate(Scene &scene, AssetID model_asset_id, EntityID parent=INVALID_ENTITY)
void attach_mesh_components(Entity *entity, const ImportedMesh &mesh_ref)
EntityID instantiate_node(Scene &scene, const ImportResult &model, size_t node_index, EntityID parent)
Serializes the ImportResult to a .hfmodel file.
static std::optional< ImportResult > load(const std::filesystem::path &filepath)
Manages a collection of entities and their hierarchical relationships.
void set_parent(EntityID child_id, EntityID parent_id)
Sets the parent of an entity.
Entity * get_entity(EntityID id)
Retrieves an entity by its ID.
constexpr AssetID INVALID_ASSET_ID
Complete result of importing a model file.
Represents an imported mesh with its material binding.