Skip to content

Commit e79e2f3

Browse files
committed
Refactor Chunk mesh handling and improve performance
Modifie la méthode `compute_mesh` pour retourner un objet `Geometry` au lieu d'un `std::optional<Geometry>`, simplifiant ainsi le code. Ajoute la méthode `update_mesh` pour mettre à jour le maillage existant et `is_active` pour vérifier l'état des voxels. Réorganise `ChunkManager::update_world_x` pour améliorer la lisibilité et la logique, tout en commentant des appels à `UpdateChunk` pour éviter des opérations inutiles.
1 parent 6113783 commit e79e2f3

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

R3DVoxel/VoxelEngine/Chunk.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ void Chunk::DeleteChunk(std::shared_ptr<GameObject> world)
167167
world->RemoveMesh(m_mesh_id);
168168
}
169169

170-
std::optional<Geometry> Chunk::compute_mesh(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map)
170+
Geometry Chunk::compute_mesh(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map)
171171
{
172-
if (m_active_voxel == false)
173-
return {};
172+
/*if (m_active_voxel == false)
173+
return {};*/
174174

175175
m_visible_voxel = { true };
176176

@@ -192,8 +192,8 @@ std::optional<Geometry> Chunk::compute_mesh(const std::map<ChunkKey, std::unique
192192
{
193193
// check if face is hidden by other chunk
194194
m_visible_voxel[voxel].xneg =
195-
chunk_map.at({ static_cast<int32_t>(m_position.x) - 1, static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) })
196-
->GetVoxel(x + Voxel::CHUNK_SIZE - 1, y, z);
195+
chunk_map.at({ static_cast<int32_t>(m_position.x) - 1, static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) })
196+
->GetVoxel(x + Voxel::CHUNK_SIZE - 1, y, z);
197197
}
198198

199199
if (x < Voxel::CHUNK_SIZE - 1)
@@ -203,8 +203,8 @@ std::optional<Geometry> Chunk::compute_mesh(const std::map<ChunkKey, std::unique
203203
else if (chunk_map.find({ static_cast<int32_t>(m_position.x) + 1, static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) }) != chunk_map.end())
204204
{
205205
m_visible_voxel[voxel].xpos =
206-
chunk_map.at({ static_cast<int32_t>(m_position.x) + 1, static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) })
207-
->GetVoxel(x - Voxel::CHUNK_SIZE + 1, y, z);
206+
chunk_map.at({ static_cast<int32_t>(m_position.x) + 1, static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) })
207+
->GetVoxel(x - Voxel::CHUNK_SIZE + 1, y, z);
208208
}
209209

210210
if (y > 0)
@@ -256,6 +256,11 @@ void Chunk::render_mesh(const Geometry& mesh, GameObject& world, std::shared_ptr
256256
world.bindMatToMesh(m_mesh_id, mat);
257257
}
258258

259+
void Chunk::update_mesh(const Geometry& mesh, GameObject& world)
260+
{
261+
world.UpdateMesh(m_mesh_id, mesh.vertices, mesh.indices);
262+
}
263+
259264
void Chunk::SetVoxel(const uint32_t x, const uint32_t y, const uint32_t z)
260265
{
261266
m_active_voxel.set(x + y * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR, true);
@@ -271,6 +276,11 @@ bool Chunk::GetVoxel(const uint32_t x, const uint32_t y, const uint32_t z) const
271276
return m_active_voxel[x + y * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR];
272277
}
273278

279+
bool Chunk::is_active() const
280+
{
281+
return m_active_voxel.any();
282+
}
283+
274284
glm::vec3 Chunk::GetPosition() const
275285
{
276286
return m_position;

R3DVoxel/VoxelEngine/Chunk.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ class Chunk
4242
void UpdateChunk(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map, GameObject& world);
4343
void DeleteChunk(std::shared_ptr<GameObject> world);
4444

45-
std::optional<Geometry> compute_mesh(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map);
45+
Geometry compute_mesh(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map);
4646
void render_mesh(const Geometry& mesh, GameObject& world, std::shared_ptr<Material> mat);
47+
void update_mesh(const Geometry& mesh, GameObject& world);
4748

4849

4950

5051
void SetVoxel(const uint32_t x, const uint32_t y, const uint32_t z);
5152
void SetBlockType(const uint32_t x, const uint32_t y, const uint32_t z, const TBlock type);
5253

5354
bool GetVoxel(const uint32_t x, const uint32_t y, const uint32_t z) const;
55+
bool is_active() const;
5456

5557
glm::vec3 GetPosition() const;
5658
int32_t mesh_id() const;

R3DVoxel/VoxelEngine/ChunkManager.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,28 @@ void ChunkManager::update_world_x(int32_t create_x, int32_t update_xplus, int32_
337337
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
338338
{
339339
// m_chunk_map.at({ create_x, y, z })->BuildChunk(m_chunk_map, mp_world, mp_world_mat);
340-
auto mesh = m_chunk_map.at({ create_x, y, z })->compute_mesh(m_chunk_map);
341-
if (mesh.has_value())
342-
m_chunk_map.at({ create_x, y, z })->render_mesh(mesh.value(), *mp_world, mp_world_mat);
340+
Chunk* new_chunk = m_chunk_map.at({ create_x, y, z }).get();
341+
if (new_chunk->is_active())
342+
{
343+
auto mesh = new_chunk->compute_mesh(m_chunk_map);
344+
new_chunk->render_mesh(mesh, *mp_world, mp_world_mat);
345+
}
343346

344-
m_chunk_map.at({ update_xplus, y, z })->UpdateChunk(m_chunk_map, *mp_world);
345-
m_chunk_map.at({ update_xmin, y, z })->UpdateChunk(m_chunk_map, *mp_world);
347+
// m_chunk_map.at({ update_xplus, y, z })->UpdateChunk(m_chunk_map, *mp_world);
348+
Chunk* front_chunk = m_chunk_map.at({ update_xplus, y, z }).get();
349+
if (front_chunk->is_active() || front_chunk->mesh_id() != -1)
350+
{
351+
auto mesh = front_chunk->compute_mesh(m_chunk_map);
352+
front_chunk->update_mesh(mesh, *mp_world);
353+
}
354+
355+
// m_chunk_map.at({ update_xmin, y, z })->UpdateChunk(m_chunk_map, *mp_world);
356+
Chunk* back_chunk = m_chunk_map.at({ update_xmin, y, z }).get();
357+
if (back_chunk->is_active() || back_chunk->mesh_id() != -1)
358+
{
359+
auto mesh = back_chunk->compute_mesh(m_chunk_map);
360+
back_chunk->update_mesh(mesh, *mp_world);
361+
}
346362
}
347363
}
348364
}

0 commit comments

Comments
 (0)