我最近才开始使用 XNA,现在我面临一个初学者问题,性能。我绘制的对象有自己的顶点缓冲区,所以当我有大约 50k 个对象时,fps 会急剧下降(从 60 到 5-12)。
我得到了我应该将我的顶点合并成块的提示,但我不知道该怎么做..
如果我能通过代码示例获得任何帮助,我将不胜感激。
编辑:这是我在 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);