Fixed up test code breaking block mesh functions, closes #122

This commit is contained in:
Lucas Dower 2023-05-21 17:13:10 +01:00
parent f7f2d23b54
commit d3b0580fef
No known key found for this signature in database
GPG Key ID: B3EE6B8499593605
3 changed files with 39 additions and 34 deletions

View File

@ -253,6 +253,8 @@ export class AppContext {
} }
AppConsole.success(LOC('assign.loaded_block_mesh')); AppConsole.success(LOC('assign.loaded_block_mesh'));
Renderer.Get.setLightingAvailable(components.calculateLighting.getValue());
AppConsole.info(LOC('assign.rendering_block_mesh')); AppConsole.info(LOC('assign.rendering_block_mesh'));
{ {
let moreBlocksToBuffer = false; let moreBlocksToBuffer = false;

View File

@ -23,7 +23,7 @@ interface Block {
} }
interface GrassLikeBlock { interface GrassLikeBlock {
id: number; hash: number;
voxelColour: RGBA_255; voxelColour: RGBA_255;
errWeight: number; errWeight: number;
faceVisibility: EFaceVisibility; faceVisibility: EFaceVisibility;
@ -40,7 +40,8 @@ export interface BlockMeshParams {
export class BlockMesh { export class BlockMesh {
private _blocksUsed: Set<string>; private _blocksUsed: Set<string>;
private _blocks: Block[]; private _blocks: Map<number, Block>;
//private _blocks: Block[];
private _voxelMesh: VoxelMesh; private _voxelMesh: VoxelMesh;
private _atlas: Atlas; private _atlas: Atlas;
private _lighting: BlockMeshLighting; private _lighting: BlockMeshLighting;
@ -63,7 +64,7 @@ export class BlockMesh {
private constructor(voxelMesh: VoxelMesh) { private constructor(voxelMesh: VoxelMesh) {
this._blocksUsed = new Set(); this._blocksUsed = new Set();
this._blocks = []; this._blocks = new Map();
this._voxelMesh = voxelMesh; this._voxelMesh = voxelMesh;
this._atlas = Atlas.getVanillaAtlas()!; this._atlas = Atlas.getVanillaAtlas()!;
this._lighting = new BlockMeshLighting(this); this._lighting = new BlockMeshLighting(this);
@ -107,7 +108,7 @@ export class BlockMesh {
const atlasPalette = new AtlasPalette(atlas, palette); const atlasPalette = new AtlasPalette(atlas, palette);
const allBlockCollection = atlasPalette.createBlockCollection([]); const allBlockCollection = atlasPalette.createBlockCollection([]);
const nonFallableBlockCollection = atlasPalette.createBlockCollection(AppRuntimeConstants.Get.FALLABLE_BLOCKS); const nonFallableBlockCollection = atlasPalette.createBlockCollection(Array.from(AppRuntimeConstants.Get.FALLABLE_BLOCKS));
const grassLikeBlocksBuffer: GrassLikeBlock[] = []; const grassLikeBlocksBuffer: GrassLikeBlock[] = [];
let countFalling = 0; let countFalling = 0;
@ -126,7 +127,7 @@ export class BlockMesh {
// Check that this block meets the fallable behaviour, we may need // Check that this block meets the fallable behaviour, we may need
// to choose a different block if the current one doesn't meet the requirements // to choose a different block if the current one doesn't meet the requirements
const isBlockFallable = AppRuntimeConstants.Get.FALLABLE_BLOCKS.includes(block.name); const isBlockFallable = AppRuntimeConstants.Get.FALLABLE_BLOCKS.has(block.name);
const isBlockSupported = this._voxelMesh.isVoxelAt(Vector3.add(voxel.position, new Vector3(0, -1, 0))); const isBlockSupported = this._voxelMesh.isVoxelAt(Vector3.add(voxel.position, new Vector3(0, -1, 0)));
if (isBlockFallable && !isBlockSupported) { if (isBlockFallable && !isBlockSupported) {
@ -141,16 +142,16 @@ export class BlockMesh {
block = atlasPalette.getBlock(voxelColour, nonFallableBlockCollection, faceVisibility, blockMeshParams.errorWeight); block = atlasPalette.getBlock(voxelColour, nonFallableBlockCollection, faceVisibility, blockMeshParams.errorWeight);
} }
if (AppRuntimeConstants.Get.GRASS_LIKE_BLOCKS.includes(block.name)) { if (AppRuntimeConstants.Get.GRASS_LIKE_BLOCKS.has(block.name)) {
grassLikeBlocksBuffer.push({ grassLikeBlocksBuffer.push({
id: this._blocks.length, hash: voxel.position.hash(),
voxelColour: voxelColour, voxelColour: voxelColour,
errWeight: blockMeshParams.errorWeight, errWeight: blockMeshParams.errorWeight,
faceVisibility: faceVisibility, faceVisibility: faceVisibility,
}); });
} }
this._blocks.push({ this._blocks.set(voxel.position.hash(), {
voxel: voxel, voxel: voxel,
blockInfo: block, blockInfo: block,
}); });
@ -158,21 +159,23 @@ export class BlockMesh {
} }
if (grassLikeBlocksBuffer.length > 0) { if (grassLikeBlocksBuffer.length > 0) {
const nonGrassLikeBlockCollection = atlasPalette.createBlockCollection(AppRuntimeConstants.Get.GRASS_LIKE_BLOCKS); const nonGrassLikeBlockCollection = atlasPalette.createBlockCollection(Array.from(AppRuntimeConstants.Get.GRASS_LIKE_BLOCKS));
for (let index=0; index < grassLikeBlocksBuffer.length; index++) { for (let index=0; index < grassLikeBlocksBuffer.length; index++) {
ProgressManager.Get.progress(taskHandle, index / grassLikeBlocksBuffer.length); ProgressManager.Get.progress(taskHandle, index / grassLikeBlocksBuffer.length);
const examined = grassLikeBlocksBuffer[index]; const examined = grassLikeBlocksBuffer[index];
const examinedBlock = this._blocks[examined.id]; const examinedBlock = this._blocks.get(examined.hash);
ASSERT(examinedBlock, 'Missing examined block');
const topBlockPosition = Vector3.add(examinedBlock.voxel.position, new Vector3(0, 1, 0)); const topBlockPosition = Vector3.add(examinedBlock.voxel.position, new Vector3(0, 1, 0));
const topBlockIndex = 0; //this._voxelMesh.getVoxelIndex(topBlockPosition); const topBlock = this._blocks.get(topBlockPosition.hash());
if (topBlock !== undefined) {
if (topBlockIndex !== undefined && !AppRuntimeConstants.Get.TRANSPARENT_BLOCKS.includes(this._blocks[topBlockIndex].blockInfo.name)) { if (!AppRuntimeConstants.Get.TRANSPARENT_BLOCKS.has(topBlock.blockInfo.name)) {
const block = atlasPalette.getBlock(examined.voxelColour, nonGrassLikeBlockCollection, examined.faceVisibility, examined.errWeight); const block = atlasPalette.getBlock(examined.voxelColour, nonGrassLikeBlockCollection, examined.faceVisibility, examined.errWeight);
examinedBlock.blockInfo = block; examinedBlock.blockInfo = block;
this._blocks[examined.id] = examinedBlock; this._blocks.set(examined.hash, examinedBlock);
this._blocksUsed.add(block.name); this._blocksUsed.add(block.name);
}; }
}
} }
} }
ProgressManager.Get.end(taskHandle); ProgressManager.Get.end(taskHandle);
@ -213,8 +216,11 @@ export class BlockMesh {
if (bestBlock !== undefined) { if (bestBlock !== undefined) {
const blockIndex = 0; //this._voxelMesh.getVoxelIndex(pos); const blockIndex = 0; //this._voxelMesh.getVoxelIndex(pos);
ASSERT(blockIndex !== undefined, 'Setting emissive block of block that doesn\'t exist'); ASSERT(blockIndex !== undefined, 'Setting emissive block of block that doesn\'t exist');
this._blocks[blockIndex].blockInfo = bestBlock;
const block = this._blocks.get(pos.hash());
ASSERT(block !== undefined);
block.blockInfo = bestBlock;
return true; return true;
} }
@ -222,14 +228,11 @@ export class BlockMesh {
} }
public getBlockAt(pos: Vector3): TOptional<Block> { public getBlockAt(pos: Vector3): TOptional<Block> {
const index = 0; //this._voxelMesh.getVoxelIndex(pos); return this._blocks.get(pos.hash());
if (index !== undefined) {
return this._blocks[index];
}
} }
public getBlocks(): Block[] { public getBlocks(): Block[] {
return this._blocks; return Array.from(this._blocks.values());
} }
public getBlockPalette() { public getBlockPalette() {
@ -246,11 +249,11 @@ export class BlockMesh {
} }
public isEmissiveBlock(block: Block) { public isEmissiveBlock(block: Block) {
return AppRuntimeConstants.Get.EMISSIVE_BLOCKS.includes(block.blockInfo.name); return AppRuntimeConstants.Get.EMISSIVE_BLOCKS.has(block.blockInfo.name);
} }
public isTransparentBlock(block: Block) { public isTransparentBlock(block: Block) {
return AppRuntimeConstants.Get.TRANSPARENT_BLOCKS.includes(block.blockInfo.name); return AppRuntimeConstants.Get.TRANSPARENT_BLOCKS.has(block.blockInfo.name);
} }
/* /*

View File

@ -38,7 +38,7 @@ export class AppRuntimeConstants {
return this._instance || (this._instance = new this()); return this._instance || (this._instance = new this());
} }
public readonly FALLABLE_BLOCKS = [ public readonly FALLABLE_BLOCKS = new Set([
'minecraft:anvil', 'minecraft:anvil',
'minecraft:lime_concrete_powder', 'minecraft:lime_concrete_powder',
'minecraft:orange_concrete_powder', 'minecraft:orange_concrete_powder',
@ -62,9 +62,9 @@ export class AppRuntimeConstants {
'minecraft:red_sand', 'minecraft:red_sand',
'minecraft:sand', 'minecraft:sand',
'minecraft:scaffolding', 'minecraft:scaffolding',
]; ]);
public readonly TRANSPARENT_BLOCKS = [ public readonly TRANSPARENT_BLOCKS = new Set([
'minecraft:frosted_ice', 'minecraft:frosted_ice',
'minecraft:glass', 'minecraft:glass',
'minecraft:white_stained_glass', 'minecraft:white_stained_glass',
@ -95,9 +95,9 @@ export class AppRuntimeConstants {
'minecraft:flowering_azalea_leaves', 'minecraft:flowering_azalea_leaves',
'minecraft:slime_block', 'minecraft:slime_block',
'minecraft:honey_block', 'minecraft:honey_block',
]; ]);
public readonly GRASS_LIKE_BLOCKS = [ public readonly GRASS_LIKE_BLOCKS = new Set([
'minecraft:grass_block', 'minecraft:grass_block',
'minecraft:grass_path', 'minecraft:grass_path',
'minecraft:podzol', 'minecraft:podzol',
@ -105,9 +105,9 @@ export class AppRuntimeConstants {
'minecraft:warped_nylium', 'minecraft:warped_nylium',
'minecraft:mycelium', 'minecraft:mycelium',
'minecraft:farmland', 'minecraft:farmland',
]; ]);
public readonly EMISSIVE_BLOCKS = [ public readonly EMISSIVE_BLOCKS = new Set([
'minecraft:respawn_anchor', 'minecraft:respawn_anchor',
'minecraft:magma_block', 'minecraft:magma_block',
'minecraft:sculk_catalyst', 'minecraft:sculk_catalyst',
@ -119,7 +119,7 @@ export class AppRuntimeConstants {
'minecraft:pearlescent_froglight', 'minecraft:pearlescent_froglight',
'minecraft:verdant_froglight', 'minecraft:verdant_froglight',
'minecraft:ochre_froglight', 'minecraft:ochre_froglight',
]; ]);
private constructor() { private constructor() {
} }