From 77fd18af3112e8c4fb15afc73057b4419a38da65 Mon Sep 17 00:00:00 2001 From: LabyStudio Date: Sun, 13 Feb 2022 00:52:32 +0100 Subject: [PATCH] implement sun --- .../net/minecraft/client/gui/IngameOverlay.js | 4 +- .../client/gui/screens/GuiLoadingScreen.js | 6 +- .../minecraft/client/gui/widgets/GuiButton.js | 2 +- .../minecraft/client/render/BlockRenderer.js | 2 +- .../minecraft/client/render/WorldRenderer.js | 66 +++++++++++++++--- .../client/render/gui/FontRenderer.js | 2 +- src/js/net/minecraft/client/world/World.js | 53 +++++++++++++- src/js/net/minecraft/util/MathHelper.js | 63 +++++++++++++++++ src/resources/{ => gui}/background.png | Bin src/resources/{ => gui}/font.png | Bin src/resources/{ => gui}/gui.png | Bin src/resources/{ => gui}/icons.png | Bin src/resources/terrain/moon.png | Bin 0 -> 874 bytes src/resources/terrain/sun.png | Bin 0 -> 663 bytes src/resources/{ => terrain}/terrain.png | Bin src/start.js | 12 ++-- style.css | 2 +- 17 files changed, 188 insertions(+), 24 deletions(-) rename src/resources/{ => gui}/background.png (100%) rename src/resources/{ => gui}/font.png (100%) rename src/resources/{ => gui}/gui.png (100%) rename src/resources/{ => gui}/icons.png (100%) create mode 100644 src/resources/terrain/moon.png create mode 100644 src/resources/terrain/sun.png rename src/resources/{ => terrain}/terrain.png (100%) diff --git a/src/js/net/minecraft/client/gui/IngameOverlay.js b/src/js/net/minecraft/client/gui/IngameOverlay.js index e28c16a..b1f2313 100644 --- a/src/js/net/minecraft/client/gui/IngameOverlay.js +++ b/src/js/net/minecraft/client/gui/IngameOverlay.js @@ -5,8 +5,8 @@ window.IngameOverlay = class extends Gui { this.minecraft = minecraft; this.window = window; - this.textureCrosshair = Gui.loadTexture("icons.png"); - this.textureHotbar = Gui.loadTexture("gui.png"); + this.textureCrosshair = Gui.loadTexture("gui/icons.png"); + this.textureHotbar = Gui.loadTexture("gui/gui.png"); } render(stack, mouseX, mouseY, partialTicks) { diff --git a/src/js/net/minecraft/client/gui/screens/GuiLoadingScreen.js b/src/js/net/minecraft/client/gui/screens/GuiLoadingScreen.js index c66ca5d..067f950 100644 --- a/src/js/net/minecraft/client/gui/screens/GuiLoadingScreen.js +++ b/src/js/net/minecraft/client/gui/screens/GuiLoadingScreen.js @@ -3,7 +3,7 @@ window.GuiLoadingScreen = class extends GuiScreen { constructor() { super(); - this.textureBackground = Gui.loadTexture("background.png"); + this.textureBackground = Gui.loadTexture("gui/background.png"); } drawScreen(stack, mouseX, mouseY, partialTicks) { @@ -50,4 +50,8 @@ window.GuiLoadingScreen = class extends GuiScreen { this.progress = progress; } + keyTyped(key) { + // Cancel key inputs + } + } \ No newline at end of file diff --git a/src/js/net/minecraft/client/gui/widgets/GuiButton.js b/src/js/net/minecraft/client/gui/widgets/GuiButton.js index 237f7d6..df88089 100644 --- a/src/js/net/minecraft/client/gui/widgets/GuiButton.js +++ b/src/js/net/minecraft/client/gui/widgets/GuiButton.js @@ -1,6 +1,6 @@ window.GuiButton = class extends Gui { - static textureGui = Gui.loadTexture("gui.png"); + static textureGui = Gui.loadTexture("gui/gui.png"); constructor(string, x, y, width, height, callback) { super(); diff --git a/src/js/net/minecraft/client/render/BlockRenderer.js b/src/js/net/minecraft/client/render/BlockRenderer.js index 2840f4d..16ba948 100644 --- a/src/js/net/minecraft/client/render/BlockRenderer.js +++ b/src/js/net/minecraft/client/render/BlockRenderer.js @@ -5,7 +5,7 @@ window.BlockRenderer = class { constructor(worldRenderer) { this.worldRenderer = worldRenderer; this.tessellator = new Tessellator(); - this.tessellator.bindTexture(worldRenderer.terrainTexture); + this.tessellator.bindTexture(worldRenderer.textureTerrain); } renderBlock(world, block, x, y, z) { diff --git a/src/js/net/minecraft/client/render/WorldRenderer.js b/src/js/net/minecraft/client/render/WorldRenderer.js index df5b11e..91c77a5 100644 --- a/src/js/net/minecraft/client/render/WorldRenderer.js +++ b/src/js/net/minecraft/client/render/WorldRenderer.js @@ -7,10 +7,15 @@ window.WorldRenderer = class { this.window = window; this.chunkSectionUpdateQueue = []; - // Load terrain - this.terrainTexture = new THREE.TextureLoader().load('src/resources/terrain.png'); - this.terrainTexture.magFilter = THREE.NearestFilter; - this.terrainTexture.minFilter = THREE.NearestFilter; + // Load terrain texture + this.textureTerrain = new THREE.TextureLoader().load('src/resources/terrain/terrain.png'); + this.textureTerrain.magFilter = THREE.NearestFilter; + this.textureTerrain.minFilter = THREE.NearestFilter; + + // Load sun texture + this.textureSun = new THREE.TextureLoader().load('src/resources/terrain/sun.png'); + this.textureSun.magFilter = THREE.NearestFilter; + this.textureSun.minFilter = THREE.NearestFilter; // Block Renderer this.blockRenderer = new BlockRenderer(this); @@ -34,7 +39,8 @@ window.WorldRenderer = class { // Create web renderer this.webRenderer = new THREE.WebGLRenderer({ canvas: this.window.canvas, - antialias: false + antialias: false, + alpha: true }); // Settings @@ -45,6 +51,8 @@ window.WorldRenderer = class { this.webRenderer.sortObjects = false; this.webRenderer.setClearColor(0x000000, 0); this.webRenderer.clear(); + + this.generateSky(); } render(partialTicks) { @@ -57,7 +65,10 @@ window.WorldRenderer = class { let cameraChunkZ = Math.floor(player.z >> 4); this.renderChunks(cameraChunkX, cameraChunkZ); - // Render actual scene and hud + // Render sky + this.renderSky(partialTicks); + + // Render actual scene this.webRenderer.render(this.scene, this.camera); } @@ -82,10 +93,10 @@ window.WorldRenderer = class { this.camera.updateProjectionMatrix(); // Setup fog - this.setupFog(player.isHeadInWater()); + this.setupFog(x, z, player.isHeadInWater(), partialTicks); } - setupFog(inWater) { + setupFog(x, z, inWater, partialTicks) { if (inWater) { let color = new THREE.Color(0.2, 0.2, 0.4); this.scene.background = color; @@ -93,8 +104,12 @@ window.WorldRenderer = class { } else { let viewDistance = WorldRenderer.RENDER_DISTANCE * ChunkSection.SIZE; - let color = new THREE.Color(0x9299ff); - this.scene.background = color; + let color = this.minecraft.world.getSkyColor(x, z, partialTicks); + this.scene.background = new THREE.Color( + ((color >> 16) & 0xFF) / 255, + ((color >> 8) & 0xFF) / 255, + (color & 0xFF) / 255 + ); this.scene.fog = new THREE.Fog(color, 0.0025, viewDistance); } } @@ -165,4 +180,35 @@ window.WorldRenderer = class { } } } + + generateSky() { + // Create sky group + this.skyGroup = new THREE.Scene(); + this.scene.add(this.skyGroup); + + // Create sun + let geometry = new THREE.PlaneGeometry(1, 1); + let material = new THREE.MeshBasicMaterial({ + color: 0xffff00, + side: THREE.FrontSide, + map: this.textureSun, + alphaMap: this.textureSun, + blending: THREE.AdditiveBlending, + transparent: true + }); + this.sun = new THREE.Mesh(geometry, material); + this.sun.translateZ(-2); + this.sun.renderOrder = 999; + this.sun.material.depthTest = false; + this.skyGroup.add(this.sun); + } + + renderSky(partialTicks) { + // Center sky + this.skyGroup.position.copy(this.camera.position); + + // Rotate sky + let angle = this.minecraft.world.getCelestialAngle(partialTicks); + this.skyGroup.rotation.set(angle * Math.PI * 2 + Math.PI / 2, 0, 0); + } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/render/gui/FontRenderer.js b/src/js/net/minecraft/client/render/gui/FontRenderer.js index 9b42a08..da131d1 100644 --- a/src/js/net/minecraft/client/render/gui/FontRenderer.js +++ b/src/js/net/minecraft/client/render/gui/FontRenderer.js @@ -8,7 +8,7 @@ window.FontRenderer = class { constructor() { this.charWidths = []; - this.texture = Gui.loadTexture("font.png") + this.texture = Gui.loadTexture("gui/font.png") let bitMap = this.createBitMap(this.texture); diff --git a/src/js/net/minecraft/client/world/World.js b/src/js/net/minecraft/client/world/World.js index b5e1587..b3fae4c 100644 --- a/src/js/net/minecraft/client/world/World.js +++ b/src/js/net/minecraft/client/world/World.js @@ -11,6 +11,8 @@ window.World = class { this.chunks = new Map(); this.lightUpdateQueue = []; + this.time = 0; + // Load world this.generator = new WorldGenerator(this, Date.now() % 100000); @@ -36,6 +38,9 @@ window.World = class { let distance2 = Math.floor(Math.pow(b.chunkX - cameraChunkX, 2) + Math.pow(b.chunkZ - cameraChunkZ, 2)); return distance2 - distance1; }); + + // Update world time + this.time++; } getChunkAt(x, z) { @@ -133,7 +138,9 @@ window.World = class { } // Add light update region to queue - this.lightUpdateQueue.push(new MetadataChunkBlock(sourceType, x1, y1, z1, x2, y2, z2)); + if (this.lightUpdateQueue.length < 10000) { + this.lightUpdateQueue.push(new MetadataChunkBlock(sourceType, x1, y1, z1, x2, y2, z2)); + } // Max light updates in queue if (this.lightUpdateQueue.length > 100000) { @@ -414,7 +421,49 @@ window.World = class { } return lastHit; - } + getCelestialAngle(partialTicks) { + return MathHelper.calculateCelestialAngle(this.time, partialTicks); + } + + getTemperature(x, z) { + return 1.24; + } + + getSkyColor(x, z, partialTicks) { + let angle = this.getCelestialAngle(partialTicks); + let brightness = Math.cos(angle * 3.141593 * 2.0) * 2.0 + 0.5; + + if (brightness < 0.0) { + brightness = 0.0; + } + if (brightness > 1.0) { + brightness = 1.0; + } + + let temperature = this.getTemperature(x, z); + let rgb = this.getSkyColorByTemp(temperature); + + let red = (rgb >> 16 & 0xff) / 255; + let green = (rgb >> 8 & 0xff) / 255; + let blue = (rgb & 0xff) / 255; + + red *= brightness; + green *= brightness; + blue *= brightness; + + return (Math.round(red * 255) << 16) | (Math.round(green * 255) << 8) | Math.round(blue * 255); + } + + getSkyColorByTemp(temperature) { + temperature /= 3; + if (temperature < -1) { + temperature = -1; + } + if (temperature > 1.0) { + temperature = 1.0; + } + return MathHelper.hsbToRgb(0.6222222 - temperature * 0.05, 0.5 + temperature * 0.1, 1.0); + } } \ No newline at end of file diff --git a/src/js/net/minecraft/util/MathHelper.js b/src/js/net/minecraft/util/MathHelper.js index d1f96a7..ccc4041 100644 --- a/src/js/net/minecraft/util/MathHelper.js +++ b/src/js/net/minecraft/util/MathHelper.js @@ -16,4 +16,67 @@ window.MathHelper = class { return degree * (Math.PI / 180); }; + static calculateCelestialAngle(time, partialTicks) { + let modTime = (time % 24000); + let angle = (modTime + partialTicks) / 24000.0 - 0.25; + + if (angle < 0.0) { + angle++; + } + if (angle > 1.0) { + angle--; + } + + angle = 1.0 - ((Math.cos(angle * Math.PI) + 1.0) / 2.0); + angle = angle + (angle - angle) / 3.0; + + return angle; + } + + static hsbToRgb(hue, saturation, brightness) { + let r = 0, g = 0, b = 0; + if (saturation === 0) { + r = g = b = Math.floor(brightness * 255.0 + 0.5); + } else { + let h = (hue - Math.floor(hue)) * 6.0; + let f = h - Math.floor(h); + let p = brightness * (1.0 - saturation); + let q = brightness * (1.0 - saturation * f); + let t = brightness * (1.0 - (saturation * (1.0 - f))); + switch (Math.floor(h)) { + case 0: + r = Math.floor(brightness * 255.0 + 0.5); + g = Math.floor(t * 255.0 + 0.5); + b = Math.floor(p * 255.0 + 0.5); + break; + case 1: + r = Math.floor(q * 255.0 + 0.5); + g = Math.floor(brightness * 255.0 + 0.5); + b = Math.floor(p * 255.0 + 0.5); + break; + case 2: + r = Math.floor(p * 255.0 + 0.5); + g = Math.floor(brightness * 255.0 + 0.5); + b = Math.floor(t * 255.0 + 0.5); + break; + case 3: + r = Math.floor(p * 255.0 + 0.5); + g = Math.floor(q * 255.0 + 0.5); + b = Math.floor(brightness * 255.0 + 0.5); + break; + case 4: + r = Math.floor(t * 255.0 + 0.5); + g = Math.floor(p * 255.0 + 0.5); + b = Math.floor(brightness * 255.0 + 0.5); + break; + case 5: + r = Math.floor(brightness * 255.0 + 0.5); + g = Math.floor(p * 255.0 + 0.5); + b = Math.floor(q * 255.0 + 0.5); + break; + } + } + return 0xff000000 | (r << 16) | (g << 8) | (b << 0); + } + } \ No newline at end of file diff --git a/src/resources/background.png b/src/resources/gui/background.png similarity index 100% rename from src/resources/background.png rename to src/resources/gui/background.png diff --git a/src/resources/font.png b/src/resources/gui/font.png similarity index 100% rename from src/resources/font.png rename to src/resources/gui/font.png diff --git a/src/resources/gui.png b/src/resources/gui/gui.png similarity index 100% rename from src/resources/gui.png rename to src/resources/gui/gui.png diff --git a/src/resources/icons.png b/src/resources/gui/icons.png similarity index 100% rename from src/resources/icons.png rename to src/resources/gui/icons.png diff --git a/src/resources/terrain/moon.png b/src/resources/terrain/moon.png new file mode 100644 index 0000000000000000000000000000000000000000..4babb189e57a939a1e73508723c9b551ea310711 GIT binary patch literal 874 zcmV-w1C{)VP)Px#1ZP1_K>z@;j|==^1poj65l~E2MF0Q*009930RsU71OfsD0s{pD1P22I2m}QQ z1O*BK0s{gA1_cHT1qKZT2Mz`Y4h9Dg000330t5yK5C;hn2MH1g3KIkc3kV7n2@4hp z3>OIu7z73j2@4kr3>gaz8Ve2_3=SO#3Ka|w9}N&74G5g`i=8wd*(3Je$w4;~H@ zBM%ZK4-+N<0Rj#YA`cTK5ELg76)6!FDiIYc4GU2=prHT%|FW{OWo2bRKtS^H@^El)YHDij?d|OB?DO;UT3T9fZ*LSCGReuw6&f_l z%gfi-*SNU2p`oGX=H`x$j=#UZR#sL{PEJ@@SXo(F5ELi_1PMXScA@|P010qNS#tmY z4#EHc4#EKyC`y0;00H7jL_t(IPpwm1dzwHL6uGP`$VRz{Zj?n7HA+>qqG-EGyS>HQ zdz-fZ|A!RC6fx=NOXqRVe6#1wnUnjk;PSj6ilV^hxx-5a94|_s03j>@N#q|=iZXyo zNktb(Eh!Ml#qyz$fU8JQL7HxurfKLJQlL~h7AXs$s%nN+t7DAoHOtV{DiDqs6>y1k ztI@OxaR{-S4NFItpz`;ZqEtmj3)_y{?s#6?b-K7`psG|n)HxZddaF;aI)kC_`@@0f zl73HDq5LEv@IcWlOx)3U5=>{)U@{&(CD_sw!0+p25h9~uUyb~ExL8KZML73IZnv>Q zP~P{Mmtujw(;3g#&z@6yee)s=#-7uRC8Ye0z=M)rv)#cYTvJM4QuZneCj-~6>80ck zp`aRd(jEp2`ufe9zI}JQSp>s2sjt+6uqQyY!ldI*m)GxUEW+4!IrTk)O$5aqA@0${ zj_1#!xcuScrPx#1ZP1_K>z@;j|==^1poj5Z%|BBMF0Q*0RR930RRI600aX71q1*F1ONvG00;&E z2?qcQ2mlKR01OEL4GI7b3jhxc01yoT5e)zm4geDm02B}a77+j!5&#tu02vej8WjK- z695|)0382!4uGd;Jz zAzH9~{6G{ZgiuNdN#gC-H==~llrhdZOVbNVOeo`7o)<-tXW)?J>=cuf=S5jnLWrsa zhtU%wrYtK;A?sQxRZCG7IZwYBF+shOwKff4w34FC`9~unX{amHSnHg#ZKG>27~MNf zC`Vm4)_LD`o$qaHz{pavN<@U^rBsdWyI};T!Mj%1Rlzrj7y_a;s89C?Fppp~S{@Qf znw3(wt{a~lz_6yS&}Se7hYA9IxIh0bc+O+z8YMQ5C^)DF0*{^Wx(|K3cqDrZXTp1) z{1xH#O|ZWyoGp$fr+Ftl#-9Y;_QUba;P83HItO@o%<1)hzcHx}0+<6^2NjwK63!3f zeV*rW>iQtCIRp<#2xY+NhH1hoP}dRy`#KSqur1SC*L&}KhkZ-z>WEB~AaA7zd1mqnVxKlm2AoYkfa$SZ{^Yo*?0(X<* xFc#n}=*4j@r?r6Mu(tlsz;Q0Pu3qDI`w#z#ECsK`>t6r>002ovPDHLkV1mG2?uGyW literal 0 HcmV?d00001 diff --git a/src/resources/terrain.png b/src/resources/terrain/terrain.png similarity index 100% rename from src/resources/terrain.png rename to src/resources/terrain/terrain.png diff --git a/src/start.js b/src/start.js index d71cf81..2f9cd86 100644 --- a/src/start.js +++ b/src/start.js @@ -69,11 +69,13 @@ function updatePreStatus(message) { // Load textures loadTexture([ - "font.png", - "gui.png", - "background.png", - "terrain.png", - "icons.png" + "gui/font.png", + "gui/gui.png", + "gui/background.png", + "gui/icons.png", + "terrain/terrain.png", + "terrain/sun.png", + "terrain/moon.png" ]).then(() => { // Load scripts loadScripts([ diff --git a/style.css b/style.css index 6751582..5854a84 100644 --- a/style.css +++ b/style.css @@ -15,7 +15,7 @@ canvas { } #background { - background-image: url(src/resources/background.png); + background-image: url(src/resources/gui/background.png); background-size: 128px 128px; image-rendering: pixelated; filter: brightness(0.5);