Improve terrain fog quality and refactor cloud rendering #52

Merged
embeddedt merged 8 commits from rdh/celeritas:sky-fog into stonecutter 2026-11-19 21:51:49 -05:00
3 changed files with 31 additions and 22 deletions
Showing only changes of commit 78f1221991 - Show all commits

No files matched your search

@@ -18,6 +18,7 @@ import java.util.Map;
import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL; import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
public final class SkyFog { public final class SkyFog {
// The capture leaves the sky texture bound here for the rest of the frame, so nothing else may use this unit.
public static final int TEXTURE_UNIT = 14; public static final int TEXTURE_UNIT = 14;
private static final Map<ChunkShaderComponent.Factory<?>, Variant> VARIANTS = new IdentityHashMap<>(); private static final Map<ChunkShaderComponent.Factory<?>, Variant> VARIANTS = new IdentityHashMap<>();
@@ -41,7 +42,13 @@ public final class SkyFog {
} }
public static void capture(int width, int height) { public static void capture(int width, int height) {
int previous = bind(); if (texture == 0) {
texture = LWJGL.glGenTextures();
}
int previous = LWJGL.glGetInteger(GL13.GL_ACTIVE_TEXTURE);
LWJGL.glActiveTexture(GL13.GL_TEXTURE0 + TEXTURE_UNIT);
LWJGL.glBindTexture(GL11.GL_TEXTURE_2D, texture);
if (SkyFog.width != width || SkyFog.height != height) { if (SkyFog.width != width || SkyFog.height != height) {
SkyFog.width = width; SkyFog.width = width;
@@ -82,17 +89,6 @@ public final class SkyFog {
captured = false; captured = false;
} }
private static int bind() {
if (texture == 0) {
texture = LWJGL.glGenTextures();
}
int previous = LWJGL.glGetInteger(GL13.GL_ACTIVE_TEXTURE);
LWJGL.glActiveTexture(GL13.GL_TEXTURE0 + TEXTURE_UNIT);
LWJGL.glBindTexture(GL11.GL_TEXTURE_2D, texture);
return previous;
}
private static final class Variant implements ChunkShaderComponent.Factory<ChunkShaderComponent> { private static final class Variant implements ChunkShaderComponent.Factory<ChunkShaderComponent> {
private final ChunkShaderComponent.Factory<?> fog; private final ChunkShaderComponent.Factory<?> fog;
private final List<String> defines; private final List<String> defines;
@@ -116,11 +112,13 @@ public final class SkyFog {
if (sampler != null) { if (sampler != null) {
sampler.setInt(TEXTURE_UNIT); sampler.setInt(TEXTURE_UNIT);
LWJGL.glActiveTexture(bind());
} }
if (edgeStart != null) { if (edgeStart != null) {
edgeStart.setFloat(fogService.getSkyFogEdgeStart()); edgeStart.setFloat(fogService.getSkyFogEdgeStart());
}
if (edgeEnd != null) {
edgeEnd.setFloat(fogService.getSkyFogEdgeEnd()); edgeEnd.setFloat(fogService.getSkyFogEdgeEnd());
} }
}; };
@@ -216,8 +216,12 @@ public abstract class SimpleWorldRenderer<WORLD, SECTIONMANAGER extends RenderSe
* Performs a render pass for the given {@link LAYER} and draws all visible chunks for it. * Performs a render pass for the given {@link LAYER} and draws all visible chunks for it.
*/ */
public void drawChunkLayer(LAYER renderLayer, double x, double y, double z) { public void drawChunkLayer(LAYER renderLayer, double x, double y, double z) {
if (!SkyFog.isCaptured() && SkyFog.isEnabled(this.renderSectionManager.getFogService())) { if (!SkyFog.isCaptured()) {
this.captureSkyFog(); if (SkyFog.isEnabled(this.renderSectionManager.getFogService())) {
this.captureSkyFog();
} else {
SkyFog.delete();
}
} }
ChunkRenderMatrices matrices = createChunkRenderMatrices(); ChunkRenderMatrices matrices = createChunkRenderMatrices();
@@ -28,12 +28,12 @@ uniform sampler2D u_SkyTex;
uniform float u_SkyFogEdgeStart; uniform float u_SkyFogEdgeStart;
uniform float u_SkyFogEdgeEnd; uniform float u_SkyFogEdgeEnd;
#ifdef LEGACY
#define texelFetch texelFetch2D
#endif
vec3 _skyColor() { vec3 _skyColor() {
#ifdef LEGACY
return texelFetch2D(u_SkyTex, ivec2(gl_FragCoord.xy), 0).rgb;
#else
return texelFetch(u_SkyTex, ivec2(gl_FragCoord.xy), 0).rgb; return texelFetch(u_SkyTex, ivec2(gl_FragCoord.xy), 0).rgb;
#endif
} }
#endif #endif
@@ -59,6 +59,8 @@ vec3 _fogBackground(vec4 fogColor) {
#endif #endif
} }
// environment: fog from the surroundings (water, weather, blindness), which fades toward the fog color.
// edge: fog hiding the render distance limit, which with sky fog fades toward the sky behind the fragment.
vec4 _blendFog(vec4 fragColor, float environment, float edge, vec4 fogColor) { vec4 _blendFog(vec4 fragColor, float environment, float edge, vec4 fogColor) {
float fogValue = max(environment, edge); float fogValue = max(environment, edge);
@@ -66,15 +68,20 @@ vec4 _blendFog(vec4 fragColor, float environment, float edge, vec4 fogColor) {
return fragColor; return fragColor;
} }
#ifdef CELERITAS_SKY_FOG
vec3 target = mix(fogColor.rgb, _skyColor(), max(edge / fogValue, smoothstep(0.9, 1.0, environment)));
#else
vec3 target = fogColor.rgb; vec3 target = fogColor.rgb;
#ifdef CELERITAS_SKY_FOG
// The dominant fog picks the color. Nearly opaque environmental fog also turns into the sky, so that fully
// fogged terrain matches the background.
float skyWeight = edge >= environment ? 1.0 : edge / environment;
skyWeight = max(skyWeight, smoothstep(0.9, 1.0, environment));
target = mix(target, _skyColor(), skyWeight);
#endif #endif
return vec4(mix(fragColor.rgb, target, fogValue * fogColor.a), fragColor.a); return vec4(mix(fragColor.rgb, target, fogValue * fogColor.a), fragColor.a);
} }
// Single-range fog. With sky fog, the render distance edge comes from u_SkyFogEdge* and fades into the sky separately.
vec4 _linearFog(vec4 fragColor, float fragDistance, vec4 fogColor, float fogStart, float fogEnd) { vec4 _linearFog(vec4 fragColor, float fragDistance, vec4 fogColor, float fogStart, float fogEnd) {
#ifdef USE_FOG #ifdef USE_FOG
float environment = _linearFogValue(fragDistance, fogStart, fogEnd); float environment = _linearFogValue(fragDistance, fogStart, fogEnd);