New Atlas + Texture Pipeline

This commit is contained in:
MDietermann
2026-05-31 20:32:46 +02:00
parent ab8816e27c
commit 4e14786668
10 changed files with 26 additions and 20 deletions

View File

@@ -4,7 +4,10 @@ namespace VoxelEngine.World.Chunk;
public sealed class ChunkMesher : IChunkMesher
{
private const int AtlasTiles = 4; // horizontal strip of 4 tiles
private const int TileSizePixels = 32; // each tile is 32x32 pixels
private const int AtlasSizePixels = 64; // atlas is 64x64 pixels (2x2 grid)
private const float TileSize = (float)TileSizePixels / AtlasSizePixels; // normalized tile size
// Per-face geometry: base corner + two in-plane edge vectors (u, v) with
// cross(u, v) == outward normal, guaranteeing CCW winding from outside.
@@ -69,17 +72,18 @@ public sealed class ChunkMesher : IChunkMesher
: (neighborOpaqueOutside?.Invoke(nx, ny, nz) ?? false);
if (neighborOpaque) continue;
ChunkMesher.AddFace(verts, x, y, z, face, block.GetTextureIndex(face.Kind));
ChunkMesher.AddFace(verts, x, y, z, face, block.GetTextureTile(face.Kind));
}
}
return verts.ToArray();
}
private static void AddFace(List<float> verts, int x, int y, int z, Face face, int texIndex)
private static void AddFace(List<float> verts, int x, int y, int z, Face face, TileCoord tile)
{
float tileWidth = 1f / ChunkMesher.AtlasTiles;
float uBase = texIndex * tileWidth;
float uBase = tile.X * ChunkMesher.TileSize;
// Image is flipped on load, so offset from the top to keep tile (0,0) = top-left.
float vBase = 1f - (tile.Y + 1) * ChunkMesher.TileSize;
foreach (int corner in ChunkMesher.Triangle)
{
@@ -89,10 +93,11 @@ public sealed class ChunkMesher : IChunkMesher
float py = y + face.Base.y + face.U.y * cu + face.V.y * cv;
float pz = z + face.Base.z + face.U.z * cu + face.V.z * cv;
float u = uBase + cu * tileWidth;
float u = uBase + cu * ChunkMesher.TileSize;
float v = vBase + cv * ChunkMesher.TileSize;
verts.Add(px); verts.Add(py); verts.Add(pz);
verts.Add(u); verts.Add(cv);
verts.Add(u); verts.Add(v);
verts.Add(face.Shade);
}
}