From 010d8832733b289f2ac5cf7c011b7a9c5bfee5b1 Mon Sep 17 00:00:00 2001 From: LabyStudio Date: Wed, 2 Feb 2022 15:18:45 +0100 Subject: [PATCH] working light system --- src/js/net/minecraft/client/Minecraft.js | 4 ++-- .../minecraft/client/render/WorldRenderer.js | 2 +- src/js/net/minecraft/client/world/Chunk.js | 24 +++++++++---------- .../minecraft/client/world/ChunkSection.js | 16 +++++-------- src/js/net/minecraft/client/world/World.js | 24 +++++-------------- .../client/world/generator/WorldGenerator.js | 4 ++-- 6 files changed, 28 insertions(+), 46 deletions(-) diff --git a/src/js/net/minecraft/client/Minecraft.js b/src/js/net/minecraft/client/Minecraft.js index 7a2acd1..49fccc9 100644 --- a/src/js/net/minecraft/client/Minecraft.js +++ b/src/js/net/minecraft/client/Minecraft.js @@ -78,11 +78,11 @@ window.Minecraft = class { this.window.mouseMotionX = 0; this.window.mouseMotionY = 0; } + + while (this.world.updateLights()) ; // Render the game this.worldRenderer.render(partialTicks); - - while (this.world.updateLights()) ; } onTick() { diff --git a/src/js/net/minecraft/client/render/WorldRenderer.js b/src/js/net/minecraft/client/render/WorldRenderer.js index a8e1544..4d7be68 100644 --- a/src/js/net/minecraft/client/render/WorldRenderer.js +++ b/src/js/net/minecraft/client/render/WorldRenderer.js @@ -133,7 +133,7 @@ window.WorldRenderer = class { chunkSection.render(); // Queue for rebuild - if (chunkSection.isQueuedForRebuild() && !this.chunkSectionUpdateQueue.includes(chunkSection)) { + if (chunkSection.isModified && !this.chunkSectionUpdateQueue.includes(chunkSection)) { this.chunkSectionUpdateQueue.push(chunkSection); } } else { diff --git a/src/js/net/minecraft/client/world/Chunk.js b/src/js/net/minecraft/client/world/Chunk.js index b5b1558..eb32724 100644 --- a/src/js/net/minecraft/client/world/Chunk.js +++ b/src/js/net/minecraft/client/world/Chunk.js @@ -35,7 +35,6 @@ window.Chunk = class { highest = this.heightMap[z << 4 | x] & 0xff; } } - } this.highestY = highest; @@ -46,11 +45,11 @@ window.Chunk = class { } - this.queueForRebuild(); + this.setModifiedAllSections(); } updateBlockLight() { - + this.setModifiedAllSections(); } notifyNeighbors(x, z) { @@ -71,7 +70,7 @@ window.Chunk = class { } else if (height < y) { this.world.updateLight(EnumSkyBlock.SKY, x, height, z, x, y, z); } - this.queueForRebuild(); + this.setModifiedAllSections(); } updateHeightMap(relX, y, relZ) { @@ -80,7 +79,7 @@ window.Chunk = class { if (y > currentHighestY) { highestY = y; } - while (highestY > 0 && Block.lightOpacity[this.getBlockAt(relX, highestY - 1, relZ)] === 0) { + while (highestY > 0 && Block.lightOpacity[this.getBlockAt(relX, highestY, relZ)] === 0) { highestY--; } if (highestY === currentHighestY) { @@ -138,7 +137,7 @@ window.Chunk = class { if (highestY !== prevHeight) { this.world.updateLight(EnumSkyBlock.SKY, x - 1, highestY, z - 1, x + 1, prevHeight, z + 1); } - this.queueForRebuild(); + this.setModifiedAllSections(); } setLightAt(sourceType, x, y, z, level) { @@ -189,7 +188,7 @@ window.Chunk = class { //this.data.setNibble(i, j, k, i1); - this.queueForRebuild(); + this.setModifiedAllSections(); return true; } @@ -219,14 +218,13 @@ window.Chunk = class { } } - queueForRebuild() { - for (let y = 0; y < this.sections.length; y++) { - this.sections[y].queueForRebuild(); - } - } - isLoaded() { return this.loaded; } + setModifiedAllSections() { + for (let y = 0; y < this.sections.length; y++) { + this.sections[y].isModified = true; + } + } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/ChunkSection.js b/src/js/net/minecraft/client/world/ChunkSection.js index f9db3ae..153dd1f 100644 --- a/src/js/net/minecraft/client/world/ChunkSection.js +++ b/src/js/net/minecraft/client/world/ChunkSection.js @@ -20,7 +20,7 @@ window.ChunkSection = class { this.group = new THREE.Object3D(); this.group.matrixAutoUpdate = false; - this.queuedForRebuild = true; + this.isModified = false; this.blocks = []; this.blockLight = []; @@ -44,7 +44,7 @@ window.ChunkSection = class { } rebuild(renderer) { - this.queuedForRebuild = false; + this.isModified = false; this.group.clear(); // Start drawing chunk section @@ -80,6 +80,8 @@ window.ChunkSection = class { setBlockAt(x, y, z, typeId) { let index = y << 8 | z << 4 | x; this.blocks[index] = typeId; + + this.isModified = true; } setLightAt(sourceType, x, y, z, lightLevel) { @@ -91,6 +93,8 @@ window.ChunkSection = class { if (sourceType === EnumSkyBlock.BLOCK) { this.blockLight[index] = lightLevel; } + + this.isModified = true; } getTotalLightAt(x, y, z) { @@ -127,12 +131,4 @@ window.ChunkSection = class { } return true; } - - queueForRebuild() { - this.queuedForRebuild = true; - } - - isQueuedForRebuild() { - return this.queuedForRebuild; - } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/World.js b/src/js/net/minecraft/client/world/World.js index 4f0a126..df13ccd 100644 --- a/src/js/net/minecraft/client/world/World.js +++ b/src/js/net/minecraft/client/world/World.js @@ -11,9 +11,6 @@ window.World = class { this.generator = new WorldGenerator(this, Date.now() % 100000); this.lightUpdateQueue = []; - - this.lightUpdateProcesses = 0; - this.lightUpdates = 0; } onTick() { @@ -24,7 +21,7 @@ window.World = class { let index = x + (z << 16); let chunk = this.chunks.get(index); if (typeof chunk === 'undefined') { - let chunk = new Chunk(this, x, z); + chunk = new Chunk(this, x, z); // Generate new chunk this.generator.generateChunk(chunk); @@ -85,13 +82,7 @@ window.World = class { return false; } - static prev = 0; - updateLight(sourceType, x1, y1, z1, x2, y2, z2, notifyNeighbor = true) { - if (this.lightUpdates >= 50) { - return; - } - let centerX = (x2 + x1) / 2; let centerZ = (z2 + z1) / 2; @@ -114,11 +105,6 @@ window.World = class { } } - if (World.prev !== this.lightUpdateQueue.length && this.lightUpdateQueue.length % 100 === 0) { - World.prev = this.lightUpdateQueue.length; - console.log(this.lightUpdateQueue.length + " in queue"); - } - this.lightUpdateQueue.push(new MetadataChunkBlock(sourceType, x1, y1, z1, x2, y2, z2)); if (this.lightUpdateQueue.length > 0x186a0) { this.lightUpdateQueue = []; @@ -230,10 +216,10 @@ window.World = class { } onBlockChanged(x, y, z) { - this.queueForRebuildInRegion(x - 1, y - 1, z - 1, x + 1, y + 1, z + 1); + this.setModified(x - 1, y - 1, z - 1, x + 1, y + 1, z + 1); } - queueForRebuildInRegion(minX, minY, minZ, maxX, maxY, maxZ) { + setModified(minX, minY, minZ, maxX, maxY, maxZ) { // To chunk coordinates minX = minX >> 4; maxX = maxX >> 4; @@ -249,7 +235,9 @@ window.World = class { for (let x = minX; x <= maxX; x++) { for (let y = minY; y <= maxY; y++) { for (let z = minZ; z <= maxZ; z++) { - this.getChunkSectionAt(x, y, z).queueForRebuild(); + if (this.chunkExists(x >> 4, z >> 4)) { + this.getChunkSectionAt(x, y, z).isModified = true; + } } } } diff --git a/src/js/net/minecraft/client/world/generator/WorldGenerator.js b/src/js/net/minecraft/client/world/generator/WorldGenerator.js index b495a7a..bcb9548 100644 --- a/src/js/net/minecraft/client/world/generator/WorldGenerator.js +++ b/src/js/net/minecraft/client/world/generator/WorldGenerator.js @@ -32,8 +32,8 @@ window.WorldGenerator = class { for (let relZ = 0; relZ < ChunkSection.SIZE; relZ++) { // Absolute position of the block - let x = chunk.x * ChunkSection.SIZE + relX; - let z = chunk.z * ChunkSection.SIZE + relZ; + let x = chunk.x * ChunkSection.SIZE + relX + 10000; // TODO fix this 10000 offset + let z = chunk.z * ChunkSection.SIZE + relZ + 10000; // Extract height value of the noise let heightValue = this.groundHeightNoise.perlin(x, z);