refactor(client): mesh chunks via the renderer and drop the client copy
This commit is contained in:
parent
edc72a0f6f
commit
d2f4beb5af
|
|
@ -8,8 +8,6 @@ use shared::protocol::chunk::ChunkMessage;
|
||||||
use shared::world::{CHUNK_SIZE, Chunk, ChunkData, ChunkPos};
|
use shared::world::{CHUNK_SIZE, Chunk, ChunkData, ChunkPos};
|
||||||
use tracing::{debug, error};
|
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.
|
/// 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.
|
// TODO: make configurable / drive from view-distance setting.
|
||||||
pub const LOAD_RADIUS: i32 = 8;
|
pub const LOAD_RADIUS: i32 = 8;
|
||||||
|
|
@ -80,7 +78,7 @@ impl ChunkManager {
|
||||||
/// Materializes, meshes, and uploads one delivered chunk, marking its position resident.
|
/// Materializes, meshes, and uploads one delivered chunk, marking its position resident.
|
||||||
fn apply_chunk(&mut self, pos: ChunkPos, data: &ChunkData, renderer: &mut renderer::Renderer) {
|
fn apply_chunk(&mut self, pos: ChunkPos, data: &ChunkData, renderer: &mut renderer::Renderer) {
|
||||||
let chunk = data.materialize(&self.baseline);
|
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.
|
// 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() {
|
if !indices.is_empty() {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
mod camera;
|
mod camera;
|
||||||
mod chunks;
|
mod chunks;
|
||||||
mod meshing;
|
|
||||||
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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<Vertex>, Vec<u32>) {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue