SpiecsEngine
 
Loading...
Searching...
No Matches

◆ CreateEntityRecursive()

Entity Spices::GltfCollection::CreateEntityRecursive ( World * world,
const std::string & tag,
uint32_t node,
const glm::mat4 & model,
std::shared_ptr< LoadingState > loadingState )
private

Create Entity recursive from gltf file nodes.

Parameters
[in]worldWorld Pointer.
[in]tagEntity name.
[in]nodegltf nodes index.
[in]modelrecursive model matrix.
[in]loadingStateLoadingState.

Create this entity.

‍**

Create this entity.

‍**

Definition at line 60 of file GltfCollection.cpp.

67 {
68 GltfNodes::Item& item = m_Nodes->m_NodesData[node];
69
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
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 }
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.
std::unique_ptr< GltfNodes > m_Nodes
static std::shared_ptr< Material > LoadMaterial(const GltfMaterials::Item &material, GltfImages *images)
Load a Material from Gltf file material component.
bool DecomposeTransform(const glm::mat4 &transform, glm::vec3 &outTranslation, glm::vec3 &outRotation, glm::vec3 &outScale)
Decompose matrix to split SRT transform.
Definition Math.cpp:15

References Spices::World::CreateEntity(), and Spices::GltfNodes::Item::mesh.