diff --git a/src/app_context.ts b/src/app_context.ts index 98007b8..2b7fdb4 100644 --- a/src/app_context.ts +++ b/src/app_context.ts @@ -253,6 +253,8 @@ export class AppContext { } AppConsole.success(LOC('assign.loaded_block_mesh')); + Renderer.Get.setLightingAvailable(components.calculateLighting.getValue()); + AppConsole.info(LOC('assign.rendering_block_mesh')); { let moreBlocksToBuffer = false; diff --git a/src/block_mesh.ts b/src/block_mesh.ts index 2937978..8d6aeb9 100644 --- a/src/block_mesh.ts +++ b/src/block_mesh.ts @@ -23,7 +23,7 @@ interface Block { } interface GrassLikeBlock { - id: number; + hash: number; voxelColour: RGBA_255; errWeight: number; faceVisibility: EFaceVisibility; @@ -40,7 +40,8 @@ export interface BlockMeshParams { export class BlockMesh { private _blocksUsed: Set; - private _blocks: Block[]; + private _blocks: Map; + //private _blocks: Block[]; private _voxelMesh: VoxelMesh; private _atlas: Atlas; private _lighting: BlockMeshLighting; @@ -63,7 +64,7 @@ export class BlockMesh { private constructor(voxelMesh: VoxelMesh) { this._blocksUsed = new Set(); - this._blocks = []; + this._blocks = new Map(); this._voxelMesh = voxelMesh; this._atlas = Atlas.getVanillaAtlas()!; this._lighting = new BlockMeshLighting(this); @@ -107,7 +108,7 @@ export class BlockMesh { const atlasPalette = new AtlasPalette(atlas, palette); 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[] = []; let countFalling = 0; @@ -126,7 +127,7 @@ export class BlockMesh { // 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 - 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))); if (isBlockFallable && !isBlockSupported) { @@ -141,16 +142,16 @@ export class BlockMesh { 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({ - id: this._blocks.length, + hash: voxel.position.hash(), voxelColour: voxelColour, errWeight: blockMeshParams.errorWeight, faceVisibility: faceVisibility, }); } - this._blocks.push({ + this._blocks.set(voxel.position.hash(), { voxel: voxel, blockInfo: block, }); @@ -158,21 +159,23 @@ export class BlockMesh { } 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++) { ProgressManager.Get.progress(taskHandle, index / grassLikeBlocksBuffer.length); 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 topBlockIndex = 0; //this._voxelMesh.getVoxelIndex(topBlockPosition); - - if (topBlockIndex !== undefined && !AppRuntimeConstants.Get.TRANSPARENT_BLOCKS.includes(this._blocks[topBlockIndex].blockInfo.name)) { - const block = atlasPalette.getBlock(examined.voxelColour, nonGrassLikeBlockCollection, examined.faceVisibility, examined.errWeight); - examinedBlock.blockInfo = block; - this._blocks[examined.id] = examinedBlock; - this._blocksUsed.add(block.name); - }; + const topBlock = this._blocks.get(topBlockPosition.hash()); + if (topBlock !== undefined) { + if (!AppRuntimeConstants.Get.TRANSPARENT_BLOCKS.has(topBlock.blockInfo.name)) { + const block = atlasPalette.getBlock(examined.voxelColour, nonGrassLikeBlockCollection, examined.faceVisibility, examined.errWeight); + examinedBlock.blockInfo = block; + this._blocks.set(examined.hash, examinedBlock); + this._blocksUsed.add(block.name); + } + } } } ProgressManager.Get.end(taskHandle); @@ -213,8 +216,11 @@ export class BlockMesh { if (bestBlock !== undefined) { const blockIndex = 0; //this._voxelMesh.getVoxelIndex(pos); 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; } @@ -222,14 +228,11 @@ export class BlockMesh { } public getBlockAt(pos: Vector3): TOptional { - const index = 0; //this._voxelMesh.getVoxelIndex(pos); - if (index !== undefined) { - return this._blocks[index]; - } + return this._blocks.get(pos.hash()); } public getBlocks(): Block[] { - return this._blocks; + return Array.from(this._blocks.values()); } public getBlockPalette() { @@ -246,11 +249,11 @@ export class BlockMesh { } 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) { - return AppRuntimeConstants.Get.TRANSPARENT_BLOCKS.includes(block.blockInfo.name); + return AppRuntimeConstants.Get.TRANSPARENT_BLOCKS.has(block.blockInfo.name); } /* diff --git a/src/constants.ts b/src/constants.ts index 73a2a16..eedb10c 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -38,7 +38,7 @@ export class AppRuntimeConstants { return this._instance || (this._instance = new this()); } - public readonly FALLABLE_BLOCKS = [ + public readonly FALLABLE_BLOCKS = new Set([ 'minecraft:anvil', 'minecraft:lime_concrete_powder', 'minecraft:orange_concrete_powder', @@ -62,9 +62,9 @@ export class AppRuntimeConstants { 'minecraft:red_sand', 'minecraft:sand', 'minecraft:scaffolding', - ]; + ]); - public readonly TRANSPARENT_BLOCKS = [ + public readonly TRANSPARENT_BLOCKS = new Set([ 'minecraft:frosted_ice', 'minecraft:glass', 'minecraft:white_stained_glass', @@ -95,9 +95,9 @@ export class AppRuntimeConstants { 'minecraft:flowering_azalea_leaves', 'minecraft:slime_block', 'minecraft:honey_block', - ]; + ]); - public readonly GRASS_LIKE_BLOCKS = [ + public readonly GRASS_LIKE_BLOCKS = new Set([ 'minecraft:grass_block', 'minecraft:grass_path', 'minecraft:podzol', @@ -105,9 +105,9 @@ export class AppRuntimeConstants { 'minecraft:warped_nylium', 'minecraft:mycelium', 'minecraft:farmland', - ]; + ]); - public readonly EMISSIVE_BLOCKS = [ + public readonly EMISSIVE_BLOCKS = new Set([ 'minecraft:respawn_anchor', 'minecraft:magma_block', 'minecraft:sculk_catalyst', @@ -119,7 +119,7 @@ export class AppRuntimeConstants { 'minecraft:pearlescent_froglight', 'minecraft:verdant_froglight', 'minecraft:ochre_froglight', - ]; + ]); private constructor() { }