Small refactor in face creation
This commit is contained in:
parent
c5370bf057
commit
61d3d67df0
73
world.js
73
world.js
@ -279,7 +279,7 @@ class ChunkMap {
|
|||||||
|
|
||||||
/** Makes a brave new (empty) world */
|
/** Makes a brave new (empty) world */
|
||||||
export function makeWorld() {
|
export function makeWorld() {
|
||||||
return {chunks: [], chunkMap: new ChunkMap()};
|
return {chunkMap: new ChunkMap()};
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeFaces(chunk, blocks, neighbors) {
|
function makeFaces(chunk, blocks, neighbors) {
|
||||||
@ -404,7 +404,6 @@ export function generateMissingChunks(world, z, x, timeLimit = 10000) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const chunk = makeChunk(i * 16, j * 16);
|
const chunk = makeChunk(i * 16, j * 16);
|
||||||
world.chunks.push(chunk);
|
|
||||||
world.chunkMap.set(i, j, chunk);
|
world.chunkMap.set(i, j, chunk);
|
||||||
|
|
||||||
updateMissingFaces(world, chunk);
|
updateMissingFaces(world, chunk);
|
||||||
@ -414,27 +413,6 @@ export function generateMissingChunks(world, z, x, timeLimit = 10000) {
|
|||||||
return world;
|
return world;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addFace(chunk, block, dir) {
|
|
||||||
const face = createChunkFace(block, dir);
|
|
||||||
if (isTransparent(block.type)) {
|
|
||||||
chunk.transparentFaces.push(face);
|
|
||||||
} else {
|
|
||||||
chunk.faces.push(face);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createChunkFace(block, dir, center = null) {
|
|
||||||
center = center ?? faceCenter(block.centerPosition, dir);
|
|
||||||
if (block.type === BlockType.WATER && dir === '+y') {
|
|
||||||
center[1] -= 0.15;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
dir,
|
|
||||||
blockIndex: block.blockIndex,
|
|
||||||
face: makeFace(dir, faceTexture(block.type, dir), faceCenter(block.centerPosition, dir)),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Generates geometry for all visible chunks. */
|
/** Generates geometry for all visible chunks. */
|
||||||
export function updateWorldGeometry(gl, world, z, x, timeLimit = 10000) {
|
export function updateWorldGeometry(gl, world, z, x, timeLimit = 10000) {
|
||||||
const ic = Math.floor(z / 16);
|
const ic = Math.floor(z / 16);
|
||||||
@ -613,6 +591,13 @@ export function markBlock(world, cameraPosition, direction, maxDistance) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function blockIndex2Ijk(bi) {
|
||||||
|
const blocki = Math.floor(bi / (256 * 16));
|
||||||
|
const blockj = Math.floor((bi % (256 * 16)) / 256);
|
||||||
|
const blockk = bi % 256;
|
||||||
|
|
||||||
|
return [blocki, blockj, blockk];
|
||||||
|
}
|
||||||
|
|
||||||
export function destroyBlock(world, block) {
|
export function destroyBlock(world, block) {
|
||||||
const trimFaces = chunk => {
|
const trimFaces = chunk => {
|
||||||
@ -642,7 +627,13 @@ export function destroyBlock(world, block) {
|
|||||||
.filter(({ block }) => block.type !== BlockType.AIR &&
|
.filter(({ block }) => block.type !== BlockType.AIR &&
|
||||||
block.type !== BlockType.UNDEFINED)
|
block.type !== BlockType.UNDEFINED)
|
||||||
.forEach(({ block, dir }) => {
|
.forEach(({ block, dir }) => {
|
||||||
addFace(block.chunk, block, dir);
|
makeFaces(block.chunk,
|
||||||
|
() => [blockIndex2Ijk(block.blockIndex)],
|
||||||
|
() => [{
|
||||||
|
block: BlockType.AIR,
|
||||||
|
dir,
|
||||||
|
faceCenter: faceCenter(block.centerPosition, dir),
|
||||||
|
}]);
|
||||||
trimFaces(block.chunk);
|
trimFaces(block.chunk);
|
||||||
|
|
||||||
if (block.chunk.buffer !== undefined) {
|
if (block.chunk.buffer !== undefined) {
|
||||||
@ -670,29 +661,25 @@ export function makeBlock(world, position, type) {
|
|||||||
{ block: blockLookup(world, bx, by, bz + 1), dir: '+z', ndir: '-z' },
|
{ block: blockLookup(world, bx, by, bz + 1), dir: '+z', ndir: '-z' },
|
||||||
];
|
];
|
||||||
|
|
||||||
const refresh = chunk => {
|
|
||||||
if (chunk.buffer !== undefined) {
|
|
||||||
chunk.buffer.delete();
|
|
||||||
delete chunk.buffer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
neighbors
|
neighbors
|
||||||
.filter(({ block }) => block.type !== BlockType.UNDEFINED)
|
.filter(({ block }) => block.type !== BlockType.UNDEFINED)
|
||||||
.forEach(({ block: nblock, dir, ndir }) => {
|
.forEach(({ block: nblock, dir, ndir }) => {
|
||||||
if (nblock.type === BlockType.AIR ||
|
makeFaces(block.chunk,
|
||||||
nblock.type === BlockType.WATER) {
|
() => [blockIndex2Ijk(block.blockIndex)],
|
||||||
addFace(block.chunk, block, dir);
|
() => [{
|
||||||
}// else {
|
block: BlockType.AIR,
|
||||||
nblock.chunk.faces = nblock.chunk.faces.filter(f => (
|
dir,
|
||||||
f.blockIndex !== nblock.blockIndex ||
|
faceCenter: faceCenter(block.centerPosition, dir),
|
||||||
f.dir !== ndir
|
}]);
|
||||||
));
|
nblock.chunk.faces = nblock.chunk.faces.filter(f => (
|
||||||
refresh(nblock.chunk);
|
f.blockIndex !== nblock.blockIndex ||
|
||||||
//}
|
f.dir !== ndir
|
||||||
|
));
|
||||||
|
nblock.chunk.transparentFaces = nblock.chunk.transparentFaces.filter(f => (
|
||||||
|
f.blockIndex !== nblock.blockIndex ||
|
||||||
|
f.dir !== ndir
|
||||||
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
refresh(block.chunk);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function initWorldGl(gl) {
|
export async function initWorldGl(gl) {
|
||||||
|
Loading…
Reference in New Issue
Block a user