XNA 初学者:如何将顶点数组合并到单个顶点缓冲区中
我最近刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要合并它们,您必须执行与此类似的操作。
To merge them you have to something similar to this.