SpiecsEngine
 
Loading...
Searching...
No Matches
GltfCollection.cpp
Go to the documentation of this file.
1/**
2* @file GltfCollection.cpp
3* @brief The GltfCollection Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
9#include "Resources/Loader/GltfLoader.h"
10#include "World/Entity.h"
11#include "World/World/World.h"
12#include "World/Components/MeshComponent.h"
13#include "World/Components/EntityComponent.h"
14#include "Resources/Mesh/GltfPack.h"
15#include "Core/Math/Math.h"
16#include "Slate/SlateInfoBar.h"
17
18namespace Spices {
19
20 GltfCollection::GltfCollection(const std::string& path)
21 {
23
24 GltfLoader::Load(path, this);
25 }
26
27 void GltfCollection::CreateEntity(World* world, const std::string& tag, Transform transform)
28 {
30
31 const std::shared_ptr<LoadingState> loadingState = std::make_shared<LoadingState>();
32
33 /**
34 * @brief Calaulate model matrix.
35 */
36 for (auto& item : m_Scenes->m_ScenesData)
37 {
38 for (const auto& node : item.nodes)
39 {
40 CreateEntityRecursive(world, tag, node, transform.ToMatrix(), loadingState);
41 }
42 }
43
44 /**
45 * @brief InfoBar output.
46 */
47 std::stringstream ss;
48 ss << "GLTF: " << tag << " is on Loading...";
49
50 auto self = shared_from_this();
51 SlateInfoBar::Create<float>(ss.str(), [=]() -> float {
52 return static_cast<float>(loadingState->loadedMeshes.load()) / static_cast<float>(self->m_Meshes->GetNMeshes());
53 }, [=](SlateInfoBar* that) {
54 return std::any_cast<float>(that->GetRate()) >= 1.0f;
55 });
56
58 }
59
61 World* world ,
62 const std::string& tag ,
63 uint32_t node ,
64 const glm::mat4& model ,
65 std::shared_ptr<LoadingState> loadingState
66 )
67 {
68 GltfNodes::Item& item = m_Nodes->m_NodesData[node];
69
70 /**
71 * @brief Create this entity.
72 */
73 auto self = shared_from_this();
74
75
76 Entity entity = world->CreateEntity(tag);
77
78 auto& transformComp = entity.GetComponent<TransformComponent>();
79
80 glm::vec3 position;
81 glm::vec3 rotation;
82 glm::vec3 scale;
83
84 DecomposeTransform(model * item.matrix, position, rotation, scale);
85 rotation = glm::vec3(glm::degrees(rotation.x), glm::degrees(rotation.y), glm::degrees(rotation.z));
86
87 transformComp.SetPosition(position);
88 transformComp.SetRotation(rotation);
89 transformComp.SetScale(scale);
90
91
92 std::vector<Entity> childrens;
93
94 for (auto& n : item.children)
95 {
96 childrens.push_back(CreateEntityRecursive(world, tag, n, item.matrix, loadingState));
97 }
98
99 if (!childrens.empty())
100 {
101 auto& entityComp = entity.AddComponent<EntityComponent>();
102
103 for (auto& e : childrens)
104 {
105 entityComp.AddEntity(e);
106 }
107 }
108
109 if (item.mesh < -0.5f)
110 {
111 return entity;
112 }
113
114 Mesh::Builder builder;
115
116 for (int i = 0; i < self->m_Meshes->m_MeshesData[item.mesh].primitives.size(); i++)
117 {
118 std::stringstream ss;
119 ss << self->m_Meshes->m_MeshesData[item.mesh].name << '_' << node;
120
121 std::shared_ptr<GltfPack> pack = std::make_shared<GltfPack>(ss.str(), [&](GltfPack* gltfPack) {
122 GltfLoader::LoadPack(gltfPack, self->m_Meshes->m_MeshesData[item.mesh].primitives[i], self->m_Accessors.get(), self->m_Buffers.get(), self->m_BufferViews.get());
123 });
124
125 std::shared_ptr<Material> material = GltfLoader::LoadMaterial(self->m_Materials->m_MaterialsData[self->m_Meshes->m_MeshesData[item.mesh].primitives[i].material], self->m_Images.get());
126
127 pack->SetMaterial(material);
128
129 builder.AddPack(pack);
130 }
131
132 std::shared_ptr<Mesh> mesh = builder.Build();
133
134 auto& meshComp = entity.AddComponent<MeshComponent>();
135
136 meshComp.SetMesh(mesh);
137
138 //++loadingState->loadedMeshes;
139
140 ///**
141 //* @brief Mark the world with MeshAddedToWorld bit.
142 //*/
143 //if (loadingState->loadedMeshes.load() == self->m_Meshes->GetNMeshes())
144 //{
145 // world->Mark(World::WorldMarkBits::MeshAddedToWorld);
146 //}
147
148 return entity;
149
150 }
151}
#define SPICES_PROFILE_ZONE
EntityComponent Class. This class defines the specific behaves of EntityComponent.
Entity Class. This class defines the specific behaves of Entity.
Definition Entity.h:20
Entity CreateEntityRecursive(World *world, const std::string &tag, uint32_t node, const glm::mat4 &model, std::shared_ptr< LoadingState > loadingState)
Create Entity recursive from gltf file nodes.
GltfCollection(const std::string &path)
Constructor Function.
void CreateEntity(World *world, const std::string &tag, Transform transform={})
Create Entity instance from gltf file.
Wrapper of Gltf file data.
static bool Load(const std::string &fileName, GltfCollection *collection)
Load a gltf file to GltfCollection.
Loader of load gltf file or items.
Definition GltfLoader.h:23
Wrapper of Gltf Json Nodes.
Definition GltfNodes.h:19
MeshComponent Class. This class defines the specific behaves of MeshComponent.
Builder Class. This class helps to create a mesh.
Definition Mesh.h:29
MeshRenderer Class. This class is a wrapper of mashpack.
Definition Mesh.h:21
TransformComponent Class. This class defines the specific behaves of TransformComponent.
void Mark(WorldMarkFlags flags)
Mark WorldMarkFlags with flags.
Definition World.h:125
Entity CreateEntity(const std::string &name="None")
Create a new empty entity in this world.
Definition World.cpp:13
@ MeshAddedToWorld
Definition World.h:47
World Class. This class defines the basic behaves of World. When we create an new world,...
Definition World.h:41
Nodes Item data.
Definition GltfNodes.h:26
Wrapper of 3D Transform.
Definition Transform.h:16