From d1ba402f4d1db1f8cd98ef880984de089429153b Mon Sep 17 00:00:00 2001 From: James Seibel Date: Tue, 24 Feb 2026 09:57:03 -0600 Subject: [PATCH] start of vanilla fade logic --- .../core/api/internal/ClientApi.java | 71 +++++++++------ .../core/render/renderer/TestRenderer.java | 1 + .../render/IMcFadeRenderer.java | 29 ++++++ .../distanthorizons/shaders/fade/frag.fsh | 24 +++++ .../shaders/fade/vanillaFade.fsh | 91 +++++++++++++++++++ .../distanthorizons/shaders/fade/vert.vsh | 15 +++ .../distanthorizons/shaders/quadApply.vsh | 15 +++ 7 files changed, 216 insertions(+), 30 deletions(-) create mode 100644 core/src/main/java/com/seibel/distanthorizons/core/wrapperInterfaces/render/IMcFadeRenderer.java create mode 100644 core/src/main/resources/assets/distanthorizons/shaders/fade/frag.fsh create mode 100644 core/src/main/resources/assets/distanthorizons/shaders/fade/vanillaFade.fsh create mode 100644 core/src/main/resources/assets/distanthorizons/shaders/fade/vert.vsh create mode 100644 core/src/main/resources/assets/distanthorizons/shaders/quadApply.vsh diff --git a/core/src/main/java/com/seibel/distanthorizons/core/api/internal/ClientApi.java b/core/src/main/java/com/seibel/distanthorizons/core/api/internal/ClientApi.java index 9c3eb066e..0002eaa9b 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/api/internal/ClientApi.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/api/internal/ClientApi.java @@ -38,6 +38,7 @@ import com.seibel.distanthorizons.core.util.objects.Pair; import com.seibel.distanthorizons.core.util.objects.RollingAverage; import com.seibel.distanthorizons.core.util.threading.ThreadPoolUtil; import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper; +import com.seibel.distanthorizons.core.wrapperInterfaces.render.IMcFadeRenderer; import com.seibel.distanthorizons.core.wrapperInterfaces.render.IMcTestRenderer; import com.seibel.distanthorizons.coreapi.DependencyInjection.ApiEventInjector; import com.seibel.distanthorizons.core.config.Config; @@ -585,7 +586,7 @@ public class ClientApi boolean renderingCancelled = ApiEventInjector.INSTANCE.fireAllEvents(DhApiBeforeRenderEvent.class, renderParams); if (!renderingCancelled) { - testRenderer.render(); + //testRenderer.render(); //LodRenderer.INSTANCE.render(renderParams, profiler); } @@ -644,20 +645,28 @@ public class ClientApi */ public void renderFadeOpaque() { - // only fade when DH is rendering - if (Config.Client.Advanced.Debugging.rendererMode.get() != EDhApiRendererMode.DISABLED - && - ( - // only fade when requested - Config.Client.Advanced.Graphics.Quality.vanillaFadeMode.get() == EDhApiMcRenderingFadeMode.DOUBLE_PASS - // or if LOD-only mode is enabled (fading is used to remove the MC render pass) - || Config.Client.Advanced.Debugging.lodOnlyMode.get() - ) - // don't fade when Iris shaders are active, otherwise the rendering can get weird - && !DhApiRenderProxy.INSTANCE.getDeferTransparentRendering()) + IMcFadeRenderer fadeRenderer = SingletonInjector.INSTANCE.get(IMcFadeRenderer.class); + if (fadeRenderer == null) { - VanillaFadeRenderer.INSTANCE.render(RENDER_STATE.mcModelViewMatrix, RENDER_STATE.mcProjectionMatrix, RENDER_STATE.partialTickTime, RENDER_STATE.clientLevelWrapper); + return; } + fadeRenderer.render(); + + + //// only fade when DH is rendering + //if (Config.Client.Advanced.Debugging.rendererMode.get() != EDhApiRendererMode.DISABLED + // && + // ( + // // only fade when requested + // Config.Client.Advanced.Graphics.Quality.vanillaFadeMode.get() == EDhApiMcRenderingFadeMode.DOUBLE_PASS + // // or if LOD-only mode is enabled (fading is used to remove the MC render pass) + // || Config.Client.Advanced.Debugging.lodOnlyMode.get() + // ) + // // don't fade when Iris shaders are active, otherwise the rendering can get weird + // && !DhApiRenderProxy.INSTANCE.getDeferTransparentRendering()) + //{ + // VanillaFadeRenderer.INSTANCE.render(RENDER_STATE.mcModelViewMatrix, RENDER_STATE.mcProjectionMatrix, RENDER_STATE.partialTickTime, RENDER_STATE.clientLevelWrapper); + //} } /** * The second fade pass. @@ -666,23 +675,25 @@ public class ClientApi */ public void renderFadeTransparent() { - // only fade when DH is rendering - if (Config.Client.Advanced.Debugging.rendererMode.get() != EDhApiRendererMode.DISABLED) - { - boolean renderFade = - ( - // only fade when requested - Config.Client.Advanced.Graphics.Quality.vanillaFadeMode.get() != EDhApiMcRenderingFadeMode.NONE - // or if LOD-only mode is enabled (fading is used to remove the MC render pass) - || Config.Client.Advanced.Debugging.lodOnlyMode.get() - ) - // don't fade when Iris shaders are active, otherwise the rendering can get weird - && !DhApiRenderProxy.INSTANCE.getDeferTransparentRendering(); - if (renderFade) - { - VanillaFadeRenderer.INSTANCE.render(RENDER_STATE.mcModelViewMatrix, RENDER_STATE.mcProjectionMatrix, RENDER_STATE.partialTickTime, RENDER_STATE.clientLevelWrapper); - } - } + + + //// only fade when DH is rendering + //if (Config.Client.Advanced.Debugging.rendererMode.get() != EDhApiRendererMode.DISABLED) + //{ + // boolean renderFade = + // ( + // // only fade when requested + // Config.Client.Advanced.Graphics.Quality.vanillaFadeMode.get() != EDhApiMcRenderingFadeMode.NONE + // // or if LOD-only mode is enabled (fading is used to remove the MC render pass) + // || Config.Client.Advanced.Debugging.lodOnlyMode.get() + // ) + // // don't fade when Iris shaders are active, otherwise the rendering can get weird + // && !DhApiRenderProxy.INSTANCE.getDeferTransparentRendering(); + // if (renderFade) + // { + // VanillaFadeRenderer.INSTANCE.render(RENDER_STATE.mcModelViewMatrix, RENDER_STATE.mcProjectionMatrix, RENDER_STATE.partialTickTime, RENDER_STATE.clientLevelWrapper); + // } + //} } ///endregion diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/TestRenderer.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/TestRenderer.java index 0b55d611e..eadf83958 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/TestRenderer.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/TestRenderer.java @@ -40,6 +40,7 @@ import java.nio.ByteOrder; * to the center of the screen to confirm DH's * apply shader is running correctly */ +@Deprecated public class TestRenderer { public static final DhLogger LOGGER = new DhLoggerBuilder().build(); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/wrapperInterfaces/render/IMcFadeRenderer.java b/core/src/main/java/com/seibel/distanthorizons/core/wrapperInterfaces/render/IMcFadeRenderer.java new file mode 100644 index 000000000..4180fc986 --- /dev/null +++ b/core/src/main/java/com/seibel/distanthorizons/core/wrapperInterfaces/render/IMcFadeRenderer.java @@ -0,0 +1,29 @@ +/* + * This file is part of the Distant Horizons mod + * licensed under the GNU LGPL v3 License. + * + * Copyright (C) 2020 James Seibel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.seibel.distanthorizons.core.wrapperInterfaces.render; + +import com.seibel.distanthorizons.coreapi.interfaces.dependencyInjection.IBindable; + +public interface IMcFadeRenderer extends IBindable +{ + + void render(); + +} diff --git a/core/src/main/resources/assets/distanthorizons/shaders/fade/frag.fsh b/core/src/main/resources/assets/distanthorizons/shaders/fade/frag.fsh new file mode 100644 index 000000000..72751ec8f --- /dev/null +++ b/core/src/main/resources/assets/distanthorizons/shaders/fade/frag.fsh @@ -0,0 +1,24 @@ +#version 150 core + +in vec2 TexCoord; +in vec4 fColor; + +out vec4 fragColor; + +uniform sampler2D uDhDepthTexture; + +// DH fade frag test +void main() +{ + float dhFragmentDepth = texture(uDhDepthTexture, TexCoord).r; + if (dhFragmentDepth == 0) + { + // no MC depth + fragColor = fColor; + } + else + { + // MC depth drawn + fragColor = vec4(0, 0, 1, 1); // green + } +} \ No newline at end of file diff --git a/core/src/main/resources/assets/distanthorizons/shaders/fade/vanillaFade.fsh b/core/src/main/resources/assets/distanthorizons/shaders/fade/vanillaFade.fsh new file mode 100644 index 000000000..da2064fa0 --- /dev/null +++ b/core/src/main/resources/assets/distanthorizons/shaders/fade/vanillaFade.fsh @@ -0,0 +1,91 @@ +#version 150 core + +in vec2 TexCoord; + +out vec4 fragColor; + +// inverted model view matrix and projection matrix +uniform mat4 uDhInvMvmProj; +uniform mat4 uMcInvMvmProj; + +uniform sampler2D uMcDepthTexture; +uniform sampler2D uDhDepthTexture; +uniform sampler2D uCombinedMcDhColorTexture; +uniform sampler2D uDhColorTexture; + +uniform float uStartFadeBlockDistance; +uniform float uEndFadeBlockDistance; +uniform float uMaxLevelHeight; + +uniform bool uOnlyRenderLods; + + + +vec3 calcViewPosition(float fragmentDepth, mat4 invMvmProj) +{ + // normalized device coordinates + vec4 ndc = vec4(TexCoord.xy, fragmentDepth, 1.0); + ndc.xyz = ndc.xyz * 2.0 - 1.0; + + vec4 eyeCoord = invMvmProj * ndc; + return eyeCoord.xyz / eyeCoord.w; +} + +/** + * Used to fade out vanilla chunks so the transition + * between DH and vanilla is smoother. + */ +void main() +{ + // includes both the vanilla chunks as well as DH + vec4 combinedMcDhColor = texture(uCombinedMcDhColorTexture, TexCoord); + // just the DH render pass + vec4 dhColor = texture(uDhColorTexture, TexCoord); + + // completely remove the MC render pass to only show LODs + // useful for debugging/troubleshooting, but doesn't improve performance since MC is still rendering + if (uOnlyRenderLods) + { + fragColor = dhColor; + return; + } + + + // ignore anything that DH hasn't drawn to + // We don't use DH's depth here because it would prevent the fade from running before DH has loaded + if (dhColor == vec4(1)) + { + // if not done vanilla clouds will render incorrectly at night + dhColor = combinedMcDhColor; + } + + float mcFragmentDepth = texture(uMcDepthTexture, TexCoord).r; + float dhFragmentDepth = texture(uDhDepthTexture, TexCoord).r; + vec3 dhVertexWorldPos = calcViewPosition(dhFragmentDepth, uDhInvMvmProj); + + // this is a work around to prevent MC clouds rendering behind DH clouds + if (dhVertexWorldPos.y > uMaxLevelHeight) + { + fragColor = vec4(combinedMcDhColor.rgb, 0.0); + } + // a fragment depth of "1" means the fragment wasn't drawn to, + // we only want to fade vanilla rendered objects, not to the sky or LODs + else if (mcFragmentDepth < 1.0) + { + // fade based on distance from the camera + vec3 mcVertexWorldPos = calcViewPosition(mcFragmentDepth, uMcInvMvmProj); + float mcFragmentDistance = length(mcVertexWorldPos.xzy); + + + // Smoothly transition between combinedMcDhColor and uDhColorTexture + // as the depth increases from the camera + float fadeStep = smoothstep(uStartFadeBlockDistance, uEndFadeBlockDistance, mcFragmentDistance); + fragColor = mix(combinedMcDhColor, dhColor, fadeStep); + fragColor.a = 1.0; + } + else + { + fragColor = vec4(combinedMcDhColor.rgb, 0.0); + } +} + diff --git a/core/src/main/resources/assets/distanthorizons/shaders/fade/vert.vsh b/core/src/main/resources/assets/distanthorizons/shaders/fade/vert.vsh new file mode 100644 index 000000000..d7f46cf36 --- /dev/null +++ b/core/src/main/resources/assets/distanthorizons/shaders/fade/vert.vsh @@ -0,0 +1,15 @@ +#version 150 core + +in vec2 vPosition; +in vec4 vColor; + +out vec4 fColor; +out vec2 TexCoord; + +// DH vert fade test +void main() +{ + gl_Position = vec4(vPosition, 0.0, 1.0); + fColor = vec4(vPosition, 0.0, 1.0); + TexCoord = vPosition.xy * 0.5 + 0.5; +} \ No newline at end of file diff --git a/core/src/main/resources/assets/distanthorizons/shaders/quadApply.vsh b/core/src/main/resources/assets/distanthorizons/shaders/quadApply.vsh new file mode 100644 index 000000000..3f614c123 --- /dev/null +++ b/core/src/main/resources/assets/distanthorizons/shaders/quadApply.vsh @@ -0,0 +1,15 @@ +#version 150 core + +in vec2 vPosition; + +out vec2 TexCoord; + +/** + * This is specifically used by application shaders. + * IE post process or pixel transfer shaders, anything that is rendered using a single rectangle. + */ +void main() +{ + gl_Position = vec4(vPosition, 1.0, 1.0); + TexCoord = vPosition.xy * 0.5 + 0.5; +} \ No newline at end of file