I’ve just recently started using XNA and now I face a beginners problem, performance. The objects I draw has their own vertex buffer, so when I have ~50k objects, the fps goes down dramatically (from 60 to 5-12).
I got the tip that I should merge my vertices into chunks, but I don’t know how to do that..
Would appreciate any help I can get with code examples.
Edit: This is the code I came up with, with help from Blau
var cubes = newChunk.Where(c => c != null && !badIndex.Contains(c.BlockType));
VertexPositionColorTextureNormal[] verts = new VertexPositionColorTextureNormal[cubes.Sum(c => c.Vertices.Count)];
int VertexOffset = 0;
var inTheRightOrder = cubes;
foreach (var cube in inTheRightOrder)
{
var cb = cube.Vertices.ToArray();
for (int v = 0; v < cb.Length; v++)
{
verts[VertexOffset + v] = cb[v];
}
VertexOffset += cb.Length;
}
VertexBuffer newVB = new VertexBuffer(device, VertexPositionColorTextureNormal.VertexDeclaration, verts.Length, BufferUsage.WriteOnly);
newVB.SetData(verts);
var ck = new Cube { Vertices = verts, BoundingBox = BoundingBox.CreateFromPoints(verts.Select(i => i.Position)), Buffer = newVB, Cubes = cubes.Count() };
Cubes.Add(ck);
To merge them you have to something similar to this.