Minor - ECS use for import object - V12.1.0

This commit is contained in:
2025-06-24 15:36:45 +02:00
parent 039b034175
commit 3adfddf44f
5 changed files with 116 additions and 89 deletions

View File

@@ -38,6 +38,7 @@
#include "ecs/components/physics_component.h"
#include "ecs/components/shader_component.h"
#include "ecs/systems/render_system.h"
#include "ecs/components/model_path_component.h"
#include <fstream>
#include <WICTextureLoader.h>

View File

@@ -0,0 +1,24 @@
#pragma once
#include "../component.h"
#include <string>
namespace ecs {
class ModelPathComponent : public Component {
public:
ModelPathComponent() = default;
explicit ModelPathComponent(const std::wstring& path) : m_path(path) {}
~ModelPathComponent() = default;
void Initialize() override {}
void Update(float deltaTime) override {}
// Getters et setters
const std::wstring& GetPath() const { return m_path; }
void SetPath(const std::wstring& path) { m_path = path; }
private:
std::wstring m_path;
};
} // namespace ecs

View File

@@ -1368,9 +1368,8 @@ void application_class::add_kobject(std::wstring& filepath)
{
std::lock_guard<std::mutex> lock(objects_mutex_);
Logger::Get().Log("Adding object", __FILE__, __LINE__);
char modelFilename[128];
vector<string> Filename;
TextureContainer KobjectsTextures;
filesystem::path p(filepath);
string filename = p.stem().string();
@@ -1386,27 +1385,69 @@ void application_class::add_kobject(std::wstring& filepath)
L"assets/Texture/BricksNRM2K.png",
L"assets/Texture/BricksGLOSS2K.png"
};
// Configurer les chemins des textures dans le conteneur
KobjectsTextures.diffusePaths.push_back(kobjTexture[0]);
if (kobjTexture.size() > 1) KobjectsTextures.normalPaths.push_back(kobjTexture[1]);
if (kobjTexture.size() > 2) KobjectsTextures.specularPaths.push_back(kobjTexture[2]);
object* newObject = new object(*this);
newObject->LoadTexturesFromPath(kobjTexture,KobjectsTextures, direct_3d_); // Load textures_ from the path
newObject->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, KobjectsTextures);
newObject->SetMass(1.0f);
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 50.0f, 0.0f));
newObject->SetName(filename);
newObject->SetId(object_id_);
newObject->SetModelPath(filepath); // Store the path as std::wstring
// V<>rifier si le mod<6F>le existe d<>j<EFBFBD> dans le cache
std::string modelKey = std::string(modelFilename);
std::shared_ptr<model_class> sharedModel;
auto it = g_model_cache.find(modelKey);
if (it != g_model_cache.end()) {
// Utiliser le mod<6F>le existant du cache
Logger::Get().Log("Using cached model for " + modelKey, __FILE__, __LINE__);
sharedModel = it->second;
}
else {
// Cr<43>er un nouveau mod<6F>le
sharedModel = std::make_shared<model_class>();
// Pr<50>charger les textures
sharedModel->PreloadTextures(direct_3d_->get_device(), direct_3d_->get_device_context(), KobjectsTextures);
// Initialiser le mod<6F>le
if (!sharedModel->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, KobjectsTextures)) {
Logger::Get().Log("Failed to initialize model for object: " + modelKey, __FILE__, __LINE__, Logger::LogLevel::Error);
return;
}
// Ajouter le mod<6F>le au cache
g_model_cache[modelKey] = sharedModel;
Logger::Get().Log("Added model to cache: " + modelKey, __FILE__, __LINE__);
}
// Cr<43>er une nouvelle entit<69>
auto entity = entity_manager_->CreateEntity();
// Ajouter un composant d'identit<69>
auto identity = entity->AddComponent<ecs::IdentityComponent>(object_id_++);
identity->SetName(filename);
identity->SetType(ecs::ObjectType::Unknown);
// Ajouter un composant de transformation
auto transform = entity->AddComponent<ecs::TransformComponent>();
transform->SetPosition(XMVectorSet(0.0f, 50.0f, 0.0f, 0.0f));
transform->SetScale(XMVectorSet(1.0f, 1.0f, 1.0f, 0.0f));
transform->UpdateWorldMatrix();
// Ajouter un composant de rendu avec le mod<6F>le partag<61>
auto render = entity->AddComponent<ecs::RenderComponent>();
render->InitializeWithModel(sharedModel);
// Ajouter un composant de shader
auto shader = entity->AddComponent<ecs::ShaderComponent>();
shader->SetActiveShader(ecs::ShaderType::LIGHTING);
// Stocker le chemin du mod<6F>le
auto modelPath = entity->AddComponent<ecs::ModelPathComponent>();
modelPath->SetPath(filepath);
object_id_++;
object_.push_back(newObject);
Logger::Get().Log("ECS entity created with ID: " + std::to_string(identity->GetId()), __FILE__, __LINE__);
update_stats_after_modification();
// V<>rifiez que l'objet a bien re<72>u les textures_
if (newObject->get_model()->GetTexture(TextureType::Diffuse,0) == nullptr)
{
Logger::Get().Log("object texture is null after initialization", __FILE__, __LINE__, Logger::LogLevel::Error);
}
}
void application_class::add_cube()