首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >数组移位/错误索引/i= [x+y*size+z*size*size]

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

Stack Overflow用户
提问于 2016-08-16 16:09:06
回答 1查看 91关注 0票数 2

我有个简单的问题。我如何在三维空间中移动一个线阵?​,它看起来太工作了,但是在X&Y轴上,我遇到了一个索引问题。我想这么做的原因很简单。我想要创建一个具有块缓冲区的体积地形,所以我只需要在视图移动时重新计算边沿上的值。

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

本质上,它们提供了一种在固定大小的多分辨率缓存中滚动潜在无限数据字段的方法。

所以我对代名词的看法是:

  1. 当视口移动时得到轴
  2. 移动轴
  3. 只为新的细胞产生一些噪音
  4. 三角化新细胞
  5. 更新所有单元格位置

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

论坛上没有人能回答我的问题.

代码语言:javascript
运行
复制
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));
                }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-16 16:15:42

试试看:

代码语言:javascript
运行
复制
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];
            }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38979685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档