#include "EditorResource.h" #include "Project.h" #include "Core/AssetExporter.h" #include "Core/GarbageCollection.h" #include "Render/Renderer.h" #include "Render/VulkanImpl/VulkanContext.h" #include "Render/VulkanImpl/VulkanShaderPassBuilder.h" #include "Render/Material.h" #include "Render/RenderTexture.h" #include "Game/Asset/TextureLoader.h" #include "Game/Asset/ShaderLoader.h" #include "Game/Asset/ModelLoader.h" #include "Game/Asset/ShaderAsset.h" #include namespace sh::editor { EditorResource::~EditorResource() { for (game::GUITexture* icon : icons) icon->Destroy(); } SH_EDITOR_API void EditorResource::LoadAllAssets(Project& project) { auto& ctx = *project.renderer.GetContext(); auto& renderer = project.renderer; render::vk::VulkanShaderPassBuilder shaderBuilder{ static_cast(*project.renderer.GetContext()) }; // TODO game::TextureLoader texLoader{ ctx }; game::ModelLoader modelLoader{ *renderer.GetContext() }; game::ShaderLoader shaderLoader{ &shaderBuilder }; auto folderTex = static_cast(project.loadedAssets.emplace_back(static_cast(texLoader.Load("textures/folder.png")))); assert(folderTex != nullptr); auto fileTex = static_cast(project.loadedAssets.emplace_back(static_cast(texLoader.Load("textures/file.png")))); assert(fileTex != nullptr); auto meshTex = static_cast(project.loadedAssets.emplace_back(static_cast(texLoader.Load("textures/meshIcon.png")))); assert(meshTex != nullptr); auto materialTex = static_cast(project.loadedAssets.emplace_back(static_cast(texLoader.Load("textures/MaterialIcon.png")))); assert(materialTex != nullptr); auto imageTex = static_cast(project.loadedAssets.emplace_back(static_cast(texLoader.Load("textures/image.png")))); assert(imageTex != nullptr); auto shaderTex = static_cast(project.loadedAssets.emplace_back(static_cast(texLoader.Load("textures/shaderIcon.png")))); assert(imageTex != nullptr); auto worldTex = static_cast(project.loadedAssets.emplace_back(static_cast(texLoader.Load("textures/worldIcon.png")))); assert(imageTex != nullptr); auto prefabTex = static_cast(project.loadedAssets.emplace_back(static_cast(texLoader.Load("textures/prefabIcon.png")))); assert(imageTex != nullptr); auto& gc = *core::GarbageCollection::GetInstance(); auto folderIcon = icons.emplace_back(core::SObject::Create()); gc.SetRootSet(folderIcon); auto fileIcon = icons.emplace_back(core::SObject::Create()); gc.SetRootSet(fileIcon); auto meshIcon = icons.emplace_back(core::SObject::Create()); gc.SetRootSet(meshIcon); auto materialIcon = icons.emplace_back(core::SObject::Create()); gc.SetRootSet(materialIcon); auto imageIcon = icons.emplace_back(core::SObject::Create()); gc.SetRootSet(imageIcon); auto shaderIcon = icons.emplace_back(core::SObject::Create()); gc.SetRootSet(shaderIcon); auto worldIcon = icons.emplace_back(core::SObject::Create()); gc.SetRootSet(worldIcon); auto prefabIcon = icons.emplace_back(core::SObject::Create()); gc.SetRootSet(prefabIcon); folderIcon->Create(*folderTex); fileIcon->Create(*fileTex); meshIcon->Create(*meshTex); materialIcon->Create(*materialTex); imageIcon->Create(*imageTex); shaderIcon->Create(*shaderTex); worldIcon->Create(*worldTex); prefabIcon->Create(*prefabTex); for (auto icon : icons) icon->SetName("icon"); render::Shader* const lineShader = shaders.insert_or_assign("Line", static_cast(shaderLoader.Load("shaders/line.shader"))).first->second; render::Shader* const errorShader = shaders.insert_or_assign("ErrorShader", static_cast(shaderLoader.Load("shaders/error.shader"))).first->second; render::Shader* const gridShader = shaders.insert_or_assign("GridShader", static_cast(shaderLoader.Load("shaders/grid.shader"))).first->second; render::Shader* const pickingShader = shaders.insert_or_assign("EditorPickingShader", static_cast(shaderLoader.Load("shaders/EditorPicking.shader"))).first->second; render::Shader* const outlineShader = shaders.insert_or_assign("OutlineShader", static_cast(shaderLoader.Load("shaders/outline.shader"))).first->second; render::Shader* const triangleShader = shaders.insert_or_assign("TriangleShader", static_cast(shaderLoader.Load("shaders/triangle.shader"))).first->second; render::Shader* const outlinePreShader = shaders.insert_or_assign("OutlinePreShader", static_cast(shaderLoader.Load("shaders/EditorOutlinePre.shader"))).first->second; render::Shader* const outlinePostShader = shaders.insert_or_assign("OutlinePostShader", static_cast(shaderLoader.Load("shaders/EditorOutlinePost.shader"))).first->second; render::Shader* const uiTextShader = shaders.insert_or_assign("UITextShader", static_cast(shaderLoader.Load("shaders/UIText.shader"))).first->second; render::Shader* const ssaoShader = shaders.insert_or_assign("SSAOShader", static_cast(shaderLoader.Load("shaders/SSAO.shader"))).first->second; ssaoShader->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f24" }); render::Texture* const blackTex = textures.insert_or_assign("BlackTexture", static_cast(texLoader.Load("textures/black.png"))).first->second; blackTex->Build(*renderer.GetContext()); blackTex->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f18" }); render::Material* const errorMat = materials.insert_or_assign("ErrorMaterial", core::SObject::Create(errorShader)).first->second; render::Material* const lineMat = materials.insert_or_assign("LineMaterial", core::SObject::Create(lineShader)).first->second; render::Material* const gridMat = materials.insert_or_assign("GridMaterial", core::SObject::Create(gridShader)).first->second; render::Material* const pickingMat = materials.insert_or_assign("PickingMaterial", core::SObject::Create(pickingShader)).first->second; render::Material* const triMat = materials.insert_or_assign("TriangleMaterial", core::SObject::Create(triangleShader)).first->second; render::Material* const outlinePreMat = materials.insert_or_assign("OutlinePreMaterial", core::SObject::Create(outlinePreShader)).first->second; render::Material* const outlinePostMat = materials.insert_or_assign("OutlinePostMaterial", core::SObject::Create(outlinePostShader)).first->second; render::Material* const uiTextMat = materials.insert_or_assign("UITextMaterial", core::SObject::Create(uiTextShader)).first->second; render::Model* const sphereModel = models.insert_or_assign("SphereModel", static_cast(modelLoader.Load("model/Sphere.obj"))).first->second; sphereModel->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f15" }); sphereModel->GetMeshes()[0]->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f19" }); render::Model* const cubeModel = models.insert_or_assign("CubeModel", static_cast(modelLoader.Load("model/cube.obj"))).first->second; cubeModel->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f16" }); cubeModel->GetMeshes()[0]->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f20" }); render::Model* const planeModel = models.insert_or_assign("PlaneModel", static_cast(modelLoader.Load("model/Plane.glb"))).first->second; planeModel->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f17" }); planeModel->GetMeshes()[0]->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f21" }); render::RenderTexture* outlineTexture = core::SObject::Create(render::TextureFormat::R8, render::TextureFormat::D24S8, false); outlineTexture->SetSize(1024, 1024); outlineTexture->Build(*renderer.GetContext()); textures.insert_or_assign("OutlineTexture", outlineTexture); errorShader->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f0f" }); uiTextShader->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f22" }); errorMat->Build(*renderer.GetContext()); errorMat->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f10" }); lineMat->SetName("Line"); lineMat->Build(*renderer.GetContext()); lineMat->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f11" }); pickingMat->SetName("PickingMaterial"); pickingMat->Build(*renderer.GetContext()); pickingMat->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f12" }); gridMat->SetProperty("color", glm::vec4{ 0.6f, 0.6f, 0.8f, 0.2f }); gridMat->Build(*renderer.GetContext()); triMat->SetProperty("color", glm::vec3{ 0.f, 1.f, 0.f }); triMat->Build(*renderer.GetContext()); outlinePreMat->Build(*renderer.GetContext()); outlinePreMat->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f13" }); outlinePostMat->SetProperty("outlineWidth", 1.0f); outlinePostMat->SetProperty("outlineColor", glm::vec4{ 41 / 255.0f, 74 / 255.0f, 122 / 255.0f, 1.0f }); outlinePostMat->SetProperty("tex", outlineTexture); outlinePostMat->Build(*renderer.GetContext()); outlinePostMat->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f14" }); uiTextMat->SetName("UITextMaterial"); uiTextMat->SetUUID(core::UUID{ "bbc4ef7ec45dce223297a224f8093f23" }); uiTextMat->Build(*renderer.GetContext()); uiTextMat->SetProperty("color", glm::vec3{ 1.f, 1.f, 1.f }); } SH_EDITOR_API auto EditorResource::GetIcon(Icon icon) -> game::GUITexture* { switch (icon) { case Icon::Folder: return icons[0]; case Icon::File: return icons[1]; case Icon::Mesh: return icons[2]; case Icon::Material: return icons[3]; case Icon::Image: return icons[4]; case Icon::Shader: return icons[5]; case Icon::World: return icons[6]; case Icon::Prefab: return icons[7]; default: return nullptr; } } SH_EDITOR_API auto EditorResource::GetShader(const std::string& name) -> render::Shader* { auto it = shaders.find(name); if (it == shaders.end()) return nullptr; return it->second; } SH_EDITOR_API auto EditorResource::GetMaterial(const std::string& name) -> render::Material* { auto it = materials.find(name); if (it == materials.end()) return nullptr; return it->second; } SH_EDITOR_API auto EditorResource::GetTexture(const std::string& name) -> render::Texture* { auto it = textures.find(name); if (it == textures.end()) return nullptr; return it->second; } SH_EDITOR_API auto EditorResource::GetModel(const std::string& name) -> render::Model* { auto it = models.find(name); if (it == models.end()) return nullptr; return it->second; } SH_EDITOR_API void EditorResource::ExtractAllAssetToLibrary(Project& project) { for (auto& [name, shaderPtr] : shaders) { if (!core::IsValid(shaderPtr)) continue; game::ShaderAsset asset{ *shaderPtr }; auto writeTime = std::filesystem::file_time_type::clock::now(); asset.SetWriteTime(writeTime.time_since_epoch().count()); const auto path = project.GetLibraryPath() / (shaderPtr->GetUUID().ToString() + ".asset"); if (core::AssetExporter::Save(asset, path, true)) { std::filesystem::last_write_time(path, writeTime); } } } }//namespace