optimized faces
This commit is contained in:
39
ChunkMesh.cs
39
ChunkMesh.cs
@@ -1,25 +1,15 @@
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Voxel
|
||||
namespace Voxel
|
||||
{
|
||||
public class ChunkMesh
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
private byte[] _packedData;
|
||||
public bool NeedsUpdate = false;
|
||||
public int Size = 0;
|
||||
private List<FaceData> _faces;
|
||||
|
||||
public ChunkMesh(int x, int y)
|
||||
{
|
||||
_packedData = new byte[0];
|
||||
_packedData = Array.Empty<byte>();
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
@@ -27,18 +17,31 @@ namespace Voxel
|
||||
public void SetFaces(List<FaceData> faces)
|
||||
{
|
||||
Size = faces.Count;
|
||||
_faces = faces;
|
||||
NeedsUpdate = true;
|
||||
_packedData = PackFaces(faces);
|
||||
}
|
||||
|
||||
public byte[] GetPackedData()
|
||||
private static byte[] PackFaces(List<FaceData> faces)
|
||||
{
|
||||
if (NeedsUpdate)
|
||||
const int BYTES_PER_FACE = 4;
|
||||
int totalFaces = faces.Count;
|
||||
var result = new byte[faces.Count * BYTES_PER_FACE];
|
||||
|
||||
for (int i = 0; i < totalFaces; i++)
|
||||
{
|
||||
_packedData = _faces.SelectMany(f => f.Pack()).ToArray();
|
||||
var face = faces[i];
|
||||
uint packed = face._data;
|
||||
int offset = i * 4;
|
||||
|
||||
// Write little-endian (important!)
|
||||
result[offset] = (byte)(packed);
|
||||
result[offset + 1] = (byte)(packed >> 8);
|
||||
result[offset + 2] = (byte)(packed >> 16);
|
||||
result[offset + 3] = (byte)(packed >> 24);
|
||||
}
|
||||
|
||||
return _packedData;
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] GetPackedData() => _packedData;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user