Model 如何在XNA中绘制数以百万计的立方体而无需空闲的模型实例?
我为一个游戏“地雷船”风格的项目工作 我开始使用“模型实例化”来生成大量具有相同模型的多维数据集。 到现在为止,一直都还不错。 我的问题是,如果我将矩阵的大小从[300-1-300](90000立方米)增加到[500-1-500](250000立方米),我的程序会大大减慢。从60帧到20帧 我真的不明白为什么。但我正确地使用了“硬件实例”技术。我还注意到,在论坛上,这种技术允许XNA绘制多达700万立方!! 你知道我的问题出在哪里吗 非常感谢 下面是我绘制实例化模型的函数:Model 如何在XNA中绘制数以百万计的立方体而无需空闲的模型实例?,model,xna,buffer,draw,Model,Xna,Buffer,Draw,我为一个游戏“地雷船”风格的项目工作 我开始使用“模型实例化”来生成大量具有相同模型的多维数据集。 到现在为止,一直都还不错。 我的问题是,如果我将矩阵的大小从[300-1-300](90000立方米)增加到[500-1-500](250000立方米),我的程序会大大减慢。从60帧到20帧 我真的不明白为什么。但我正确地使用了“硬件实例”技术。我还注意到,在论坛上,这种技术允许XNA绘制多达700万立方!! 你知道我的问题出在哪里吗 非常感谢 下面是我绘制实例化模型的函数: // Draw th
// Draw the 3D map (world) of game
public void drawWorld(GameTime gameTime)
{
/ * Draw all the structures that make up the world with the instancing system * /
Array.Resize(ref instanceTransforms, smallListInstance.Count);
for (int i = 0; i < ListInstance.Count; i++)
{
instanceTransforms[i] = ListInstance[i].Transform;
}
DrawModelHardwareInstancing(myModel, myTexture2D,instancedModelBones,instanceTransforms, arcadia.camera.View, arcadia.camera.Projection);
}
// ### end function drawWorld
多谢各位!多亏了你的建议,我终于解决了我的问题 我只画在我视野“照相机”前面的模型。 仅在需要“编辑”、“新位置等”时更新模型 我的最后一步是只画6个立方体面中的3个,以提高性能 编码示例以仅绘制前面的模型 公共边界截头体截头体{get;private set;}
//Initialize
Matrix viewProjection = View * Projection;
Frustum = new BoundingFrustum(viewProjection);
//Update
Matrix viewProjection = View * Projection;
Frustum.Matrix = viewProjection;
//Check instance and make the instanceTransformation for draw
foreach (var object in ListInstance.Where(m => Frustum.Contains(m.BoundingBox) != ContaintmentType.Disjoint)
{
instanceTransforms[index].Add(smallListInstance[i].Transform);
}
// And now draw ...
你真的需要每帧更新你的顶点缓冲区和索引缓冲区吗?如果你想渲染一百万个立方体,你可能不应该在每一帧更新vertexbuffer,因为它们变化不大。这难道不是问题吗?在每一帧中循环许多对象仍然非常耗时。mhh非常有趣!但是你能告诉我在哪里更新我的vertexbuffer吗?你说这个?Game.GraphicsDevice.SetVertexBuffers(新的VertexBufferBinding(meshPart.VertexBuffer,meshPart.VertexOffset,0),新的VertexBufferBinding(instanceVertexBuffer,0,1));Game.GraphicsDevice.index=meshPart.IndexBuffer;我还没有用立方体做过,但我想它应该能用。如果我创建一个1000x1000点的网格,我很容易创建一个vertexbuffer和一个indexbuffer,并确保一个点不会出现两次,然后存储vertexbuffer和indexbuffer,那么我不需要重新创建它,除非它被更改。但在您的情况下,您可能必须支持更改,然后为不同的区域创建多个缓冲区才有意义,因此您只需更新一个vertexbuffer,比如100,而不是在每帧100*1000之间循环。只是个主意,我喜欢你的主意!今晚我会尝试类似的方法,我会回来通知您。您还可以使用Select扩展来进一步改进:foreach(ListInstance.Where中的var对象(m=>Frustum.Contains(m.BoundingBox)!=containmentType.Disjoint)例如,通过这种方式,您只循环遍历所有需要的对象,而不是跳过一些不需要的对象。它将只循环遍历位于平截头体视图中的对象。
//Initialize
Matrix viewProjection = View * Projection;
Frustum = new BoundingFrustum(viewProjection);
//Update
Matrix viewProjection = View * Projection;
Frustum.Matrix = viewProjection;
//Check instance and make the instanceTransformation for draw
foreach (var object in ListInstance.Where(m => Frustum.Contains(m.BoundingBox) != ContaintmentType.Disjoint)
{
instanceTransforms[index].Add(smallListInstance[i].Transform);
}
// And now draw ...