3

使用下面的代码,1 个最大化网格以 60 FPS 绘制,2 个最大化网格以 33~ FPS 绘制,3 个最大化网格以 28~ FPS 绘制,4 个最大化网格以 20~ FPS 绘制。我做错了什么,还是我达到了某种极限?我似乎没有画很多多边形,但我还是编程新手,所以我不太了解。请提供一些效率建议。谢谢你。

class PolygonManager
{
    List<List<VertexPositionColor>> vertices;
    VertexBuffer vertexBuffer;
    List<List<int>> indices;
    IndexBuffer indexBuffer;
    int meshRef;
    int indexRef;
    Random random;

    public PolygonManager()
    {
        vertices = new List<List<VertexPositionColor>>();
        vertices.Add(new List<VertexPositionColor>());
        indices = new List<List<int>>();
        indices.Add(new List<int>());
        meshRef = -1;
        indexRef = 0;
        random = new Random();
    }

    public void CreateMesh(int length, int width, Vector3 position, Color color)
    {
        meshRef = -1;
        indexRef = 0;
        for (int i = 0; i < vertices.Count; i++)
        {
            if (vertices[i].Count <= 65536 - (length * width))
                meshRef = i;
        }

        if (meshRef == -1)
        {
            vertices.Add(new List<VertexPositionColor>());
            indices.Add(new List<int>());
            meshRef = vertices.Count - 1;
        }

        indexRef = vertices[meshRef].Count;

        for (int y = 0; y < length; y++)
        {
            for (int x = 0; x < width; x++)
            {
                vertices[meshRef].Add(new VertexPositionColor(new Vector3(x, 0, y) + position,
                                      new Color(color.R + (random.Next(-10, 10) / 100), color.G + (random.Next(-10, 10) / 100), color.B + (random.Next(-10, 10) / 100))));
            }
        }

        for (int y = 0; y < length - 1; y++)
        {
            for (int x = 0; x < width - 1; x++)
            {
                int topLeft = x + y * width;
                int topRight = (x + 1) + y * width;
                int lowerLeft = x + (y + 1) * width;
                int lowerRight = (x + 1) + (y + 1) * width;

                indices[meshRef].Add(topLeft + indexRef);
                indices[meshRef].Add(lowerRight + indexRef);
                indices[meshRef].Add(lowerLeft + indexRef);

                indices[meshRef].Add(topLeft + indexRef);
                indices[meshRef].Add(topRight + indexRef);
                indices[meshRef].Add(lowerRight + indexRef);
            }
        }
    }

    public void Draw(GraphicsDevice graphicsDevice, BasicEffect basicEffect)
    {
        for (int v = 0; v < vertices.Count; v++)
        {
            vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), vertices[v].Count, BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionColor>(vertices[v].ToArray());
            graphicsDevice.SetVertexBuffer(vertexBuffer);
            indexBuffer = new IndexBuffer(graphicsDevice, typeof(int), indices[v].Count, BufferUsage.WriteOnly);
            indexBuffer.SetData<int>(indices[v].ToArray());
            graphicsDevice.Indices = indexBuffer;
            foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
            {
                effectPass.Apply();
                for (int i = 0; i < 6; i++)
                {
                    graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices[v].Count, 0, indices[v].Count/3);
                }
            }
        }
    }
}
4

1 回答 1

4

移动初始化缓冲区的代码并将数据写入 draw 方法之外应该会显着提高性能。

创建顶点和索引缓冲区是一项昂贵的操作。对于静态网格(顶点不变),您可以重用缓冲区。

如果顶点/索引经常更改(每帧一次),请使用动态缓冲区。

于 2013-02-26T20:53:00.843 回答