From 5d5e4622213d8a410dfd72066fe1c70427d1ee07 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sun, 30 Mar 2025 16:49:58 -0500 Subject: [PATCH] Fix the sun/moon and stars not rendering --- .../renderer/shaders/DhApplyShader.java | 26 ++++++++++++++++++- .../renderer/shaders/FogApplyShader.java | 7 +++++ core/src/main/resources/shaders/apply.frag | 23 ++++++++++++++-- .../src/main/resources/shaders/fog/apply.frag | 17 ++++++++++-- 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/DhApplyShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/DhApplyShader.java index 6dddd1e08..da3bd509c 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/DhApplyShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/DhApplyShader.java @@ -44,6 +44,7 @@ public class DhApplyShader extends AbstractShaderRenderer // uniforms public int gDhColorTextureUniform; + public int gDepthMapUniform; @@ -60,6 +61,7 @@ public class DhApplyShader extends AbstractShaderRenderer // uniform setup this.gDhColorTextureUniform = this.shader.getUniformLocation("gDhColorTexture"); + this.gDepthMapUniform = this.shader.getUniformLocation("gDhDepthTexture"); } @@ -97,13 +99,24 @@ public class DhApplyShader extends AbstractShaderRenderer GLMC.disableDepthTest(); - // blending isn't needed, we're just directly merging the MC and DH textures + // blending isn't needed, we're manually merging the MC and DH textures + // Note: this prevents the sun/moon and stars from rendering through transparent LODs, + // however this also fixes transparent LODs from glowing when rendered against the sky during the day GLMC.disableBlend(); + // old blending logic in case it's ever needed: + //GLMC.enableBlend(); + //GL32.glBlendEquation(GL32.GL_FUNC_ADD); + //GLMC.glBlendFunc(GL32.GL_ONE, GL32.GL_ONE_MINUS_SRC_ALPHA); + GLMC.glActiveTexture(GL32.GL_TEXTURE0); GLMC.glBindTexture(LodRenderer.getActiveColorTextureId()); GL32.glUniform1i(this.gDhColorTextureUniform, 0); + GLMC.glActiveTexture(GL32.GL_TEXTURE1); + GLMC.glBindTexture(LodRenderer.getActiveDepthTextureId()); + GL32.glUniform1i(this.gDepthMapUniform, 1); + // Copy to MC's framebuffer GLMC.glBindFramebuffer(GL32.GL_FRAMEBUFFER, targetFrameBuffer); @@ -142,12 +155,23 @@ public class DhApplyShader extends AbstractShaderRenderer GLMC.disableDepthTest(); // blending isn't needed, we're just directly merging the MC and DH textures + // Note: this prevents the sun/moon and stars from rendering through transparent LODs, + // however this also fixes GLMC.disableBlend(); + // old blending logic in case it's ever needed: + //GLMC.enableBlend(); + //GL32.glBlendEquation(GL32.GL_FUNC_ADD); + //GLMC.glBlendFunc(GL32.GL_ONE, GL32.GL_ONE_MINUS_SRC_ALPHA); + GLMC.glActiveTexture(GL32.GL_TEXTURE0); GLMC.glBindTexture(LodRenderer.getActiveColorTextureId()); GL32.glUniform1i(this.gDhColorTextureUniform, 0); + GLMC.glActiveTexture(GL32.GL_TEXTURE1); + GLMC.glBindTexture(LodRenderer.getActiveDepthTextureId()); + GL32.glUniform1i(this.gDepthMapUniform, 1); + GL32.glFramebufferTexture(GL32.GL_DRAW_FRAMEBUFFER, GL32.GL_COLOR_ATTACHMENT0, targetColorTextureId, 0); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogApplyShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogApplyShader.java index 0263c9285..7f0ca9156 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogApplyShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogApplyShader.java @@ -45,6 +45,7 @@ public class FogApplyShader extends AbstractShaderRenderer // uniforms public int colorTextureUniform; + public int depthTextureUniform; @@ -63,6 +64,7 @@ public class FogApplyShader extends AbstractShaderRenderer // uniform setup this.colorTextureUniform = this.shader.getUniformLocation("uColorTexture"); + this.depthTextureUniform = this.shader.getUniformLocation("uDepthTexture"); } @@ -78,6 +80,11 @@ public class FogApplyShader extends AbstractShaderRenderer GLMC.glActiveTexture(GL32.GL_TEXTURE0); GLMC.glBindTexture(this.fogTexture); GL32.glUniform1i(this.colorTextureUniform, 0); + + GLMC.glActiveTexture(GL32.GL_TEXTURE1); + GLMC.glBindTexture(LodRenderer.getActiveDepthTextureId()); + GL32.glUniform1i(this.depthTextureUniform, 1); + } diff --git a/core/src/main/resources/shaders/apply.frag b/core/src/main/resources/shaders/apply.frag index 70a9dcefc..ba11b49ef 100644 --- a/core/src/main/resources/shaders/apply.frag +++ b/core/src/main/resources/shaders/apply.frag @@ -5,10 +5,29 @@ in vec2 TexCoord; out vec4 fragColor; uniform sampler2D gDhColorTexture; +uniform sampler2D gDhDepthTexture; - +/** + * LOD application shader + * + * This merges the rendered LODs into Minecraft's texture/FBO + */ void main() { - fragColor = texture(gDhColorTexture, TexCoord); + fragColor = vec4(0.0); + + // a fragment depth of "1" means the fragment wasn't drawn to, + // only update fragments that were drawn to + float fragmentDepth = texture(gDhDepthTexture, TexCoord).r; + if (fragmentDepth != 1) + { + fragColor = texture(gDhColorTexture, TexCoord); + } + else + { + // use the original MC texture if no LODs were drawn to this fragment + discard; + } } + diff --git a/core/src/main/resources/shaders/fog/apply.frag b/core/src/main/resources/shaders/fog/apply.frag index 192b3b056..e74a053e5 100644 --- a/core/src/main/resources/shaders/fog/apply.frag +++ b/core/src/main/resources/shaders/fog/apply.frag @@ -5,10 +5,23 @@ in vec2 TexCoord; out vec4 fragColor; uniform sampler2D uColorTexture; +uniform sampler2D uDepthTexture; - +/** + * Fog application shader + * + * This merges the rendered fog onto DH's rendered LODs + */ void main() { - fragColor = texture(uColorTexture, TexCoord); + fragColor = vec4(0.0); + + // a fragment depth of "1" means the fragment wasn't drawn to, + // only update fragments that were drawn to + float fragmentDepth = textureLod(uDepthTexture, TexCoord, 0).r; + if (fragmentDepth != 1) + { + fragColor = texture(uColorTexture, TexCoord); + } }