feat: add multi-chunk world with cross-chunk face culling

Add a World that stores chunks in a Dictionary keyed by chunk
coordinate, with floor-division world<->chunk coordinate mapping
(correct for negative coords). The mesher takes an optional neighbor
sampler so faces at chunk borders are culled against adjacent chunks
instead of always meshing the shell. Render one ChunkRenderer per
chunk, placed via the model matrix. Reorganize the World layer into
Blocks/ and Chunk/ sub-namespaces.
This commit is contained in:
2026-05-28 15:32:45 +02:00
parent 70c986529b
commit 49d35cab5b
13 changed files with 145 additions and 28 deletions

View File

@@ -1,4 +1,4 @@
namespace VoxelEngine.World;
namespace VoxelEngine.World.Blocks;
public enum BlockFace
{

View File

@@ -1,6 +1,4 @@
using VoxelEngine.World.Blocks;
namespace VoxelEngine.World;
namespace VoxelEngine.World.Blocks;
public sealed class BlockRegistry
{

View File

@@ -1,4 +1,4 @@
namespace VoxelEngine.World;
namespace VoxelEngine.World.Blocks;
public interface IBlock
{

View File

@@ -1,4 +1,4 @@
namespace VoxelEngine.World;
namespace VoxelEngine.World.Chunk;
public sealed class Chunk
{

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
using VoxelEngine.World.Blocks;
namespace VoxelEngine.World;
namespace VoxelEngine.World.Chunk;
public sealed class ChunkMesher : IChunkMesher
{
@@ -45,7 +45,7 @@ public sealed class ChunkMesher : IChunkMesher
{ (0, 0), (1, 0), (1, 1), (0, 1) };
private static readonly int[] Triangle = { 0, 1, 2, 0, 2, 3 };
public float[] Build(Chunk chunk, BlockRegistry registry)
public float[] Build(Chunk chunk, BlockRegistry registry, Func<int, int, int, bool>? neighborOpaqueOutside = null)
{
List<float> verts = new();
@@ -64,7 +64,9 @@ public sealed class ChunkMesher : IChunkMesher
// Out-of-chunk neighbors are treated as non-opaque (single chunk).
bool neighborOpaque =
Chunk.InBounds(nx, ny, nz) && registry.Get(chunk[nx, ny, nz]).IsOpaque;
Chunk.InBounds(nx, ny, nz)
? registry.Get(chunk[nx, ny, nz]).IsOpaque
: (neighborOpaqueOutside?.Invoke(nx, ny, nz) ?? false);
if (neighborOpaque) continue;
ChunkMesher.AddFace(verts, x, y, z, face, block.GetTextureIndex(face.Kind));

View File

@@ -0,0 +1,9 @@
using VoxelEngine.World.Blocks;
namespace VoxelEngine.World.Chunk;
public interface IChunkMesher
{
// Returns interleaved vertices: [pos.x, pos.y, pos.z, u, v, shade] per vertex.
float[] Build(Chunk chunk, BlockRegistry registry, Func<int, int, int, bool>? neighborOpaqueOutside = null);
}

View File

@@ -1,7 +0,0 @@
namespace VoxelEngine.World;
public interface IChunkMesher
{
// Returns interleaved vertices: [pos.x, pos.y, pos.z, u, v, shade] per vertex.
float[] Build(Chunk chunk, BlockRegistry registry);
}

11
World/IWorld.cs Normal file
View File

@@ -0,0 +1,11 @@
using OpenTK.Mathematics;
using VoxelEngine.World.Blocks;
namespace VoxelEngine.World;
public interface IWorld
{
public void SetChunk(Vector3i chunkPos, Chunk.Chunk chunk);
public Chunk.Chunk? GetChunk(Vector3i chunkPos);
public IBlock GetBlock(int worldX, int worldY, int worldZ);
}

63
World/World.cs Normal file
View File

@@ -0,0 +1,63 @@
using System.Collections;
using OpenTK.Mathematics;
using VoxelEngine.World.Blocks;
namespace VoxelEngine.World;
public sealed class World : IWorld, IEnumerable<KeyValuePair<Vector3i, Chunk.Chunk>>
{
private readonly Dictionary<Vector3i, Chunk.Chunk> _chunks = new();
private readonly BlockRegistry _registry;
public World(BlockRegistry registry)
{
this._registry = registry;
}
public void SetChunk(Vector3i chunkPos, Chunk.Chunk chunk)
{
this._chunks[chunkPos] = chunk;
}
public Chunk.Chunk? GetChunk(Vector3i chunkPos)
{
return this._chunks.GetValueOrDefault(chunkPos);
}
public IBlock GetBlock(int worldX, int worldY, int worldZ)
{
Vector3i chunkPos = new(
World.FloorDiv(worldX, Chunk.Chunk.Size),
World.FloorDiv(worldY, Chunk.Chunk.Size),
World.FloorDiv(worldZ, Chunk.Chunk.Size)
);
Chunk.Chunk? chunk = this.GetChunk(chunkPos);
if (chunk == null)
{
return this._registry.Air;
}
int localX = worldX - chunkPos.X * Chunk.Chunk.Size;
int localY = worldY - chunkPos.Y * Chunk.Chunk.Size;
int localZ = worldZ - chunkPos.Z * Chunk.Chunk.Size;
return this._registry.Get(chunk[localX, localY, localZ]);
}
private static int FloorDiv(int value, int size)
{
int quotient = value / size;
if (value % size != 0 && value < 0)
{
quotient--;
}
return quotient;
}
public IEnumerator<KeyValuePair<Vector3i, Chunk.Chunk>> GetEnumerator()
=> this._chunks.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
}