-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathChunkHeightmap.cs
95 lines (68 loc) · 2.32 KB
/
ChunkHeightmap.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
86
87
88
89
90
91
92
93
94
95
namespace meshing;
public unsafe partial struct Chunk
{
public void OnVoxelAdded(int i, int j, int k)
{
int access = GetHeightmapAccess(i, k);
if (j < *(minAltitude + access))
*(minAltitude + access) = (byte)j;
var max = maxAltitude + access;
if (j >= *max)
*max = (byte)j;
}
public void OnVoxelRemoved(int i, int j, int k)
{
// Precalculate 1D array accesses
var hAccess = GetHeightmapAccess(i, k);
var xzAccess = i * Constants.ChunkSize + k * Constants.ChunkSizeSquared;
var access = xzAccess + j;
// Update the min
UpdateMinHeightmap(j, hAccess, access, xzAccess);
// Update the max
UpdateMaxHeightmap(j, hAccess, access, xzAccess);
}
void UpdateMinHeightmap(int j, int hAccess, int access, int xzAccess)
{
var min = minAltitude + hAccess;
// Bail if we didn't remove the lowest voxel
if (*min != j && *min != (byte)Constants.ChunkSize)
return;
// Calculate how far up to search
var chunkTop = xzAccess + Constants.ChunkSize;
var amount = chunkTop - access;
var ptr = voxels + access;
// Search up until we find a voxel
for (; amount > 0; amount--)
{
if (ptr++->index == 0)
continue;
// Store the Y part of the access in the heightmap
*min = (byte)(access & Constants.ChunkMask);
return;
}
// If no voxel was found
*min = (byte)Constants.ChunkSize;
}
void UpdateMaxHeightmap(int j, int hAccess, int access, int xzAccess)
{
var max = maxAltitude + hAccess;
// Bail if we didn't remove the highest voxel
if (*max != j && *max != 0)
return;
// Calculate how far down to search
var chunkBottom = xzAccess;
var amount = access - chunkBottom;
var ptr = voxels + access;
// Search down until we find a voxel
for (; amount > 0; amount--)
{
if (ptr--->index == 0)
continue;
// Store the Y part of the access in the heightmap
*max = (byte)(access & Constants.ChunkMask);
return;
}
// If no voxel was found
*max = 0;
}
}