how to get the index of x y z | how to get x y z from index inside a 1D array Javascript (web)

Channel:
Subscribers:
513
Published on ● Video Link: https://www.youtube.com/watch?v=ctvMLSTgD90



Duration: 2:25
30 views
0


code:
let chunk_of_data = new Uint8Array(32*32*32)

function getIndex(x, y, z){
return x + y*32 + z*32*32
}

let block1 = getIndex(2, 4, 2);

console.log(block1)

function getPosition(index){
let x = index % 32;
let y = Math.floor( index / 32 ) % 32;
let z = Math.floor( index / 32 / 32)
return {x, y, z}
}
console.log(getPosition(block1))