gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:21:59 27 4
gpt4 key购买 nike

我有一个简单的问题。如何在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com