perf: call blockLokup() less often

This commit is contained in:
Paul Mathieu 2021-12-16 14:10:57 -08:00
parent 6cf6306b96
commit 963568bec1

View File

@ -114,23 +114,20 @@ export function blockLookup(world, x, y, z) {
}; };
} }
function makeChunkBuffer(gl, data, z0, x0, lookup) { function makeChunkBuffer(gl, data, z0, x0, blockLookup) {
const sideNeighbors = (bi, i, j, k) => { const lookup = (i, j, k) => {
if (i === 0 || j === 0 || i === 15 || j === 15) { if (i < 0 || j < 0 || i > 15 || j > 15) {
return [ return blockLookup(i, j, k);
}
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 + 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] },
{ 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] },
]; ];
}
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] },
];
};
const faces = []; 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) { if (block === BlockType.AIR) {
faces.push(makeFace(dir, sideTexture, faceCenter)); faces.push(makeFace(dir, sideTexture, faceCenter));
} }