implement water fog

This commit is contained in:
LabyStudio
2022-02-01 11:06:34 +01:00
parent a3c21b3727
commit cc3da60a6e
9 changed files with 27 additions and 19 deletions
+2 -2
View File
@@ -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() {
@@ -4,7 +4,8 @@ window.Tessellator = class {
this.material = new THREE.MeshBasicMaterial({
vertexColors: THREE.VertexColors,
side: THREE.BackSide,
transparent: true
transparent: true,
depthTest: true
});
}
@@ -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
@@ -31,7 +31,7 @@ window.ChunkSection = class {
}
}
render(renderLayer) {
render() {
}
@@ -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);
}
@@ -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();
}
@@ -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();
}
@@ -1,4 +0,0 @@
window.EnumWorldBlockLayer = class {
static SOLID = 0;
static CUTOUT = 0;
}
-1
View File
@@ -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",