2016-08-16 84 views
2

我有一個簡單的問題。我怎樣才能移動一個線性陣列的三維? 這似乎太工作,但在X & Y軸我有索引問題。 爲什麼我想要做到這一點很簡單。我想創建一個塊緩衝區的體積地形,所以我只需要在視口移動時重新計算邊緣的值。陣列移位/錯誤索引/ i = [x + y * size + z * size * size]

我看了一下這個系統的文章:

本質上它們提供了一種方式,通過一個固定大小的多分辨率緩存滾動一個潛在的無限數據 場。

所以我對生成部分pipline是:

  1. 當視移動獲得軸
  2. 移軸
  3. 僅生成新的細胞一些噪音
  4. 三角測量新cells
  5. 更新所有單元格位置

enter image description here

這裏是我的其他圖片: http://forum.unity3d.com/threads/array-shifting-wrong-index-i-x-y-size-z-size-size.425448/#post-2751774

沒有人在統一論壇可以回答我的問題...

public int size; 
public float speed; 

private byte[] volume; 
private byte[] shifted; 

public bool widthShift, heightShift, depthShift; 

private int widthOffset = 0; 
private int heightOffset = 0; 
private int depthOffset = 0; 

private float time = 0; 
private int cube; 

void Start() 
{ 
    volume = new byte[size * size * size]; 
    shifted = new byte[size * size * size]; 

    cube = size * size * size; 

    for (int x = 0; x < size; x++) 
     for (int y = 0; y < size; y++) 
      for(int z = 0; z < size; z++) 
       volume[x + y * size + z * size * size] = (x == 0 || y == 0 || z == 0) ? (byte)1 : (byte)0; 
} 

void Update() 
{ 
    time += Time.fixedDeltaTime * speed; 

    if (time > 1) 
    { 
     time = 0; 

     widthOffset = (widthOffset >= size) ? 0 : widthOffset; 
     heightOffset = (heightOffset >= size) ? 0 : heightOffset; 
     depthOffset = (depthOffset >= size) ? 0 : depthOffset; 

     if (widthShift) 
      widthOffset++; 
     else 
      widthOffset = 0; 

     if (heightShift) 
      heightOffset++; 
     else 
      heightOffset = 0; 

     if (depthShift) 
      depthOffset++; 
     else 
      depthOffset = 0; 

     Shift(widthOffset, heightOffset, depthOffset); 
    } 
} 

void Shift(int xOff, int yOff, int zOff) 
{ 
    for (int x = 0; x < size; x++) 
     for (int y = 0; y < size; y++) 
      for(int z = 0; z < size; z++) 
      { 
       int i = ((x + xOff) + (y + yOff) * size + (z + zOff) * size * size); 
       i = (i >= cube) ? (i - cube) : i; 

       shifted[x + y * size + z * size * size] = volume[i]; 
      } 
} 

void OnDrawGizmos() 
{ 
    if(Application.isPlaying) 
     for(int x = 0; x < size; x++) 
      for(int y = 0; y < size; y++) 
       for(int z = 0; z < size; z++) 
       { 
        Gizmos.color = (shifted[x + y * size + z * size * size] == 1) ? new Color32(0, 255, 0, 255) : new Color32(255, 0, 0, 4); 
        Gizmos.DrawWireCube(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f), new Vector3(0.95f, 0.95f, 0.95f)); 
       } 
} 
+0

我得說,對於新用戶來說,這個問題真的是我們將會格式化並詢問。這並不是我在瀏覽審閱隊列時經常看到的內容。 :)歡迎來到堆棧溢出! –

回答

2

試試看:

void Shift(int xOff, int yOff, int zOff) 
{ 
    for (int x = 0; x < size; x++) 
     for (int y = 0; y < size; y++) 
      for(int z = 0; z < size; z++) 
      { 
       int nx = (x + xOff) % size; 
       int ny = (y + yOff) % size; 
       int nz = (z + zOff) % size; 
       int i = (nx + ny * size + nz * size * size); 

       shifted[x + y * size + z * size * size] = volume[i]; 
      } 
} 
+0

工程就像一個魅力! – Michael

+1

@Michael如果確實如此,請將其標記爲已接受的答案 – galenus

+0

非常酷! – Fattie