2012-03-03 18 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

「ゆっくり」を定義します。何に比べて遅い? –

+0

"とても遅い"を定義します。 – millimoose

+0

OPがゆっくりと定義する方法は重要ですか?コードを見て、可能な最適化を見つけようとしてください。 – Marlon

答えて

1

あなたは方法は本質的に低速運転であることを内をやっている何もありません。

いくつかの時間を削るためにできる最適化はいくつかあります。しかし、何も実質的ではない。あなたのリストにこの方法をやっているかもしれません

物事は潜在的にAdd方法は、それが(のような定期的にnewでそれらを再作成)することができよりかなり遅くなる原因となります。

しかし実際の問題は、(あなたのコメントで言及したように)1秒間に150000回と呼んでいる可能性が高いです。あなたは常にこのようなものを言及するべきです - 一つの文脈で速いものが別の文脈では遅いかもしれないので。

さらに、このメソッドが生成するデータをGPUにロードすることを前提としています。おそらく、フレームごとに少なくとも1回は、新しい配列にコピーすることになります。これはおそらくあまりにも頻繁であり、速度低下の本当の原因です。

あなたがしなければならないことは、読み込み時にすべての頂点/インデックスデータを生成し、描画時にそれを参照するだけです。

ゲームの実行中にデータを変更する必要がある場合は、このメソッドを呼び出す頻度を減らしたり、少ないデータでタッチする方法を見つけてください(たとえば、再生する必要があるときに再生する、変更したデータのみを再生するなど)。 )。

関連する問題