c# - 数组移位/错误索引/i = [x+y*size+z*size*size]

标签 c# arrays unity-game-engine

我有一个简单的问题。如何在 3 维中移动线性数组?​ 这似乎太有效了,但在 X 和 Y 轴上我遇到了索引问题。 我想这样做的原因很简单。我想创建一个带有 block 缓冲区的体积地形,因此我只需在视口(viewport)移动时重新计算边缘上的值。

我读过一篇关于该系统的文章:

Essentially they provide a way to scroll a potentially infinite data field through a fixed size multi-resolution cache.

所以我的生成部分的管道是:

  1. 当视口(viewport)移动时获取轴
  2. 移动轴
  3. 仅为新细胞生成一些噪音
  4. 对新单元格进行三角测量
  5. 更新所有单元格位置

enter image description here

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

unity 论坛中没有人可以回答我的问题...

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));
                }
}

最佳答案

尝试一下:

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];
            }
}

关于c# - 数组移位/错误索引/i = [x+y*size+z*size*size],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38979685/

相关文章:

unity-game-engine - 我的 Canvas 因遮挡而消失

android - 如何从基于 Unity 的应用程序获取数据到第三方 Android 应用程序?

c# - 在 Windows Phone 8.1 中使用什么进行 Paypal 付款

c# - 从 BackgroundWorker 转向日志类的 TPL

arrays - Julia:大型数组的有限打印

c# - LINQ - 按多个属性对列表进行分组并返回带有数组成员的对象

c# - Win7的Virtual Machines文件夹如何获取?

c# - 从 UWP XAML 中的 HyperlinkBut​​ton 中删除下划线

java - 在 Java 中比较数组

animation - Unity 动画在预览中播放,但在玩游戏时不播放