diff --git a/world.js b/world.js index 5cae85c..4ec2777 100644 --- a/world.js +++ b/world.js @@ -114,23 +114,20 @@ export function blockLookup(world, x, y, z) { }; } -function makeChunkBuffer(gl, data, z0, x0, lookup) { - const sideNeighbors = (bi, i, j, k) => { - if (i === 0 || j === 0 || i === 15 || j === 15) { - return [ - { block: lookup(i - 1, j, k), dir: '-z', faceCenter: [x0 + j, k, z0 + i - 0.5] }, - { block: lookup(i + 1, j, k), dir: '+z', faceCenter: [x0 + j, k, z0 + i + 0.5] }, - { block: lookup(i, j - 1, k), dir: '-x', faceCenter: [x0 + j - 0.5, k, z0 + i] }, - { block: lookup(i, j + 1, k), dir: '+x', faceCenter: [x0 + j + 0.5, k, z0 + i] }, - ]; +function makeChunkBuffer(gl, data, z0, x0, blockLookup) { + const lookup = (i, j, k) => { + if (i < 0 || j < 0 || i > 15 || j > 15) { + return blockLookup(i, j, k); } - return [ - { block: data[bi - 256 * 16], dir: '-z', faceCenter: [x0 + j, k, z0 + i - 0.5] }, - { block: data[bi + 256 * 16], dir: '+z', faceCenter: [x0 + j, k, z0 + i + 0.5] }, - { block: data[bi - 256], dir: '-x', faceCenter: [x0 + j - 0.5, k, z0 + i] }, - { block: data[bi + 256], dir: '+x', faceCenter: [x0 + j + 0.5, k, z0 + i] }, - ]; + return data[256*(16*i + j) + k]; }; + const sideNeighbors = (i, j, k) => + [ + { block: lookup(i - 1, j, k), dir: '-z', faceCenter: [x0 + j, k, z0 + i - 0.5] }, + { block: lookup(i + 1, j, k), dir: '+z', faceCenter: [x0 + j, k, z0 + i + 0.5] }, + { block: lookup(i, j - 1, k), dir: '-x', faceCenter: [x0 + j - 0.5, k, z0 + i] }, + { block: lookup(i, j + 1, k), dir: '+x', faceCenter: [x0 + j + 0.5, k, z0 + i] }, + ]; const faces = []; @@ -159,7 +156,7 @@ function makeChunkBuffer(gl, data, z0, x0, lookup) { } })(); - for (let {block, dir, faceCenter} of sideNeighbors(bi, i, j, k)) { + for (let {block, dir, faceCenter} of sideNeighbors(i, j, k)) { if (block === BlockType.AIR) { faces.push(makeFace(dir, sideTexture, faceCenter)); }