I am trying to generate a mesh from a chunk like in minecraft which I “think” I got working like so.
[BurstCompile]
public struct MeshWriter : IJobParallelFor
{
[ReadOnly] public NativeParallelHashMap<int4, Node> nodes;
[ReadOnly] public int x, y, z, scale;
public NativeStream.Writer verts;
public NativeStream.Writer tris;
public NativeStream.Writer uvs;
public void Execute(int y2)
{
verts.BeginForEachIndex(y2);
tris.BeginForEachIndex(y2);
uvs.BeginForEachIndex(y2);
int index = 0;
for(int x2 = 0; x2 < scale; x2++) {
for (int z2 = 0; z2 < scale; z2++) {
nodes.TryGetValue(new(x + x2, y + y2, z + z2, 0), out Node node);
Node.Build(ref this, ref node, ref index);
}
}
verts.EndForEachIndex();
tris.EndForEachIndex();
uvs.EndForEachIndex();
}
}
Now I am trying to read from the triangle stream and realign the indices.
[BurstCompile]
public struct MeshReader : IJob
{
public int scale;
public NativeStream.Reader verts;
public NativeStream.Reader tris;
//[WriteOnly] NativeList<int> faces;
public void Execute()
{
for(int i = 0; i < scale; i++)
{
verts.BeginForEachIndex(i);
tris.BeginForEachIndex(i);
for(var v = 0; v < tris.Count(); v++) {
Debug.Log(tris.Read<int>());
}
verts.EndForEachIndex();
tris.EndForEachIndex();
}
}
}
But I don’t know how to do it.
The NativeStream has buffers right? I thought I could assign each Y lvl of the chunk to one buffer and after it’s all populated I could read it back but now looking at the NativeStream.Reader I can’t figure out the syntax for that. It looks like to read it you have to read it all at once instead of each buffer? Or do I need to use IJobParallelFor again and set the buffer in the Execute(int index)? If that’s the case then how do I read how many items are in the buffer?