Skip to content

Instantly share code, notes, and snippets.

@mikolalysenko
Created March 7, 2013 18:30
Show Gist options
  • Save mikolalysenko/5110487 to your computer and use it in GitHub Desktop.
Save mikolalysenko/5110487 to your computer and use it in GitHub Desktop.
Sketch of greedy meshing in 3D
var map = getChunkBitmap()
, boxes = []
for(var z=0; z<CHUNK_Z; ++z) {
for(var y=0; y<CHUNK_Y; ++y) {
for(var x=0; x<CHUNK_X; ++x) {
if(map[x,y,z]) {
var w = 0, h = 0, d = 0
//Compute width
while(w+x < CHUNK_X && map[x+w,y,z]) {
++w
}
//Compute height
while(h+y < CHUNK_Y && all(map[x...x+w,h+y,z])) {
++h
}
//Fill cube
while(d+z < CHUNK_Z && all(map[x..x+w,y..h+y,d+z) {
++d
}
//Add box
boxes.push([[x,y,z],[x+w,y+h,z+d]])
//Clear array
map[x..x+w, y..y+h, z..z+d] = 0
}
}
}
}
return boxes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment