From fef75ec64b0077ad308e9925f4ab9ac632106a87 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Mon, 8 May 2023 22:08:08 +0200 Subject: [PATCH] Started Working on an assetsystem for loading more complex models --- Editor/src/AssetManagement/Asset.h | 45 +++++++++-- Editor/src/AssetManagement/AssetFinder.h | 74 +++++++++++++------ .../AssetLoaders/ModelLoader.cpp | 9 ++- .../AssetLoaders/ModelLoader.h | 4 +- .../{AssetManager.cpp => AssetRegistry.cpp} | 22 +++--- .../{AssetManager.h => AssetRegistry.h} | 17 +++-- Editor/src/MainMenuBar.cpp | 10 +-- YoggieEngine/src/Scene/Components.h | 2 + 8 files changed, 124 insertions(+), 59 deletions(-) rename Editor/src/AssetManagement/{AssetManager.cpp => AssetRegistry.cpp} (83%) rename Editor/src/AssetManagement/{AssetManager.h => AssetRegistry.h} (66%) diff --git a/Editor/src/AssetManagement/Asset.h b/Editor/src/AssetManagement/Asset.h index 53ede22..b8c66f3 100644 --- a/Editor/src/AssetManagement/Asset.h +++ b/Editor/src/AssetManagement/Asset.h @@ -8,18 +8,49 @@ enum class ASSET_TYPE { Unknown = -1, Mesh, Texture, - Material + Material, + Shader, + Model, + File }; class Asset { public: Asset(const char* name): name(name) {} - uuid::v4::UUID id = uuid::v4::UUID::New(); - virtual ASSET_TYPE GetType() { return ASSET_TYPE::Unknown; } - const char* GetName() const { return name.c_str(); } - bool isFolder = false ; -protected: - std::string name; + virtual ASSET_TYPE GetType() { return detectAssetType(); } + const char* GetName() const { return name.c_str(); } + void setName(std::string& name) { name = name.c_str(); } + void setFilPath(std::string& path) { file = std::filesystem::path(path); } + + std::string getId() { return Id.String(); } + + +protected: + uuid::v4::UUID Id = uuid::v4::UUID::New(); + + std::string name; + std::filesystem::path file; + + + + ASSET_TYPE detectAssetType() { + auto ext = (file.extension()).string(); + if (ext == ".obj" || ext == ".gltf" || ext == ".fbx" || ext == ".stl") { + return ASSET_TYPE::Model; + } + else if (ext == ".yproj") { + return ASSET_TYPE::File; + } + else if (ext == ".vs" || ext == ".fs") { + return ASSET_TYPE::File; + } + else { + std::cout << "unknown file!" << std::endl; + return ASSET_TYPE::Unknown; + } + } + + }; \ No newline at end of file diff --git a/Editor/src/AssetManagement/AssetFinder.h b/Editor/src/AssetManagement/AssetFinder.h index 7e4684a..cc114bb 100644 --- a/Editor/src/AssetManagement/AssetFinder.h +++ b/Editor/src/AssetManagement/AssetFinder.h @@ -1,16 +1,51 @@ #pragma once #include "../../YoggieEngine/src/YoggieEngine.h" #include "EditorWindow.h" -#include "AssetManagement/AssetManager.h" +#include "AssetManagement/AssetRegistry.h" -class AssetFinder : EditorWindow { +const char* hidden_extensions [] { + ".exe", + ".pdb", + ".idb", + ".dll", + ".ini" +}; + +class AssetFinder : public EditorWindow { public: - AssetFinder() : EditorWindow("Assets"), - folderIcon ("rsc/FolderIcon.png"), - assetIcon ("rsc/AssetIcon.png") - {} + AssetFinder () : EditorWindow("Assets") {} + AssetFinder(const std::filesystem::path& projectdirectory) : EditorWindow("Assets") + { + for (auto& dir_entry : std::filesystem::directory_iterator(projectdirectory)) { + auto filepath = dir_entry.path(); + + if (dir_entry.is_directory() || dir_entry.is_symlink() || dir_entry.is_socket()) + continue; + + bool has_hidden_extension = false; + for (auto hide : hidden_extensions) { + if (filepath.extension() == hide) + { + has_hidden_extension = true; + break; + } + } + + if (has_hidden_extension) + continue; + + Asset asset(filepath.filename().string().c_str()); + asset.setFilPath(filepath.string()); + spdlog::info("Created asset: {0}", asset.GetName()); + files.push_back(asset); + + } + + } void Draw() override { + + //assetIcon = YoggieEngine::Texture("rsc/AssetIcon.png"); ImGui::DragInt("IconSize", &iconSize, 1, 30, 90); ImGui::DragInt("Maximum Columns", &maxColumns, 1, 1, 6); if (ImGui::BeginTable("##resources", 3)) @@ -20,10 +55,10 @@ public: ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f)); - /* + int row = 0; int column = 0; - for (auto& asset : AssetManager::assets) { + for (auto& asset : files ) { if (column % 3 == 0) { ImGui::TableNextRow(); column = 0; @@ -32,27 +67,18 @@ public: ImGui::TableSetColumnIndex(column); - if (asset.isFolder) { - ImGui::ImageButton( - (ImTextureID)folderIcon.GetID(), - ImVec2{ (float)iconSize,(float)iconSize }); - ImGui::Text(asset.GetName(), row); + ImGui::ImageButton( + (ImTextureID)assetIcon.GetID(), + ImVec2{ (float)iconSize, (float)iconSize }); + ImGui::Text(asset.GetName(), row); - } - else { - ImGui::ImageButton( - (ImTextureID)assetIcon.GetID(), - ImVec2{ (float)iconSize, (float)iconSize }); - ImGui::Text(asset.GetName(), row); - - } column++; } - */ - + + ImGui::PopStyleColor(3); ImGui::EndTable(); const GLuint textures[2]{ assetIcon.GetID(), folderIcon.GetID() }; @@ -62,6 +88,8 @@ public: } private: + std::vector files = std::vector(); + int iconSize = 60; int maxColumns = 3; diff --git a/Editor/src/AssetManagement/AssetLoaders/ModelLoader.cpp b/Editor/src/AssetManagement/AssetLoaders/ModelLoader.cpp index a4dad6e..112b37b 100644 --- a/Editor/src/AssetManagement/AssetLoaders/ModelLoader.cpp +++ b/Editor/src/AssetManagement/AssetLoaders/ModelLoader.cpp @@ -85,16 +85,23 @@ std::vector processNode(aiNode* node, const aiScene* scene) Asset ModelLoader::LoadAsset(std::filesystem::path& path) { + + Assimp::Importer importer; const aiScene* scene = importer.ReadFile(path.string(), aiProcess_Triangulate | aiProcess_FlipUVs); aiNode* currentNode = scene->mRootNode; - spdlog::debug("Loading meshes!"); + + std::cout << "Loading meshes!" << std::endl; auto meshes = processNode(currentNode, scene); + std::cout << "Model file contained " << meshes.size() << " meshes" << std::endl; + + + return Asset("Mesh"); diff --git a/Editor/src/AssetManagement/AssetLoaders/ModelLoader.h b/Editor/src/AssetManagement/AssetLoaders/ModelLoader.h index e9db61f..58b0fd5 100644 --- a/Editor/src/AssetManagement/AssetLoaders/ModelLoader.h +++ b/Editor/src/AssetManagement/AssetLoaders/ModelLoader.h @@ -2,9 +2,7 @@ #include "AssetLoader.h" class ModelLoader : AssetLoader { - +public: Asset LoadAsset(std::filesystem::path& path); - - }; \ No newline at end of file diff --git a/Editor/src/AssetManagement/AssetManager.cpp b/Editor/src/AssetManagement/AssetRegistry.cpp similarity index 83% rename from Editor/src/AssetManagement/AssetManager.cpp rename to Editor/src/AssetManagement/AssetRegistry.cpp index 904d146..da457b6 100644 --- a/Editor/src/AssetManagement/AssetManager.cpp +++ b/Editor/src/AssetManagement/AssetRegistry.cpp @@ -1,35 +1,31 @@ -#include "AssetManager.h" +#include "AssetRegistry.h" #include #include #include -#include "AssetLoaders/ModelLoader.h" /* * this is still a very naive approach to the asset manager */ -void AssetManager::Init() +AssetRegistry::AssetRegistry() { -// Assets = std::map(); - modelLoader = ModelLoader(); - } -void AssetManager::RegisterAsset(Asset& asset) + +void AssetRegistry::RegisterAsset(Asset& asset) { -// Assets.try_emplace(asset.id, asset); + Assets.try_emplace(asset.getId() , asset); } -void AssetManager::UnregisterAsset(Asset& asset) { -// auto old = Assets[asset.id]; -// Assets[asset.id] = NULL; +void AssetRegistry::UnregisterAsset(Asset& asset) { + Assets.erase(asset.getId()); } -YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath) +YoggieEngine::Mesh* AssetRegistry::LoadFromAssetFile(const std::filesystem::path assetPath) { YoggieEngine::Mesh* imported = nullptr; @@ -89,7 +85,7 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path } -YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder) +YoggieEngine::Renderable* AssetRegistry::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder) { /* diff --git a/Editor/src/AssetManagement/AssetManager.h b/Editor/src/AssetManagement/AssetRegistry.h similarity index 66% rename from Editor/src/AssetManagement/AssetManager.h rename to Editor/src/AssetManagement/AssetRegistry.h index 0fda5d1..9bc3565 100644 --- a/Editor/src/AssetManagement/AssetManager.h +++ b/Editor/src/AssetManagement/AssetRegistry.h @@ -1,23 +1,26 @@ #pragma once #include #include "Asset.h" -#include "AssetLoaders/ModelLoader.h" #include "uuid.h" -class AssetManager { +class AssetRegistry { public: - void Init(); - + AssetRegistry(); + //AssetRegistry(AssetPack); + void RegisterAsset(Asset& asset); void UnregisterAsset(Asset& asset); + + void Update(); + + static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath); static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder); private: - std::map Assets; - ModelLoader modelLoader; - + int unique_number = 0; + std::map Assets = std::map (); }; \ No newline at end of file diff --git a/Editor/src/MainMenuBar.cpp b/Editor/src/MainMenuBar.cpp index cee558f..d78b539 100644 --- a/Editor/src/MainMenuBar.cpp +++ b/Editor/src/MainMenuBar.cpp @@ -1,6 +1,6 @@ #include "MainMenuBar.h" #include -#include "AssetManagement/AssetManager.h" +#include "AssetManagement/AssetRegistry.h" @@ -19,8 +19,8 @@ void MainMenuBar::ApplicationMenu(Project& project) { switch (result) { case(NFD_OKAY): Project::LoadProject(path, project); - //AssetManager::setAssetPath(project.GetProjectDirectory()); - //AssetManager::BuildAssetView(); + //AssetRegistry::setAssetPath(project.GetProjectDirectory()); + //AssetRegistry::BuildAssetView(); break; case(NFD_CANCEL): break; @@ -116,7 +116,7 @@ void MainMenuBar::SceneMenu(Project& project, YoggieEngine::Scene& scene) { case(NFD_OKAY): // Import Model - AssetManager::LoadFromSource( + AssetRegistry::LoadFromSource( path, "build/Debug/Assets"//project.get()->GetProjectDirectory() / "Assets" ); @@ -136,7 +136,7 @@ void MainMenuBar::SceneMenu(Project& project, YoggieEngine::Scene& scene) { switch (result) { case(NFD_OKAY): { - YoggieEngine::Mesh* importedMesh = AssetManager::LoadFromAssetFile(path); + YoggieEngine::Mesh* importedMesh = AssetRegistry::LoadFromAssetFile(path); if (importedMesh != nullptr) { auto full_name = std::filesystem::path(path); diff --git a/YoggieEngine/src/Scene/Components.h b/YoggieEngine/src/Scene/Components.h index 544b39a..6a3ac9d 100644 --- a/YoggieEngine/src/Scene/Components.h +++ b/YoggieEngine/src/Scene/Components.h @@ -65,6 +65,8 @@ namespace YoggieEngine { struct Render3DComponent { + Renderable* renderable; + unsigned int VAO = 0; unsigned int IBO = 0; Mesh mesh;