From cc3da60a6e1c8f5d566f7907cb2b88483e49b4ac Mon Sep 17 00:00:00 2001 From: LabyStudio Date: Tue, 1 Feb 2022 11:06:34 +0100 Subject: [PATCH] implement water fog --- src/js/net/minecraft/client/entity/Player.js | 4 ++-- .../minecraft/client/render/Tessellator.js | 3 ++- .../minecraft/client/render/WorldRenderer.js | 23 +++++++++++++------ .../minecraft/client/world/ChunkSection.js | 2 +- src/js/net/minecraft/client/world/World.js | 4 ++++ .../net/minecraft/client/world/block/Block.js | 2 +- .../client/world/block/BlockWater.js | 3 +-- .../net/minecraft/util/EnumWorldBlockLayer.js | 4 ---- src/start.js | 1 - 9 files changed, 27 insertions(+), 19 deletions(-) delete mode 100644 src/js/net/minecraft/util/EnumWorldBlockLayer.js diff --git a/src/js/net/minecraft/client/entity/Player.js b/src/js/net/minecraft/client/entity/Player.js index 55179fd..71a3c88 100644 --- a/src/js/net/minecraft/client/entity/Player.js +++ b/src/js/net/minecraft/client/entity/Player.js @@ -171,11 +171,11 @@ window.Player = class { } isInWater() { - return false; + return this.world.getBlockAt(this.getBlockPosX(), this.getBlockPosY(), this.getBlockPosZ()) === Block.WATER.getId(); } isHeadInWater() { - return false; + return this.world.getBlockAt(this.getBlockPosX(), Math.floor(this.y + this.getEyeHeight() + 0.12), this.getBlockPosZ()) === Block.WATER.getId(); } jump() { diff --git a/src/js/net/minecraft/client/render/Tessellator.js b/src/js/net/minecraft/client/render/Tessellator.js index 464817b..a5ff94a 100644 --- a/src/js/net/minecraft/client/render/Tessellator.js +++ b/src/js/net/minecraft/client/render/Tessellator.js @@ -4,7 +4,8 @@ window.Tessellator = class { this.material = new THREE.MeshBasicMaterial({ vertexColors: THREE.VertexColors, side: THREE.BackSide, - transparent: true + transparent: true, + depthTest: true }); } diff --git a/src/js/net/minecraft/client/render/WorldRenderer.js b/src/js/net/minecraft/client/render/WorldRenderer.js index f55bf38..595a884 100644 --- a/src/js/net/minecraft/client/render/WorldRenderer.js +++ b/src/js/net/minecraft/client/render/WorldRenderer.js @@ -10,7 +10,7 @@ window.WorldRenderer = class { || !!document.createElement('canvas').getContext('webgl')); // Create cameras - this.camera = new THREE.PerspectiveCamera(0, 1, 0.001, 1000000); + this.camera = new THREE.PerspectiveCamera(0, 1, 0.001, 1000); this.camera.rotation.order = 'ZYX'; this.camera.up = new THREE.Vector3(0, 0, 1); @@ -56,7 +56,7 @@ window.WorldRenderer = class { let player = this.minecraft.player; let cameraChunkX = Math.floor(player.x >> 4); let cameraChunkZ = Math.floor(player.z >> 4); - this.renderChunks(cameraChunkX, cameraChunkZ, EnumWorldBlockLayer.SOLID); + this.renderChunks(cameraChunkX, cameraChunkZ); // Render window this.webRenderer.render(this.scene, this.camera); @@ -83,12 +83,14 @@ window.WorldRenderer = class { this.camera.updateProjectionMatrix(); // Setup fog - this.setupFog(); + this.setupFog(player.isHeadInWater()); } setupFog(inWater) { if (inWater) { - + let color = new THREE.Color(0.2, 0.2, 0.4); + this.scene.background = color; + this.scene.fog = new THREE.Fog(color, 0.0025, 5); } else { let viewDistance = WorldRenderer.RENDER_DISTANCE * ChunkSection.SIZE; @@ -98,7 +100,7 @@ window.WorldRenderer = class { } } - renderChunks(cameraChunkX, cameraChunkZ, renderLayer) { + renderChunks(cameraChunkX, cameraChunkZ) { let world = this.minecraft.world; for (let i in world.chunks) { @@ -112,7 +114,6 @@ window.WorldRenderer = class { // Make chunk visible chunk.group.visible = true; - // For all chunk sections for (let y in chunk.sections) { let chunkSection = chunk.sections[y]; @@ -123,7 +124,7 @@ window.WorldRenderer = class { chunkSection.group.visible = true; // Render chunk section - chunkSection.render(renderLayer); + chunkSection.render(); // Queue for rebuild if (chunkSection.isQueuedForRebuild() && !this.chunkSectionUpdateQueue.includes(chunkSection)) { @@ -156,6 +157,14 @@ window.WorldRenderer = class { let chunk = chunkSection.chunk; if (!chunk.isLoaded()) { world.loadChunk(chunk); + + // Rebuild neighbor chunks to update transparent blocks + for (let relX = -1; relX <= 1; relX++) { + for (let relZ = -1; relZ <= 1; relZ++) { + let neighborChunk = world.getChunkAt(chunk.x + relX, chunk.z + relZ); + neighborChunk.queueForRebuild(); + } + } } // Rebuild chunk diff --git a/src/js/net/minecraft/client/world/ChunkSection.js b/src/js/net/minecraft/client/world/ChunkSection.js index 01f7fb6..ce3e407 100644 --- a/src/js/net/minecraft/client/world/ChunkSection.js +++ b/src/js/net/minecraft/client/world/ChunkSection.js @@ -31,7 +31,7 @@ window.ChunkSection = class { } } - render(renderLayer) { + render() { } diff --git a/src/js/net/minecraft/client/world/World.js b/src/js/net/minecraft/client/world/World.js index 9d8ffb9..0fcc014 100644 --- a/src/js/net/minecraft/client/world/World.js +++ b/src/js/net/minecraft/client/world/World.js @@ -68,6 +68,10 @@ window.World = class { return chunkSection == null ? 0 : chunkSection.getBlockAt(x & 15, y & 15, z & 15); } + getBlockAtFace(x, y, z, face) { + return this.getBlockAt(x + face.x, y + face.y, z + face.z); + } + getChunkSectionAt(chunkX, layerY, chunkZ) { return this.getChunkAt(chunkX, chunkZ).getSection(layerY); } diff --git a/src/js/net/minecraft/client/world/block/Block.js b/src/js/net/minecraft/client/world/block/Block.js index a25b934..dc00058 100644 --- a/src/js/net/minecraft/client/world/block/Block.js +++ b/src/js/net/minecraft/client/world/block/Block.js @@ -35,7 +35,7 @@ window.Block = class { } shouldRenderFace(world, x, y, z, face) { - let typeId = world.getBlockAt(x + face.x, y + face.y, z + face.z); + let typeId = world.getBlockAtFace(x, y, z, face); return typeId === 0 || Block.getById(typeId).isTransparent(); } diff --git a/src/js/net/minecraft/client/world/block/BlockWater.js b/src/js/net/minecraft/client/world/block/BlockWater.js index f14259c..89855ab 100644 --- a/src/js/net/minecraft/client/world/block/BlockWater.js +++ b/src/js/net/minecraft/client/world/block/BlockWater.js @@ -4,7 +4,6 @@ window.BlockWater = class extends Block { super(id, textureSlotId); } - getOpacity() { return 0.3; } @@ -14,7 +13,7 @@ window.BlockWater = class extends Block { } shouldRenderFace(world, x, y, z, face) { - let typeId = world.getBlockAt(x + face.x, y + face.y, z + face.z); + let typeId = world.getBlockAtFace(x, y, z, face); return typeId === 0 || typeId !== this.id && Block.getById(typeId).isTransparent(); } diff --git a/src/js/net/minecraft/util/EnumWorldBlockLayer.js b/src/js/net/minecraft/util/EnumWorldBlockLayer.js deleted file mode 100644 index aebac37..0000000 --- a/src/js/net/minecraft/util/EnumWorldBlockLayer.js +++ /dev/null @@ -1,4 +0,0 @@ -window.EnumWorldBlockLayer = class { - static SOLID = 0; - static CUTOUT = 0; -} \ No newline at end of file diff --git a/src/start.js b/src/start.js index 59aff2a..009089d 100644 --- a/src/start.js +++ b/src/start.js @@ -52,7 +52,6 @@ loadScripts([ "src/js/net/minecraft/util/Timer.js", "src/js/net/minecraft/util/Random.js", "src/js/net/minecraft/util/Vector3.js", - "src/js/net/minecraft/util/EnumWorldBlockLayer.js", "src/js/net/minecraft/util/MovingObjectPosition.js", "src/js/net/minecraft/util/MathHelper.js", "src/js/net/minecraft/util/BoundingBox.js",