Skip to content

Commit 32beab7

Browse files
committed
Made it so only chunks containing at least one voxel are greedymeshed
1 parent 2aee803 commit 32beab7

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

Assets/Voxels/Scripts/Chunk.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public enum Direction { RIGHT = 0, LEFT = 1, FORWARD = 2, BACK = 3, UP = 4, DOWN
3131

3232
private bool isDirty = false;
3333
private bool isMeshing = true;
34-
//public bool isEmpty = true;
34+
private bool containsVoxel = false;
3535

3636
public void MarkDirty()
3737
{
@@ -40,11 +40,12 @@ public void MarkDirty()
4040
}
4141

4242
public bool IsDirty => isDirty;
43+
public bool ContainsVoxel => containsVoxel;
4344

4445
public void Remesh()
4546
{
4647

47-
if (!IsDirty || isMeshing) return;
48+
if (!IsDirty || isMeshing || !ContainsVoxel) return;
4849
isMeshing = true; // Prevent trying to remesh while already meshing
4950
isDirty = false;
5051
var greedyEntry = Performance.Begin(Performance.ChunkGreedyMeshing);
@@ -68,17 +69,15 @@ public Chunk(Vector3 chunkPos)
6869
this.chunkObj.GetComponent<Renderer>().material = Generation.instance.terrainMat;
6970
this.chunkObj.transform.position = this.chunkPos;
7071

71-
//blockArray1D = new byte[CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_LENGTH];
72-
7372
chunks.TryAdd(this.chunkPos, this);
7473

7574
// Accepting an action in the queued task allows us to manually notify the AsyncHelpder when this thread should
7675
// be considered done. QueueTask tries to ensure only a certain number of tasks are running at a given time, if
7776
// a task spawns another task or other async action this task will complete before the work is done and another
78-
// task will be run from the queue, accepting the completion action will delay the que from starting another
77+
// task will be run from the queue, accepting the completion action will delay the queue from starting another
7978
// task until we want it to.
8079

81-
80+
// NOTE: calling complete() is necessary for the task to offically end
8281
AsyncHelper.QueueTask(complete =>
8382
{
8483
var generationEntry = Performance.Begin(Performance.ChunkGeneration);
@@ -90,8 +89,14 @@ public Chunk(Vector3 chunkPos)
9089
// make sure there's an object with MainThreadDispatcher component in the scene and submit work to it as so
9190
AsyncHelper.RunOnMainThread(() =>
9291
{
93-
// For some reason, the following doesn't work
94-
//if(isEmpty) { return };
92+
// Only greedy mesh chunks with at least one voxel
93+
if (!ContainsVoxel)
94+
{
95+
//Debug.Log("chunk does not contain any voxels");
96+
isMeshing = false;
97+
complete();
98+
return;
99+
}
95100

96101
var greedyEntry = Performance.Begin(Performance.ChunkGreedyMeshing);
97102
VoxelManager.GreedyMeshResult result = voxelManager.GreedyMesh(this);
@@ -187,7 +192,8 @@ public void SetBlockArray()
187192

188193
}
189194
}
190-
//isEmpty = false;
195+
196+
containsVoxel = true;
191197
}
192198
}
193199

Assets/Voxels/Scripts/VoxelManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public void Cleanup()
226226

227227
public GreedyMeshResult GreedyMesh(Chunk chunk)
228228
{
229+
229230
// We want to take a 2D cross-section of our voxels; a 2D representation of each "layer" on each axis
230231
// Then, we want to use our greedy meshing algorthim on that cross-section
231232

0 commit comments

Comments
 (0)