Culling, rendering improvements and optimizations, block removing test.
This commit is contained in:
121
Chunk.cs
121
Chunk.cs
@@ -8,7 +8,7 @@
|
||||
public readonly int PositionY;
|
||||
|
||||
private Dictionary<ushort, BlockData> _blockData;
|
||||
private byte[] _blocks;
|
||||
private Blocks[] _blocks;
|
||||
|
||||
public Chunk(int positionX, int positionY)
|
||||
{
|
||||
@@ -16,18 +16,23 @@
|
||||
PositionY = positionY;
|
||||
|
||||
_blockData = new Dictionary<ushort, BlockData>();
|
||||
_blocks = new byte[Size * Size * Height];
|
||||
_blocks = new Blocks[Size * Size * Height];
|
||||
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public void SetBlock(int x, int y, int z, byte blockId)
|
||||
public void SetBlock(int x, int y, int z, Blocks block)
|
||||
{
|
||||
int i = GetBlockIndex(x, y, z);
|
||||
_blocks[i] = blockId;
|
||||
_blocks[i] = block;
|
||||
}
|
||||
|
||||
public byte GetBlock(int x, int y, int z)
|
||||
public void SetBlockIndex(int i, Blocks block)
|
||||
{
|
||||
_blocks[i] = block;
|
||||
}
|
||||
|
||||
public Blocks GetBlock(int x, int y, int z)
|
||||
{
|
||||
int i = GetBlockIndex(x, y, z);
|
||||
return _blocks[i];
|
||||
@@ -40,23 +45,9 @@
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
for (int x = 0; x < Size; x++)
|
||||
{
|
||||
for (int z = 0; z < Size; z++)
|
||||
{
|
||||
for (int y = 0; y < Size; y++)
|
||||
{
|
||||
byte blockId = 1;
|
||||
SetBlock(x, y, z, blockId);
|
||||
Console.WriteLine(
|
||||
"Set block "
|
||||
+ x.ToString() + ", "
|
||||
+ y.ToString() + ", "
|
||||
+ z.ToString() + " "
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Random rng = new Random();
|
||||
for (int i = 0; i < Size * Size * 16; i++)
|
||||
_blocks[i] = (Blocks)rng.Next(5);
|
||||
}
|
||||
|
||||
// todo
|
||||
@@ -71,60 +62,25 @@
|
||||
|
||||
private int GetBlockIndex(int x, int y, int z)
|
||||
{
|
||||
return x + y * Size + z * Size * Size;
|
||||
return x + z * Size + y * Size * Size;
|
||||
}
|
||||
|
||||
public List<FaceData> GetFaces()
|
||||
private static readonly (int dx, int dy, int dz)[] Offsets = new (int, int, int)[6]
|
||||
{
|
||||
List<FaceData> list = new List<FaceData>();
|
||||
|
||||
for (byte i = 0; i < 6; i++)
|
||||
{
|
||||
FaceData faceData = new FaceData();
|
||||
|
||||
faceData.Facing = (Orientation)i;
|
||||
|
||||
list.Add(faceData);
|
||||
}
|
||||
|
||||
for (byte i = 0; i < 6; i++)
|
||||
{
|
||||
FaceData faceData = new FaceData();
|
||||
|
||||
faceData.Facing = (Orientation)i;
|
||||
|
||||
faceData.X = 1;
|
||||
|
||||
list.Add(faceData);
|
||||
}
|
||||
|
||||
for (byte i = 0; i < 6; i++)
|
||||
{
|
||||
FaceData faceData = new FaceData();
|
||||
|
||||
faceData.Facing = (Orientation)i;
|
||||
( 1, 0, 0), // +X
|
||||
(-1, 0, 0), // -X
|
||||
( 0, 1, 0), // +Y
|
||||
( 0, -1, 0), // -Y
|
||||
( 0, 0, 1), // +Z
|
||||
( 0, 0, -1) // -Z
|
||||
};
|
||||
|
||||
|
||||
faceData.X = 1;
|
||||
faceData.Y = 1;
|
||||
faceData.Z = 1;
|
||||
public ChunkMesh GetChunkMesh()
|
||||
{
|
||||
ChunkMesh chunkMesh = new ChunkMesh(Size * Size * Height / 2);
|
||||
|
||||
list.Add(faceData);
|
||||
}
|
||||
|
||||
for (byte i = 0; i < 6; i++)
|
||||
{
|
||||
FaceData faceData = new FaceData();
|
||||
|
||||
faceData.Facing = (Orientation)i;
|
||||
|
||||
|
||||
faceData.X = 1;
|
||||
faceData.Y = 2;
|
||||
faceData.Z = 1;
|
||||
|
||||
list.Add(faceData);
|
||||
}
|
||||
// offsets table
|
||||
|
||||
for (int x = 0; x < Size; x++)
|
||||
{
|
||||
@@ -132,23 +88,40 @@
|
||||
{
|
||||
for (int y = 0; y < Height; y++)
|
||||
{
|
||||
for (byte i = 0; i < 6; i++)
|
||||
for (byte face = 0; face < 6; face++)
|
||||
{
|
||||
int indexBase = y * Size * Size + z * Size + x;
|
||||
Blocks block = _blocks[indexBase];
|
||||
|
||||
if (block == Blocks.Air) continue; // ignore if air
|
||||
|
||||
int nx = x + Offsets[face].dx;
|
||||
int ny = y + Offsets[face].dy;
|
||||
int nz = z + Offsets[face].dz;
|
||||
|
||||
// check neighbor, ignore if at chunk edge
|
||||
if (nx >= 0 && nx < Size &&
|
||||
ny >= 0 && ny < Height &&
|
||||
nz >= 0 && nz < Size &&
|
||||
_blocks[GetBlockIndex(nx, ny, nz)] != 0)
|
||||
continue;
|
||||
|
||||
FaceData faceData = new FaceData();
|
||||
|
||||
faceData.Facing = (Orientation)i;
|
||||
faceData.Facing = (Orientation)face;
|
||||
faceData.Texture = BlockDefinitions.Blocks[block].FaceTextures[face];
|
||||
|
||||
faceData.X = (byte)x;
|
||||
faceData.Y = (byte)y;
|
||||
faceData.Z = (byte)z;
|
||||
|
||||
//list.Add(faceData);
|
||||
chunkMesh.Faces.Add(faceData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
return chunkMesh;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user