Started Working on an assetsystem for loading more complex models

main
Nigel Barink 2023-05-08 22:08:08 +02:00
parent 3c38e2a988
commit fef75ec64b
8 changed files with 124 additions and 59 deletions

View File

@ -8,18 +8,49 @@ enum class ASSET_TYPE {
Unknown = -1, Unknown = -1,
Mesh, Mesh,
Texture, Texture,
Material Material,
Shader,
Model,
File
}; };
class Asset { class Asset {
public: public:
Asset(const char* name): name(name) {} Asset(const char* name): name(name) {}
uuid::v4::UUID id = uuid::v4::UUID::New();
virtual ASSET_TYPE GetType() { return ASSET_TYPE::Unknown; } virtual ASSET_TYPE GetType() { return detectAssetType(); }
const char* GetName() const { return name.c_str(); }
bool isFolder = false ;
protected:
std::string name;
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;
}
}
}; };

View File

@ -1,16 +1,51 @@
#pragma once #pragma once
#include "../../YoggieEngine/src/YoggieEngine.h" #include "../../YoggieEngine/src/YoggieEngine.h"
#include "EditorWindow.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: public:
AssetFinder() : EditorWindow("Assets"), AssetFinder () : EditorWindow("Assets") {}
folderIcon ("rsc/FolderIcon.png"), AssetFinder(const std::filesystem::path& projectdirectory) : EditorWindow("Assets")
assetIcon ("rsc/AssetIcon.png") {
{} 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 { void Draw() override {
//assetIcon = YoggieEngine::Texture("rsc/AssetIcon.png");
ImGui::DragInt("IconSize", &iconSize, 1, 30, 90); ImGui::DragInt("IconSize", &iconSize, 1, 30, 90);
ImGui::DragInt("Maximum Columns", &maxColumns, 1, 1, 6); ImGui::DragInt("Maximum Columns", &maxColumns, 1, 1, 6);
if (ImGui::BeginTable("##resources", 3)) if (ImGui::BeginTable("##resources", 3))
@ -20,10 +55,10 @@ public:
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f)); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f));
/*
int row = 0; int row = 0;
int column = 0; int column = 0;
for (auto& asset : AssetManager::assets) { for (auto& asset : files ) {
if (column % 3 == 0) { if (column % 3 == 0) {
ImGui::TableNextRow(); ImGui::TableNextRow();
column = 0; column = 0;
@ -32,27 +67,18 @@ public:
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
if (asset.isFolder) { ImGui::ImageButton(
ImGui::ImageButton( (ImTextureID)assetIcon.GetID(),
(ImTextureID)folderIcon.GetID(), ImVec2{ (float)iconSize, (float)iconSize });
ImVec2{ (float)iconSize,(float)iconSize }); ImGui::Text(asset.GetName(), row);
ImGui::Text(asset.GetName(), row);
}
else {
ImGui::ImageButton(
(ImTextureID)assetIcon.GetID(),
ImVec2{ (float)iconSize, (float)iconSize });
ImGui::Text(asset.GetName(), row);
}
column++; column++;
} }
*/
ImGui::PopStyleColor(3); ImGui::PopStyleColor(3);
ImGui::EndTable(); ImGui::EndTable();
const GLuint textures[2]{ assetIcon.GetID(), folderIcon.GetID() }; const GLuint textures[2]{ assetIcon.GetID(), folderIcon.GetID() };
@ -62,6 +88,8 @@ public:
} }
private: private:
std::vector <Asset> files = std::vector<Asset>();
int iconSize = 60; int iconSize = 60;
int maxColumns = 3; int maxColumns = 3;

View File

@ -85,16 +85,23 @@ std::vector<YoggieEngine::Mesh> processNode(aiNode* node, const aiScene* scene)
Asset ModelLoader::LoadAsset(std::filesystem::path& path) { Asset ModelLoader::LoadAsset(std::filesystem::path& path) {
Assimp::Importer importer; Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path.string(), aiProcess_Triangulate | aiProcess_FlipUVs); const aiScene* scene = importer.ReadFile(path.string(), aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode; aiNode* currentNode = scene->mRootNode;
spdlog::debug("Loading meshes!");
std::cout << "Loading meshes!" << std::endl;
auto meshes = processNode(currentNode, scene); auto meshes = processNode(currentNode, scene);
std::cout << "Model file contained " << meshes.size() << " meshes" << std::endl;
return Asset("Mesh"); return Asset("Mesh");

View File

@ -2,9 +2,7 @@
#include "AssetLoader.h" #include "AssetLoader.h"
class ModelLoader : AssetLoader { class ModelLoader : AssetLoader {
public:
Asset LoadAsset(std::filesystem::path& path); Asset LoadAsset(std::filesystem::path& path);
}; };

View File

@ -1,35 +1,31 @@
#include "AssetManager.h" #include "AssetRegistry.h"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include "AssetLoaders/ModelLoader.h"
/* /*
* this is still a very naive approach to the asset manager * this is still a very naive approach to the asset manager
*/ */
void AssetManager::Init() AssetRegistry::AssetRegistry()
{ {
// Assets = std::map<uuid::v4::UUID&, Asset>();
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) { void AssetRegistry::UnregisterAsset(Asset& asset) {
// auto old = Assets[asset.id]; Assets.erase(asset.getId());
// Assets[asset.id] = NULL;
} }
YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath) YoggieEngine::Mesh* AssetRegistry::LoadFromAssetFile(const std::filesystem::path assetPath)
{ {
YoggieEngine::Mesh* imported = nullptr; 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)
{ {
/* /*

View File

@ -1,23 +1,26 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include "Asset.h" #include "Asset.h"
#include "AssetLoaders/ModelLoader.h"
#include "uuid.h" #include "uuid.h"
class AssetManager { class AssetRegistry {
public: public:
void Init(); AssetRegistry();
//AssetRegistry(AssetPack);
void RegisterAsset(Asset& asset); void RegisterAsset(Asset& asset);
void UnregisterAsset(Asset& asset); void UnregisterAsset(Asset& asset);
void Update();
static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath); static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath);
static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder); static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder);
private: private:
std::map<uuid::v4::UUID , Asset> Assets; int unique_number = 0;
ModelLoader modelLoader; std::map<std::string , Asset> Assets = std::map<std::string, Asset> ();
}; };

View File

@ -1,6 +1,6 @@
#include "MainMenuBar.h" #include "MainMenuBar.h"
#include <nfd.h> #include <nfd.h>
#include "AssetManagement/AssetManager.h" #include "AssetManagement/AssetRegistry.h"
@ -19,8 +19,8 @@ void MainMenuBar::ApplicationMenu(Project& project) {
switch (result) { switch (result) {
case(NFD_OKAY): case(NFD_OKAY):
Project::LoadProject(path, project); Project::LoadProject(path, project);
//AssetManager::setAssetPath(project.GetProjectDirectory()); //AssetRegistry::setAssetPath(project.GetProjectDirectory());
//AssetManager::BuildAssetView(); //AssetRegistry::BuildAssetView();
break; break;
case(NFD_CANCEL): case(NFD_CANCEL):
break; break;
@ -116,7 +116,7 @@ void MainMenuBar::SceneMenu(Project& project, YoggieEngine::Scene& scene) {
case(NFD_OKAY): case(NFD_OKAY):
// Import Model // Import Model
AssetManager::LoadFromSource( AssetRegistry::LoadFromSource(
path, path,
"build/Debug/Assets"//project.get()->GetProjectDirectory() / "Assets" "build/Debug/Assets"//project.get()->GetProjectDirectory() / "Assets"
); );
@ -136,7 +136,7 @@ void MainMenuBar::SceneMenu(Project& project, YoggieEngine::Scene& scene) {
switch (result) { switch (result) {
case(NFD_OKAY): case(NFD_OKAY):
{ {
YoggieEngine::Mesh* importedMesh = AssetManager::LoadFromAssetFile(path); YoggieEngine::Mesh* importedMesh = AssetRegistry::LoadFromAssetFile(path);
if (importedMesh != nullptr) if (importedMesh != nullptr)
{ {
auto full_name = std::filesystem::path(path); auto full_name = std::filesystem::path(path);

View File

@ -65,6 +65,8 @@ namespace YoggieEngine {
struct Render3DComponent { struct Render3DComponent {
Renderable* renderable;
unsigned int VAO = 0; unsigned int VAO = 0;
unsigned int IBO = 0; unsigned int IBO = 0;
Mesh mesh; Mesh mesh;