Skip to content

Instantly share code, notes, and snippets.

@kevzettler
Last active May 1, 2017 15:59
Show Gist options
  • Save kevzettler/8c31f915ce78883b88a969b9de1be90c to your computer and use it in GitHub Desktop.
Save kevzettler/8c31f915ce78883b88a969b9de1be90c to your computer and use it in GitHub Desktop.
nested iteration, global palette accumulated from children
var paletteMap = {};
var palette = [];
//Iterate over child voxel meshes
Qbjson.matrixList.map((Mesh) => {
var x = Mesh.sizeX;
var y = Mesh.sizeY;
var z = Mesh.sizeZ;
var n = ndarray(
new Int32Array(x * y * z),
[x, y, z]
);
var paletteIndex;
// Iterate over each voxel in the voxel mesh push the voxel colors to the global palette
Mesh.matrix.forEach(function(voxel){
var r = voxel.r / 255;
var g = voxel.g / 255;
var b = voxel.b / 255;
if(paletteMap[r] === undefined){
paletteMap[r] = {};
}
if(paletteMap[r][g] === undefined){
paletteMap[r][g] = {};
}
if(paletteMap[r][g][b] === undefined){
palette.push([r,g,b, 1.0]);
paletteIndex = palette.length;
paletteMap[r][g][b] = paletteIndex;
}else{
paletteIndex = paletteMap[r][g][b];
}
//Push each voxel coord to the Ndarray
n.set(
voxel.x,
voxel.y,
voxel.z,
paletteIndex
);
});
return {
ndvoxels: n,
offset: [Mesh.posX, Mesh.posY, Mesh.posZ],
name: Mesh.name
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment