Added prefab thumbnails in filebrowser + fix filebrowser crash

This commit is contained in:
Antoine Pilote
2024-07-11 04:51:13 -04:00
parent f36a4f1ba2
commit cc87da1001
6 changed files with 139 additions and 7 deletions

3
.gitmodules vendored
View File

@@ -25,3 +25,6 @@
[submodule "Nuake/dependencies/Coral"]
path = Nuake/dependencies/Coral
url = https://github.com/antopilo/Coral.git
[submodule "Nuake/dependencies/recastnavigation"]
path = Nuake/dependencies/recastnavigation
url = https://github.com/antopilo/recastnavigation.git

View File

@@ -69,13 +69,26 @@ Ref<Nuake::Texture> ThumbnailManager::GenerateThumbnail(const std::string& path,
Matrix4 view = Matrix4(1.0f);
view = glm::lookAt(Vector3(1, -1.0, 0), Vector3(0, 0, 0), Vector3(0, 1, 0));;
auto file = FileSystem::GetFile(path);
if (file->GetFileType() == FileType::Prefab)
{
Ref<Scene> scene = Scene::New();
auto cam = scene->CreateEntity("Camera");
cam.AddComponent<CameraComponent>();
TransformComponent& camTransform = cam.GetComponent<TransformComponent>();
camTransform.SetLocalPosition({ 0.0f, 0.0f, 2.0f });
auto& previewLight = scene->CreateEntity("_directionalLight").AddComponent<LightComponent>();
previewLight.SetCastShadows(false);
previewLight.Type = LightType::Directional;
scene->GetEnvironment()->CurrentSkyType = SkyType::ProceduralSky;
scene->GetEnvironment()->ProceduralSkybox->SunDirection = { 0.58f, 0.34f, -0.74f };
auto& camComponent = cam.AddComponent<CameraComponent>();
camComponent.CameraInstance->Fov = 45.0f;
camComponent.CameraInstance->AspectRatio = 1.0f;
m_ShadedFramebuffer->SetTexture(texture);
Ref<Prefab> prefab = Prefab::InstanceInScene(path, scene);
scene->Update(0.01f);
scene->Draw(*m_ShadedFramebuffer.get());

View File

@@ -369,10 +369,10 @@ namespace Nuake
ImU32 rectColor = IM_COL32(255, 255, 255, 16);
ImGui::GetWindowDrawList()->AddRectFilled(prevScreenPos + ImVec2(0, 100) - startOffset, prevScreenPos + ImVec2(100, 150) + offsetEnd, rectColor, 1.0f);
std::string visibleName = file->GetName();
const uint32_t MAX_CHAR_NAME = 35;
const uint32_t MAX_CHAR_NAME = 32;
if (file->GetName().size() >= MAX_CHAR_NAME)
{
visibleName = std::string(file->GetName().begin(), file->GetName().begin() + MAX_CHAR_NAME - 3) + "...";
visibleName = std::string(visibleName.begin(), visibleName.begin() + MAX_CHAR_NAME - 3) + "...";
}
ImGui::TextWrapped(visibleName.c_str());
@@ -412,11 +412,11 @@ namespace Nuake
// ImGui::PopStyleVar();
// }
//
if (Editor->Selection.File == file)
if (Editor->Selection.File == file && Editor->Selection.File->GetFileType() != FileType::Prefab)
{
ThumbnailManager::Get().MarkThumbnailAsDirty(file->GetRelativePath());
}
//
// if(pressed)
// {
// Editor->Selection = EditorSelection(file);

View File

@@ -34,6 +34,24 @@ namespace Nuake
return newPrefab;
}
Ref<Prefab> Prefab::InstanceInScene(const std::string& path, Ref<Scene> scene)
{
Ref<Prefab> newPrefab = CreateRef<Prefab>();
newPrefab->Path = path;
if (FileSystem::FileExists(path, false))
{
std::string prefabTextContent = FileSystem::ReadFile(path);
if (!prefabTextContent.empty())
{
newPrefab->DeserializeIntoScene(json::parse(prefabTextContent), scene);
}
}
return newPrefab;
}
void Prefab::EntityWalker(Entity entity)
{
Root = entity;

View File

@@ -22,6 +22,7 @@ namespace Nuake {
static Ref<Prefab> CreatePrefabFromEntity(Entity entity);
static Ref<Prefab> New(const std::string& path);
static Ref<Prefab> InstanceInScene(const std::string& path, Ref<Scene> targetScene);
Prefab()
{
@@ -70,6 +71,102 @@ namespace Nuake {
END_SERIALIZE();
}
bool DeserializeIntoScene(const json& j, Ref<Scene> scene)
{
if (j == "")
return false;
Path = j["Path"];
if (j.contains("DisplayName"))
{
DisplayName = j["DisplayName"];
}
if (j.contains("Description"))
{
Description = j["Description"];
}
if (j.contains("Entities"))
{
std::map<uint32_t, uint32_t> newIdsLut;
for (json e : j["Entities"])
{
Entity entity = { scene->m_Registry.create(), scene.get() };
entity.Deserialize(e); // Id gets overriden by serialized id.
auto& nameComponent = entity.GetComponent<NameComponent>();
bool isRoot = false;
if (nameComponent.ID == j["Root"])
{
isRoot = true; // We found the root entity of the prefab.
auto& parentComponent = entity.GetComponent<ParentComponent>();
parentComponent.HasParent = false;
parentComponent.ParentID = 0;
entity.GetComponent<TransformComponent>().Translation = { 0, 0, 0 };
}
uint32_t oldId = nameComponent.ID;
uint32_t newId = OS::GetTime();
nameComponent.Name = nameComponent.Name;
nameComponent.ID = newId;
newIdsLut[oldId] = newId;
if (isRoot)
{
Root = entity;
}
AddEntity(entity);
}
// Set reference to the parent entity to children
for (auto& e : Entities)
{
if (e.GetID() == Root.GetID())
{
continue;
}
auto& parentC = e.GetComponent<ParentComponent>();
auto parent = scene->GetEntityByID(newIdsLut[parentC.ParentID]);
parent.AddChild(e);
}
// Since the bones point to an entity, and we are instancing a prefab, the new skeleton is gonna be pointing to the wrong
// bones, we need to remap the skeleton to the new entities. We are simply reussing the same map we are using for the
// reparenting. Pretty neat.
std::function<void(SkeletonNode&)> recursiveBoneRemapping = [&recursiveBoneRemapping, &newIdsLut](SkeletonNode& currentBone)
{
currentBone.EntityHandle = newIdsLut[currentBone.EntityHandle];
for (SkeletonNode& bone : currentBone.Children)
{
recursiveBoneRemapping(bone);
}
};
// Do the remapping of the skeleton
for (auto& e : Entities)
{
if (e.GetID() == Root.GetID() || !e.HasComponent<SkinnedModelComponent>())
{
continue;
}
auto& skinnedModelComponent = e.GetComponent<SkinnedModelComponent>();
if (!skinnedModelComponent.ModelResource)
{
continue;
}
SkeletonNode& currentBone = skinnedModelComponent.ModelResource->GetSkeletonRootNode();
recursiveBoneRemapping(currentBone);
}
}
return true;
}
bool Deserialize(const json& j)
{
if (j == "")
@@ -89,7 +186,7 @@ namespace Nuake {
if (j.contains("Entities"))
{
const auto& scene = Engine::GetCurrentScene();
auto scene = Engine::GetCurrentScene();
std::map<uint32_t, uint32_t> newIdsLut;
for (json e : j["Entities"])