2012-03-03 296 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上 - 可能通過將其複製到新陣列 - 每幀至少一次。這可能是過於頻繁,並且是速度損失的真正原因。

您可能應該做的是在加載時生成所有頂點/索引數據,然後僅在繪製時引用它。或者,如果您需要在遊戲運行時修改數據,請設法減少調用此方法的次數,並在您執行時少觸及數據(例如:僅在需要時才重新生成,僅重新生成修改後的數據等等)。