Small refactor in face creation
This commit is contained in:
		
							
								
								
									
										73
									
								
								world.js
									
									
									
									
									
								
							
							
						
						
									
										73
									
								
								world.js
									
									
									
									
									
								
							@@ -279,7 +279,7 @@ class ChunkMap {
 | 
			
		||||
 | 
			
		||||
/** Makes a brave new (empty) world */
 | 
			
		||||
export function makeWorld() {
 | 
			
		||||
    return {chunks: [], chunkMap: new ChunkMap()};
 | 
			
		||||
    return {chunkMap: new ChunkMap()};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function makeFaces(chunk, blocks, neighbors) {
 | 
			
		||||
@@ -404,7 +404,6 @@ export function generateMissingChunks(world, z, x, timeLimit = 10000) {
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                const chunk = makeChunk(i * 16, j * 16);
 | 
			
		||||
                world.chunks.push(chunk);
 | 
			
		||||
                world.chunkMap.set(i, j, chunk);
 | 
			
		||||
 | 
			
		||||
                updateMissingFaces(world, chunk);
 | 
			
		||||
@@ -414,27 +413,6 @@ export function generateMissingChunks(world, z, x, timeLimit = 10000) {
 | 
			
		||||
    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. */
 | 
			
		||||
export function updateWorldGeometry(gl, world, z, x, timeLimit = 10000) {
 | 
			
		||||
    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) {
 | 
			
		||||
    const trimFaces = chunk => {
 | 
			
		||||
@@ -642,7 +627,13 @@ export function destroyBlock(world, block) {
 | 
			
		||||
        .filter(({ block }) => block.type !== BlockType.AIR &&
 | 
			
		||||
            block.type !== BlockType.UNDEFINED)
 | 
			
		||||
        .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);
 | 
			
		||||
 | 
			
		||||
            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' },
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    const refresh = chunk => {
 | 
			
		||||
        if (chunk.buffer !== undefined) {
 | 
			
		||||
            chunk.buffer.delete();
 | 
			
		||||
            delete chunk.buffer;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    neighbors
 | 
			
		||||
        .filter(({ block }) => block.type !== BlockType.UNDEFINED)
 | 
			
		||||
        .forEach(({ block: nblock, dir, ndir }) => {
 | 
			
		||||
            if (nblock.type  === BlockType.AIR ||
 | 
			
		||||
                nblock.type === BlockType.WATER) {
 | 
			
		||||
                addFace(block.chunk, block, dir);
 | 
			
		||||
            }// else {
 | 
			
		||||
                nblock.chunk.faces = nblock.chunk.faces.filter(f => (
 | 
			
		||||
                    f.blockIndex !== nblock.blockIndex ||
 | 
			
		||||
                    f.dir !== ndir
 | 
			
		||||
                ));
 | 
			
		||||
                refresh(nblock.chunk);
 | 
			
		||||
            //}
 | 
			
		||||
            makeFaces(block.chunk,
 | 
			
		||||
                () => [blockIndex2Ijk(block.blockIndex)],
 | 
			
		||||
                () => [{
 | 
			
		||||
                    block: BlockType.AIR,
 | 
			
		||||
                    dir,
 | 
			
		||||
                    faceCenter: faceCenter(block.centerPosition, dir),
 | 
			
		||||
                }]);
 | 
			
		||||
            nblock.chunk.faces = nblock.chunk.faces.filter(f => (
 | 
			
		||||
                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) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user