From d2f4beb5af62c96278bba3ec034c2cfc9924f128 Mon Sep 17 00:00:00 2001 From: Serkyo Date: Thu, 23 Jul 2026 02:12:25 +0200 Subject: [PATCH] refactor(client): mesh chunks via the renderer and drop the client copy --- crates/client/src/chunks.rs | 4 +- crates/client/src/main.rs | 1 - crates/client/src/meshing.rs | 208 ----------------------------------- 3 files changed, 1 insertion(+), 212 deletions(-) delete mode 100644 crates/client/src/meshing.rs diff --git a/crates/client/src/chunks.rs b/crates/client/src/chunks.rs index c20cffd..784581b 100644 --- a/crates/client/src/chunks.rs +++ b/crates/client/src/chunks.rs @@ -8,8 +8,6 @@ use shared::protocol::chunk::ChunkMessage; use shared::world::{CHUNK_SIZE, Chunk, ChunkData, ChunkPos}; use tracing::{debug, error}; -use crate::meshing; - /// Radius, in chunks, of the region kept resident around the camera center. Also the radius the client subscribes with, so the server's resident set matches the client's. // TODO: make configurable / drive from view-distance setting. pub const LOAD_RADIUS: i32 = 8; @@ -80,7 +78,7 @@ impl ChunkManager { /// Materializes, meshes, and uploads one delivered chunk, marking its position resident. fn apply_chunk(&mut self, pos: ChunkPos, data: &ChunkData, renderer: &mut renderer::Renderer) { let chunk = data.materialize(&self.baseline); - let (vertices, indices) = meshing::generate_mesh(&chunk); + let (vertices, indices) = renderer::meshing::generate_mesh(&chunk); // Uploading a zero-length buffer is invalid, so an all-air chunk skips the renderer entirely. It is still marked resident below so a later delivery is not double-counted. if !indices.is_empty() { diff --git a/crates/client/src/main.rs b/crates/client/src/main.rs index 44cb910..f5a816e 100644 --- a/crates/client/src/main.rs +++ b/crates/client/src/main.rs @@ -6,7 +6,6 @@ mod camera; mod chunks; -mod meshing; use std::time::Instant; diff --git a/crates/client/src/meshing.rs b/crates/client/src/meshing.rs deleted file mode 100644 index da41f3d..0000000 --- a/crates/client/src/meshing.rs +++ /dev/null @@ -1,208 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only - -use renderer::mesh::Vertex; -use shared::world::{BlockId, CHUNK_SIZE, Chunk}; - -#[expect( - clippy::cast_precision_loss, - clippy::cast_possible_truncation, - clippy::too_many_lines, - reason = "voxel coordinates and vertex counts are small and lossless as f32/u32; the per-face unrolling is intentionally long" -)] -pub fn generate_mesh(chunk: &Chunk) -> (Vec, Vec) { - let mut vertices = Vec::new(); - let mut indices = Vec::new(); - - for x in 0..CHUNK_SIZE { - for y in 0..CHUNK_SIZE { - for z in 0..CHUNK_SIZE { - let block = chunk.get(x, y, z); - - if block == BlockId::AIR { - continue; - } - - let fx = x as f32; - let fy = y as f32; - let fz = z as f32; - - if y == CHUNK_SIZE - 1 || chunk.get(x, y + 1, z) == BlockId::AIR { - let base_idx = vertices.len() as u32; - - vertices.push(Vertex { - position: [fx - 0.5, fy + 0.5, fz + 0.5], - color: [0.2, 0.8, 0.2], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy + 0.5, fz + 0.5], - color: [0.2, 0.8, 0.2], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy + 0.5, fz - 0.5], - color: [0.2, 0.8, 0.2], - }); - vertices.push(Vertex { - position: [fx - 0.5, fy + 0.5, fz - 0.5], - color: [0.2, 0.8, 0.2], - }); - - indices.extend_from_slice(&[ - base_idx, - base_idx + 1, - base_idx + 2, - base_idx + 2, - base_idx + 3, - base_idx, - ]); - } - - if y == 0 || chunk.get(x, y - 1, z) == BlockId::AIR { - let base_idx = vertices.len() as u32; - - vertices.push(Vertex { - position: [fx - 0.5, fy - 0.5, fz - 0.5], - color: [0.1, 0.4, 0.1], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy - 0.5, fz - 0.5], - color: [0.1, 0.4, 0.1], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy - 0.5, fz + 0.5], - color: [0.1, 0.4, 0.1], - }); - vertices.push(Vertex { - position: [fx - 0.5, fy - 0.5, fz + 0.5], - color: [0.1, 0.4, 0.1], - }); - indices.extend_from_slice(&[ - base_idx, - base_idx + 1, - base_idx + 2, - base_idx + 2, - base_idx + 3, - base_idx, - ]); - } - - if x == CHUNK_SIZE - 1 || chunk.get(x + 1, y, z) == BlockId::AIR { - let base_idx = vertices.len() as u32; - - vertices.push(Vertex { - position: [fx + 0.5, fy - 0.5, fz + 0.5], - color: [0.15, 0.6, 0.15], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy - 0.5, fz - 0.5], - color: [0.15, 0.6, 0.15], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy + 0.5, fz - 0.5], - color: [0.15, 0.6, 0.15], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy + 0.5, fz + 0.5], - color: [0.15, 0.6, 0.15], - }); - indices.extend_from_slice(&[ - base_idx, - base_idx + 1, - base_idx + 2, - base_idx + 2, - base_idx + 3, - base_idx, - ]); - } - - if x == 0 || chunk.get(x - 1, y, z) == BlockId::AIR { - let base_idx = vertices.len() as u32; - - vertices.push(Vertex { - position: [fx - 0.5, fy - 0.5, fz - 0.5], - color: [0.15, 0.6, 0.15], - }); - vertices.push(Vertex { - position: [fx - 0.5, fy - 0.5, fz + 0.5], - color: [0.15, 0.6, 0.15], - }); - vertices.push(Vertex { - position: [fx - 0.5, fy + 0.5, fz + 0.5], - color: [0.15, 0.6, 0.15], - }); - vertices.push(Vertex { - position: [fx - 0.5, fy + 0.5, fz - 0.5], - color: [0.15, 0.6, 0.15], - }); - indices.extend_from_slice(&[ - base_idx, - base_idx + 1, - base_idx + 2, - base_idx + 2, - base_idx + 3, - base_idx, - ]); - } - - if z == CHUNK_SIZE - 1 || chunk.get(x, y, z + 1) == BlockId::AIR { - let base_idx = vertices.len() as u32; - - vertices.push(Vertex { - position: [fx - 0.5, fy - 0.5, fz + 0.5], - color: [0.18, 0.7, 0.18], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy - 0.5, fz + 0.5], - color: [0.18, 0.7, 0.18], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy + 0.5, fz + 0.5], - color: [0.18, 0.7, 0.18], - }); - vertices.push(Vertex { - position: [fx - 0.5, fy + 0.5, fz + 0.5], - color: [0.18, 0.7, 0.18], - }); - indices.extend_from_slice(&[ - base_idx, - base_idx + 1, - base_idx + 2, - base_idx + 2, - base_idx + 3, - base_idx, - ]); - } - - if z == 0 || chunk.get(x, y, z - 1) == BlockId::AIR { - let base_idx = vertices.len() as u32; - - vertices.push(Vertex { - position: [fx + 0.5, fy - 0.5, fz - 0.5], - color: [0.18, 0.7, 0.18], - }); - vertices.push(Vertex { - position: [fx - 0.5, fy - 0.5, fz - 0.5], - color: [0.18, 0.7, 0.18], - }); - vertices.push(Vertex { - position: [fx - 0.5, fy + 0.5, fz - 0.5], - color: [0.18, 0.7, 0.18], - }); - vertices.push(Vertex { - position: [fx + 0.5, fy + 0.5, fz - 0.5], - color: [0.18, 0.7, 0.18], - }); - indices.extend_from_slice(&[ - base_idx, - base_idx + 1, - base_idx + 2, - base_idx + 2, - base_idx + 3, - base_idx, - ]); - } - } - } - } - - (vertices, indices) -}