From 00af52a66aba575be1cb7d24e7cec2cc6d45bf17 Mon Sep 17 00:00:00 2001 From: LabyStudio Date: Mon, 31 Jan 2022 21:32:35 +0100 Subject: [PATCH] implement shading --- .../minecraft/client/render/BlockRenderer.js | 7 ++++++- .../net/minecraft/client/render/Tessellator.js | 17 ++++++++++++++++- src/js/net/minecraft/util/EnumBlockFace.js | 16 ++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/js/net/minecraft/client/render/BlockRenderer.js b/src/js/net/minecraft/client/render/BlockRenderer.js index 7a79512..030e1fc 100644 --- a/src/js/net/minecraft/client/render/BlockRenderer.js +++ b/src/js/net/minecraft/client/render/BlockRenderer.js @@ -43,13 +43,18 @@ window.BlockRenderer = class { let textureIndex = typeId; let minU = (textureIndex % 16) / 16.0; let maxU = minU + (16 / 256); - let minV = parseInt(textureIndex / 16); // TODO Math.round + let minV = Math.round(textureIndex / 16); let maxV = minV + (16 / 256); // Flip V minV = 1 - minV; maxV = 1 - maxV; + // Classic lightning + let brightness = 0.9 / 15.0 * 15 + 0.1; + let color = brightness * face.getShading(); + this.tessellator.setColor(color, color, color); + if (face === EnumBlockFace.BOTTOM) { this.addBlockCorner(world, face, minX, minY, maxZ, minU, maxV); this.addBlockCorner(world, face, minX, minY, minZ, minU, minV); diff --git a/src/js/net/minecraft/client/render/Tessellator.js b/src/js/net/minecraft/client/render/Tessellator.js index 738318c..810911b 100644 --- a/src/js/net/minecraft/client/render/Tessellator.js +++ b/src/js/net/minecraft/client/render/Tessellator.js @@ -2,8 +2,10 @@ window.Tessellator = class { constructor() { this.material = new THREE.MeshBasicMaterial({ + vertexColors: THREE.VertexColors, color: 0xffffff, - side: THREE.BackSide + side: THREE.BackSide, + overdraw: 1 }); } @@ -15,6 +17,13 @@ window.Tessellator = class { this.addedVertices = 0; this.vertices = []; this.uv = []; + this.colors = []; + } + + setColor(red, green, blue) { + this.red = red; + this.green = green; + this.blue = blue; } addVertexWithUV(x, y, z, u, v) { @@ -28,11 +37,17 @@ window.Tessellator = class { // Add UV this.uv.push(u); this.uv.push(v); + + // Add colors + this.colors.push(this.red); + this.colors.push(this.green); + this.colors.push(this.blue); } draw(group) { let geometry = new THREE.BufferGeometry(); geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(this.vertices), 3)); + geometry.addAttribute('color', new THREE.BufferAttribute(new Float32Array(this.colors), 3)); geometry.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(this.uv), 2)); geometry.setIndex(new THREE.BufferAttribute(new Uint32Array([0, 2, 1, 0, 3, 2]), 1)); diff --git a/src/js/net/minecraft/util/EnumBlockFace.js b/src/js/net/minecraft/util/EnumBlockFace.js index a356f49..08a8e6a 100644 --- a/src/js/net/minecraft/util/EnumBlockFace.js +++ b/src/js/net/minecraft/util/EnumBlockFace.js @@ -6,6 +6,22 @@ window.EnumBlockFace = class { this.z = z; } + getShading() { + return this.isXAxis() ? 0.6 : this.isYAxis() ? 1.0 : 0.8; + } + + isXAxis() { + return this.x !== 0; + } + + isYAxis() { + return this.y !== 0; + } + + isZAxis() { + return this.z !== 0; + } + static values() { return [ EnumBlockFace.TOP,