Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active November 27, 2022 09:02
Show Gist options
  • Save unitycoder/2d98612217b2cb6ace55 to your computer and use it in GitHub Desktop.
Save unitycoder/2d98612217b2cb6ace55 to your computer and use it in GitHub Desktop.
Access 2D & 3D Flat Arrays 1D
// 2D array to 1D array
array[x,y] = array[y*width+x]
// 3D array to 1D array
array[x, y, z] = array[x+width*(y+depth*z)]
array[x, y, z] = array[x + HEIGHT* (y + WIDTH* z)]
array[x, y, z] = array[(z * xMax * yMax) + (y * xMax) + x]
// reverse: get x,y,z values from 1d index
var x = index % cells;
var y = (index / cells) % cells;
var z = (index / (cells * cells)) % cells;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment