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

发布于 2024-12-18 07:21:57 字数 1020 浏览 2 评论 0原文

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装爱人 2024-12-25 07:21:57

要合并它们,您必须执行与此类似的操作。

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;
}

To merge them you have to something similar to this.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文