Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created June 16, 2020 21:31
Show Gist options
  • Save jeffvella/82412e91135df041cc8e836bd22cd505 to your computer and use it in GitHub Desktop.
Save jeffvella/82412e91135df041cc8e836bd22cd505 to your computer and use it in GitHub Desktop.
using Unity.Mathematics;
namespace Unity.Collections
{
public static class SpatialIndexingUtility
{
public static int3 Get3DIndices(int idx, int3 size)
{
int yzLength = size.y * size.z;
int x = idx / (yzLength);
idx -= (x * yzLength);
int y = idx / size.z;
int z = idx % size.z;
return new int3(x, y, z);
}
public static int GetIndex(int x, int y, int z, int3 size)
{
return x * (size.y * size.z) * size.z + z;
}
public static int GetIndex(int2 indices, int3 size)
{
return indices.x * (size.y * size.z) + 0 * size.z + indices.y;
}
public static int GetIndex(int3 indices, int3 size)
{
return indices.x * (size.y * size.z) + indices.y * size.z + indices.z;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment