XNA初学者:如何将顶点数组合并到单个顶点缓冲区中

时间:2011-11-26 16:48:05

标签: c# xna vertices

我刚刚开始使用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);

1 个答案:

答案 0 :(得分:1)

要合并它们,你必须有类似的东西。

int NumVerts = Objects.Sum(o => o.Vertex.Length);
int NumIndexes = Objects.Sum(o => o.Index.Length);

VertexPositionColor[] Vertex = new VertexPositionColor[NumVerts];
int[] Index = new int[NumIndexes];

int VertexOffset = 0;
int IndexOffset = 0;
foreach (Object object in Objects)
{
    for (int v=0; v<object.Vertex.Length; v++)
    {
        Vertex[VertexOffset+v] = object.Vertex[v] + VertexOffset;
    }

    for (int i=0; i<object.Index.Length; i++)
    {
        Index[IndexOffset+i] = object.Index[i] + VertexOffset;
    }

    VertexOffset += object.Vertex.Length;
    IdnexOffset += object.Index.Length;
}