Skip to content

Commit 88ecdf2

Browse files
committed
Changed BlockArray1D to a NativeArray<byte>
1 parent bcf2620 commit 88ecdf2

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Assets/Voxels/Scripts/Chunk.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4-
4+
using Unity.Collections;
55
using UnityEngine;
66
using Voxels.Scripts.Dispatcher;
77
using Voxels.Scripts.Utils;
@@ -23,15 +23,15 @@ public enum Direction { RIGHT = 0, LEFT = 1, FORWARD = 2, BACK = 3, UP = 4, DOWN
2323
public GameObject chunkObj;
2424
public Vector3Int chunkPos;
2525

26-
//public byte[,,] blockArray; // these bytes should reference different blocks. If they are 0 it is air. A byte goes to 0-255
27-
public byte[] blockArray1D;
26+
public NativeArray<byte> blockArray1D = new NativeArray<byte>(CHUNK_WIDTH * CHUNK_LENGTH * CHUNK_HEIGHT, Allocator.Persistent);
2827
// x + (CHUNK_WIDTH * z) + (CHUNK_WIDTH * CHUNK_LENGTH * y)
2928

3029
public VoxelManager voxelManager = new VoxelManager();
3130
public bool isGenerated = false;
3231

3332
private bool isDirty = false;
3433
private bool isMeshing = true;
34+
//public bool isEmpty = true;
3535

3636
public void MarkDirty()
3737
{
@@ -43,6 +43,7 @@ public void MarkDirty()
4343

4444
public void Remesh()
4545
{
46+
4647
if (!IsDirty || isMeshing) return;
4748
isMeshing = true; // Prevent trying to remesh while already meshing
4849
isDirty = false;
@@ -67,7 +68,7 @@ public Chunk(Vector3 chunkPos)
6768
this.chunkObj.GetComponent<Renderer>().material = Generation.instance.terrainMat;
6869
this.chunkObj.transform.position = this.chunkPos;
6970

70-
blockArray1D = new byte[CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_LENGTH];
71+
//blockArray1D = new byte[CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_LENGTH];
7172

7273
chunks.TryAdd(this.chunkPos, this);
7374

@@ -76,6 +77,8 @@ public Chunk(Vector3 chunkPos)
7677
// a task spawns another task or other async action this task will complete before the work is done and another
7778
// task will be run from the queue, accepting the completion action will delay the que from starting another
7879
// task until we want it to.
80+
81+
7982
AsyncHelper.QueueTask(complete =>
8083
{
8184
var generationEntry = Performance.Begin(Performance.ChunkGeneration);
@@ -87,6 +90,9 @@ public Chunk(Vector3 chunkPos)
8790
// make sure there's an object with MainThreadDispatcher component in the scene and submit work to it as so
8891
AsyncHelper.RunOnMainThread(() =>
8992
{
93+
// For some reason, the following doesn't work
94+
//if(isEmpty) { return };
95+
9096
var greedyEntry = Performance.Begin(Performance.ChunkGreedyMeshing);
9197
VoxelManager.GreedyMeshResult result = voxelManager.GreedyMesh(this);
9298

@@ -129,7 +135,6 @@ public void GenerateChunk()
129135
}
130136
public void SetBlockArray()
131137
{
132-
133138
float blockSize = Generation.BLOCK_SIZE;
134139

135140
for(int i = 0; i < CHUNK_WIDTH * CHUNK_LENGTH; i++)
@@ -157,6 +162,7 @@ public void SetBlockArray()
157162
if (slope > 1f)
158163
{
159164
blockArray1D[CalculateBlockIndex(x, y, z)] = (byte)(Generation.instance.stoneBlock.block_ID + 1);
165+
160166
}
161167
else
162168
{
@@ -177,7 +183,7 @@ public void SetBlockArray()
177183

178184
}
179185
}
180-
186+
//isEmpty = false;
181187
}
182188
}
183189

0 commit comments

Comments
 (0)