为什么这个功能这么慢?

时间:2012-03-03 19:20:37

标签: c# xna

我似乎无法弄清楚为什么它如此缓慢所以我希望别人可能知道:/。 chunk_vertices和chunk_indices是列表。

    public void get_cube_at_position(int x, int y, int z,Color colour)
    {
        int length;
        if (y > y_size - 2)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);

        }
        else if (blocks[x, y + 1, z] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);

        }
        if ( y != 0 && blocks[x, y - 1, z] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);
        }
        if (x > x_size - 2)
        {
        }
        else if (blocks[x + 1, y, z] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, 1 + y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, 1 + y, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);
        }
        if (x != 0 && blocks[x - 1, y, z] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);
        }
        if (z > z_size - 2)
        {
        }
        else if (blocks[x, y, z + 1] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, 1 + z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, 1 + z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);

        }
        if (z != 0 && blocks[x, y, z - 1] == 0)
        {
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z), colour));
            chunk_vertices.Add(new VertexPositionColor(new Vector3(1 + x, y + 1, z), colour));
            length = chunk_vertices.Count - 4;
            chunk_indices.Add(0 + length);
            chunk_indices.Add(1 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(3 + length);
            chunk_indices.Add(2 + length);
            chunk_indices.Add(1 + length);
        }
    }

1 个答案:

答案 0 :(得分:1)

中没有任何东西该方法本来就是一个缓慢的操作。

你可以采取一些微观优化措施来削减一些时间。但没什么大不了的。

您可能对列表外部执行的操作这种方法可能会导致Add方法比可能慢得多(比如定期使用new重新创建它们)。

但真正的问题可能是你每秒只调用150000次(正如你在评论中提到的那样)。你应该总是提到这样的东西 - 因为在一个环境中快速的东西在另一个环境中可能会很慢。

此外,假设您正在将此方法生成的数据加载到GPU上 - 可能是将其复制到新阵列 - 每帧至少一次。这可能是经常发生的,也是速度损失的真正原因。

您可能应该做的是在加载时生成所有顶点/索引数据,然后仅在绘制时引用它。

或者,如果您需要在游戏运行时修改数据,请找出不经常调用此方法的方法,并在执行操作时触及较少的数据(例如:仅在必要时重新生成,仅重新生成修改后的数据等)