2012-03-03 140 views
0

我似乎无法弄清楚为什么它如此之慢,所以我希望别人可能知道:/。 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); 
     } 
    } 
+0

定义“慢”。比较慢什么? – 2012-03-03 19:21:58

+0

定义“这么慢”。 – millimoose 2012-03-03 19:22:18

+0

OP是如何定义缓慢的?只需查看代码并尝试查找可能的优化。 – Marlon 2012-03-03 19:23:27

回答

1

没有什么本质上是一个缓慢的操作。

你可以做一些微观优化来削减一些时间。但没有实质性的。

事情你可能会做您的名单这种方法有可能导致Add方法是相当慢于它可能是(像定期与new重新创建它们)。

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

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

您可能应该做的是在加载时生成所有顶点/索引数据,然后仅在绘制时引用它。或者,如果您需要在游戏运行时修改数据,请设法减少调用此方法的次数,并在您执行时少触及数据(例如:仅在需要时才重新生成,仅重新生成修改后的数据等等)。