Skip to content

Commit

Permalink
WIP: Prefab instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
aantropov committed Apr 16, 2024
1 parent 1ffabae commit 33fbe76
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 4 deletions.
21 changes: 21 additions & 0 deletions Runtime/AssetRegistry/AssetRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ namespace Sailor
return true;
}

template<typename TTextType, typename TFilepath>
static bool ReadTextFile(const TFilepath& filename, TTextType& outText)
{
std::ifstream file(filename, std::ios::in | std::ios::ate);
if (!file.is_open())
{
return false;
}

std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);

std::stringstream buffer;
buffer << file.rdbuf();
outText = buffer.str();

file.close();

return true;
}

template<typename TBinaryType, typename TFilepath>
static void WriteBinaryFile(const TFilepath& filename, const TVector<TBinaryType>& buffer)
{
Expand Down
12 changes: 12 additions & 0 deletions Runtime/AssetRegistry/Model/ModelImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ void ProcessNode_Assimp(TVector<ModelImporter::MeshContext>& outParsedMeshes, ai
}
//////////////////////////

YAML::Node Model::Serialize() const
{
YAML::Node res;
SERIALIZE_PROPERTY(res, m_fileId);
return res;
}

void Model::Deserialize(const YAML::Node& inData)
{
DESERIALIZE_PROPERTY(inData, m_fileId);
}

void Model::Flush()
{
if (m_meshes.Num() == 0)
Expand Down
6 changes: 5 additions & 1 deletion Runtime/AssetRegistry/Model/ModelImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "RHI/Mesh.h"
#include "RHI/Material.h"
#include "Math/Bounds.h"
#include "Core/YamlSerializable.h"

namespace Sailor::RHI
{
Expand All @@ -28,7 +29,7 @@ namespace Sailor
{
using ModelPtr = TObjectPtr<class Model>;

class Model : public Object
class Model : public Object, public IYamlSerializable
{
public:

Expand All @@ -48,6 +49,9 @@ namespace Sailor
SAILOR_API const Math::AABB& GetBoundsAABB() const { return m_boundsAabb; }
SAILOR_API const Math::Sphere& GetBoundsSphere() const { return m_boundsSphere; }

SAILOR_API virtual YAML::Node Serialize() const override;
SAILOR_API virtual void Deserialize(const YAML::Node& inData) override;

protected:

TVector<RHI::RHIMeshPtr> m_meshes;
Expand Down
12 changes: 10 additions & 2 deletions Runtime/AssetRegistry/Prefab/PrefabImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,22 @@ Tasks::TaskPtr<PrefabPtr> PrefabImporter::LoadPrefab(FileId uid, PrefabPtr& outP

struct Data {};
promise = Tasks::CreateTaskWithResult<TSharedPtr<Data>>("Load prefab",
[prefab, assetInfo, this]()
[prefab, assetInfo, this]() mutable
{
TSharedPtr<Data> res = TSharedPtr<Data>::Make();

std::string text;
AssetRegistry::ReadTextFile(assetInfo->GetAssetFilepath(), text);

YAML::Node node = YAML::Load(text);
prefab->Deserialize(node);

return res;

})->Then<PrefabPtr>([prefab](TSharedPtr<Data> data) mutable
{
return prefab;
}, "Update Prefab", Tasks::EThreadType::RHI)->ToTaskWithResult();
}, "Preload resources", Tasks::EThreadType::RHI)->ToTaskWithResult();

outPrefab = loadedPrefab = prefab;
promise->Run();
Expand Down
3 changes: 3 additions & 0 deletions Runtime/AssetRegistry/Prefab/PrefabImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ namespace Sailor
TVector<ReflectionData> m_gameObjects{};

friend class PrefabImporter;

// We need that for object instantiation
friend class World;
};

class PrefabImporter final : public TSubmodule<PrefabImporter>, public IAssetInfoHandlerListener
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Engine/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ namespace Sailor
using TexturePtr = TObjectPtr<class Texture>;
using ShaderSetPtr = TObjectPtr<class ShaderSet>;
using FrameGraphPtr = TObjectPtr<class FrameGraph>;
using PrefabPtr = TObjectPtr<class Prefab>;

using GameObjectPtr = TObjectPtr<class GameObject>;
using WorldPtr = class World*;
using ObjectPtr = TObjectPtr<class Object>;
using FrameGraphPtr = TObjectPtr<class FrameGraph>;

using ComponentPtr = TObjectPtr<class Component>;
using CameraComponentPtr = TObjectPtr<class CameraComponent>;
using MeshRendererComponentPtr = TObjectPtr<class MeshRendererComponent>;
Expand Down
42 changes: 42 additions & 0 deletions Runtime/Engine/World.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Engine/World.h"
#include "Engine/GameObject.h"
#include "Engine/EngineLoop.h"
#include "AssetRegistry/Prefab/PrefabImporter.h"
#include <Components/TestComponent.h>
#include <ECS/TransformECS.h>

Expand Down Expand Up @@ -96,6 +97,47 @@ void World::Tick(FrameState& frameState)
RHI::Renderer::GetDriverCommands()->EndCommandList(m_commandList);
}

GameObjectPtr World::Instantiate(PrefabPtr prefab, const glm::vec3& worldPosition, const std::string& name)
{
check(prefab->m_gameObjects.Num() > 0);

TVector<GameObjectPtr> gameObjects;

for (uint32_t j = 0; j < prefab->m_gameObjects.Num(); j++)
{
GameObjectPtr gameObject = Instantiate(worldPosition, prefab->m_gameObjects[j].m_name);

for (uint32_t i = 0; i < prefab->m_gameObjects[j].m_components.Num(); i++)
{
ReflectionInfo& reflection = prefab->m_components[i];
ComponentPtr newComponent = Reflection::CreateObject<Component>(reflection.GetTypeInfo(), GetAllocator());
gameObject->AddComponentRaw(newComponent);
newComponent->ApplyReflection(reflection);
}

gameObjects.Add(gameObject);
}

GameObjectPtr root;

for (uint32_t i = 0; i < gameObjects.Num(); i++)
{
auto& go = gameObjects[i];
uint32_t parentIndex = prefab->m_gameObjects[i].m_parentIndex;

if (parentIndex != -1)
{
go->SetParent(gameObjects[parentIndex]);
}
else
{
root = go;
}
}

return root;
}

GameObjectPtr World::Instantiate(const glm::vec3& worldPosition, const std::string& name)
{
auto newObject = GameObjectPtr::Make(m_allocator, this, name);
Expand Down
1 change: 1 addition & 0 deletions Runtime/Engine/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Sailor
SAILOR_API World(World&&) = default;
SAILOR_API World& operator=(World&&) = default;

SAILOR_API GameObjectPtr Instantiate(PrefabPtr prefab, const glm::vec3& worldPosition = glm::vec3(0, 0, 0), const std::string& name = "Untitled");
SAILOR_API GameObjectPtr Instantiate(const glm::vec3& worldPosition = glm::vec3(0, 0, 0), const std::string& name = "Untitled");
SAILOR_API void Destroy(GameObjectPtr object);

Expand Down

0 comments on commit 33fbe76

Please sign in to comment.