From 634738a1fc1dc2b017ba0fec208145d3320297d0 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 13 Jul 2024 11:35:25 -0500 Subject: [PATCH] Remove minimum near fog distance and rename fog uniforms --- .../core/render/fog/LodFogConfig.java | 12 ++-- .../renderer/shaders/FogApplyShader.java | 15 ++--- .../render/renderer/shaders/FogShader.java | 55 +++++++++---------- .../src/main/resources/shaders/fog/apply.frag | 8 +-- core/src/main/resources/shaders/fog/fog.frag | 34 ++++++------ 5 files changed, 60 insertions(+), 64 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/fog/LodFogConfig.java b/core/src/main/java/com/seibel/distanthorizons/core/render/fog/LodFogConfig.java index be4adf73e..8e0873b93 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/fog/LodFogConfig.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/fog/LodFogConfig.java @@ -173,7 +173,7 @@ public class LodFogConfig str.append("" + "float getNearFogThickness(float dist) \n" + "{ \n" + - " return linearFog(dist, nearFogStart, nearFogLength, 0.0, 1.0); \n" + + " return linearFog(dist, uNearFogStart, uNearFogLength, 0.0, 1.0); \n" + "} \n"); @@ -182,7 +182,7 @@ public class LodFogConfig str.append("\n" + "float getFarFogThickness(float dist) { return 0.0; } \n" + "float getHeightFogThickness(float dist) { return 0.0; } \n" + - "float calculateFarFogDepth(float horizontal, float dist, float nearFogStart) { return 0.0; } \n" + + "float calculateFarFogDepth(float horizontal, float dist, float uNearFogStart) { return 0.0; } \n" + "float calculateHeightFogDepth(float vertical, float realY) { return 0.0; } \n" + "float mixFogThickness(float near, float far, float height) \n" + "{ \n" + @@ -215,13 +215,13 @@ public class LodFogConfig "} \n"); - // Generate method: calculateFarFogDepth(float horizontal, float dist, float nearFogStart); + // Generate method: calculateFarFogDepth(float horizontal, float dist, float uNearFogStart); str.append("" + - "float calculateFarFogDepth(float horizontal, float dist, float nearFogStart) \n" + + "float calculateFarFogDepth(float horizontal, float dist, float uNearFogStart) \n" + "{ \n" + " return " + (heightFogMixMode == EDhApiHeightFogMixMode.BASIC ? - "(dist - nearFogStart)/(1.0 - nearFogStart);" : - "(horizontal - nearFogStart)/(1.0 - nearFogStart);") + + "(dist - uNearFogStart)/(1.0 - uNearFogStart);" : + "(horizontal - uNearFogStart)/(1.0 - uNearFogStart);") + "} \n"); // Generate method: float mixFogThickness(float near, float far, float height); 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 45e2d8d39..4906b75b2 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 @@ -19,13 +19,10 @@ package com.seibel.distanthorizons.core.render.renderer.shaders; -import com.seibel.distanthorizons.core.config.Config; import com.seibel.distanthorizons.core.render.glObject.shader.ShaderProgram; import com.seibel.distanthorizons.core.render.renderer.FogRenderer; import com.seibel.distanthorizons.core.render.renderer.LodRenderer; -import com.seibel.distanthorizons.core.render.renderer.SSAORenderer; import com.seibel.distanthorizons.core.render.renderer.ScreenQuad; -import com.seibel.distanthorizons.core.util.RenderUtil; import org.lwjgl.opengl.GL32; /** @@ -42,8 +39,8 @@ public class FogApplyShader extends AbstractShaderRenderer public int fogTexture; // uniforms - public int gColorTextureUniform; - public int gDepthTextureUniform; + public int colorTextureUniform; + public int depthTextureUniform; @@ -61,8 +58,8 @@ public class FogApplyShader extends AbstractShaderRenderer new String[]{ "vPosition" }); // uniform setup - this.gColorTextureUniform = this.shader.getUniformLocation("gColorTexture"); - this.gDepthTextureUniform = this.shader.getUniformLocation("gDepthTexture"); + this.colorTextureUniform = this.shader.getUniformLocation("uColorTexture"); + this.depthTextureUniform = this.shader.getUniformLocation("uDepthTexture"); } @@ -77,11 +74,11 @@ public class FogApplyShader extends AbstractShaderRenderer { GL32.glActiveTexture(GL32.GL_TEXTURE0); GL32.glBindTexture(GL32.GL_TEXTURE_2D, this.fogTexture); - GL32.glUniform1i(this.gColorTextureUniform, 0); + GL32.glUniform1i(this.colorTextureUniform, 0); GL32.glActiveTexture(GL32.GL_TEXTURE1); GL32.glBindTexture(GL32.GL_TEXTURE_2D, LodRenderer.getActiveDepthTextureId()); - GL32.glUniform1i(this.gDepthTextureUniform, 1); + GL32.glUniform1i(this.depthTextureUniform, 1); } diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java index a35037253..5987af82e 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/shaders/FogShader.java @@ -51,15 +51,16 @@ public class FogShader extends AbstractShaderRenderer // Uniforms - public int fogColorUniform; - public int fogScaleUniform; - public int fogVerticalScaleUniform; - public int nearFogStartUniform; - public int nearFogLengthUniform; - public int fullFogModeUniform; + public int uFogColor; + public int uFogScale; + public int uFogVerticalScale; + public int uNearFogStart; + public int uNearFogLength; + public int uFullFogMode; - public int gInvertedModelViewProjectionUniform; - public int gDepthMapUniform; + /** Inverted Model View Projection matrix */ + public int uInvMvmProj; + public int uDepthMap; @@ -82,18 +83,19 @@ public class FogShader extends AbstractShaderRenderer // all uniforms should be tryGet... // because disabling fog can cause the GLSL to optimize out most (if not all) uniforms - this.gInvertedModelViewProjectionUniform = this.shader.getUniformLocation("gInvMvmProj"); - this.gDepthMapUniform = this.shader.getUniformLocation("gDepthMap"); + this.uDepthMap = this.shader.getUniformLocation("uDepthMap"); + this.uInvMvmProj = this.shader.getUniformLocation("uInvMvmProj"); // Fog uniforms - this.fogColorUniform = this.shader.tryGetUniformLocation("fogColor"); - this.fullFogModeUniform = this.shader.tryGetUniformLocation("fullFogMode"); - this.fogScaleUniform = this.shader.tryGetUniformLocation("fogScale"); - this.fogVerticalScaleUniform = this.shader.tryGetUniformLocation("fogVerticalScale"); + this.uFogScale = this.shader.tryGetUniformLocation("uFogScale"); + this.uFogVerticalScale = this.shader.tryGetUniformLocation("uFogVerticalScale"); + this.uFogColor = this.shader.tryGetUniformLocation("uFogColor"); + this.uFullFogMode = this.shader.tryGetUniformLocation("uFullFogMode"); // near fog - this.nearFogStartUniform = this.shader.tryGetUniformLocation("nearFogStart"); - this.nearFogLengthUniform = this.shader.tryGetUniformLocation("nearFogLength"); + this.uNearFogStart = this.shader.tryGetUniformLocation("uNearFogStart"); + this.uNearFogLength = this.shader.tryGetUniformLocation("uNearFogLength"); + } @@ -107,23 +109,20 @@ public class FogShader extends AbstractShaderRenderer { if (this.inverseMvmProjMatrix != null) { - this.shader.setUniform(this.gInvertedModelViewProjectionUniform, this.inverseMvmProjMatrix); + this.shader.setUniform(this.uInvMvmProj, this.inverseMvmProjMatrix); } int lodDrawDistance = Config.Client.Advanced.Graphics.Quality.lodChunkRenderDistanceRadius.get() * LodUtil.CHUNK_WIDTH; - int vanillaDrawDistance = MC_RENDER.getRenderDistance() * LodUtil.CHUNK_WIDTH; - vanillaDrawDistance += LodUtil.CHUNK_WIDTH * 2; // Give it a 2 chunk boundary for near fog. // Fog - if (this.fullFogModeUniform != -1) this.shader.setUniform(this.fullFogModeUniform, MC_RENDER.isFogStateSpecial() ? 1 : 0); - if (this.fogColorUniform != -1) this.shader.setUniform(this.fogColorUniform, MC_RENDER.isFogStateSpecial() ? this.getSpecialFogColor(partialTicks) : this.getFogColor(partialTicks)); + if (this.uFullFogMode != -1) this.shader.setUniform(this.uFullFogMode, MC_RENDER.isFogStateSpecial() ? 1 : 0); + if (this.uFogColor != -1) this.shader.setUniform(this.uFogColor, MC_RENDER.isFogStateSpecial() ? this.getSpecialFogColor(partialTicks) : this.getFogColor(partialTicks)); - float nearFogLen = vanillaDrawDistance * 0.2f / lodDrawDistance; - float nearFogStart = vanillaDrawDistance * (VERSION_CONSTANTS.isVanillaRenderedChunkSquare() ? (float) Math.sqrt(2.0) : 1.0f) / lodDrawDistance; - if (this.nearFogStartUniform != -1) this.shader.setUniform(this.nearFogStartUniform, nearFogStart); - if (this.nearFogLengthUniform != -1) this.shader.setUniform(this.nearFogLengthUniform, nearFogLen); - if (this.fogScaleUniform != -1) this.shader.setUniform(this.fogScaleUniform, 1.f / lodDrawDistance); - if (this.fogVerticalScaleUniform != -1) this.shader.setUniform(this.fogVerticalScaleUniform, 1.f / MC.getWrappedClientLevel().getMaxHeight()); + float nearFogStart = (VERSION_CONSTANTS.isVanillaRenderedChunkSquare() ? (float) Math.sqrt(2.0) : 1.0f) / lodDrawDistance; + if (this.uNearFogStart != -1) this.shader.setUniform(this.uNearFogStart, nearFogStart); + if (this.uNearFogLength != -1) this.shader.setUniform(this.uNearFogLength, 0.0f); + if (this.uFogScale != -1) this.shader.setUniform(this.uFogScale, 1.f / lodDrawDistance); + if (this.uFogVerticalScale != -1) this.shader.setUniform(this.uFogVerticalScale, 1.f / MC.getWrappedClientLevel().getMaxHeight()); } private Color getFogColor(float partialTicks) { @@ -166,7 +165,7 @@ public class FogShader extends AbstractShaderRenderer GL32.glActiveTexture(GL32.GL_TEXTURE0); GL32.glBindTexture(GL32.GL_TEXTURE_2D, LodRenderer.getActiveDepthTextureId()); - GL32.glUniform1i(this.gDepthMapUniform, 0); + GL32.glUniform1i(this.uDepthMap, 0); ScreenQuad.INSTANCE.render(); diff --git a/core/src/main/resources/shaders/fog/apply.frag b/core/src/main/resources/shaders/fog/apply.frag index 390599b07..7241d1161 100644 --- a/core/src/main/resources/shaders/fog/apply.frag +++ b/core/src/main/resources/shaders/fog/apply.frag @@ -4,8 +4,8 @@ in vec2 TexCoord; out vec4 fragColor; -uniform sampler2D gColorTexture; -uniform sampler2D gDepthTexture; +uniform sampler2D uColorTexture; +uniform sampler2D uDepthTexture; @@ -13,12 +13,12 @@ void main() { fragColor = vec4(1.0); - float fragmentDepth = textureLod(gDepthTexture, TexCoord, 0).r; + float fragmentDepth = textureLod(uDepthTexture, TexCoord, 0).r; // a fragment depth of "1" means the fragment wasn't drawn to, // only update fragments that were drawn to if (fragmentDepth != 1) { - fragColor = texture(gColorTexture, TexCoord); + fragColor = texture(uColorTexture, TexCoord); } } diff --git a/core/src/main/resources/shaders/fog/fog.frag b/core/src/main/resources/shaders/fog/fog.frag index 04eb3f7c1..eed4c6b6b 100644 --- a/core/src/main/resources/shaders/fog/fog.frag +++ b/core/src/main/resources/shaders/fog/fog.frag @@ -3,17 +3,17 @@ in vec2 TexCoord; out vec4 fragColor; -uniform sampler2D gDepthMap; +uniform sampler2D uDepthMap; // inverted model view matrix and projection matrix -uniform mat4 gInvMvmProj; +uniform mat4 uInvMvmProj; -uniform float fogScale; -uniform float fogVerticalScale; -uniform float nearFogStart; -uniform float nearFogLength; -uniform int fullFogMode; +uniform float uFogScale; +uniform float uFogVerticalScale; +uniform vec4 uFogColor; +uniform int uFullFogMode; -uniform vec4 fogColor; +uniform float uNearFogStart; +uniform float uNearFogLength; /* ========MARCO DEFINED BY RUNTIME CODE GEN========= @@ -53,7 +53,7 @@ vec3 calcViewPosition(float fragmentDepth) { vec4 ndc = vec4(TexCoord.xy, fragmentDepth, 1.0); ndc.xyz = ndc.xyz * 2.0 - 1.0; - vec4 eyeCoord = gInvMvmProj * ndc; + vec4 eyeCoord = uInvMvmProj * ndc; return eyeCoord.xyz / eyeCoord.w; } @@ -66,19 +66,19 @@ vec3 calcViewPosition(float fragmentDepth) { void main() { float vertexYPos = 100.0f; - float fragmentDepth = texture(gDepthMap, TexCoord).r; - fragColor = vec4(fogColor.rgb, 0.0); + float fragmentDepth = texture(uDepthMap, TexCoord).r; + fragColor = vec4(uFogColor.rgb, 0.0); // a fragment depth of "1" means the fragment wasn't drawn to, // we only want to apply Fog to LODs, not to the sky outside the LODs if (fragmentDepth < 1.0) { - if (fullFogMode == 0) { + if (uFullFogMode == 0) { // render fog based on distance from the camera vec3 vertexWorldPos = calcViewPosition(fragmentDepth); - float horizontalDist = length(vertexWorldPos.xz) * fogScale; - float heightDist = calculateHeightFogDepth(vertexWorldPos.y, vertexYPos) * fogVerticalScale; - float farDist = calculateFarFogDepth(horizontalDist, length(vertexWorldPos.xyz) * fogScale, nearFogStart); + float horizontalDist = length(vertexWorldPos.xz) * uFogScale; + float heightDist = calculateHeightFogDepth(vertexWorldPos.y, vertexYPos) * uFogVerticalScale; + float farDist = calculateFarFogDepth(horizontalDist, length(vertexWorldPos.xyz) * uFogScale, uNearFogStart); float nearFogThickness = getNearFogThickness(horizontalDist); float farFogThickness = getFarFogThickness(farDist); @@ -89,7 +89,7 @@ void main() float dither = InterleavedGradientNoise(gl_FragCoord.xy) - 0.5; fragColor.a += dither / 255.0; } - else if (fullFogMode == 1) { + else if (uFullFogMode == 1) { // render everything with the fog color fragColor.a = 1.0; } @@ -101,7 +101,7 @@ void main() // a uniform we don't have to worry about GLSL optimizing away different // options when testing, causing a bunch of headaches if we just want to render the screen red. - float depthValue = textureLod(gDepthMap, TexCoord, 0).r; + float depthValue = textureLod(uDepthMap, TexCoord, 0).r; fragColor.rgb = vec3(depthValue); // Convert depth value to grayscale color fragColor.a = 1.0; }