From 4a86c112ddd54e7e469d33ca223177f0cf9d3c68 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 11 Dec 2021 17:31:09 -0800 Subject: [PATCH] Cylindrical fog - having the world position is very nice - mojang manages to do multiple matrix multiplications --- .../jozufozu/flywheel/light/GPULightVolume.java | 11 ++++++----- .../flywheel/shaders/context/crumbling.glsl | 2 +- .../flywheel/flywheel/shaders/context/fog.glsl | 14 ++++++++++++-- .../flywheel/flywheel/shaders/context/world.glsl | 6 +----- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java b/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java index d463a54d7..cb7d3727e 100644 --- a/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java +++ b/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java @@ -55,6 +55,12 @@ public class GPULightVolume extends LightVolume { int sizeZ = box.sizeZ(); glTexImage3D(GL_TEXTURE_3D, 0, GL30.GL_RG8, sizeX, sizeY, sizeZ, 0, GL30.GL_RG, GL_UNSIGNED_BYTE, 0); + glTexture.setParameteri(GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexture.setParameteri(GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexture.setParameteri(GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); + glTexture.setParameteri(GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT); + glTexture.setParameteri(GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); + glTexture.unbind(); oldState.makeActive(); } @@ -73,11 +79,6 @@ public class GPULightVolume extends LightVolume { textureUnit.makeActive(); glTexture.bind(); - glTexture.setParameteri(GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexture.setParameteri(GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexture.setParameteri(GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); - glTexture.setParameteri(GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT); - glTexture.setParameteri(GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); uploadTexture(); } diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/context/crumbling.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/context/crumbling.glsl index d55e86289..c9ab41cbe 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/context/crumbling.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/context/crumbling.glsl @@ -18,7 +18,7 @@ void FLWFinalizeNormal(inout vec3 normal) { #if defined(VERTEX_SHADER) void FLWFinalizeWorldPos(inout vec4 worldPos) { #if defined(USE_FOG) - FragDistance = length(worldPos.xyz - uCameraPos); + FragDistance = cylindrical_distance(worldPos.xyz, uCameraPos); #endif gl_Position = uViewProjection * worldPos; diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/context/fog.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/context/fog.glsl index 49380b14f..ada0be19e 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/context/fog.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/context/fog.glsl @@ -1,5 +1,3 @@ -#define USE_FOG - #if defined(VERTEX_SHADER) out float FragDistance; #elif defined(FRAGMENT_SHADER) @@ -8,6 +6,18 @@ in float FragDistance; uniform vec4 uFogColor; uniform vec2 uFogRange; +float cylindrical_distance(vec3 worldPos, vec3 cameraPos) { + float distXZ = length(worldPos.xz - cameraPos.xz); + float distY = abs(worldPos.y - cameraPos.y); + return max(distXZ, distY); +} + +float cylindrical_distance(vec3 worldPos) { + float distXZ = length(worldPos.xz); + float distY = abs(worldPos.y); + return max(distXZ, distY); +} + float FLWFogFactor() { return (uFogRange.y - FragDistance) / (uFogRange.y - uFogRange.x); } diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl index f6f94ebf2..138c5aca9 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl @@ -16,9 +16,7 @@ void FLWFinalizeNormal(inout vec3 normal) { #if defined(VERTEX_SHADER) void FLWFinalizeWorldPos(inout vec4 worldPos) { - #if defined(USE_FOG) - FragDistance = length(worldPos.xyz - uCameraPos); - #endif + FragDistance = cylindrical_distance(worldPos.xyz, uCameraPos); gl_Position = uViewProjection * worldPos; } @@ -41,13 +39,11 @@ vec4 FLWBlockTexture(vec2 texCoords) { } void FLWFinalizeColor(vec4 color) { - #if defined(USE_FOG) float a = color.a; float fog = clamp(FLWFogFactor(), 0., 1.); color = mix(uFogColor, color, fog); color.a = a; - #endif #if defined(ALPHA_DISCARD) if (color.a < ALPHA_DISCARD) {