-
Notifications
You must be signed in to change notification settings - Fork 35
/
Chunk.cs
85 lines (61 loc) · 2.46 KB
/
Chunk.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
namespace meshing;
public unsafe partial struct Chunk
{
// Client
public void Reset(int cx, int cy, int cz, bool fake)
{
// Ensure we were disposed properly before being re-allocated
Assert(!Allocated);
// Store the world position of this chunk
chunkPosX = (byte)cx;
chunkPosY = (byte)cy;
chunkPosZ = (byte)cz;
// Allocate voxel data
bytes_voxels = Constants.BYTES_PER_CHUNK;
voxels = (Voxel*)Allocator.AllocZeroed(bytes_voxels);
SetDirty();
// Two fake chunks are allocated, one that's completely empty and one that's full
// The empty chunk is used for raytracing, and full chunk is used to prevent meshing the outside of the map
this.fake = fake;
// To speed up meshing, each chunk stores two heightmaps, which contain the min and max altitude of each column in the chunk.
// This saves looping over the whole chunk when meshing
bytes_maxAltitude = bytes_minAltitude = Constants.ChunkSizeSquared;
minAltitude = (byte*)Allocator.AllocZeroed(bytes_minAltitude);
maxAltitude = (byte*)Allocator.AllocZeroed(bytes_maxAltitude);
// Prepare the min heightmap
var ptr = minAltitude;
for (int i = Constants.ChunkSizeSquared; i > 0; i--)
*ptr++ = Constants.ChunkSize;
}
public void SetVoxel(int x, int y, int z, byte index)
{
var i = WorldToLocal(x);
var j = WorldToLocal(y);
var k = WorldToLocal(z);
// Update the voxel
var b = voxels + GetAccessLocal(i, j, k);
b->Reset(index);
// Update the client-side min+max heightmap for faster meshing
if (index > 0)
OnVoxelAdded(i, j, k);
else
OnVoxelRemoved(i, j, k);
SetDirty();
}
// Shortcut functions
public void SetDirty() => dirty = true;
public void UnsetDirty() => dirty = false;
public bool IsDirty() => dirty;
public static int WorldToLocal(int a) => a & Constants.ChunkMask;
public static int GetHeightmapAccess(int i, int k) => i | (k * Constants.ChunkSize);
public static int GetAccessLocal(int i, int j, int k)
{
Assert(i >= 0);
Assert(j >= 0);
Assert(k >= 0);
Assert(i < Constants.ChunkSize);
Assert(j < Constants.ChunkSize);
Assert(k < Constants.ChunkSize);
return j | (i * Constants.ChunkSize) | (k * Constants.ChunkSizeSquared);
}
}