C# 数组移位/索引错误/i=[x+;y*大小+;z*大小*size]
我有一个简单的问题。如何在三维中移动线性阵列? 这似乎太有用了,但在X&Y轴上,我遇到了索引问题。 我想这样做的原因很简单。我想创建一个具有块缓冲区的体积地形,因此我只需要在视口移动时重新计算边上的值 我读过一篇关于这个系统的文章: 本质上,它们提供了一种滚动潜在无限数据的方法 字段通过固定大小的多分辨率缓存 因此,我对“新一代”部分的建议是:C# 数组移位/索引错误/i=[x+;y*大小+;z*大小*size],c#,arrays,unity3d,C#,Arrays,Unity3d,我有一个简单的问题。如何在三维中移动线性阵列? 这似乎太有用了,但在X&Y轴上,我遇到了索引问题。 我想这样做的原因很简单。我想创建一个具有块缓冲区的体积地形,因此我只需要在视口移动时重新计算边上的值 我读过一篇关于这个系统的文章: 本质上,它们提供了一种滚动潜在无限数据的方法 字段通过固定大小的多分辨率缓存 因此,我对“新一代”部分的建议是: 当视口移动时,获取轴 移动轴 仅为新单元生成一些噪波 对新细胞进行三角剖分 更新所有单元格位置 以下是我的其他图片: 联合论坛中没有人能回答我的问
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));
}
}
public int size;
公众浮标速度;
专用字节[]卷;
私有字节[]移位;
公共边界宽度偏移、高度偏移、深度偏移;
私有int-widthOffset=0;
私有int heightOffset=0;
私有int depthOffset=0;
私有浮动时间=0;
私有整数立方体;
void Start()
{
卷=新字节[大小*大小*大小];
移位=新字节[大小*大小*大小];
立方体=大小*大小*大小;
用于(int x=0;x1)
{
时间=0;
widthOffset=(widthOffset>=尺寸)?0:widthOffset;
heightOffset=(heightOffset>=尺寸)?0:heightOffset;
depthOffset=(depthOffset>=大小)?0:depthOffset;
如果(宽度偏移)
宽度偏移量++;
其他的
宽度偏移=0;
如果(高度偏移)
heightOffset++;
其他的
高度偏移=0;
if(深度移位)
depthOffset++;
其他的
深度偏移=0;
移位(宽度偏移、高度偏移、深度偏移);
}
}
无效移位(int-xOff、int-yOff、int-zOff)
{
用于(int x=0;x=立方体)?(i-立方体):i;
移位的[x+y*大小+z*大小*大小]=体积[i];
}
}
void OnDrawGizmos()
{
if(Application.isPlaying)
用于(int x=0;x
试试看:
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];
}
}
void Shift(int-xOff、int-yOff、int-zOff)
{
用于(int x=0;x
我得说,对于一个新用户来说,这个问题的格式和问法都非常好。我在查看评论队列时不太经常看到这一点。:)欢迎来到堆栈溢出!