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

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

@@ -34,7 +34,7 @@ public sealed class Game : GameWindow
GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.CullFace);
this._shader = new Shader("Assets/shaders/chunk.vert", "Assets/shaders/chunk.frag"); this._shader = new Shader("Assets/shaders/chunk.vert", "Assets/shaders/chunk.frag");
this._texture = new Texture("Assets/textures/atlas.png"); this._texture = new Texture("Assets/textures/VoxelShadeAtlas.png");
this._world = new World.World(this._registry); this._world = new World.World(this._registry);

View File

@@ -34,9 +34,9 @@ public class BlockRegistryTests
var registry = new BlockRegistry(); var registry = new BlockRegistry();
IBlock grass = registry.Get(3); IBlock grass = registry.Get(3);
Assert.Equal(0, grass.GetTextureIndex(BlockFace.Top)); Assert.Equal(new TileCoord(0, 1), grass.GetTextureTile(BlockFace.Top));
Assert.Equal(1, grass.GetTextureIndex(BlockFace.Bottom)); Assert.Equal(new TileCoord(0, 0), grass.GetTextureTile(BlockFace.Bottom));
Assert.Equal(3, grass.GetTextureIndex(BlockFace.North)); Assert.Equal(new TileCoord(1, 1), grass.GetTextureTile(BlockFace.North));
} }
[Fact] [Fact]

View File

@@ -10,6 +10,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="OpenTK" Version="4.9.4" /> <PackageReference Include="OpenTK" Version="4.9.4" />
<PackageReference Include="SimplexNoise" Version="2.0.0" />
<PackageReference Include="StbImageSharp" Version="2.30.15" /> <PackageReference Include="StbImageSharp" Version="2.30.15" />
</ItemGroup> </ItemGroup>

View File

@@ -6,5 +6,5 @@ public sealed class AirBlock : IBlock
public string Name => "Air"; public string Name => "Air";
public bool IsOpaque => false; public bool IsOpaque => false;
public bool IsSolid => false; public bool IsSolid => false;
public int GetTextureIndex(BlockFace face) => 0; public TileCoord GetTextureTile(BlockFace face) => new(0, 0);
} }

View File

@@ -6,5 +6,5 @@ public sealed class DirtBlock : IBlock
public string Name => "Dirt"; public string Name => "Dirt";
public bool IsOpaque => true; public bool IsOpaque => true;
public bool IsSolid => true; public bool IsSolid => true;
public int GetTextureIndex(BlockFace face) => 1; public TileCoord GetTextureTile(BlockFace face) => new(0, 0);
} }

View File

@@ -7,10 +7,10 @@ public sealed class GrassBlock : IBlock
public bool IsOpaque => true; public bool IsOpaque => true;
public bool IsSolid => true; public bool IsSolid => true;
public int GetTextureIndex(BlockFace face) => face switch public TileCoord GetTextureTile(BlockFace face) => face switch
{ {
BlockFace.Top => 0, // grass top BlockFace.Top => new(0, 1), // grass top
BlockFace.Bottom => 1, // dirt BlockFace.Bottom => new(0, 0), // dirt
_ => 3 // grass side _ => new(1, 1) // grass side
}; };
} }

View File

@@ -11,8 +11,8 @@ public interface IBlock
// Reserved for future collision (not used in the baseline). // Reserved for future collision (not used in the baseline).
bool IsSolid { get; } bool IsSolid { get; }
// Atlas tile index for the given face. // Atlas tile coordinate for the given face.
int GetTextureIndex(BlockFace face); TileCoord GetTextureTile(BlockFace face);
// Interaction hooks — capability present now, system built post-baseline. // Interaction hooks — capability present now, system built post-baseline.
void OnInteract() { } void OnInteract() { }

View File

@@ -6,5 +6,5 @@ public sealed class StoneBlock : IBlock
public string Name => "Stone"; public string Name => "Stone";
public bool IsOpaque => true; public bool IsOpaque => true;
public bool IsSolid => true; public bool IsSolid => true;
public int GetTextureIndex(BlockFace face) => 2; public TileCoord GetTextureTile(BlockFace face) => new(1, 0);
} }

View File

@@ -4,7 +4,10 @@ namespace VoxelEngine.World.Chunk;
public sealed class ChunkMesher : IChunkMesher 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 // Per-face geometry: base corner + two in-plane edge vectors (u, v) with
// cross(u, v) == outward normal, guaranteeing CCW winding from outside. // 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); : (neighborOpaqueOutside?.Invoke(nx, ny, nz) ?? false);
if (neighborOpaque) continue; 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(); 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 = tile.X * ChunkMesher.TileSize;
float uBase = texIndex * tileWidth; // 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) 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 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 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(px); verts.Add(py); verts.Add(pz);
verts.Add(u); verts.Add(cv); verts.Add(u); verts.Add(v);
verts.Add(face.Shade); verts.Add(face.Shade);
} }
} }