feat: derive atlas tile size from loaded texture dimensions

This commit is contained in:
MDietermann
2026-05-31 21:43:53 +02:00
parent 50f2bc4221
commit c81cd3c561
4 changed files with 46 additions and 13 deletions

View File

@@ -5,9 +5,15 @@ namespace VoxelEngine.World.Chunk;
public sealed class ChunkMesher : IChunkMesher
{
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
private readonly float _tileU; // normalized tile width
private readonly float _tileV; // normalized tile height
public ChunkMesher(int atlasWidthPixels, int atlasHeightPixels)
{
this._tileU = (float)ChunkMesher.TileSizePixels / atlasWidthPixels;
this._tileV = (float)ChunkMesher.TileSizePixels / atlasHeightPixels;
}
// Per-face geometry: base corner + two in-plane edge vectors (u, v) with
// cross(u, v) == outward normal, guaranteeing CCW winding from outside.
@@ -72,18 +78,18 @@ public sealed class ChunkMesher : IChunkMesher
: (neighborOpaqueOutside?.Invoke(nx, ny, nz) ?? false);
if (neighborOpaque) continue;
ChunkMesher.AddFace(verts, x, y, z, face, block.GetTextureTile(face.Kind));
this.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, TileCoord tile)
private void AddFace(List<float> verts, int x, int y, int z, Face face, TileCoord tile)
{
float uBase = tile.X * ChunkMesher.TileSize;
float uBase = tile.X * this._tileU;
// 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;
float vBase = 1f - (tile.Y + 1) * this._tileV;
foreach (int corner in ChunkMesher.Triangle)
{
@@ -93,8 +99,8 @@ 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 * ChunkMesher.TileSize;
float v = vBase + cv * ChunkMesher.TileSize;
float u = uBase + cu * this._tileU;
float v = vBase + cv * this._tileV;
verts.Add(px); verts.Add(py); verts.Add(pz);
verts.Add(u); verts.Add(v);