Created
June 16, 2020 21:31
-
-
Save jeffvella/82412e91135df041cc8e836bd22cd505 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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