This repository has been archived by the owner on Feb 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoxelworldSystem.cs
232 lines (182 loc) · 9.16 KB
/
VoxelworldSystem.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using UnityEngine.Assertions;
namespace Cube.Voxelworld {
public class VoxelworldSystem : MonoBehaviour {
public static int chunkSize = 16;
public Material chunkMaterial;
public int chunkLoadRangeXZ = 1;
public int chunkLoadRangeY = 1;
public int chunkMaxLoaded = 100;
public List<VoxelworldViewer> viewers = new List<VoxelworldViewer>();
public VoxelTypeManager voxelTypeManager = new VoxelTypeManager();
ChunkProvider _chunkProvider;
List<IVoxelworldListener> _listeners = new List<IVoxelworldListener>();
Dictionary<IntVector3, Chunk> _chunks = new Dictionary<IntVector3, Chunk>();
HashSet<IntVector3> _dirtyChunks = new HashSet<IntVector3>();
Dictionary<IntVector3, ChunkProvider.Request> _loadingChunks = new Dictionary<IntVector3, ChunkProvider.Request>();
Dictionary<IntVector3, float> _positionAgeMap = new Dictionary<IntVector3, float>();
public Voxel GetVoxelAtWorldPosition(Vector3 worldPosition) {
var chunkPosition = WorldToChunkPosition(worldPosition);
Chunk chunk;
if (!_chunks.TryGetValue(chunkPosition, out chunk))
return Voxel.empty;
var blockPosition = WorldToBlockLocalPosition(worldPosition);
return chunk.voxelData.Get(blockPosition.x, blockPosition.y, blockPosition.z);
}
/// <returns>The previous Voxel</returns>
public Voxel SetVoxelAtWorldPosition(Vector3 worldPosition, Voxel voxel) {
var chunkPosition = WorldToChunkPosition(worldPosition);
Chunk chunk;
if (!_chunks.TryGetValue(chunkPosition, out chunk))
return Voxel.empty;
var blockLocalPosition = WorldToBlockLocalPosition(worldPosition);
var prevVoxel = chunk.voxelData.Get(blockLocalPosition.x, blockLocalPosition.y, blockLocalPosition.z);
if (prevVoxel.Equals(voxel))
return prevVoxel;
chunk.voxelData.Set(blockLocalPosition.x, blockLocalPosition.y, blockLocalPosition.z, voxel);
TouchChunk(chunkPosition, blockLocalPosition);
foreach (var listener in _listeners) {
listener.ChangedVoxel(voxel, chunk, blockLocalPosition);
}
return prevVoxel;
}
static public IntVector3 WorldToChunkPosition(Vector3 worldPositon) {
return new IntVector3() {
x = Mathf.FloorToInt(worldPositon.x / chunkSize),
y = Mathf.FloorToInt(worldPositon.y / chunkSize),
z = Mathf.FloorToInt(worldPositon.z / chunkSize)
};
}
static public IntVector3 WorldToBlockWorldPosition(Vector3 worldPositon) {
return new IntVector3(Mathf.FloorToInt(worldPositon.x),
Mathf.FloorToInt(worldPositon.y),
Mathf.FloorToInt(worldPositon.z));
}
static public IntVector3 WorldToBlockLocalPosition(Vector3 worldPositon) {
var chunkPosition = WorldToChunkPosition(worldPositon);
var blockPosition = WorldToBlockWorldPosition(worldPositon);
return new IntVector3(blockPosition.x - chunkPosition.x * chunkSize,
blockPosition.y - chunkPosition.y * chunkSize,
blockPosition.z - chunkPosition.z * chunkSize);
}
public void AddListener(IVoxelworldListener listener) {
_listeners.Add(listener);
}
void Awake() {
_chunkProvider = new ChunkProvider(voxelTypeManager);
gameObject.SetSystem(this);
}
void Update() {
RebuildDirtyChunks();
UpdatePendingChunks();
LoadNewChunks();
//UnloadOldChunks();
}
void TouchChunk(IntVector3 chunkPosition, IntVector3 blockLocalPosition) {
_dirtyChunks.Add(chunkPosition);
if (blockLocalPosition.x == 0) {
_dirtyChunks.Add(chunkPosition + new IntVector3(-1, 0, 0));
} else if (blockLocalPosition.x == chunkSize - 1) {
_dirtyChunks.Add(chunkPosition + new IntVector3(1, 0, 0));
}
if (blockLocalPosition.y == 0) {
_dirtyChunks.Add(chunkPosition + new IntVector3(0, -1, 0));
} else if (blockLocalPosition.y == chunkSize - 1) {
_dirtyChunks.Add(chunkPosition + new IntVector3(0, 1, 0));
}
if (blockLocalPosition.z == 0) {
_dirtyChunks.Add(chunkPosition + new IntVector3(0, 0, -1));
} else if (blockLocalPosition.z == chunkSize - 1) {
_dirtyChunks.Add(chunkPosition + new IntVector3(0, 0, 1));
}
}
void RebuildDirtyChunks() {
var newDirtyChunks = new HashSet<IntVector3>();
foreach (var pos in _dirtyChunks) {
// Chunks currently loading are still dirty
if (_loadingChunks.ContainsKey(pos)) {
newDirtyChunks.Add(pos);
continue;
}
if (!_chunks.ContainsKey(pos))
continue;
ReloadChunk(pos, 0);
}
_dirtyChunks = newDirtyChunks;
}
void LoadNewChunks() {
var chunkPriorityPairsToLoad = new List<KeyValuePair<int, IntVector3>>();
foreach (var viewer in viewers) {
var viewerPosition = WorldToChunkPosition(viewer.transform.position);
for (int x = viewerPosition.x - chunkLoadRangeXZ; x <= viewerPosition.x + chunkLoadRangeXZ; ++x) {
for (int y = viewerPosition.y - chunkLoadRangeY; y <= viewerPosition.y + chunkLoadRangeY; ++y) {
for (int z = viewerPosition.z - chunkLoadRangeXZ; z <= viewerPosition.z + chunkLoadRangeXZ; ++z) {
var chunkPosition = new IntVector3(x, y, z);
_positionAgeMap[chunkPosition] = Time.time;
var chunkLoadedOrLoading = _chunks.ContainsKey(chunkPosition)
|| _loadingChunks.ContainsKey(chunkPosition);
if (chunkLoadedOrLoading)
continue;
var priority = (int)((viewerPosition - chunkPosition).magnitude * 10f) + 1;
chunkPriorityPairsToLoad.Add(new KeyValuePair<int, IntVector3>(priority, chunkPosition));
}
}
}
}
foreach (var pair in chunkPriorityPairsToLoad.OrderBy(p => p.Key)) {
LoadChunk(pair.Value, pair.Key);
}
}
public void LoadChunk(IntVector3 chunkPosition, int priority) {
Assert.IsTrue(!_loadingChunks.ContainsKey(chunkPosition));
var request = _chunkProvider.Load(chunkPosition, priority);
_loadingChunks.Add(chunkPosition, request);
}
public void ReloadChunk(IntVector3 chunkPosition, int priority) {
var chunk = _chunks[chunkPosition];
var request = _chunkProvider.Remesh(chunkPosition, chunk, priority);
_loadingChunks.Add(chunkPosition, request);
}
void UnloadOldChunks() {
while (_chunks.Keys.Count > chunkMaxLoaded) {
IntVector3? oldestChunkPosition = null;
float oldestChunkAge = float.MaxValue;
foreach (var pair in _positionAgeMap) {
if (pair.Value < oldestChunkAge) {
oldestChunkPosition = pair.Key;
oldestChunkAge = pair.Value;
}
}
if (!oldestChunkPosition.HasValue)
break;
GameObject.Destroy(_chunks[oldestChunkPosition.Value].gameObject);
_dirtyChunks.Remove(oldestChunkPosition.Value);
_chunks.Remove(oldestChunkPosition.Value);
_positionAgeMap.Remove(oldestChunkPosition.Value);
break; // One per frame
}
}
void UpdatePendingChunks() {
var loaded = new List<IntVector3>();
foreach (var pair in _loadingChunks) {
var request = pair.Value;
if (!request.done)
continue;
var chunk = _chunkProvider.GetResult(request);
var chunkGO = chunk.gameObject;
chunkGO.transform.parent = transform;
var meshRenderer = chunkGO.GetComponent<MeshRenderer>();
meshRenderer.material = chunkMaterial;
loaded.Add(pair.Key);
_chunks[pair.Key] = chunk;
_positionAgeMap[pair.Key] = Time.time;
break; // One per frame
}
foreach (var pos in loaded) {
_loadingChunks.Remove(pos);
}
}
}
}